- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的目标是对齐 <h1>
在 <img>
底部的顶部和旁边使用 CSS 和 HTML。当您缩放窗口时,图像的大小会增加(垂直和水平),我希望文本在图像顶部对齐,紧跟底线。目前我使用一定百分比的宽度和高度来对齐文本,但您永远不知道文本是否真的会在底部对齐。
我还提供了一个具有预期结果的示例。蓝色文本与底部对齐的红色框是我想要完成的,我使用了一个具有可变高度和宽度的元素。
因此我想是否可以缩放 <img>
的容器与图像本身成比例,我可以获得相同的结果。
container{
position:relative;
height:100%;
}
img{
position: absolute;
width:100%;
height:auto;
z-index:1;
}
h1{
position:absolute;
top:30vw;
left:50vw;
color:red;
z-index:10;
}
.variable-container{
position:relative;
text-align:center;
height:70vw;
width:50vw;
background-color:#de2d3d;
}
h2{
width:100%;
position:absolute;
bottom:10px;
color:blue;
}
<div class="container">
<h1>Title</h1>
<img src="https://placebear.com/g/200/100.jpg">
</div>
<!-- This is what I am trying to accomplish -->
<div class="variable-container">
<h2>
Aligned bottom of box
</h2>
</div>
最佳答案
所以这里的主要问题是嵌套的 img
元素已被定位为 absolute
,将其从正常文档流中取出。因此,外部元素(包含父元素)无法根据内部元素(嵌套元素)进行缩放,因为它不再与文档流相关。
嵌套 img
元素的定位:
img
元素 position
属性从 absolute
更改对于 relative
,此属性可能会被完全删除(如在此范围内似乎没有必要)嵌套h1
元素的定位:
嵌套的 h1
元素的定位也已重新设计,以水平居中一个绝对定位元素你可以总是简单地用单位声明 left
和 right
属性0
的值,并且由于 h1
是一个 block 元素,只需声明text-align: center
使文本居中。
为了相对于包含元素的一致定位,使用bottom
属性而不是 top
属性;自从要求是让这个元素保持相对于包含元素的底部。如果要求对立(相对于包含的顶部定位元素),然后使用 top
属性将是适用的。
图片纵横比问题:
background-image
替代品可以称为好吧。代码片段演示:
注意:为了演示,您可以手动调整包含元素(右下角)的大小。
.container{
position:relative;
height:100%;
}
img{
position: relative; /* to scale outer el same as inner el, inner el can't be out of normal document flow */
width:100%;
height:auto;
z-index:1;
}
h1{
position:absolute;
/* rather use `bottom` property if text needs to stay at bottom, and use an absolute unit value like `px` for most consistent positioning */
bottom:30px;
/* simply center an absolutely positionied element with properties `left` & `right` with values of `0` */
left:0;
right: 0;
text-align: center; /* then center text of block element */
color:red;
z-index:10;
margin: auto; /* unset vendor margin property */
}
.bg-img {
background-image: url(https://placebear.com/g/200/100.jpg);
background-size: cover;
height: 100%;
}
/* For the sake of demonstration */
.resize-demonstration {
overflow: hidden;
resize: auto;
padding: 15px;
border: 2px dashed #ccc;
}
<h2>Embedded Image</h2>
<div class="resize-demonstration">
<div class="container">
<h1>Title</h1>
<img src="https://placebear.com/g/200/100.jpg">
</div>
</div>
<h2>Background Image</h2>
<div class="resize-demonstration">
<div class="container bg-img" style="height: 300px">
<h1>Title</h1>
</div>
</div>
关于html - 如何使用 html 和 css 缩放(外部)元素与(内部)img 元素相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47767396/
我想替换每一个 带结束标记 字符串中的标记。该字符串实际上是一个 html 文档,其中 img 标记由我生成,并且始终如下所示: Src 是用户输入,所以它可以是任何东西。我做了一个正则表达式,不确
我使用数组通过 getElementsByClassName 存储我所有的 imgs。 我需要知道哪个 img 被点击或 mouseover/mouseout,所以我使用循环来检查哪个 img 被点击
我正在尝试使用图像制作一款类似 Match3 的游戏,但我无法进行比较。我正在为固定数量的 atm 执行此操作,只是为了让它正常工作,稍后应该在 foreach 循环中。如果有什么区别的话,该函数位于
我希望你能帮助我:) 我想定义 img 的高度,相对于图像的“实际”宽度。但宽度是动态的,因为它占父对象的百分比(wxample 的浏览器窗口)。 为什么我需要高度?:没有高度它工作正常,但我需要它,
我知道这个话题被讨论了很多,但我找不到任何适合我的解决方案。所以,我的代码大致是: var img =me.images[curID] var f = function() { var
我试图在一个页面上列出多个图像,但当您单击图像时,它会以模式打开。 它适用于第一张图片,但不适用于其他图片,我假设这是一个 JS 问题,我尝试设置一个空的 var,然后将其设置为获取元素 ID(每个
任务:我们正在通过 HttpWebRequest 抓取 HTML 内容(约 6,000 个调用)。该字符串经过修剪并存储在 SQL Server 2014 数据库中,以便作为 XML 进行处理。 问题
我从上面得到这个错误,不知道如何避免它。我的目的是获取屏幕截图,然后对其进行模板匹配,以查看此时屏幕上是否显示图标。到目前为止,它只是图标的位置。我的代码: #include "opencv2/hig
我有一个包含图像的容器,该图像是从应用程序加载的,因此容器的尺寸是已知的,而图像会有所不同。 我目前有以下 css: #secondary_photos { height: 100px; wi
我正在尝试设置一个随分辨率缩放但看起来仍然不错的页面背景..这就是我正在使用的.. 站点是http://www.gd-gaming.com/wordpress ,如果你用 Firebug 检查它,它只
目前我有 如何删除包装 img 标签的 p 标签? 所以我可以得到.. 最佳答案 使用 $('p > img').unwrap(''); 这将删除 img 周围的所有 p。您应该使用 cl
我想要动画 3 .svg图片: 和css : .sequence { position: relative; } .sequence img { position: ab
我有外部 RSS 提要填充以下重复出现的类 elements 。 {teaserImage} {teaserImage} {teaserImage} 我想简单地获取 :first 实例,该实例也是来自
这是一个独特的问题: 我不想使用浏览器 JavaScript 来解决这个问题,请继续阅读... 我要转换 通过编译应用程序( ng build 或 ng serve )到 Base64 img 标签,
悬停在 中的第一张图片上标记,我需要使用 CSS 增加第二张图片的不透明度。我试过使用 +和 ~运营商,无法让它发挥作用。任何帮助将不胜感激。 最佳答案 a:hover + img
我已经尝试解决这个问题有一段时间了,但我迷路了,有人吗? for(var i=0; i<10; i++) { var Img = new Image(); Img.onload = (
这就是我想要实现的目标: 当用户将鼠标悬停在较小的图像之一上时: 较小的图像 + 文本应替换较大的图像 + 文本。 当用户没有悬停时;将大集返回到其原始图像和文字。 这就是我到目前为止所拥有的。它没有
我知道如何在 php 中执行此操作,但我需要在 javascript/jquery 中完成此操作。 我正在尝试执行以下操作: $('#NewBox').html( $('#OldBox').html(
我正在使用一个 CMS (ExpressionEngine),它将段落标签包裹在图像周围。我正在使用响应式图像(最大宽度:100%),并且由于我还在段落上定义宽度,因此它会导致问题。我想使用 jQue
Tinymce 正在删除我的 img 结束标记并生成无效的 xhtml。 它变成了这个 进入这个 我也在使用 codemagic,但是在查看 html 时它仍然显示 .我也试过包括 , 但输出是
我是一名优秀的程序员,十分优秀!