博主信息
博文 20
粉丝 0
评论 1
访问量 19003
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
css常用的选择器--2018年8月16日
南风的博客
原创
766人浏览过

实例

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>常用选择器</title>
    <style>
        ul{
            /*padding: 0;*/
            margin: 0;
            width: 550px;
            border: 1px dashed #666;
            padding: 10px 5px;
            hegiht:50px;
        }
        ul:after{
            conter:"";
            display: block;
            clear: both;
        }
        ul li{
            list-style: none;
            float: left;
            width: 40px;
            height: 40px;
            line-height: 40px;
            text-align: center;
            border-radius: 50px;
            box-shadow: 2px 2px 2px #888;
            background-color: skyblue;
            margin: 5px;
        }
        /*id选择器*/
        #item1{
            background-color: coral;
        }
        /*类选择器/class选择器*/
        .itme2{
            background-color: gold;
        }
        /*属性选择器: 属性名*/
        ul li[class]{
            background-color: blueviolet;
        }
        /*属性选择器: 属性值*/
        ul li[class="item2"]{
            background-color: gray;
        }
        /*属性选择器: 以指定属性值开头的元素*/
        ul li[class^="cat"]{
            background-color: brown;
        }
        /*属性选择器: 以指定属性值结尾的元素*/
        ul li[class$="pig"]{
            background-color: red;
        }
        /*属性选择器: 属性值中包含指定的子串*/
        ul li[class*="t"]{
            background-color: green;
        }
        /*后代选择器/层级选择器/派生选择器/*/
        body ul li{
            border: 1px solid black;
        }
        /*子选择器*/
        ul > li[class$="pig"]{
            background-color: greenyellow;
        }
        /*相邻选择器*/
        ul li[class$="pig"] ~ *{
            background-color: black;
            color: white;
        }
        /*相邻兄弟选择器*/
        ul li[class$="pig"] + li {
            background-color: pink;
            color: black;
        }
        /*群组选择器*/
        h1,p{
            font-size: 2rem;
            font-weight: lighter;
            margin: 0;
        }
        /*伪类选择器:链接*/
        a{
            font-size: 2rem;
        }
        /*访问前*/
        a:link{
            color: red;
        }
        /*访问后*/
        a:visited{
            color: orange;
        }
        /*获取焦点的时候*/
        a:focus{
            color: purple;
        }
        /*鼠标悬停*/
        a:hover{
            color: red;
        }
        /*鼠标点击的时候,就是鼠标按下激活的时候*/
        a:active{
            color: blue;
        }
        /*伪类选择器:位置*/
        ul li:first-child{
            background-color: #efefef !important;
        }
        ul li:last-child{
            background-color: red;
        }
        ul li:nth-child(5){
            background-color: #FCE80F;
        }
        ul li:nth-child(evern){
            /*偶数:evern/2n,奇数 odd/2n+1*/
            background-color: blueviolet !important;
        }
        /*伪类选择器:根据子元素的数量*/
        ol :only-child{
            /*只有一个子元素*/
            background-color: lawngreen;
        }
        ol li:only-child{
            /*只有一个子元素*/
            background-color: lawnblue;
        }
        ul li:nth-last-child(3){
            /*倒数第n个*/
            background-color: wheat !important;
        }
        ol li:nth-of-type(2){
            /*选定多个子元素中的第n个子元素*/
            background-color: wheat;
        }
        :empty{
            /*选择当前空元素*/
            width: 220px;
            height: 270px;
            background-color: coral;
        }
        :empty::after{
            content: '看到我了吗?';
        }
        :empty:before{
            /*通过伪类添加的元素,默认都是行内元素,不支持宽高设置,可以通过设置背景图标的方式设置*/
            content:url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1534421158242&di=1a3d2a17bed01036d29f8283a5911192&imgtype=0&src=http%3A%2F%2Fi1.hdslb.com%2Fbfs%2Farchive%2F763293ce06bf1e684ef0ea3da43ae5008d8564b8.jpg");
            width: 50px;
        }
    </style>
</head>
<body>
<ul>
    <li id="item1">1</li>
    <li>2</li>
    <li class="cat dog pig">3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
    <li>8</li>
    <li>9</li>
    <li>10</li>
</ul>
<h1>css选择器</h1>
<p>css       选择器</p>
<a href="http://www.php.cn">php中文网</a>
<ol>
    <li>列表项1</li>
</ol>
<ol>
    <li>列表项1</li>
    <li>列表项2</li>
    <li>列表项3</li>
</ol>
<ol>
    <li>列表项1</li>
    <li>列表项2</li>
    <li>列表项3</li>
    <li>列表项4</li>
</ol>
<div></div>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

手写代码:

QQ截图20180817155820.png

总结:

元素的单位:
1.px 屏幕像素,相对于显示器
2.em 元素单位,相对于当前元素字体大小,1em = 16px;
3.rem 根元素单位  r=root,  1rem= 1em = 16px;

 

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学