1. 程式人生 > >CSS非佈局樣式和重點內容

CSS非佈局樣式和重點內容

CSS基礎非佈局樣式

  • Cascading Style Sheet 層疊 、樣式、表
    可以簡單理解為PS中圖層的樣式和疊加

選擇器

selecter{
	prop:value;
}

選擇器用於匹配HTML元素,有不同的匹配規則,多個選擇器可以疊加


解析方式和效能
瀏覽器的解析方式是從右往左的

body div .class{
	color:red;
}
//瀏覽器先查詢.class,再查詢對應元素是否在div元素中再去查詢是否在body元素中

選擇器的分類

在這裡插入圖片描述

選擇器的權重

名稱 權重(不能進位)
important max
行間樣式 1000
id 100
class | 屬性 | 偽類 10
標籤 | 偽元素 1
萬用字元 0

字型分類

  • serif
  • sans-serif
  • monispace
  • cuisive
  • fantasy

這塊對於設計出身的小夥伴來說非常好理解。需要注意的地方就是在設定font-family的時候指定字型組是不要引號的

多字型fallback機制,一個個往下找,沒有就找同類的,再沒有就有啥用啥。┑( ̄Д  ̄)┍
fallback機制是直接面對文字的,而不是某個元素

.chinese{
            font-family: "PingFang SC", "Microsoft Yahei", monospace;
        }

引入自定義字型

        @font-face {
            font-family: "IF";
            src: url("./IndieFlower.ttf");//引用遠端字型需要對方允許跨域或者在頭部使用link到對方指定的css
        }
.custom-font{ font-family: IF; }

iconfont:可以理解為做成字型的向量圖示


行和行高

  • 行高的構成
  • 行高的相關現象和方案
  • 行高的調整

相關的知識點

行內元素的行高會撐起外部block,不會引起內部的變化 。

行內元素高度和字型大小有關係和line-height沒有關係。

同一塊級元素內的行內元素預設是以基線對齊的(所有內聯元素包括img都受其影響)
可以通過vertical-align修改對齊方式(基線 中線 頂線 底線)
vertical-align:0px;這樣寫的話是以基線為基礎的,正值是向上偏移


背景

  • 單色背景
  • 漸變色背景
  • 多背景疊加
  • 背景圖片和屬性(雪碧圖)
  • base64
  • 多解析度適配

單色背景的寫法

        .class{
        	height:90px;
            background:rgba(255,0,0, .3); 
            background:hsla(360,50%,80%,0.5)
            background:white
        }

漸變色背景

        .class2{
            height:90px;
            background: -webkit-linear-gradient(left, red, green);/* 由左到右 */
            background: linear-gradient(to right, red, green);/* 由左到右,標準寫法 */
            background: linear-gradient(180deg, red, green); /* 0deg是從下到上,順時針旋轉*/
            background: linear-gradient(135deg, red 0, green 10%, yellow 50%, blue 100%);/* 參照PS漸變,百分比寫的是位置*/
            background: linear-gradient(135deg, transparent 0, transparent 49.5%, green 49.5%, green 50.5%, transparent 50.5%, transparent 100%),
                		linear-gradient(45deg, transparent 0, transparent 49.5%, red 49.5%, red 50.5%, transparent 50.5%, transparent 100%);
            background-size: 30px 30px;/* 規定單個背景的大小*/
        }

background屬性和雪碧圖

background-repeat:no-repeat; //禁止平鋪; repeat-x /repeat-y
background-position:center center //也可以指定左上邊距
background-size: 30px 30px;
width:20px;
height:20px;
background:url(./test_bg.png) no-repeat;
background-position: -17px -5px;//相對於容器而非背景圖
background-size: 30px 30px;
//通過修改位置和大小來定位需要的區域

適應高清屏需要縮小圖片來增加dpi


base64 圖片

