::before (:before)

转载自:https://developer.mozilla.org/zh-CN/docs/Web/CSS/::before

CSS中,::before 创建一个伪元素,其将成为匹配选中的元素的第一个子元素。常通过 content 属性来为一个元素添加修饰性的内容。此元素默认为行内元素。

/* Add a heart before links */
a::before {
  content: "";
}

注意: 由::before 和::after 生成的伪元素 包含在元素格式框内, 因此并能应用在替换元素上 比如<img><br> 元素。

语法

/* CSS3 语法 */
element::before { 样式 }  

/* (单冒号)CSS2 过时语法 (仅用来支持 IE8) */
element:before  { 样式 }  

/* 在每一个p元素前插入内容 */
p::before { content: "Hello world!"; } 

CSS3 引入 ::before  是为了将伪类伪元素区别开来。浏览器也接受由CSS 2 引入的 :before 写法。

示例

加入引用标记

使用 ::before 伪元素的一个简单示例就是用于加入引号。此处同时使用了 ::before 和 ::after 来插入引用性文本。

HTML 内容
<q>一些引用</q>, 他说, <q>比没有好。</q>.
CSS 内容
q::before { 
  content: "«";
  color: blue;
}
q::after { 
  content: "»";
  color: red;
}
结果

修饰实例

我们可以用几乎任何方法定义 content 中的文字和图片样式。

HTML 内容
<span class="ribbon">Notice where the orange box is.</span>
CSS 内容
.ribbon {
   background-color: #5BC8F7;
}

.ribbon::before {
   content:          "Look at this orange box.";
   background-color: #FFBA10;
   border-color:     black;
   border-style:     dotted;
}
最终结果

待办列表

在本例中我们将使用伪元素来创建一个简单的待办列表。这个方法也可用于 UI 的小幅度更改和用户体验的提升。

HTML 内容
<ul>
  <li>Buy milk</li>
  <li>Take the dog for a walk</li>
  <li>Exercise</li>
  <li>Write code</li>
  <li>Play music</li>
  <li>Relax</li>
</ul>
CSS 内容
li {
  list-style-type: none;
  position: relative;
  margin: 2px;
  padding: 0.5em 0.5em 0.5em 2em;
  background: lightgrey;
  font-family: sans-serif;
}

li.done {
  background: #CCFF99;
}

li.done::before {
  content: '';
  position: absolute;
  border-color: #009933;
  border-style: solid;
  border-width: 0 0.3em 0.25em 0;
  height: 1em;
  top: 1.3em;
  left: 0.6em;
  margin-top: -1em;
  transform: rotate(45deg);
  width: 0.5em;
}
JavaScript 内容
var list = document.querySelector('ul');
list.addEventListener('click', function(ev) {
  if( ev.target.tagName === 'LI') {
     ev.target.classList.toggle('done'); 
  }
}, false);

下面展示的是最终得到的结果。请注意我们没有使用任何图标,对勾标识实际上是使用 CSS 定义了样式的 ::before 伪元素。接下来建立几个待办事项来完成它们吧。

最终结果

注释

在Firefox3.5中,fixed绝对定位的布局不被允许在元素前生成一个独立的元素(按照CSS规范,:after :before 伪类元素与其他盒模型元素是可以相互影响的,就像他们是真正的元素一样,不过是被插入到相关元素中罢了),他们可以被用来对非表格布局进行改善(例:实现元素在中心位置),只要置于中心的内容包含在元素中,那么内容的前后列不能够被添加前置或后置的兄弟元素。(i.e., it is perhaps more semantically correct to add an additional span as below, than it would to add an empty <div/> before and after)(记住,一定要给float元素添加width属性,否则它将不会浮动)

HTML 内容
<div class="example">
<span id="floatme">"Floated Before" should be generated on the left of the 
viewport and not allow overflow in this line to flow under it. Likewise 
should "Floated After" appear on the right of the viewport and not allow this 
line to flow under it.</span>
</div>
CSS 内容
#floatme { float: left; width: 50%; }

/* To get an empty column, just indicate a hex code for a non-breaking space: \a0 as the content (use \0000a0 when following such a space with other characters) */
.example::before {
  content: "Floated Before";
  float: left;
  width: 25%
}
.example::after {
  content: "Floated After";
  float: right;
  width:25%
}

