CSS選擇器、盒子模型、浮動與定位等詳解

一、CSS

1、學習目標

  1. CSS的作用及簡單使用(入門)
  2. CSS選擇器(重難點)
  3. 美化網頁
  4. 盒子模型
  5. 浮動與定位
  6. 網頁動畫

1.1、什麼是CSS

  • CSS:層疊級聯樣式表(Cascading Style Sheet),作用是爲了美化網頁,給網頁增添樣式,包括字體、顏色、位置、背景圖、定位與浮動等

  • CSS發展史:

    • CSS1.0
    • CSS2.0:DIV(塊) + CSS,產生了HTML 與 CSS 結構分離的思想,讓網頁變得簡單
    • CSS2.1:增添浮動,定位
    • CSS3.0:增添圓角,陰影,動畫…. 瀏覽器兼容性更好

1.2、CSS樣式的兩種寫的方法

  1. 使用style標籤在HTML代碼中添加CSS樣式(不推薦使用,要求CSS樣式與HTML代碼分開寫)
<!--規範,在HTML代碼中可以在<style>標籤中可以編寫css的代碼 ,每一行CSS代碼之間,最好使用分號結尾
    語法:
        選擇器 {
            聲明1;
            聲明2;
            聲明3;
        }
-->

舉例:給h1標籤中的字設置顏色

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        h1{
            color: red;
        }
    </style>
</head>
    
<body>
<h1>我是標題</h1>
</body>
    
</html>
  1. 單獨在一個CSS文件中編寫樣式(推薦使用)
h1{
     color: red;
}
  1. 使用CSS的優勢
    1. 內容與樣式分離
    2. 網頁結構表現統一,可以實現複用
    3. 使用SEO,容易被搜索引擎收錄,擴大影響
    4. 建議CSS樣式與HTML文件獨立

1.3、CSS的3種導入方式

  1. 內部樣式(在HTML文件中,不推薦)

     <!--內部樣式-->
        <style>
            h1{
                color: green;
            }
        </style>
    
  2. 外部樣式-鏈接式(CSS與HTML分離,推薦使用)

    <!--鏈接式-->
    <link rel="stylesheet" href="css/style.css">
  3. 外部樣式-導入式(CSS與HTML分離,推薦使用,是CSS2.1纔有的)

    <!--導入式-->
    <style>
        @import url("css/style.css");
    </style>
  4. 行內樣式(在HTML文件中,不推薦)

    <!--行內樣式:在標籤元素中,編寫一個style屬性,編寫樣式-->
    <h1 style="color:red;">我是標題</h1>
  5. 幾種樣式的優先級

    就近原則,哪個近哪個生效

2、選擇器(重要)

作用:選擇頁面上的某一個或某一類元素

