[英]How do I make the background color cover my entire DIV and not just the text in it?
I have a DIV that contains image and text ... 我有一个包含图像和文本的DIV ...
<div id="control" class="btn">
<div id="btn_container"><img width="100" height="100" src="https://cdn3.iconfinder.com/data/icons/minecraft-icons/512/Stone_Pickaxe.png" alt="Mining pick" /></div>
<span>Start</span>
</div>
I would like to have a background color in my DIV, so I did this 我想在DIV中使用背景色,所以我这样做了
.btn
{
position: relative;
margin-bottom:20px;
padding: 0 10px;
text-align: left;
font-size: 16px;
color: #333;
background:gray;
display: inline-block;
}
but the background color is only appearing on my image and not in the DIV -- https://jsfiddle.net/xwdnvcy5/17/ . 但背景颜色仅出现在我的图像上,而不出现在DIV中-https: //jsfiddle.net/xwdnvcy5/17/ 。 How do I make the background color appear in the entirety of the DIV?
如何使背景色出现在整个DIV中? I would prefer not to hard-code a height and width in my DIV as I plan to use this class for other elements.
我不打算在DIV中硬编码高度和宽度,因为我计划将此类用于其他元素。
The issue is with the absolutely positioned img, I've modified the link. 问题在于绝对定位的img,我修改了链接。 If you want to move around the elements make the text span relative.
如果要在元素周围移动,请使文本跨度相对。 https://jsfiddle.net/sandeepcnath/xwdnvcy5/19/
https://jsfiddle.net/sandeepcnath/xwdnvcy5/19/
The reason for your issue is that absolutely positioned elements don't take up space in the layout. 出现此问题的原因是,绝对定位的元素不会占用布局中的空间。 Only the text span was getting bg-color because it was the only element inside .btn occupying space.
只有文本范围变得bg-color,因为它是.btn内唯一占用空间的元素。
.btn
{
position: relative;
margin-bottom:20px;
padding: 0 10px;
text-align: left;
font-size: 16px;
color: #333;
background:gray;
border-radius:5px;
display: inline-block;
text-align: center;
}
#btn_container
{
}
.btn img
{
}
.btn span
{
display:-block;
vertical-align:middle;
}
The problem is that your div#btn_container
don't have any size because the <img>
inside has an absolute
positioning 问题是您的
div#btn_container
没有任何大小,因为其中的<img>
具有absolute
位置
position: absolute 位置: 绝对
The element is removed from the normal document flow;
该元素已从常规文档流中删除; no space is created for the element in the page layout...
没有为页面布局中的元素创建空间。
Read more about position
on MDN . 阅读有关MDN
position
更多信息。
One possible solution is removing the .btn img
position property. 一种可能的解决方案是删除
.btn img
position属性。 In this scenario it will set to static
by default (Keep in mind that top
, right
, bottom
, left
, and z-index
properties have no effect when position: static
and that's why I comment those in the code snippet bellow. I also removed the margins to keep the image inside the container <div>
): 在这种情况下,默认情况下它将设置为
static
(请记住top
, right
, bottom
, left
和z-index
属性在position: static
时不起作用,这就是为什么我在下面的代码段中注释了这些原因。我也删除了将图像保留在容器<div>
的边距 ):
.btn { position: relative; margin-bottom:20px; padding: 0 10px; text-align: left; font-size: 16px; color: #333; background:gray; display: inline-block; } #btn_container { border-radius:5px; background:red; position: relative; } .btn img { /* static psoitioning don't care about position props like: left: 50%; top: 50%;*/ /* remove margins to keep image inside the container div margin-left: -15px; margin-top: -15px;*/ /*position: absolute;*/ /* line removed */ } .btn span { display:inline-block; vertical-align:middle; }
<div id="control" class="btn"> <div id="btn_container"> <img width="100" height="100" src="https://cdn3.iconfinder.com/data/icons/minecraft-icons/512/Stone_Pickaxe.png" alt="Mining pick" /> </div> <span>Start</span> </div>
Remove display: inline-block;
删除
display: inline-block;
. 。
如果您摆脱了这个位置:.btn img上的绝对位置将有所帮助-我只是试着看看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.