第六十七篇 css样式与布局

一、高级选择器

1.后代选择器

1.用空格隔开。对于E F,表示所有属于E元素后代的F元素,有这个样式,空格表示后代,它描述的是祖先结构

2.需要注意的是:对于这两个标签不一定是连续紧挨着的,只要保持一个后代的关联即可,也就是说,选择的是后代,不一定是儿子

2.子代选择器

1.用>连接,对于E>F,表示所有属于E元素子代的F元素,有这个样式,>表示子代,它描述的是父子结构

2.需要注意的是:子代选择器只能匹配直接父子关系,如果中间隔代,则不能匹配

3.兄弟选择器

**1.用+号连接,对于E + F,表示与E相邻的兄弟元素F,有这个样式,+表示相邻,它描述的是相邻的兄弟结构。需要注意的是:只能匹配第一个F标签

2.用号连接,对于EF,表示与E相邻的兄弟元素F,有这个样式,~表示相邻,它描述的是相邻的兄弟结构。需要注意的是:标签的上下结构形成兄弟标签,兄弟选择器可以匹配直接兄弟关系或间接兄弟关系形成的层次,即可以匹配多个兄弟关系的选择器

4.群组选择器

用逗号隔开多个选择器,可以控制多个选择器

5.交叉选择器

1.紧挨着,没有任何连接符,交叉选择器本质上是对一个目标标签的多个名字(标签、类、id)的同时标识

2.标签名、class名、id名可以是对同一个目标标签的修饰,需要注意的是在一个页面中id只有一个,class属性可以有多个值

6.选择器的优先级

1.简单选择器存在优先级,优先级的前提就是不同选择器同时控制同一标签的同一属性

2.复杂选择器的种类并不会影响优先级,它是通过同类型(简单选择器)的个数来确定优先级

3.简单选择器的优先级起关键性作用,也就是一个id选择器要大于无限个class选择器,一个class选择器要大于无限个标签选择器

二、伪类选择器

1.伪类中的nth-child与nth-of-type属性

1.用冒号(:)连接,对于E:F,表示E元素中的所有F元素,有这个样式

2.伪类选择器优先级与类相同

3.nth-child:在同一结构下都是相同选择器时使用。先确定位置,再筛选选择器,也就是不按指定标签的个数匹配,而是无论哪个标签都算在内

4.nth-of-type:在同一结构下不全是相同选择器时使用。先确定选择器,再匹配位置,也就是只数指定标签的个数

2.属性选择器

1.[属性名]查找所有有该属性的标签

2.[属性名=属性值]查找所有该属性是指定属性值的标签

3.[属性名^=值]以该属性且为指定属性值开头的所有的标签

4.[属性名*=值]模糊查询,包含该属性值的所有标签

三、a标签的四大伪类

1.a标签的四大伪类

标签未被访问、标签已被访问、标签被悬浮、标签被激活

<!doctype html>
<html>
<head>
	<style>
	
        a:link {color: #FF0000}		/* 未访问的链接 */
        
        a:visited {color: #00FF00}	/* 已访问的链接 */
        
        a:hover {color: #FF00FF;
        /* 鼠标样式:wait、row-resize、none、text、pointer、default */
        cursor:pointer;}	/* 鼠标移动到链接上 */
        
        a:active {color: #0000FF;
        cursor:wait;}	/* 选定的链接 */
        
	</style>
</head>
<body>
</body>
</html>

2.reset操作

在开发中往往用不到四种伪类,可以通过reset操作对a标签进行样式设置:清除系统默认样式

a {
	color: black;
	text-decoration: none;
}

3.普通标签的伪类运用

<!doctype html>
<html>
<head>
    <style>
    
		/*字体*/
        .btn {
            font: bold 20px/45px 'STSong';
            text-align: center;
        }
        
        /*边界圆角*/
        .btn {
            border-radius: 5px;
        }
        
        /*不允许文本操作*/
        body {
            user-select: none;
        }

        /*伪类*/
        .btn:hover {
            cursor: pointer;
            background-color: orangered;
        }
        .btn:active {
            background-color: brown;
        }
        
    </style>
</head>
<body>
	
	<div class="btn">按钮</div>

    <!--
    标签没有被访问过
    标签被悬浮
    标签被激活
    标签已被访问过
    -->
    
</body>
</html>

四、文本属性

1.文字大小:font-size

2.字体颜色:color

3.字族(字体):font-family

4.字重(字体宽度):font-weight

5.字体样式:font-style

6.文本划线(上划线、下划线、中划线...):text-decoration

7.文本水平位置(居中、贴右...):text-align

8.行高:line-height

9.首行缩进:text-indent

10.文字间距:letter-spacing

11.整体设置

五、边界圆角

border-radius

1.左上为第一个角,顺时针编号

2.不足找对角

3.用/划分时,横纵分离,先横后纵

4.可以填固定值(像素)或百分比

六、背景图片

.box {
	background-image:url("img/图片.gif")
	
	/*尽量只设置宽,高等比缩放,不失真*/
	background-size
	
	/*平铺: repeat-x repeat-y repeat no-repeat*/
	background-repeat
	
	/*1.只设置x轴,y轴默认center*/
	/*2.
	x轴:left/center/right/具体像素/百分百,
	y轴:top/center/bottom/具体像素/百分百
	*/
	background-position: center center;
}

1.显示比屏幕大的图片时,尽量只设置宽,高会等比缩放

2.显示比屏幕小的图片时,通过平铺与位置(x轴或y轴)来进行处理

七、精灵图案例

精灵图就是操作大图在显示区域的位置

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>精灵图案例</title>
    <style>
        /*精灵图就是操作大图在显示区域的位置*/
        .box {
            width: 500px;
            height: 100px;
            background-color: orangered;
            border: solid;
        }
        .box {
            background-image: url("img/bg.png");
            background-position-y: -150px;
        }
        .box:hover {
            cursor: pointer;
            background-position-y: -250px;
        }
    </style>
    <style>
        .b1 {
            width: 155px;
            height: 48px;
            border: solid;
            background-image: url("img/bg.png");
        }
        .b1:hover {
            cursor: pointer;
            background-position-y: -48px;
        }
    </style>
    <style>
        .b2 {
            width: 157px;
            height: 48px;
            border: solid;
            background-image: url("img/bg.png");
            background-position: -155px 0;
        }
        .b2:hover {
            cursor: pointer;
            background-position: -155px -48px;
        }
    </style>
</head>
<body>
    <div class="box"></div>

    <div class="b1"></div>

    <div class="b2"></div>
</body>
</html>

八、显示方式

1.display属性中有三种显示方式:block、inline、inline-block

2.block:支持设置宽和高,自带换行。基本上所有有宽和高、参与位置布局的都是block

3.inline:不支持设置宽和高,宽高只能由文本撑开,不带换行,一行显示不下会自动换行(保留数据的整体性)。用于存放文本

4.inline-block:支持设置宽和高,不带换行,一行显示不下会自动换行(以标签整体换行,标签左右需要有间距)。不要主动设置该显示方式,系统的两个img、input都设置为了单标签(不会嵌套任何东西)。如果要用inline-block参与布局,为了标签布局不受内容影响,可以设置vertical-align:top

5.文本垂直对齐方式:vertical-align:baseline\top\bottom

九、盒模型布局

margin \ border \ padding \ content

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>盒模型布局</title>
    <style>
        body {
            margin: 0;
            padding: 100px 0 0 200px;
        }

        /*盒模型组成部分: */
        /*margin + border + padding + content
        1.每部分都有自己的独立区域
        2.content是宽x高,作为内容或子标签的显示区域
        3.padding是内边距,没有自身颜色,完成内容的内移(保证显示区域大小不变,可以响应减小content)
        4.border是边框,有宽度、样式、颜色,自身透明(transparent)时,是可以透出背景颜色的
        5.margin是外边距,控制盒子的显示位置,left、top控制自身,right、bottom影响兄弟
        注:margin的偏移依据当前所在位置
        */
        div {
            width: 100px;
            height: 100px;
            background-color: red;
        }
        owen {
            /*margin: 10px;*/
            margin-top: 50px;
            margin-left: 10px;
            /*margin-right: 10px;*/
            /*margin-bottom: 100px;*/
        }

        owen {
            /*border-color: black;*/
            /*border-width: 3px;*/

            /*none solid dashed dotted*/
            /*border-style: solid;*/

            border: red dashed 10px;
        }

        owen {
            /*padding: 上右下左,不足找对边*/
            /*padding: 10px 20px 30px;*/
            padding: 10px;
        }
        owen {
            display: block;
            /*width: 100px;*/
            width: 80px;
            /*height: 100px;*/
            height: 80px;
            background-color: orange;
        }
    </style>
</head>
<body>
    <!--
    盒模型:
    概念:广义上页面中所有的标签都称之为盒子,狭义上布局的盒子指的是display:block
    -->
    <owen>123</owen>
    <div></div>
</body>
</html>

十、浮点布局

1.父级在宽度不固定时,高度不要设置死

2.父级清浮动:

.class:after {
	content:'';
	display:block;
	clear:both;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>浮动布局</title>
    <style>
        .wrap {
            border: 10px solid yellow;
            width: 300px;
            /*父级在宽度不固定时高度不能设置死*/
            /*height: 300px;*/
        }
        /*清浮动:父级清浮动,就是在自己宽度是否确定下,都能保证父级的高度刚刚好包裹子集*/
        .wrap:after {
            content: '';
            display: block;
            clear: both;
        }

        .box {
            width: 100px;
            height: 100px;
            background-color: orange;
            border-radius: 50%;
            font: 20px/100px 'Arial';
            color: blue;
            text-align: center;
        }
        .box {
            float: left;
        }
        .b {
            width: 500px;
            height: 100px;
            background-color: red;
        }
        /*浮动布局:
        1.子集浮动参照父级宽度
        2.子集浮动不再撑开父级高度
        3.父级高度需要自己处理,否则会影响兄弟布局,采用清浮动处理
        */
    </style>
    <style>

		/*会显示在标签内容的前面*/
        h1:before {
            content: '123';
        }
        
        /*当控制的控制自身完成所有布局(包含所有子集布局),再执行该时间点*/
        h1:after {
            content: '000';
        }
    </style>
</head>
<body>
    <h1>内容</h1>

    <!--.wrap>.box{$}*9-->
    <div class="wrap">
        <div class="box">1</div>
        <div class="box">2</div>
        <div class="box">3</div>
        <div class="box">4</div>
        <div class="box">5</div>
        <div class="box">6</div>
        <div class="box">7</div>
        <div class="box">8</div>
        <div class="box">9</div>
    </div>
    <div class="b"></div>
</body>
</html>

十一、两种布局总结

1.通过盒模型,自动获取外边距留白区域,能用内边距尽量使用内边距

2.通过浮动布局来实现子标签内容在父标签中显示,父级一定要用清浮动

3.父子级顶端产生距离,推荐使用padding

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>两种布局总结</title>

    <style>
        body, h1 {
            margin: 0;
        }
    </style>
    <style>
        .header {
            width: 1210px;
            height: 100px;
            background-color: orange;
            /*自动获取留白区域*/
            /*margin-left: auto;*/
            /*margin-right: auto;*/
            margin: 0 auto;
        }
        .header:after {
            content: '';
            display: block;
            clear: both;
        }
        .header a {
            display: block;
            width: 500px;
            height: 100px;
            background-color: red;
            float: left;
        }
        .header form {
            /*background-color: pink;*/
            float: right;
            /*父子级顶端产生距离,建议使用padding*/
            padding-top: 30px;
        }
        .header input {
            /*margin-right: 20px;*/
            width: 220px;
            height: 30px;
            border: none;
            font-size: 17px;
            vertical-align: top;
        }
        .header button {
            width: 32px;
            height: 32px;
            border: none;
            background-color: red;
            outline: none;
            color: white;
            margin-right: 30px;
            vertical-align: top;
        }

    </style>
</head>
<body>
    <!--盒模型:margin、padding协调操作,能用padding尽量用padding,再考虑用margin-->
    <!--浮动:需要左右排列的block采用浮动布局,且父级一定要清浮动-->
    <div class="header">
        <h1>
            <a href=""></a>
        </h1>
        <form method="get" action="https://www.baidu.com/s">
            <input type="text" name="wd">
            <button type="submit">Go</button>
        </form>
    </div>


</body>
</html>
posted @ 2019-07-31 15:57  newking_itman  阅读(148)  评论(0)    收藏  举报