天天看點

css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

一、介紹

css3為了區分僞類和僞元素,僞元素采用雙冒号寫法。

常見僞類——:hover,:link,:active,:target,:not(),:focus。

常見僞元素——::first-letter,::first-line,::before,::after,::selection。

::before和::after下特有的content,用于在css渲染中向元素邏輯上的頭部或尾部添加内容。

這些添加不會出現在DOM中,不會改變文檔内容,不可複制,僅僅是在css渲染層加入。

是以不要用:before或:after展示有實際意義的内容,盡量使用它們顯示修飾性内容,例如圖示。

舉例:網站有些聯系電話,希望在它們前加一個icon☎,就可以使用:before僞元素,如下:

<!DOCTYPE html>
<meta charset="utf-8" />
<style type="text/css">
    .phoneNumber::before {
    content:'\260E';
    font-size: 15px;
}
</style>
<p class="phoneNumber">12345645654</p>
           
css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

Note:這些特殊字元的html,js和css的寫法是不同的,具體可檢視html特殊字元的html,js,css寫法彙總。

二、content屬性

::before和::after必須配合content屬性來使用,content用來定義插入的内容,content必須有值,至少是空。預設情況下,僞類元素的display是預設值inline,可以通過設定display:block來改變其顯示。

content可取以下值。

1、string

使用引号包一段字元串,将會向元素内容中添加字元串。如:a:after{content:""}

舉例:

<!DOCTYPE html>
<meta charset="utf-8" />
<style type="text/css">
p::before{
    content: "《";
    color: blue;
}
p::after{
    content: "》";
    color: blue;
}
</style>
<p>平凡的世界</p>
           
css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

2、attr()

通過attr()調用目前元素的屬性,比如将圖檔alt提示文字或者連結的href位址顯示出來。

<style type="text/css">
a::after{
    content: "(" attr(href) ")";
}
</style>
<a href="http://www.cnblogs.com/starof" target="_blank" rel="external nofollow" >starof</a>
           
css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

3、url()/uri()

用于引用媒體檔案。

舉例:“百度”前面給出一張圖檔,後面給出href屬性。

<style>
a::before{
    content: url("https://www.baidu.com/img/baidu_jgylogo3.gif");
}
a::after{
    content:"("attr(href)")";
}
a{
    text-decoration: none;
}
</style>
---------------------------
<body>
<a href="http://www.baidu.com" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >百度</a>
</body>  
           

效果:

css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

4、counter()

調用計數器,可以不使用清單元素實作序号功能。

配合counter-increment和counter-reset屬性使用:

h2:before { counter-increment: chapter; content: "Chapter " counter(chapter) ". " }

代碼:

<style>
body{
    counter-reset: section;
}
h1{
    counter-reset: subsection;
}
h1:before{
    counter-increment:section;
    content:counter(section) "、";
}
h2:before{
    counter-increment:subsection;
    content: counter(section) "." counter(subsection) "、";
}
</style>
------------------------------------------------
<body>
<h1>HTML tutorials</h1>
<h2>HTML Tutorial</h2>
<h2>XHTML Tutorial</h2>
<h2>CSS Tutorial</h2>

<h1>Scripting tutorials</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>

<h1>XML tutorials</h1>
<h2>XML</h2>
<h2>XSL</h2>
</body>   
           

效果:

css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

了解更多可參考:https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Counters

三、使用

1、清除浮動

清除浮動方法有多種,現在最常用的就是下面這種方法,僅需要以下樣式即可在元素尾部自動清除浮動

.cf:before,
.cf:after {
    content: " ";
    display: table; 
}
.cf:after {
    clear: both;
}
.cf {
    *zoom: 1;
}      

2、模拟float:center的效果

float沒有center這個取值,但是可以通過僞類來模拟實作。

這個效果實作很有意思,左右通過::before float各自留出一半圖檔的位置,再把圖檔絕對定位上去。

核心css如下:

