CSS3巧妙地利用伪元素和伪类,让HTML结构更加清晰合理
为了区分伪类和伪元素,css3使用冒号来书写伪元素。
伪类 -- :hover, :link, :active, :visited, :first-child, :last-child, nth-child(n), :not(), :focus
伪元素 -- ::before, ::after, ::first-letter, ::first-line, ::selection
伪元素:[伪元素用于在某些转盘上放置特殊效果。简而言之,伪元素创建一个虚拟容器。该容器不包含 DOM 元素,但可以包含内容。我们可以自定义样式添加伪元素]
::first-letter -- 用于选择指定选择器的首字母(例如将指定选择器的首字母设置为红色)
p:first-letter
{
color: #f00;
font-size: 32px;
text-transform: capitalize;
}
::first -line -- 用于选择指定选择器的第一行(例如将所选选择器的第一行背景颜色设置为红色)
p:first-line
{
color: #fff;
background: #f00;
}
: :selection -- 用于匹配用户选择的部分(例如将所选文本的字体设置为红色)
p::selection
{
color:#f00;
}
p::-moz-selection
{
color:#f00;
}
:: before -- 到指定的选择器 在所选元素的内容之前添加内容(例如在所选元素的内容之前添加新内容)
::after -- 在内容之后添加内容指定选择器的内容(例如在选定元素的内容后添加内容) 在内容后添加新内容) 巧用 ::after/::before 伪元素制作CheckBox开关按钮
HTML部分:
<label class="agreeBtn">
<input type="checkbox" checked />
<span class="active"></span>
I agree with <a href="/">terms&conditions</a>
</label>
CSS部分:
.agreeBtn {
position: relative;
}
.agreeBtn input {
opacity: 0;
}
.agreeBtn span {
position: relative;
display: inline-block;
left: -10px;
top: 6px;
width: 36px;
height: 20px;
border-radius: 30px;
background-color: #eee;
-webkit-transition: background-color .3s;
-moz-transition: background-color .3s;
-ms-transition: background-color .3s;
-o-transition: background-color .3s;
transition: background-color .3s;
}
.agreeBtn span.active {
background-color: #999;
}
.agreeBtn span::after {
position: absolute;
content: '';
top: 2px;
left: 2px;
width: 16px;
height: 16px;
border-radius: 50%;
box-shadow: 1px 0 3px rgba(0,0,0,0.1);
background-color: #fff;
-webkit-transition: .3s;
-moz-transition: .3s;
-ms-transition: .3s;
-o-transition: .3s;
transition: .3s;
}
.agreeBtn span.active::after {
-ms-transform: translateX(16px);
-moz-transform: translateX(16px);
-webkit-transform: translateX(16px);
-o-transform: translateX(16px);
transform: translateX(16px);
}
使用伪元素 ::after/::before 创建复选框切换按钮
巧用 ::after/::before 伪元素更改select 元素的默认样式
HTML部分:
<div class="user_select">
<select class="form-control">
<option value="PhD" selected="selected">PhD</option>
<option value="PhD Candidate">PhD Candidate</option>
<option value="Master">Master</option>
<option value="Master Candidate">Master Candidate</option>
<option value="Bachelor">Bachelor</option>
<option value="None">None</option>
</select>
</div>
CSS部分:
.usermr_content select{
appearance: none;
-moz-appearance: none;
-webkit-appearance: none;
}
.user_select{
position: relative;
}
.user_select::after{
position: absolute;
content: '';
right: 5px;
top: 11px;
border-top: 6px solid #666;
border-left: 3px solid transparent;
border-right: 3px solid transparent;
}
使用伪元素 : :after/::before 改变 Select 元素的默认样式
巧用 ::after/::before 伪元素制作左右按钮
HTML部分:
<span class="btn leftBtn"></span>
<span class="btn rightBtn"></span>
CSS部分:
.btn{
position: relative;
display: block;
width: 42px;
height: 42px;
border-radius: 2px;
background-color: #36403f;
}
.btn:hover{
background-color: #f55362;
}
.btn::after{
position: absolute;
content: '';
top: 10px;
left: 15px;
borde: 10px solid transparent;
}
.leftBtn::after {
border-right-color: #fff;
}
.rightBtn::after {
border-left-color: #fff;
}
使用伪元素: :after/::before 伪元素创建左右按钮
巧用 ::after/::before 伪元素实现立体按钮效果
HTML部分:
<div class="loginBtn"><input type="button" value="登 录"></div>
CSS部分:
.loginBtn input {
width: 100%;
height: 50px;
border-radius: 4px;
box-shadow: 0 0 4px rgba(0,0,0,.6);
line-height: 50px;
text-align: center;
letter-spacing: 1px;
color: #fff;
background: -webkit-linear-gradient(top, #4585db, #396fb7);
}
.loginBtn{
position: relative;
}
.loginBtn::before {
position: absolute;
content: '';
top: 0;
left: 0;
height: 100%;
width: 100%;
border-radius: 4px;
box-shadow: 0 -3px 0 #75adeb;
}
::after/::before 使用伪元素元素创建三维按钮效果
:使用 :after/::before 伪元素在照片上创建卷曲阴影 照片更加三维。 巧妙地使用::after/::before伪元素在照片上创建扭曲的阴影,使照片更具立体感。
巧妙地使用::after/::before伪元素来实现Tab切换风格。 使用 ::after/::before 伪元素实现 Tab 切换样式
使用 ::after/::before 伪元素内容实现 + - 按钮。巧妙地利用::after/::before伪元素的内容来实现+按钮
伪类:【伪类的目的是利用选择器来查找不在其中的信息DOM树,不能是CSS选择器正常获取的信息]
:hover //鼠标悬停时显示的效果
:link //链接在正常情况下(即页面刚加载完成时)显示的效果
:active //当元素处于激活状态(鼠标在元素上按下还没有松开)时所显示的效果
:visited //链接被点击后显示的效果
:first-child //选择器匹配属于其父元素的首个子元素的指定选择器
:last-child //选择器匹配属于其父元素的最后一个子元素的指定选择器
:nth-child(n) //选择器匹配属于其父元素的第 N 个子元素,不论元素的类型
:not() //匹配非指定选择器的每个元素(eg. 匹配所有类名不为active的p元素 p:not(".active"))
:focus //元素获得光标焦点时的效果(eg. input 输入框 input:focus)
作者:Storm Dumb
链接:https://juejin.im/post/5a31f6775188256e174e192c
版权所有sss。商业转载请联系作者获得许可。非商业转载请注明出处。
版权声明
本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。