/* For styling */
.example::before, .example::after, .first {
  background: yellow;
  color: red;
}
输出

规范

Specification Status Comment
CSS Pseudo-Elements Level 4
::before
Working Draft No significant changes to the previous specification.
CSS Transitions Working Draft Allows transitions on properties defined on pseudo-elements.
CSS Animations Working Draft Allows animations on properties defined on pseudo-elements.
Selectors Level 3
::before
Recommendation Introduces the two-colon syntax.
CSS Level 2 (Revision 1)
::before
Recommendation Initial definition, using the one-colon syntax

浏览器兼容性

特性 Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
:before support (Yes) 1.0 (1.7 or earlier) [1] 8.0 4 4.0
::before support (Yes) 1.5 (1.8) [1] 9.0 7 4.0
对于动画和过渡的支持 26 4.0 (2.0) 未实现 未实现 未实现

[1] 版本低于 3.5 的 Firefox 浏览器仅可使用 CSS 2.0 版本的 :before。不允许 position、 float、 list-style-* 和其它的一些显示特性。 Firefox 3.5 移除了这些限制。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ::before是CSS伪元素,它可以在某个元素的内容之前插入内容。这对于在元素之前添加图标或者为元素添加装饰效果很有用。 例如,你可以使用::before伪元素在每个段落之前插入一个箭头图标: ```css p::before { content: "➜"; margin-right: 10px; } ``` 在这个例子中,每个段落都会在内容之前显示一个箭头图标,并且图标和内容之间会有一个10像素的间距。 需要注意的是,::before伪元素只能用于插入内容,不能用于修改元素的布局或者样式。如果你想要修改元素的布局或者样式,应该使用其他的CSS属性。 ### 回答2: ::before是CSS中的一个伪元素,用于在指定元素的内容之前添加样式和内容。它通常与content属性一起使用,用于向元素前添加文本、图标或其他装饰效果。 ::before的用途有以下几个方面: 1. 实现图标和装饰效果:::before可以用来在指定元素前添加图标,通过设置content属性为对应的Unicode编码或图片链接,可以在元素前显示一个小图标。此外,::before还可以用来添加装饰效果,如在链接前添加箭头或下划线等。 2. 清除浮动:当使用浮动布局时,经常出现父级元素不被撑起的问题,这时可以使用::before伪元素来清除浮动。设置::before的content属性为"",并设置clear属性为both,可以在浮动元素后插入一个空的伪元素,从而使父元素正确布局。 3. 创建计数器与序列:通过结合content属性和计数器(counter)特性,可以使用::before创建序列或计数器效果。可以将content设置为counter的名称,然后通过增加counter的值来实现自动编号,并在元素前显示编号。 4. 创建自定义覆盖样式:::before还可以用于创建自定义的样式覆盖效果。通过设置::before的样式,可以在元素前添加自定义的装饰或样式,如用它来添加复选框或单选框的自定义样式等。 总之,::before是一个非常强大和灵活的CSS伪元素,可用于在指定元素前添加内容、图标、装饰效果或样式覆盖,提供了丰富的样式创作和实现需求的可能性。 ### 回答3: ::before是CSS中的一种伪元素,它用于在选定元素的内容前面插入一个生成的内容。 ::before的用途主要有两个方面: 1. 用于在元素前插入文本或图标:通过设置::before的content属性,可以在选定元素的内容前面插入文本或图标,并通过其他CSS属性(如font-family、font-size、color等)对其进行样式设置。这样可以方便地在元素前面添加额外的信息或装饰性图标,而不需要修改HTML结构。 2. 用于清除浮动:当元素使用了浮动(float)属性后,其周围的内容会环绕在其周围,而不是出现在其下方。这可能导致其他元素位于浮动元素上方,布局混乱。为了解决这个问题,可以在浮动元素的样式中使用::before伪元素,并设置clear属性为both。这样就可以在浮动元素前插入一个空内容的伪元素,并设置clear属性后,使得后续元素不再受到浮动元素的影响,从而起到了清除浮动的效果。 总之,::before用途广泛,除了可以在元素前插入文本或图标外,还可以用于解决布局中的浮动问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值