#page-wrap { width: 60%; margin: 40px auto; position: relative; }
#logo { position: absolute; top: 0; left: 50%; margin-left: -125px; }
#l, #r { width: 49%; }
#l { float: left; }
#r { float: right; }
#l:before, #r:before { content: ""; width: 125px; height: 250px; }
#l:before { float: right; }
#r:before { float: left; }      

完整代碼如下:

css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

 View Code

css3僞類和僞元素基本使用一、介紹二、content屬性三、使用
css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

出自:https://css-tricks.com/float-center/

3、做出各種圖形效果

舉例:一個六角星

<style>
#star-six {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid red;
  position: relative;
}
#star-six::after {
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-top: 100px solid red;
  position: absolute;
  content: "";
  top: 30px;
  left: -50px;
}
</style>
<body>
<div id="star-six"></div>
</body>
           

#star-six的div是一個正三角行,#star-six::after是一個倒三角形,通過絕對定位,調整其位置即可實作六角星的效果。

css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

點我檢視更多。

4、不使用圖檔建立小圖示

舉例:比如一個電話

很巧妙的應用一個div左border加圓角當機身,::before和::after配合圓角當聽筒。

<style type="text/css">
    #phone{width:50px;height:50px;border-left:6px solid #EEB422;border-radius:20%;transform:rotate(-30deg);-webkit-transform:rotate(-30deg);margin:20px;margin-right:0px;position:relative;display: inline-block;top: -5px;}
    #phone:before{width:15px;height:15px;background:#EEB422;border-radius: 20%;content: "";position: absolute;left:-2px;top: 1px;}
    #phone:after{width:15px;height:15px;background:#EEB422;border-radius: 20%;content: "";position: absolute;left:-3px;top: 34px;}
</style>
<div id="wraper">
    <div id="phone"></div>
</div>
           
css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

更多圖示:

css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

 View Code

css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

這個效果來自:http://www.w3cfuns.com/blog-5444604-5402127.html

有大神用僞元素建立了84種小圖示,具體可檢視http://nicolasgallagher.com/pure-css-gui-icons/

5、顯示列印網頁的URL

<style>
@media print {
  a[href]:after {
    content: " (" attr(href) ") ";
  }
}
</style><body>
<a href="http://www.baidu.com" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >百度</a>
</body>  
           
css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

6、給blockquote添加引号

經常用到給blockquote 引用段添加巨大的引号作為背景,可以用 ::before 來代替 background 。好處是即可以給背景留下空間,還可以直接使用文字而非圖檔:

<meta charset="utf-8"/>
<style type="text/css">
    blockquote::before {
    content: open-quote;
    color: #ddd;
    z-index: -1;
    font-size:80px;
}
</style>
<blockquote>引用一個段落,雙引号用::before僞元素實作</blockquote>
           
css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

7、超連結特效

舉例:配合 CSS定位實作一個滑鼠移上去,超連結出現方括号的效果

<meta charset="utf-8" />
<style type="text/css">
body{
    background-color: #425a6c;
}
    a {
    position: relative;
    display: inline-block;
    outline: none;
    color: #fff;
    text-decoration: none;
    font-size: 32px;
    padding: 5px 20px;
}
a:hover::before, a:hover::after { position: absolute; }
a:hover::before { content: "\5B"; left: -10px; }
a:hover::after { content: "\5D"; right:  -10px; }
</style>
<a>滑鼠移上去出現方括号</a>
           
css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

更多創意連結特效可參考: Creative Link Effects 。

8、::before和::after實作多背景圖檔

舉例:一個标簽應用5張背景圖

css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

 View Code

css3僞類和僞元素基本使用一、介紹二、content屬性三、使用

原效果來自:Multiple Backgrounds and Borders with CSS 2.1

這個效果看的真的是腦洞大開,雖然多背景圖用css3的background-image很容易就能實作。但是這篇文章是10年寫的,已經過去5年了,想想也正是他們這樣的嘗試和努力才加速了css3标準的制定,讓今天的開發更easy。今天的我們又能為5年後的開發人員做些什麼貢獻呢?