2.1、基本選擇器

  1. 標籤選擇器(選擇一類元素,使用 {}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        /*標籤選擇器,會選擇到頁面上所有的這個標籤的元素*/
        /*使用標籤選擇器對所有的h1標籤進行樣式的設置*/
        h1{
            color: #a13d30;
            background: #3cbda6;
            border-radius: 24px;
        }
        /*使用標籤選擇器對所有的p標籤進行樣式的設置*/
        p{
            font-size: 80px;
        }
    </style>

</head>
<body>
    
<h1>好好學習</h1>
<h1>天天向上</h1>
<p>開開心心</p>
    
</body>
</html>
  1. 類選擇器(選擇所有class屬性相同的元素,用 .class屬性名{}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        /*類選擇器格式: .class的名稱{}
        只要class屬性的值相同,都可以選擇上
        */
        .class1{
            color: #3748ff;
        }
        .class2{
            color: #a24fff;
        }
    </style>

</head>
<body>

<h1 class="class1">標題1</h1>
<h1 class="class2">標題2</h1>
<h1 class="class1">標題3</h1>
<p class="class2">P標籤</p>
    
</body>
</html>
  1. ID選擇器(選擇指定ID屬性值的元素,ID屬性值全局唯一,用 #ID名稱{}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        /* id選擇器   : id必須保證全局唯一
           格式:#id名稱{}
        */
        #id1{
            color: #ff008a;
        }
    </style>
    
</head>
<body>

<h1 id="id1">標題1</h1>

</body>
</html>
  1. 基本選擇器的優先級
    不遵循就近原則,固定的:ID選擇器> class 選擇器 > 標籤選擇器

2.2、層次選擇器

  1. 後代選擇器(從當前元素開始,後面的所有代元素,用 body 標籤名稱{}
/*後代選則器*/
body p{
    background: red;
}
  1. 子代選擇器(從當前的元素開始,後面的一代元素,用 body>標籤名{}
/*子選擇器*/
body>p{
    background: #3cbda6;
}
  1. 相鄰兄弟選擇器(當前元素的同代向下的第一個元素,用.active+標籤名{}
/*相鄰弟選擇器: 只有一個,相鄰(向下) */
.active + p{
	background: #a13d30;
}
  1. 通用選擇器(當前元素的同代向下的所有元素,用 .active~標籤名{}
/*通用兄弟選則器,當前選中元素的向下的所有兄弟元素*/
.active~p{
    background: #02ff00;
}

2.3、結構僞類選擇器

後面帶有冒號

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <!--避免使用,class,id選擇器-->
    <style>
        /*ul的第一個子元素*/
        ul li:first-child{
            background: #02ff00;
        }

        /*ul的最後一子元素*/
        ul li:last-child{
            background: #ff4832;
        }

        /* 選中 p1 : 定位到父元素,選擇當前的第一個元素
        選擇當前p元素的父級元素,選中父級元素的第一個,並且是當前元素才生效! ,順序
        */
        p:nth-child(1){
            background: #2700ff;
        }

        /*選中父元素下的p元素的第二個,類型 */
        p:nth-of-type(2){
            background: yellow;
        }

        /*a:hover{*/
            /*background: #000b3e;*/
        /*}*/

    </style>

</head>
<body>

    <p>p1</p>
    <p>p2</p>
    <p>p3</p>
    <ul>
        <li>li1</li>
        <li>li2</li>
        <li>li3</li>
    </ul>

</body>
</html>

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-bKHYQse7-1581226642321)(assets/1580986110536.png)]

2.4、屬性選擇器

格式:標籤名[條件]{},條件可以是ID屬性,也可以是class屬性

條件連接符 含義
= 完全相等
*= 包含這個元素
^= 以這個元素開頭
$= 以這個元素結尾
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .demo a{
            float: left;
            display: block;
            height: 50px;
            width: 50px;
            border-radius: 10px;
            background: #2700ff;
            text-align: center;
            color: gainsboro;
            text-decoration: none;
            margin-right: 5px;
            font: bold 20px/50px Arial;
        }

        /*存在id屬性的元素        a[]{}*/
        /*a[id]{
            background: yellow;
        }*/
        
        /*id=first的元素*/
        /*a[id=first]{
           background: #63ff23;
        }*/

        /*class 中有 links的元素*/
        /*a[class*="links"]{
            background: yellow;
        }*/

        /*!*選中href中以http開頭的元素*!*/
        /*a[href^=http]{
            background: yellow;
        }*/

        a[href$=jpg]{
            background: yellow;
        }

    </style>

</head>
<body>

<p class="demo">

    <a href="http://www.baidu.com" class="links item first" id="first">1</a>
    <a href="http://blog.kuangstudy.com" class="links item active" target="_blank" title="test">2</a>
    <a href="images/123.html" class="links item">3</a>
    <a href="images/123.png" class="links item">4</a>
    <a href="images/123.jpg" class="links item">5</a>
    <a href="abc" class="links item">6</a>
    <a href="/a.pdf" class="links item">7</a>
    <a href="/abc.pdf" class="links item">8</a>
    <a href="abc.doc" class="links item">9</a>
    <a href="abcd.doc" class="links item last">10</a>

</p>

</body>
</html>

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-XY42d3xa-1581226642322)(assets/1580987733016.png)]

3、美化網頁元素

3.1、爲什麼美化網頁

  1. 有效傳遞頁面信息
  2. 凸顯頁面主題
  3. 提高用戶的體驗,吸引用戶

span標籤,將要重點突出的字,使用span標籤套起來(相當於h標籤)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        #title1{
            font-size: 50px;
        }
    </style>

</head>
<body>

歡迎學習 <span id="title1">CSS</span>

</body>
</html>

3.2、字體樣式

屬性 含義
font-family 字體
font-size 字體大小
font-weight 字體粗細
color 字體顏色
<style>
    body{
        font-family: "Arial Black", 楷體;
        color: #a13d30;
    }
    h1{
        font-size: 50px;
    }
    .p1{
        font-weight: bolder;
    }
</style>

3.3、文本樣式

樣式 屬性
顏色 color(rgb/rgba)
文本對齊方式 text-align(居中center)
首行縮進 text-indent(2em)
行高 line-height
裝飾(下劃線) text-decoration
文本圖片水平對齊 vertical-align(middle)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <!--
    顏色:
        單詞
        RGB 0~F
        RGBA  A:0~1

    text-align : 排版,居中,
    text-indent: 2em;  段落首行縮進
    height: 300px;
    line-height: 300px;
        行高,和 塊的高度一致,就可以上下居中
    -->
    <style>
        h1{
            color: rgba(0,255,255,0.9);
            text-align: center;
        }
        .p1{
            text-indent: 2em;
        }
        .p3{
            background: #2700ff;
            height: 300px;
            line-height: 300px;
        }
        /*下劃線*/
        .l1{
            text-decoration: underline;
        }
        /*中劃線*/
        .l2{
            text-decoration: line-through;
        }
        /*上劃線*/
        .l3{
            text-decoration: overline;
        }
        /*超鏈接去下劃線*/
        a{
            text-decoration: none;
        }

        /*<!--
        水平對齊 參照物,  a,b
        -->*/
        img,span{
            vertical-align: middle;
        }

    </style>
    
</head>
<body>

<a href="">123</a>

<p class="l1">1231231</p>
<p class="l2">1231231</p>
<p class="l3">1231231</p>

<h1>故事介紹</h1>

<p class="p1">
    平靜安詳的元泱境界,每隔333年,總會有一個神祕而恐怖的異常生物重生,它就是魁拔!魁拔的每一次出現,都會給元泱境界帶來巨大的災難!
</p>

<p>
    在偏遠的獸國窩窩鄉,蠻大人和蠻吉每天爲取得象徵成功和光榮的妖俠紋耀而刻苦修煉,卻把他們生活的村莊攪得雞犬不寧。村民們絞盡腦汁把他們趕走。一天,消滅魁拔的徵兵令突然傳到窩窩鄉,村長趁機慫恿蠻大人和蠻吉從軍參戰。
</p>

<p class="p3">
    Since there’s no help, come let us kiss and part;Nay, I have done, you get no more of me,And I am glad, yea glad with all my heartThat thus so cleanly I myself can free.
</p>

<p>
    <img src="images/a.png" alt="">
    <span>asdasdajsldjalksdjalksd</span>
</p>

q
</body>
</html>

3.4、陰影

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-1XadN3h5-1581226642323)(assets/1580988647526.png)]

標籤的 text-shadow屬性,後面4個值分別是:陰影顏色、水平偏移、垂直偏移、陰影半徑

/*text-shadow: 陰影顏色,水平偏移,垂直偏移,陰影半徑*/
#price{
    text-shadow: #3cc7f5 10px -10px 2px;
}

3.5、超鏈接僞類

鼠標懸浮在元素上的事件:標籤名:hover{顏色等屬性設置}

/*默認的顏色*/
a{
    text-decoration: none;
    color: #000;
}
/*鼠標懸浮的狀態(只需要記住 :hover)*/
a:hover{
    color: orange;
    font-size: 50px;
}

3.6、列表

list-style:選擇列表前的圖案,none(去掉原點)、circle (空心圓)、decimal (數字)、square(正方形)

/*ul li
ul{
    background: #a0a0a0;
}*/

ul li{
    height: 30px;
    list-style: none;
    text-indent: 1em;
}

3.7、背景

背景顏色:background

背景圖片

<style>
div{
    width: 1000px;
    height: 700px;
    border: 1px solid red;
    background-image: url("images/tx.jpg");
    /*默認是全部平鋪的 repeat*/
}
.div1{
    background-repeat: repeat-x;
}
.div2{
    background-repeat: repeat-y;
}
.div3{
    background-repeat: no-repeat;
}
</style>

3.8、漸變

background-color: #FFFFFF;
background-image: linear-gradient(115deg, #FFFFFF 0%, #6284FF 50%, #FF0000 100%)

4、盒子模型

4.1、什麼是盒子模型

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-ZAZt9dkc-1581226642324)(assets/1580989269093.png)]

margin:外邊距、padding: 內邊距、border:邊框

盒子模型由內容區、填充、邊框和空白邊四部分組成。內容區是盒子模型的中心,呈現盒子的主要信息內容;填充是內容區和邊框之間的空間;邊框是環繞內容區和填充的邊界;空白邊位於盒子的最外圍,是添加在邊框外周圍的空間。

4.2、邊框

使用border屬性進行邊框的設置,如邊框粗細、樣式、顏色,語法爲: border:粗細 樣式 顏色

默認的外邊距margin=0,內邊距padding=0,邊框爲none

4.3、內外邊距

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <!--外邊距的妙用:居中元素
     margin: 0 auto;
    -->
    <style>

        #box{
            width: 300px;
            border: 1px solid red;
            margin: 0 auto;
        }
        /*
        順時針旋轉
        margin:0
        margin:0 1px
        margin:0 1px  2px   3px
        */
        h2{
            font-size: 16px;
            background-color: #3cbda6;
            line-height: 30px;
            color: white;
            margin:0 1px 2px 3px;
        }

        form{
            background: #3cbda6;
        }
        input{
            border: 1px solid black;
        }
        div:nth-of-type(1){
            padding: 10px 2px;
        }
    </style>

</head>
<body>

<div id="box">
    <h2>會員登錄</h2>
    <form action="#">
        <div>
            <span>用戶名:</span>
            <input type="text">
        </div>
        <div>
            <span>密碼:</span>
            <input type="text">
        </div>
        <div>
            <span>郵箱:</span>
            <input type="text">
        </div>
    </form>
</div>

</body>
</html>

盒子的計算方式,要包含內外邊距與邊框,margin + border + padding + 內容寬度

4.4、圓角邊框

border-radius屬性

<style>
div{
    width: 100px;
    height: 100px;
    border: 10px solid red;
    border-radius: 100px;
}
</style>

4.5、盒子陰影

box-shadow屬性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

<!-- margin: 0 auto; 居中
要求: 塊元素,塊元素有固定的寬度
-->
    <style>
        img{
            border-radius: 50px;
            box-shadow: 10px 10px 100px yellow;
        }
    </style>

</head>

<body>

    <div style="width: 500px;display: block;text-align: center">
        <img src="images/tx.jpg" alt="">
    </div>

</body>
    
</html>

5、浮動

5.1、標準文檔流

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-nPUVDTrM-1581226642324)(assets/1580990111949.png)]

塊級元素:獨自佔用一行,比如:h1~h6 p div 列表…

行內元素:不獨自佔用一行,比如:span a img strong…

行內元素可以被包含在塊級元素之間,但是塊級元素不能被包含在塊級元素之間

5.2、display

使用display屬性解決元素間的排列問題,display屬性的值: block(塊元素)、inline (行內元素)、inline-block(塊元素,但是可以內聯在一行)、none,但是使用的不多,更多的使用float屬性

<style>
    div{
        width: 100px;
        height: 100px;
        border: 1px solid red;
        display: none;
    }
    span{
        width: 100px;
        height: 100px;
        border: 1px solid red;
        display: inline-block;
    }
</style>

5.3、float

  1. 左右浮動
div {
    margin:10px;
    padding:5px;
}
#father {
    border:1px #000 solid;
}
.layer01 {
    border:1px #F00 dashed;
    display: inline-block;
    float: right;
}
.layer02 {
    border:1px #00F dashed;
    display: inline-block;
    float: right;
}
.layer03 {
    border:1px #060 dashed;
    display: inline-block;
    float: right;
}
.layer04 {
    border:1px #666 dashed;
    font-size:12px;
    line-height:23px;
    display: inline-block;
    float: right;
}

5.4、父級邊框塌陷問題

使用clear屬性解決

屬性值 含義
clear: right 右側不允許有浮動元素
clear: left 左側不允許有浮動元素
clear: both 兩側不允許有浮動元素
clear: none 清除浮動

解決方案

  1. 增加父級元素的高度
#father {
    border:1px #000 solid;
    height: 800px;
}
  1. 增加一個空的div標籤,清除浮動
<div class="clear"></div>

.clear{
    clear: both;
    margin: 0;
    padding: 0;
}
  1. 在父級元素中增加一個overflow屬性,overflow: hidden

  2. 父類添加一個僞類after

#father:after{
    content: '';
    display: block;
    clear: both;
}

幾種解決方案比較

  1. 浮動元素後面增加空div,雖然簡單,但是代碼中儘量避免空div
  2. 設置父元素的高度,也簡單,但元素假設有了固定的高度,就會被限制
  3. overflow,也簡單,但在有下拉的一些場景避免使用
  4. 父類添加一個僞類:after,寫法稍微複雜一點,但是沒有副作用,推薦使用!

