【问题标题】:How do I get <hr> margin working?如何让 <hr> 边距工作?
【发布时间】:2016-06-11 03:33:11
【问题描述】:

我正在尝试在我的 Wordpress 页面中添加一个“hr”来分隔我的内容。我的样式如下:

p {
-webkit-font-smoothing: antialiased;
color: #555555; 
font-family: Helvetica, 'Helvetica Neue', Arial, 'Lato', sans-serif; 
font-size: 16px; 
font-weight: 400; 
line-height: 1.25em;
padding: 0px;
margin: 0 0 12px 0;
}

h4 {
color: #111111;
font-family: Helvetica, 'Helvetica Neue', Arial, sans-serif; 
font-size: 16px;
font-weight: 900; 
line-height: 1.2em;
margin: 0 0 8px 0; 
}

hr { 
display: block;
width: 100%;
height: 1px;
background: #cccccc; 
border: none;
outline: none;
padding: 0px !important;
margin: 26px 0 13px 0 !important; 
}

我在 Wordpress 页面中使用的 HTML 如下:

<h4>Make your selection</h4>
<p>
<input type="radio" name="left-or-right" value="left" checked="checked" />
Turn Left<br />
<input type="radio" name="left-or-right" value="right" /> Turn Right</p>

<hr>

我对“hr”的问题是它的 margin-top 26px 并没有将它推离上面的“p”。相反,“hr”和“p”的边距似乎重叠。

到目前为止,我已经能够通过添加以下内容来解决此问题:

.generic-container {
width: 100%;
height: auto;
padding: 0px;
margin: 0px;
display: block;
overflow: hidden;
}

<div class="generic-container">
<h4>Make your selection</h4>
<p>
<input type="radio" name="left-or-right" value="left" checked="checked" />
Turn Left<br />
<input type="radio" name="left-or-right" value="right" /> Turn Right
</p>
</div>

<hr>

好像有点啰嗦

<div class="generic-container"> 

在页面每个部分的顶部,然后在每个“hr”上方添加“/div”以将其关闭。

我尝试添加“溢出:隐藏;”进入...

p

main.content

.entry-content

...但这似乎不起作用。

谁能给我一些关于如何激活“hr”边距的建议,将其从上面的元素移开?

感谢您的阅读。


在回复@symlink 后添加到下方:

感谢大家的回复。 尽管将“p” margin-bottom 更改为 padding-bottom,但“下面”的代码仍然不起作用(仍在尝试不使用“generic-container”):

CSS

.width-80pc {
width: 80%;
}
.width-20pc {
width: 20%;
}

.float-left {
float: left;
}

.float-right {
float: right;
}

p.toby-small-text {
color: inherit;
font-weight: inherit;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.25em;
padding-bottom: 5px;
margin: 0px !important; 
}

.text-align-left {
text-align: left;
}

HTML

<div class="width-80pc float-left">
<h4>This review is about this service</h4>
<p><input type="radio" name="service" value="one" disabled> Service One</p>
</div>

<div class="width-20pc float-right">
<p class="toby-small-text text-align-left"><a>Service Two</a></p>
</div>

<hr>

在我对@Marc Audet 的回复“评论”后添加如下:

该代码的作用是使 hr 上方的框远离 hr,而不是其下方的框。

根据 Marc 的建议,我创建了这个以使 hr 的 abovebebeath 框远离 hr:

CSS

.width-80pc {
width: 80%;
outline: 2px dotted blue;
}
.width-20pc {
width: 20%;
outline: 2px dotted purple;
}
.float-left {
float: left;
}
.float-right {
float: right;
}
p.toby-small-text {
color: inherit;
font-weight: inherit;
font-family: Helvetica, Arial, sans-serif;
font-size: 12px;
line-height: 1.25em;
margin: 0px !important;
}
.text-align-left {
text-align: left;
}

hr {
clear: both;  /* Takes care of you floated elements */
padding-top: 40px;
/* padding-bottom: 20px; This doesn't create space under the hr line */
border: none;
border-bottom: 2px solid red;
height: 0px;
background: transparent;
}

.after-hr {
padding: 0;
margin: 0;
height: 20px;
background: transparent;
}

/* This doesn't seem to work
.after-hr::after {
clear: both;
}
*/

h4, p, p.toby-small-text {
padding: 0 15px 0 15px;
}

HTML

<div class="width-80pc float-left">
<h4>This review is about this service</h4>
<p><input type="radio" name="service" value="one" disabled>Service One</p>
</div>

<div class="width-20pc float-right">
<p class="toby-small-text text-align-left"><a>Service Two</a></p>
</div>

<hr><div class="after-hr"></div>

<div class="width-80pc float-left">
<p>I also need the "hr" to keep away from the boxes underneath it.<br>
By creating ".after-hr" I seem to be able to make some space under it.<br>
I tried to use "hr::after" but it doesn't seem to work.</p>
</div>

如果有人可以建议如何在“hr”下方创建空间而不必在 HTML 中使用“hr”和“after-hr”,我欢迎任何建议。

也许有一些方法可以让“hr::after”完成这项工作。 谢谢

