使用伪元素绘制双线

时间:2018-01-08 10:44:49

标签: css pseudo-element

我需要使用伪元素在我的段落下绘制双线,但我不应该按照给出的规格使用显示块。任何人都可以帮忙。下面是我的代码(使用显示块):

p:after {
  content: "";
  display: block;
  width: 40px;
  margin: 20px auto 0;
  border-bottom: 1px solid;
  border-top: 1px solid;
  height: 7px;
}
<h1>The box-sizing Property</h1>
<p>Defines how the width and height of an element are calculated: should they include padding and borders, or not.</p>

<h2>box-sizing: content-box (default):</h2>
<p>Width and height only apply to the content of the element:</p>
<div id="example1">This div has a width of 300px. But the full width is 300px + 20px (left and right border) + 60px (left and right padding) = 380px!</div>

<h2>box-sizing: border-box:</h2>
<p>Width and height apply to all parts of the element: content, padding and borders:</p>
<div id="example2">Here, the full width is 300px, no matter what!</div>

提前致谢!

2 个答案:

答案 0 :(得分:1)

您可以在display: flex元素上使用p代替。

p {
  display: flex;
  flex-direction: column;
}
p:after {
  content: "";
  width: 40px;
  margin: 20px auto 0;
  border-bottom: 1px solid black;
  border-top: 1px solid black;
  height: 7px;
}
<h1>The box-sizing Property</h1>
<p>Defines how the width and height of an element are calculated: should they include padding and borders, or not.</p>

<h2>box-sizing: content-box (default):</h2>
<p>Width and height only apply to the content of the element:</p>
<div id="example1">This div has a width of 300px. But the full width is 300px + 20px (left and right border) + 60px (left and right padding) = 380px!</div>

<h2>box-sizing: border-box:</h2>
<p>Width and height apply to all parts of the element: content, padding and borders:</p>
<div id="example2">Here, the full width is 300px, no matter what!</div>

或者您可以absolute/relative使用transform: translateX个职位。

p {
  position: relative;
  padding-bottom: 20px;
}

p:after {
  content: "";
  width: 40px;
  border-bottom: 1px solid black;
  border-top: 1px solid black;
  height: 7px;
  position: absolute;
  left: 50%;
  bottom: -7px; 
  transform: translateX(-50%);
}
<h1>The box-sizing Property</h1>
<p>Defines how the width and height of an element are calculated: should they include padding and borders, or not.</p>

<h2>box-sizing: content-box (default):</h2>
<p>Width and height only apply to the content of the element:</p>
<div id="example1">This div has a width of 300px. But the full width is 300px + 20px (left and right border) + 60px (left and right padding) = 380px!</div>

<h2>box-sizing: border-box:</h2>
<p>Width and height apply to all parts of the element: content, padding and borders:</p>
<div id="example2">Here, the full width is 300px, no matter what!</div>

答案 1 :(得分:0)

如果您仅限display:block使用display:table而禁止使用

&#13;
&#13;
p:after {
  content: "";
  display:table;
  width: 40px;
  margin: 20px auto 0;
  border-bottom: 1px solid;
  border-top: 1px solid;
  height: 7px;
}
&#13;
<h1>The box-sizing Property</h1>
<p>Defines how the width and height of an element are calculated: should they include padding and borders, or not.</p>

<h2>box-sizing: content-box (default):</h2>
<p>Width and height only apply to the content of the element:</p>
<div id="example1">This div has a width of 300px. But the full width is 300px + 20px (left and right border) + 60px (left and right padding) = 380px!</div>

<h2>box-sizing: border-box:</h2>
<p>Width and height apply to all parts of the element: content, padding and borders:</p>
<div id="example2">Here, the full width is 300px, no matter what!</div>
&#13;
&#13;
&#13;

或者使inline-block宽度为100%,而不是使用线性渐变来创建两条线:

&#13;
&#13;
p:after {
  content: "";
  display:inline-block;
  width: 100%;
  margin: 20px auto 0;
  background:linear-gradient(to right,#000,#000) 50% 0%/40px 1px no-repeat,
             linear-gradient(to right,#000,#000) 50% 100%/40px 1px no-repeat;
  height: 9px;
}
&#13;
<h1>The box-sizing Property</h1>
<p>Defines how the width and height of an element are calculated: should they include padding and borders, or not.</p>

<h2>box-sizing: content-box (default):</h2>
<p>Width and height only apply to the content of the element:</p>
<div id="example1">This div has a width of 300px. But the full width is 300px + 20px (left and right border) + 60px (left and right padding) = 380px!</div>

<h2>box-sizing: border-box:</h2>
<p>Width and height apply to all parts of the element: content, padding and borders:</p>
<div id="example2">Here, the full width is 300px, no matter what!</div>
&#13;
&#13;
&#13;