5.5、display與float對比

  1. display方向不可以控制
  2. float浮動起來會脫離標準文檔流,需要解決父級邊框塌陷問題

6、定位

6.1、相對定位

相對於自己原來的位置進行指定的偏移,任然在標準文檔裏,原來的位置會被保留。

相對定位:position: relative

方向 含義
top 距離原來的位置向上的距離
bottom 距離原來的位置向下的距離
left 距離原來的位置向左的距離
right 距離原來的位置向上右的距離
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <!-- 相對定位
    相對於自己原來的位置進行偏移~
    -->
    <style>
        body{
            padding: 20px;
        }
        div{
            margin: 10px;
            padding: 5px;
            font-size: 12px;
            line-height: 25px;
        }
        #father{
            border: 1px solid #666;
            padding: 0;
        }
        #first{
            background-color: #a13d30;
            border: 1px dashed #b27530;
            position: relative; /*相對定位:上下左右*/
            top: -20px;
            left: 20px;
        }
        #second{
            background-color: #255099;
            border: 1px dashed #255066;
        }
        #third{
            background-color: #1c6699;
            border: 1px dashed #1c6615;
            position: relative;
            bottom: -10px;
            right: 20px;
        }
    </style>

</head>
<body>

<div id="father">
    <div id="first">第一個盒子</div>
    <div id="second">第二個盒子</div>
    <div id="third">第三個盒子</div>
</div>

</body>
</html>

6.2、絕對定位

基於某一個固定的位置定位,不在標準文檔流中,原來的位置不會被保留

  1. 在沒有父級元素定位的前提下,相對於瀏覽器進行定位
  2. 有父級元素,相對於父級元素定位
  3. 在父級元素的範圍內進行偏移

絕對定位:position: absolute

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        div{
            margin: 10px;
            padding: 5px;
            font-size: 12px;
            line-height: 25px;
        }
        #father{
            border: 1px solid #666;
            padding: 0;
            position: relative;
        }
        #first{
            background-color: #a13d30;
            border: 1px dashed #b27530;
        }
        #second{
            background-color: #255099;
            border: 1px dashed #255066;
            position: absolute;

            left: 100px;
        }
        #third{
            background-color: #1c6699;
            border: 1px dashed #1c6615;
        }
    </style>

</head>
<body>

<div id="father">
    <div id="first">第一個盒子</div>
    <div id="second">第二個盒子</div>
    <div id="third">第三個盒子</div>
</div>

</body>
</html>

6.3、固定定位

相對於屏幕(瀏覽器邊框)進行定位,不隨頁面的變化而變

固定定位:position: fixed

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        body{
            height: 1000px;
        }
        div:nth-of-type(1){ /*絕對定位:相對於瀏覽器*/
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
            right: 0;
            bottom: 0;
        }
        div:nth-of-type(2){ /*fixed,固定定位*/
            width: 50px;
            height: 50px;
            background: yellow;
            position: fixed;
            right: 0;
            bottom: 0;
        }
    </style>

</head>
<body>

<div>div1</div>
<div>div2</div>

</body>
</html>

6.4、z-index

z-index 屬性指定一個元素的堆疊順序,擁有更高堆疊順序的元素總是會處於堆疊順序較低的元素的前面。

默認是0,最高無限

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>

        #content{
            width: 380px;
            padding: 0px;
            margin: 0px;
            overflow: hidden;
            font-size: 12px;
            line-height: 25px;
            border: 1px #000 solid;
        }
        ul,li{
            padding: 0px;
            margin: 0px;
            list-style: none;
        }
        /*父級元素相對定位*/
        #content ul{
            position: relative;
        }
        .tipText, .tipBg{
            position: absolute;
            width: 380px;
            height: 25px;
            top: 216px;
        }
        .tipText{
            color: white;
            /*z-index: 0;*/
        }
        .tipBg{
            background: #000;
            opacity: 0.5; /*背景透明度*/
            filter: Alpha(opacity=50);
        }

    </style>
</head>
<body>

<div id="content">
    <ul>
        <li><img src="images/bg.jpg" alt=""> </li>
        <li class="tipText">我要學習CSS</li>
        <li class="tipBg"></li>
        <li>時間:2019-01-01</li>
        <li>地點:中國陝西</li>
    </ul>
</div>

</body>
</html>
Title
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章