怎么利用纯CSS实现页面换肤?CSS实现换肤方法

2020-07-23 20:42:57
本篇文章给大家带来的内容是关于怎么利用纯CSS实现页面换肤?CSS实现换肤方法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

微信截图_20181010092658.png

1.同一类型的属性要写在同一个样式里面。
例如:background-size和background要在同一个样式里面,具体看下文代码。

2.关于伪类的使用
:after和:before,是在某个元素下新生成的元素,可以想象成是这个元素的兄弟元素。元素伪类继承这个元素的所有属性及样式,也可以单独设置样式。:after和:before伪类一定含有content属性。

3.:target伪类
:target伪类是个冷门的伪类,它必须配合锚点使用,效果是如果该锚点被选中,这个锚点渲染:target伪类定义的样式。

4.阴影的使用
无论是text-shadow还是box-shadow都可以设置多阴影,代码里面有用例。

5.z-index属性
该属性只有定位为相对定位和固定定位的元素有z-index属性,即position:relative/absolute。

HTML代码:

<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title></title>        <link rel="stylesheet" href="css/style.css" />    </head>    <body>        <img class="bg bg1" id="bg1" src="img/1.jpg"/>        <img class="bg bg2" id="bg2" src="img/2.jpg"/>        <img class="bg bg3" id="bg3" src="img/3.jpg"/>        <div class="father">            <a href="#bg1">背景一</a>            <a href="#bg2">背景二</a>            <a href="#bg3">背景三</a>        </div>    </body> </html>

css代码:

*{    margin: 0;    padding: 0;}html,body{    height: 100%;    overflow: hidden;}img{    position: absolute;    height: 100%;    width: 100%;}.father{    position: absolute;    z-index: 99;    width: 550px;    top: 40%;    left: 50%;    transform: translate(-50%,-50%);}a{      cursor: pointer;    position: absolute;    text-align: center;    text-decoration: none;    width: 150px;    height: 200px;    float: left;    margin-right: 50px;    line-height: 200px;    border-radius: 20px;}a:nth-child(1){    background: lightblue;    left: 0px;}a:nth-child(2){    background: lightcoral;    left: 200px;}a:nth-child(3){    background: #cec;    left: 400px;}a:after{    content: '';    width: 125px;    height: 125px;    border: 3px solid white;    position: absolute;    top: -60px;    left: 9px;    opacity: 0.7;    border-radius: 50%;}/*background-size必须和background在同一个样式元素内设置样式*/a:nth-child(1):after{    background: url(../img/1.jpg);    background-size: 80%;}a:nth-child(2):after{    background: url(../img/2.jpg);    background-size: 80%;}a:nth-child(3):after{    background: url(../img/3.jpg);    background-size: 80%;}a:hover:after{    opacity: 0.9;}a:hover{    color: white;    font-family: "微软雅黑";    /*多阴影,用的不多,冷门小知识*/    text-shadow: 0 0 3px blue,                 0 0 9px darkturquoise,                 0 0 15px lightcoral;}/*:target伪类必须配合锚点使用,效果是锚点被选中时的样式*/img:target{    z-index: 30;}/*:not伪类*/img:not(:target){    z-index: 0;}

最后的效果大家自己试一试吧,注意我的页面是使用图片做背景,背景图的引入链接和自己的文件结构有关,url记得改。

如果你能看到最后,再分享一个很常用的知识:
父元素里面的子元素浮动,使父元素出现高度塌陷的时候,我们一般都会给父元素添加:after伪类,设置清除浮动,附上我的万能清除法。

万能清除法

父元素:after{    content: "";/*有时会用content:"."*/    clear:both;    display:block;    height:0;    overflow:hidden;    visibility:hidden;}