【问题讨论】:

    标签: html css overflow margin hidden


    【解决方案1】:

    这听起来像collapsing margins issue。您可以通过使用padding 代替p 而不是margin 来修复它。

    【讨论】:

    • 嗨@Nora 谢谢你的建议。刚刚尝试过,它确实适用于某些部分,例如 hr 上方的标准段落。但是,当我的 hr 低于两个 div,一个向左浮动(80% 宽度),另一个向右浮动(20% 宽度)时,它不起作用。
    【解决方案2】:

    您是否尝试过在其他浏览器中查看它?我正在使用 Chrome,老实说它看起来不错(第一位,在添加通用容器之前),它显示在段落下方,我没有看到任何重叠。 (我还不能添加图片给你看)。

    【讨论】:

    • 嗨@Jayc,感谢您在这里回复。我也在使用 Chrome。可见元素不重叠,重叠的是边距。可见元素之间的空间应该更大。
    【解决方案3】:

    添加generic-container div 并不冗长。当您应用overflow:hidden 时发生的情况是您正在强制generic-containter 建立一个新的块格式化上下文根。这做了几件事,其中之一是确保 div 内的边距包含在 div 内。好消息是您不需要添加到容器 div 中的任何 CSS,除了 overflow:hidden

    p {
        -webkit-font-smoothing: antialiased;
        color: #555555;
        font-family: Helvetica, 'Helvetica Neue', Arial, 'Lato', sans-serif;
        font-size: 16px;
        font-weight: 400;
        line-height: 1.25em;
        margin: 0 0 12px 0;
    }
    
    h4 {
        color: #111111;
        font-family: Helvetica, 'Helvetica Neue', Arial, sans-serif;
        font-size: 16px;
        font-weight: 900;
        line-height: 1.2em;
        margin: 0 0 8px 0;
    }
    
    hr {
        height: 1px;
        background: #cccccc;
        border: none;
        outline: none;
        margin: 26px 0 13px 0;
    }
    
    .generic-container {
        background:lightblue;
        overflow: hidden;
    }
    <div class="generic-container">
        <h4>Make your selection</h4>
        <p>
            <input type="radio" name="left-or-right" value="left" checked="checked" /> Turn Left
            <br />
            <input type="radio" name="left-or-right" value="right" /> Turn Right</p>
    </div>
    <hr>

    在此处阅读有关块格式化上下文的更多信息:

    http://www.sitepoint.com/understanding-block-formatting-contexts-in-css/

    【讨论】:

    • 谢谢@symlink 我将能够删除我不需要的代码。由于我的一些页面有很多需要用“hr”分解的部分,是否可以将“overflow:hidden”添加到css中的一两个div中,所以我不需要填写我的
      的页面
    • 您必须将overflow:hidden(或触发 BFC 根的其他 CSS 值之一)应用于您希望以这种方式运行的每个块容器。 BFC 根容器的子容器不继承其父容器的格式规则。例如,注意jsfiddle.net/6La4cacw 中黄色块容器中的 p 标签如何在其父级之外有边距。
    • 我现在已根据您的建议简化了“通用容器”。非常感谢
    【解决方案4】:

    这是另一种基于具有浮动元素的 sn-p 的方法。

    hr 元素上,您可以添加clear: both,以便hr 元素位于所有浮动元素的下方。

    要处理水平线顶部的垂直空白, 确保将边框设置为仅显示底部(或顶部)并在顶部添加一些填充以控制分隔。

    如果要控制hr元素后的间距,可以调整margin-bottom,如下图。这似乎工作得很好。

    我在 Firefox 中测试过,应该可以在其他浏览器中使用。

    .width-80pc {
      width: 80%;
      outline: 1px dotted blue;
    }
    .width-20pc {
      width: 20%;
      outline: 1px dotted blue;
    }
    .float-left {
      float: left;
    }
    .float-right {
      float: right;
    }
    p.toby-small-text {
      color: inherit;
      font-weight: inherit;
      font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
      font-size: 14px;
      line-height: 1.25em;
      padding-bottom: 5px;
      margin: 0px !important;
    }
    .text-align-left {
      text-align: left;
    }
    hr {
      clear: both; /* takes care of you floated elements */
      padding-top: 20px;
      margin-bottom: 20px;
      border: none;
      border-bottom: 2px solid red;
    }
    p.extra {
      border: 1px dotted blue;
      margin: 0;
    }
    <div class="width-80pc float-left">
      <h4>This review is about this service</h4>
      <p><input type="radio" name="service" value="one" disabled>Service One</p>
    </div>
    
    <div class="width-20pc float-right">
      <p class="toby-small-text text-align-left"><a>Service Two</a></p>
    </div>
    
    <hr>
    
    <p class="extra">Some more text.</p>

    【讨论】:

    • 嗨@Marc 谢谢你的帮助。我尝试了您的代码,它运行良好,使上述元素远离 hr.不幸的是,我无法让 hr 以下的元素远离它。添加“padding-bottom”只是在顶部添加了更多填充。但是,使用您的方法,我在“.after-hr”中添加了一些内容
    • @Toby 我再次查看了这个问题,您可以通过将值设置为底部边距来控制hr 元素后面的空白。请查看更新的答案。
    • 嗨@Marc,由于最初的“margin-top”失败,我驳回了回到“margin”的想法,这促使我在这里发布我的问题。但是,我已经按照您的建议添加了一个“margin-bottom”,它似乎工作正常。我会密切关注这一点,因为我在我的 WP 页面上使用了很多不同的部分。非常感谢。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签