::之后出现伪元素

时间:2017-09-21 11:45:44

标签: html css pseudo-element

我正在尝试在渐变的顶部和底部创建一个微妙的波。但是,:: after伪元素出现在主要内容之前而不是之后。目前它显示为:: before,:: after,主要内容,但我希望它显示为:: before,main content,:: after。

这是我的代码:

#gradient {
  background: #0068a9;/* For browsers that do not support gradients */
  background: -webkit-linear-gradient(rgba(0, 104, 169, 1), rgba(0, 104, 169, .9));/*Safari 5.1-6*/
  background: -o-linear-gradient(rgba(0, 104, 169, 1), rgba(0, 104, 169, .9));/*Opera 11.1-12*/
  background: -moz-linear-gradient(rgba(0, 104, 169, 1), rgba(0, 104, 169, .9));/*Fx 3.6-15*/
  background: linear-gradient(rgba(0, 104, 169, 1), rgba(0, 104, 169, .9));/*Standard*/
  width: 100%;
  height: 300px;
  min-height: 0px;
  display: block;
}

#gradient::before,
#gradient::after {
  display: block;
  content: "";
  width: 100%;
  height: 75px;
}

#gradient::before {
  background: #f2f2f2 url("http://www.qumidesignco.com/clients/preparedcapital/pc_topcurve.png") center top;
}

#gradient::after {
  background: #f2f2f2 url("http://www.qumidesignco.com/clients/preparedcapital/pc_bottomcurve.png") center top;
}
<div style="background:#f2f2f2; width: 100%; height: 300px; min-height: 0px; display:block;"></div>
<div id="gradient"></div>
<div style="background:#f2f2f2; width: 100%; height: 300px; min-height: 0px; display:block;"></div>

2 个答案:

答案 0 :(得分:0)

正常后出现。问题是你的div高度。 我添加了背景来展示它们的位置。红色在之前,绿色在之后

&#13;
&#13;
#gradient {
  background: #0068a9;/* For browsers that do not support gradients */
  background: -webkit-linear-gradient(rgba(0, 104, 169, 1), rgba(0, 104, 169, .9));/*Safari 5.1-6*/
  background: -o-linear-gradient(rgba(0, 104, 169, 1), rgba(0, 104, 169, .9));/*Opera 11.1-12*/
  background: -moz-linear-gradient(rgba(0, 104, 169, 1), rgba(0, 104, 169, .9));/*Fx 3.6-15*/
  background: linear-gradient(rgba(0, 104, 169, 1), rgba(0, 104, 169, .9));/*Standard*/
  width: 100%;
  height: 150px;
  min-height: 0px;
  display: block;
}

#gradient::before,
#gradient::after {
  display: block;
  content: "";
  width: 100%;
  height: 75px;
}

#gradient::before {
  background: #f2f2f2 url("http://www.qumidesignco.com/clients/preparedcapital/pc_topcurve.png") center top;
background-color: red;
}

#gradient::after {
  background: #f2f2f2 url("http://www.qumidesignco.com/clients/preparedcapital/pc_bottomcurve.png") center top;
background-color: green;
}
&#13;
<div style="background:#f2f2f2; width: 100%; height: 300px; min-height: 0px; display:block;"></div>
<div id="gradient"></div>
<div style="background:#f2f2f2; width: 100%; height: 300px; min-height: 0px; display:block;"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

gradient div中的relative :before and :afterabsolute top:0:before以及bottom:0中的:after

&#13;
&#13;
#gradient {
  background: #0068a9;/* For browsers that do not support gradients */
  background: -webkit-linear-gradient(rgba(0, 104, 169, 1), rgba(0, 104, 169, .9));/*Safari 5.1-6*/
  background: -o-linear-gradient(rgba(0, 104, 169, 1), rgba(0, 104, 169, .9));/*Opera 11.1-12*/
  background: -moz-linear-gradient(rgba(0, 104, 169, 1), rgba(0, 104, 169, .9));/*Fx 3.6-15*/
  background: linear-gradient(rgba(0, 104, 169, 1), rgba(0, 104, 169, .9));/*Standard*/
  width: 100%;
  height: 300px;
  min-height: 0px;
  display: block;
  position: relative;
}

#gradient::before,
#gradient::after {
  display: block;
  content: "";
  width: 100%;
  height: 75px;
  position: absolute;
}

#gradient::before {
  background: #f2f2f2 url("http://www.qumidesignco.com/clients/preparedcapital/pc_topcurve.png") center top;
  top:0;
}

#gradient::after {
  background: #f2f2f2 url("http://www.qumidesignco.com/clients/preparedcapital/pc_bottomcurve.png") center top;
  bottom: 0;
}
&#13;
<div style="background:#f2f2f2; width: 100%; height: 300px; min-height: 0px; display:block;"></div>
<div id="gradient"></div>
<div style="background:#f2f2f2; width: 100%; height: 300px; min-height: 0px; display:block;"></div>
&#13;
&#13;
&#13;