让我的h2边框底部只是下划线文本而不是div

时间:2017-01-20 07:07:26

标签: html css css3 pseudo-element

我想制作h2的边框底部:填充文本而不是div。 这是我的代码:

.text_box{
   margin: 0 auto;
   width: 355px;
   background:red;
}

h2{
   margin: 0 auto;
   font-weight: 100;
   font-size: 1em;
   text-align: center;
   padding:20px;
}

h2:after{
   content:'';
   background:black;
    width:100%;
    height:2px;
    display:block;
}
<html>
  <head>
  <title></title>
  </head>
<body>
<div class="text_box">
    <h2>NEWS & ACCOLADES</h2>
</div>
</body>
</html>

JSFiddle:http://codepen.io/rezasan/pen/YNZQEQ

3 个答案:

答案 0 :(得分:2)

Here is the solution of your question.

.text_box {
    margin: 0 auto;
    width: 355px;
    background: red;
    text-align: center;
}
h2 {
    margin: auto;
    font-weight: 100;
    font-size: 1em;
    text-align: center;
    padding: 20px;
    position: relative;
    display: inline-block;
}
h2:after {
    content: '';
    background: black;
    width: 100%;
    height: 2px;
    display: block;
 
}
<div class="text_box">
        <h2>NEWS & ACCOLADES</h2>
</div>

答案 1 :(得分:0)

.text_box {
margin: 0 auto;
width: 355px;
background: red;
text-align: center;
}


h2 {
margin: auto;
font-weight: 100;
font-size: 1em;
text-align: center;
padding: 20px;
position: relative;
display: inline-block;

}

h2:after {
content: '';
background: black;
width: 100%;
height: 2px;
display: block;

}

答案 2 :(得分:0)

使用position样式属性您可以执行此操作

   	.text_box{
  margin: 0 auto;
  width: 355px;
  background:red;
}

h2{
 margin: auto;
 font-weight: 100;
 font-size: 1em;
 text-align: center;
 padding:20px;
}

.underline:after{
    background: black none repeat scroll 0 0;
    content: "";
    display: block;
    height: 2px;
    margin-left: 65px;
    position: absolute;
    width: 182px;
}
 <div class="text_box">
      <h2 class="underline">NEWS & ACCOLADES</h2>
</div>