background:red url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHoAAAA2CAYAAAAIw43zAAAMJGlDQ1BJQ0MgUHJvZmlsZQAASImVlwdUU0kXx+eVJCQktEAEpITeBOlVeo1UqYKNkAQSSgiBoGJHFxVYCyoWrMiqiG0tgCwqYlcWwd4XRFCUdbFgQ+WbJICu+5XzzTnz5nfu3Lnzv/PmzXkDgHIUWyTKRFUAyBLmiaOD/ZiTE5OYpE5ABtpADRgAYzYnV+QbFRUGYBlp/17e3QKItL1uLY31z/7/WlS5vFwOAEgU5BRuLicL8lEAcGeOSJwHAKEP2o1m5okgE6FKoC6GAiEbSzlNzq5STpFzmMwnNtofcjIAClQ2W5wGgJJUFzOfkwbjKJVCthVyBULIjZC9OHw2F/JnyOOysrIhK5tDNk/5Lk7a32KmjMZks9NGWZ6LrCgECHJFmezZ/+dy/O+SlSkZmcMIVipfHBItzVm6bhnZoVKmQr4oTImIhKwG+YaAK/OXcg9fEhI37P+Bk+sP1wwwAECpXHZAKGQdyIaSjDjfYfZ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UylNSUVNZpio12oZx/1Hvbhikh95NoeSyAgcz0hAdJg3OUO3Cc7QfLj60sPQh4njDHe9QUm3mTdaKdVkp6N0j3AGWO5w9ztlp+5Vb9zghvW6MHFXAOnPUpNjVo7keXpyqB9t70fwyvgH0/CCLkCNxPBAJOoikBwVATW3o54joNslQUtNxbY8pob6cPv1MG7OqMiMoEFlTgrStX6SKbr7nQy0pIwgU2wonpanIUywAZIxlWmWB21zqC0gN12xlDcztu2BQx4M9JrXaJ6byluDe2UHoNM4qMybOCk5B21zrCdHJr3gyCQfbBaTJi+EB/Mr+38Ieje5697JabS2mzEujKhQtTcWpWlR0Pi6Bj5nNmFkkEztfWNoMLnevVVnJqE4RSvanAlfbjWPw2DohSk5g+zWg7guywC5yGmlv8jhpm4uAivIKpDq74f8XRroQjQIVC9Dty4uYNpsxohxBl0Tv9EyJ+vupMMLp2K2IX17ABd3ataMz0MTuDq+6MSPvouBptdK0ZYMtKLx0SfPQNNrpWhLBlrR+OiTZ6DptVK0JQOtaHz0yTPQ9Fop2pKBVjQ++uQZaHqtFG3JQCsaH33yDDS9Voq2ZKAVjY8+eQaaXitFW/4FdhzkzmcYrBcAAAAASUVORK5CYII=);
background-repeat: no-repeat;

圖片轉換成base64 體積會增大大約1/3
有傳輸優勢,但是會增加瀏覽器的負載,適用於小圖
生產環境中一般交給打包框架來做


邊框

  • 線形 大小 顏色
  • 邊框背景圖
  • 邊框銜接(預設斜切)//畫三角?23333
/*基礎使用方式*/
border: 1px solid red; 
border:5px solid red; 
border:5px dotted red; 
border:5px dashed red;
border-radius:2px;

/*邊框背景圖(九宮格、中間可複用)*/
width:400px;
height: 200px;
/* border-width: 30px; */
border:30px solid transparent;
border-image:url(./border.png) 30 round;  //round repeat 

滾動

  • 滾動行為和滾動條

在這裡插入圖片描述


文字折行

  • overflow-wrap (word-wrap) 通用換行控制
  • word-break 針對多位元組文字 單詞是否換行(中文句子也是單詞)
  • white-space 空白處是否斷行
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>warp</title>
    <style>
        .c1{
            border: 1px solid;
            width: 8em;
            overflow-wrap: normal;
            word-break: normal;
            white-space: normal;
        }

    </style>
</head>
<body>
    <div class="c1">
        This is a long and Supercalifragilisticexpialidocious sentence. 一二三四五六,七八九零一二,這是一句巨長巨長又沒有空間可以換行的句子哦。
    </div>
</body>
</html>

裝飾性屬性

  • 字重 font-weight
  • 斜體 font-style:itatic
  • 下劃線 text-decoration
  • 指標 cursor
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>fonts</title>
    <style>
        .weight{
            font-weight: normal;/*400*/
            font-weight: bold;/*700*/
            font-weight: bolder;
            font-weight: lighter;
            font-weight: 200;
        }
    </style>
</head>
<body class="body" id="body">
    <div class="weight">你好 Hello World</div>
</body>
</html>

CSS HACK(比較過時)

主要用於相容性
是一種不合法但生效的寫法,主要用於區分不同瀏覽器
缺點,難理解、難維護、易失效
替代方案:特性檢測,針對性加class

body{
	width:180px;
	width:160px\9;
}

在這裡插入圖片描述


**CSS 自定義checkbox**

圖片自己替換

 <!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>checkbox</title>
    <style>
        .checkbox{

        }
        .checkbox input{
            display: none;
        }
        .checkbox input + label{
            background:url(./checkbox1.png) left center no-repeat;
            background-size:20px 20px;
            padding-left:20px;
        }
        .checkbox input:checked + label{
            background-image:url(./checkbox2.png);
        }
    </style>
</head>
<body>
    <div class="checkbox">
        <input type="checkbox" id="handsome"/>
        <label for="handsome">我很帥</label>
    </div>
</body>
</html>