简体   繁体   English

如何从父CSS中排除元素

[英]How to exclude element from parent CSS

I have a question to let an HR element ignore the padding from the div it is placed in. It has a padding of: padding:0 20px 0 20px; 我有一个问题要让HR元素忽略放置它的div中的填充。它的填充为: padding:0 20px 0 20px; , but that effects my header and text, which i like. ,但这会影响我喜欢的标题和文本。 But I want to use the HR tag as a 100% divider. 但是我想将HR标签用作100%分隔符。

It looks like this: 看起来像这样:

*------------*
| Header tag |
| ---------- |
| some text  |
| some text  |
*------------*

But how can I make it so that it works like this: 但是我怎样才能使它像这样工作:

*------------*
| Header tag |
+------------+
| some text  |
| some text  |
*------------*

So that the HR element is on actual 100% width, so excluding the padding. 因此HR元素的实际宽度为100%,因此不包括填充。

Any suggestions? 有什么建议么?

By using a negative margin, which is as much as the padding. 通过使用负余量,该余量与填充量一样多。

In this HTML code: 在此HTML代码中:

<div>
    Text
    <hr />
    Text
</div>

Use this CSS: 使用此CSS:

div {
    width: 200px;
    padding: 10px;
    border: 1px solid black;
}
hr {
    margin-left: -10px;
    margin-right: -10px;
}

Also see this demo . 另请参阅此演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM