CSS基础

前言:

CSS,层叠样式表,用于美化网页的。

分为:行内样式,行间样式,外部样式。

(1).行内样式:直接在元素标签中写"style="xxx:yyy;""

(2).行间样式:在<head>标签的<style></style>中写样式代码。

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
    </style>
</head>
View Code

(3).外部样式:把很多CSS写成一个文件,在<link>标签中的href属性里引入这个CSS文件。注意:要把这个标签写在<style>标签的外面。

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
    </style>
    <link rel="stylesheet" href="css/index.css">
</head>
View Code

(4).优先级问题

行内样式>行间样式=外部样式。(级别一样,哪个样式离这个标签最近就用那个,就近原则)

一、选择器

用处:用于精准地选中元素,并赋予样式。

(一).元素选择器。

    <style>
        /*星号*通配符,匹配所有元素*/
        * {
            margin: 0;
            padding: 0;
        }

        /*该样式,匹配所有的p标签*/
        p {
            text-align: center;
        }
    </style>
View Code

(二).class选择器

借助了一个"类"(python中的类),一处定义,可以多处使用。

语法:".classname{}"

    <style>
        /*这是类选择器*/
        .bigbox {
            width: 100px;
            height: 100px;
        }
    </style>
View Code

(三).id选择器

用过标签的id属性,选择对应的元素。id选择器唯一!

语法:"#idname{}"

    <style>
        /*这是id选择器*/
        #banner {
            width: 100px;
            height: 100px;
        }
    </style>
View Code

(四).群组选择器

可以同时选择多个标签的选择器。(同时选中多个,把多个选择器放在一起)

    <style>
        /*群组选择器*/
        p, span, .num {
        }
    </style>
View Code

(五).层次选择器

(1).子代。只能选择儿子这一代。

    <style>
        /*子代*/
        /*只能选中div下一级的span。如果有嵌套,就选不了*/
        div > span {
        }
    </style>
View Code

(2).后代。儿子、孙子都能选中。

    <style>
        /*后代*/
        /*div下面,所有的span都能被选中。不管套得多深*/
        div span {
        }
    </style>
View Code

(3).

(4).

猜你喜欢

转载自www.cnblogs.com/quanquan616/p/8971161.html
今日推荐