Html 如何在没有填充区域的情况下为 div 设置背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5896351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:26:20  来源:igfitidea点击:

How to background a div without the padding area

htmlcss

提问by kwichz

I have this class on CSS :

我在 CSS 上有这门课:

.mgmtError{ 
  width:716px; 
  float:left; 
  background-color:#FF0000; 
  padding:10px; 
  text-align:center;
} 

and I'd like to color the background not in the padding area. I tryed with margin, but seems that it have some troubles with IE.

我想为不在填充区域中的背景着色。我尝试了保证金,但似乎它与 IE 有一些麻烦。

Any others solution?

还有其他解决方案吗?

采纳答案by DanielB

If you are able to access the HTML try:

如果您能够访问 HTML,请尝试:

.mgmtError {
  width: 716px;
  float: left;
  padding: 10px;
  text-align: center;
  background-color: blue;
}

.mgmtError div {
  background-color: #FF0000;
}
<div class="mgmtError">
  <div>Content</div>
</div>

回答by Yacine Zalouani

You can use

您可以使用

background-clip: content-box;

Also make sure to use background-colorproperty, not background.

还要确保使用background-colorproperty,而不是background.