简体   繁体   English

如何让我的背景颜色覆盖我的整个DIV不仅仅是在它的文本?

[英]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 (请记住toprightbottomleftz-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.

相关问题 应用于`ul li`时,如何使我的背景颜色填充div的整个宽度? - How do I make my background-color fill entire width of div when applied to a `ul li`? 如何让 CSS 的背景色覆盖整个浏览器窗口? - How to make the background color of CSS cover entire browser window? 如何在滚动时更改链接的整个背景而不仅仅是文本? - How do i make the entire background of the link change when scrolled over rather than just the text? 如何使div的背景不透明但文本不透明? - How do I make my background for a div have an opacity but not the text? 如何使我的图像覆盖背景html? - How do I make my image cover the background html? 如何使Vue for循环生成的按钮覆盖div的整个区域? - How can I make my Vue for-loop generated buttons cover the entire area of my div? 如何拉伸背景图像以覆盖整个 HTML 元素? - How do I stretch a background image to cover the entire HTML element? 如何使背景图像覆盖整个背景而不重复图像并且导航链接位于右上角 - How do I make the background image cover the entire background without the image repeating and the navigation link positioned at the top-right in line 我怎样才能让我的 DIV 只是它所包含的文本的大小 - How can I make my DIV just the size of the text it encloses 如何隐藏没有背景色的div的基础文本? - How do I hide the underlying text of a div without background color?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM