温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何利用纯CSS实现页面换肤

发布时间:2020-09-26 11:04:21 来源:亿速云 阅读:138 作者:小新 栏目:web开发

这篇文章主要介绍了如何利用纯CSS实现页面换肤,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。

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;
}

感谢你能够认真阅读完这篇文章,希望小编分享如何利用纯CSS实现页面换肤内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css
AI