整合營(yíng)銷服務(wù)商

          電腦端+手機(jī)端+微信端=數(shù)據(jù)同步管理

          免費(fèi)咨詢熱線:

          CSS實(shí)用技巧(上)

          CSS實(shí)用技巧(上)

          張?chǎng)涡竦摹禖SS世界》這本書,強(qiáng)烈推薦前端er仔細(xì)閱讀下,里面詳細(xì)說明了許多不怎么被注意的CSS特性,對(duì)前端進(jìn)階很有幫助。
          本文簡(jiǎn)要列舉書中前四章比較實(shí)用的知識(shí)點(diǎn),書中干貨很多,值得一讀。

          實(shí)用技巧

          文字少的時(shí)候居中顯示,多的時(shí)候居左顯示

          利用元素的包裹性,元素的尺寸由內(nèi)部元素決定,且永遠(yuǎn)小于容器的寬度。

          具有包裹性的元素:inline-block、浮動(dòng)元素、絕對(duì)定位元素。

          <style>
            .content{
              display: inline-block;
              text-align: left;
            }
            .box{
              text-align: center;
            }
          </style>
          <div class="box">
            <span class="content"></span>
          </div>
          

          你不知道的min-width/min-height, max-width/max-height

          • 初始值

          min-width/min-height初始值是auto,max-height/max-width初始值是none

          設(shè)置min-height漸變效果,需要指定min-height的值。

          <style>
            .box{
              min-height: 20px;
              width: 200px;
              background-color: coral;
              transition: min-height .5s;;
            }
            .box:hover{
              min-height: 300px;
            }
          </style>
          <template>
            <div class="box"></div>
          </template>
          
          • 覆蓋規(guī)則

          超越important,超越最大。簡(jiǎn)而言之,min-width/min-height,max-width/max-height會(huì)覆蓋width/height。當(dāng)min-xxxmax-xxx設(shè)置相沖突時(shí),實(shí)際結(jié)果以min-xxx為準(zhǔn)。

          不可忽略的幽靈空白節(jié)點(diǎn)

          如下代碼,div沒有設(shè)置寬高,span為空白標(biāo)簽,但是div的高度卻為18px,這個(gè)高度是由字體大小和行高決定的,要想去除這個(gè)影響,需要將font-size設(shè)置為0

          <style>
            div {
              background-color: #cd0000;
            }
            span {
              display: inline-block;
            }
          </style>
          <template>
           <div><span></span></div>
          </template>
          

          圖片根據(jù)寬高自適應(yīng)

          指定圖片寬高,使圖片自適應(yīng)寬高,常用的兩種方式,第一種是background,第二種是object-fit。常用屬性如下:

          background-size

          object-fit

          CSS屬性

          說明

          cover

          cover

          覆蓋

          會(huì)存在圖片展示不全

          contain

          contain

          包含

          等比縮放,空白自動(dòng)填充

          --

          fill(默認(rèn))

          填充

          不符合尺寸,橫向、縱向壓縮

          功能強(qiáng)大的content

          CSS content屬性結(jié)合before/after偽類,能實(shí)現(xiàn)很多意想不到的效果。

          • ...動(dòng)態(tài)加載效果
          <style>
          dot {
            display: inline-block;
            height: 1em;
            line-height: 1;
            text-align: left;
            vertical-align: -.25em;
            overflow: hidden;
          } 
          dot::before {
            display: block;
            content: '...\A..\A.';
            white-space: pre-wrap;
            animation: dot 3s infinite step-start both;
          } 
          @keyframes dot {
            33% { transform: translateY(-2em); }
            66% { transform: translateY(-1em); }
          }
          </style>
          <template>
            <div>
              正在加載中<dot>...</dot>
            </div>
          </template>
          
          • contenteditable可編輯元素placeholder
          <style>
          .edit{
            width: 200px;
            height: 50px;
            background-color: azure;
          }
          .edit:empty::before{
            content: attr(data-placeholder);
          }
          </style>
          <template>
            <div class="edit" contenteditable="true" data-placeholder="this is a placeholder"></div>
          </template>
          

          padding的妙用

          background-clip可以設(shè)置background作用范圍,結(jié)合padding,可以做出一些好玩的東西。

          • 雙層圓點(diǎn)
          <style>
          .icon-dot {
            display: inline-block;
            width: 100px; height: 100px;
            padding: 10px;
            border: 10px solid;
            border-radius: 50%;
            background-color: currentColor;
            background-clip: content-box;
          }
          </style>
          <template>
            <span class="icon-dot"></span>
          </template>
          

          margin的奇淫技巧

          • 左右兩列等高布局
          <style>
          .column-box {
            overflow: hidden;
          } 
          .column-left,
          .column-right {
            margin-bottom: -9999px;
            padding-bottom: 9999px;
            float: left;
          }
          .column-left{
            width: 100px;
            background-color: #ccc;
          }
          .column-right{
            width: 100px;
            background-color: aquamarine;
          }
          </style>
          <template>
          <div class="column-box">
              <div class="column-left">
                123
              </div>
              <div class="column-right">
                456    
              </div>
            </div>
          </template>
          
          • 一端固定,另外一端自適應(yīng)
          <style>
          .left{
            width: 200px;
            height: 100%;
            float: left;
          }
          .right{
            margin-left: 200px;
          }
          </style>
          <template>
            <body>
              <div class='left'></div>
              <div class='right'></div>
            </body>
          </template>
          
          • 塊級(jí)元素垂直水平居中

          家好,我是IT共享者,人稱皮皮。這篇文章我們來講講CSS的文本樣式。

          一、文本顏色Color

          顏色屬性被用來設(shè)置文字的顏色。

          顏色是通過CSS最經(jīng)常的指定:

          • 十六進(jìn)制值 - 如"#FF0000"。
          • 一個(gè)RGB值 - "RGB(255,0,0)"。
          • 顏色的名稱 - 如"紅"。

          一個(gè)網(wǎng)頁的文本顏色是指在主體內(nèi)的選擇:

          <html>
              <head>
                  <meta charset="utf-8">
                  <meta name="viewport" content="width=640, user-scalable=no">
                  <title>項(xiàng)目</title>
                  <style>
                      body {
                          color: blue;
                      }
          
          
                      h1 {
                          color: #00ff00;
                      }
          
          
                      h2 {
                          color: rgb(255, 0, 0);
                      }
          </style>
              </head>
          
          
              <body>
                  <h2>hello world</h2>
                  <h1>welcome to CaoZhou</h1>
              </body>
          
          
          </html>

          注:對(duì)于W3C標(biāo)準(zhǔn)的CSS:如果你定義了顏色屬性,你還必須定義背景色屬性。


          二、屬性

          1. text-align 文本的對(duì)齊方式

          文本排列屬性是用來設(shè)置文本的水平對(duì)齊方式。

          文本可居中或?qū)R到左或右,兩端對(duì)齊。

          當(dāng)text-align設(shè)置為"justify",每一行被展開為寬度相等,左,右外邊距是對(duì)齊(如雜志和報(bào)紙)。

          <!doctype html>
          <html lang="en">
          
          
              <head>
                  <meta charset="UTF-8">
                  <title>Document</title>
                  <style>
                      h1 {
                          text-align: center;
                      }
          
          
                      p.date {
                          text-align: right;
                      }
          
          
                      p.main {
                          text-align: justify;
                      }
          </style>
              </head>
          
          
              <body>
          
          
                  <p class="date">2015 年 3 月 14 號(hào)</p>
                  <p class="main"> 從前有個(gè)書生,和未婚妻約好在某年某月某日結(jié)婚。到那一天,未婚妻卻嫁給了別人。書生受此打擊, 一病不起。  這時(shí),路過一游方僧人,從懷里摸出一面鏡子叫書生看。書生看到茫茫大海,一名遇害的女子一絲不掛地躺在海灘上。路過一人, 看一眼,搖搖頭,走了。又路過一人,將衣服脫下,給女尸蓋上,走了。再路過一人,過去,挖個(gè)坑,小心翼翼把尸體掩埋了。  僧人解釋道, 那具海灘上的女尸,就是你未婚妻的前世。你是第二個(gè)路過的人,曾給過他一件衣服。她今生和你相戀,只為還你一個(gè)情。但是她最終要報(bào)答一生一世的人,是最后那個(gè)把她掩埋的人,那人就是他現(xiàn)在的丈夫。書生大悟,病愈。
          
          
                  </p>
                  <p><b>注意:</b> 重置瀏覽器窗口大小查看 "justify" 是如何工作的。</p>
              </body>
          
          
          </html>

          2. text-decoration文本修飾

          text-decoration 屬性用來設(shè)置或刪除文本的裝飾。

          從設(shè)計(jì)的角度看 text-decoration屬性主要是用來刪除鏈接的下劃線:

          <!doctype html>
          <html lang="en">
          
          
              <head>
                  <meta charset="UTF-8">
                  <title>Document</title>
                  <style>
                      .none {}
          
          
                      .del {
                          text-decoration: none;
                      }
          </style>
              </head>
          
          
              <body>
                  <p>原來的樣子</p>
                  <a href="#" class="none">wwwwwwwwwwwwwwwwww</a>
                  <p>去掉下劃線</p>
                  <a href="#" class="del">wwwwwwwwwwwwwwwwwwwww</a>
              </body>
          
          
          </html>

          也可以這樣裝飾文字:

          <html>
              <head>
                  <meta charset="utf-8">
                  <meta name="viewport" content="width=640, user-scalable=no">
                  <title>項(xiàng)目</title>
                  <style>
                      h1 {
                          text-decoration: overline;
                      }
          
          
                      h2 {
                          text-decoration: line-through;
                      }
          
          
                      h3 {
                          text-decoration: underline;
                      }
          </style>
              </head>
          
          
              <body>
                  <h1>This is heading 1</h1>
                  <h2>This is heading 2</h2>
                  <h3>This is heading 3</h3>
              </body>
          
          
          </html>

          注:不建議強(qiáng)調(diào)指出不是鏈接的文本,因?yàn)檫@常常混淆用戶。


          3. text-transform文本轉(zhuǎn)換

          text-transform文本轉(zhuǎn)換屬性是用來指定在一個(gè)文本中的大寫和小寫字母。

          • uppercase:轉(zhuǎn)換為全部大寫。
          • lowercase:轉(zhuǎn)換為全部小寫。
          • capitalize :每個(gè)單詞的首字母大寫。
          <!DOCTYPE html>
          <html>
          
          
              <head>
                  <meta charset="utf-8">
                  <meta name="viewport" content="width=640, user-scalable=no">
                  <title>項(xiàng)目</title>
                  <style>
                      p.uppercase {
                          text-transform: uppercase;
                      }
          
          
                      p.lowercase {
                          text-transform: lowercase;
                      }
          
          
                      p.capitalize {
                          text-transform: capitalize;
                      }
          </style>
              </head>
          
          
              <body>
                  <p class="uppercase">This is some text.</p>
                  <p class="lowercase">This is some text.</p>
                  <p class="capitalize">This is some text.</p>
              </body>
          
          
          </html>

          4. text-indent文本縮進(jìn)

          text-indent文本縮進(jìn)屬性是用來指定文本的第一行的縮進(jìn)。

          p {text-indent:50px;}

          5. letter-spacing 設(shè)置字符間距

          增加或減少字符之間的空間。

          <style>
               h1 {
                 letter-spacing:2px;
          }
                h2 {
                  letter-spacing:-3px;
          }
          </style>

          6. line-height設(shè)置行高

          指定在一個(gè)段落中行之間的空間。

          <html>
              <head>
                  <meta charset="utf-8">
                  <meta name="viewport" content="width=640, user-scalable=no">
                  <title>項(xiàng)目</title>
                  <style>
                      p.small {
                          line-height: 70%;
                      }
          
          
                      p.big {
                          line-height: 200%;
                      }
          </style>
              </head>
          
          
              <body>
                  <p>
                      This is a paragraph with a standard line-height.<br> This is a paragraph with a standard line-height.<br> The default line height in most browsers is about 110% to 120%.<br>
                  </p>
          
          
                  <p class="small">
                      This is a paragraph with a smaller line-height.<br> This is a paragraph with a smaller line-height.<br> This is a paragraph with a smaller line-height.<br> This is a paragraph with a smaller line-height.<br>
                  </p>
          
          
                  <p class="big">
                      This is a paragraph with a bigger line-height.<br> This is a paragraph with a bigger line-height.<br> This is a paragraph with a bigger line-height.<br> This is a paragraph with a bigger line-height.<br>
                  </p>
          
          
              </body>
          
          
          </html>

          7. word-spacing 設(shè)置字間距

          增加一個(gè)段落中的單詞之間的空白空間。

          <html>
              <head>
                  <meta charset="utf-8">
                  <meta name="viewport" content="width=640, user-scalable=no">
                  <title>項(xiàng)目</title>
                  <style type="text/css">
                      p {
                          word-spacing: 30px;
                      }
          </style>
              </head>
          
          
              <body>
          
          
                  <p>
                      This is some text. This is some text.
                  </p>
          
          
              </body>
          
          
          </html>

          8. vertical-align 設(shè)置元垂直居中

          設(shè)置文本的垂直對(duì)齊圖像。

          <html>
              <head>
                  <meta charset="utf-8">
                  <meta name="viewport" content="width=640, user-scalable=no">
                  <title>項(xiàng)目</title>
                  <style>
                      img{
                          width: 200px;
                          height: 100px;
                      }
                      img.top {
                          vertical-align: text-top;
          
          
                      }
          
          
                      img.bottom {
                          vertical-align: text-bottom;
          
          
                      }
          </style>
              </head>
          
          
              <body>
                  <p>An <img src="img/logo.png"  /> image with a default alignment.</p>
                  <p>An <img class="top" src="img/logo.png" /> image with a text-top alignment.</p>
                  <p>An <img class="bottom" src="img/logo.png" /> image with a text-bottom alignment.</p>
              </body>
          
          
          </html>

          9. text-shadow 設(shè)置文本陰影

          設(shè)置文本陰影。

          <html>
              <head>
                  <meta charset="utf-8">
                  <meta name="viewport" content="width=640, user-scalable=no">
                  <title>項(xiàng)目</title>
                  <style>
                   h1{
                      text-shadow: 2px 2px #FF0000;
               }
          </style>
              </head>
          
          
              <body>
              <h1>Text-shadow effect</h1>
              </body>
          
          
          </html>

          三、總結(jié)

          本文主要介紹了CSS文本樣式實(shí)際應(yīng)用中應(yīng)該如何去操作,通過講解文本中對(duì)應(yīng)的屬性去改變文本的表現(xiàn)形式。使用豐富的效果圖的展示,能夠更直觀的看到運(yùn)行的效果,能夠更好的理解。使用Html語言,代碼結(jié)構(gòu)更佳的清晰,能夠幫助你更好的學(xué)習(xí)。

          eta標(biāo)簽介紹

          meta標(biāo)簽是HTML語言head區(qū)域的一個(gè)輔助性標(biāo)簽,常用于定義頁面的說明,關(guān)鍵字,最后修改的日期和其他的元數(shù)據(jù)。這些元數(shù)據(jù)將服務(wù)于瀏覽器,搜索引擎和其他網(wǎng)絡(luò)服務(wù)。

          meta標(biāo)簽的組成

          meta標(biāo)簽共有兩個(gè)屬性,分別是http-equiv屬性和name屬性。

          name屬性

          name屬性主要是用于描述網(wǎng)頁,比如網(wǎng)頁的關(guān)鍵詞,敘述等。與之對(duì)應(yīng)的屬性值為content,content中的內(nèi)容是對(duì)name填入類型的具體描述,便于搜索引擎抓取。

          meta標(biāo)簽中name屬性語法格式是:

          <meta name="參數(shù)" content="具體的描述">

          其中name屬性共有以下幾種參數(shù)。(A-C為常用屬性)

          (1) keywords(關(guān)鍵字)

          說明:用于告訴搜索引擎,你網(wǎng)頁的關(guān)鍵字。舉例:

          <meta name="keywords" content="PHP中文網(wǎng)">

          (2)description(網(wǎng)站內(nèi)容的描述)

          說明:用于告訴搜索引擎,你網(wǎng)站的主要內(nèi)容。舉例:

          <meta name="description" content="php中文網(wǎng)提供大量免費(fèi)、原創(chuàng)、高清的php視頻教程">

          (3)viewport(移動(dòng)端的窗口)

          說明:這個(gè)概念較為復(fù)雜,具體的會(huì)在下篇博文中講述。這個(gè)屬性常用于設(shè)計(jì)移動(dòng)端網(wǎng)頁。在用bootstrap,AmazeUI等框架時(shí)候都有用過viewport。

          <meta name="viewport" content="width=device-width, initial-scale=1">

          (4) robots(定義搜索引擎爬蟲的索引方式)

          說明:robots用來告訴爬蟲哪些頁面需要索引,哪些頁面不需要索引。content的參數(shù)有all,none,index,noindex,follow,nofollow。默認(rèn)是all。

          <meta name="robots" content="none">

          具體參數(shù)如下:

          1、none : 搜索引擎將忽略此網(wǎng)頁,等價(jià)于noindex,nofollow。

          2、noindex : 搜索引擎不索引此網(wǎng)頁。

          3、nofollow: 搜索引擎不繼續(xù)通過此網(wǎng)頁的鏈接索引搜索其它的網(wǎng)頁。

          4、all : 搜索引擎將索引此網(wǎng)頁與繼續(xù)通過此網(wǎng)頁的鏈接索引,等價(jià)于index,follow。

          5、index : 搜索引擎索引此網(wǎng)頁。

          6、follow : 搜索引擎繼續(xù)通過此網(wǎng)頁的鏈接索引搜索其它的網(wǎng)頁。

          (5)author(作者)

          說明:用于標(biāo)注網(wǎng)頁作者舉例:

          <meta name="author" content="PHP中文網(wǎng)">

          (6) generator(網(wǎng)頁制作軟件)

          說明:用于標(biāo)明網(wǎng)頁是什么軟件做的舉例: (不知道能不能這樣寫):

          <meta name="generator" content="Sublime Text3">

          (7)copyright(版權(quán))

          說明:用于標(biāo)注版權(quán)信息舉例:

          <meta name="copyright" content="PHP中文網(wǎng)"> //代表該網(wǎng)站為PHP中文網(wǎng)個(gè)人版權(quán)所有。

          (8)revisit-after(搜索引擎爬蟲重訪時(shí)間)

          說明:如果頁面不是經(jīng)常更新,為了減輕搜索引擎爬蟲對(duì)服務(wù)器帶來的壓力,可以設(shè)置一個(gè)爬蟲的重訪時(shí)間。如果重訪時(shí)間過短,爬蟲將按它們定義的默認(rèn)時(shí)間來訪問。舉例:

          <meta name="revisit-after" content="7 days" >

          (9)renderer(雙核瀏覽器渲染方式)

          說明:renderer是為雙核瀏覽器準(zhǔn)備的,用于指定雙核瀏覽器默認(rèn)以何種方式渲染頁面。比如說360瀏覽器。舉例:

          <meta name="renderer" content="webkit"> //默認(rèn)webkit內(nèi)核

          <meta name="renderer" content="ie-comp"> //默認(rèn)IE兼容模式

          <meta name="renderer" content="ie-stand"> //默認(rèn)IE標(biāo)準(zhǔn)模式

          http-equiv屬性

          http-equiv顧名思義,相當(dāng)于HTTP的作用。

          meta標(biāo)簽中http-equiv屬性語法格式是:

          <meta http-equiv="參數(shù)" content="具體的描述">

          其中http-equiv屬性主要有以下幾種參數(shù):

          (1) content-Type(設(shè)定網(wǎng)頁字符集)(推薦使用HTML5的方式)

          說明:用于設(shè)定網(wǎng)頁字符集,便于瀏覽器解析與渲染頁面舉例:

          <meta http-equiv="content-Type" content="text/html;charset=utf-8"> //舊的HTML,不推薦

          <meta charset="utf-8"> //HTML5設(shè)定網(wǎng)頁字符集的方式,推薦使用UTF-8

          (2)X-UA-Compatible(瀏覽器采取何種版本渲染當(dāng)前頁面)

          說明:用于告知瀏覽器以何種版本來渲染頁面。(一般都設(shè)置為最新模式,在各大框架中這個(gè)設(shè)置也很常見。)

          <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/> //指定IE和Chrome使用最新版本渲染當(dāng)前頁面

          (3) cache-control(指定請(qǐng)求和響應(yīng)遵循的緩存機(jī)制)

          說明:指導(dǎo)瀏覽器如何緩存某個(gè)響應(yīng)以及緩存多長(zhǎng)時(shí)間

          <meta http-equiv="cache-control" content="no-cache">

          共有以下幾種用法:

          no-cache: 先發(fā)送請(qǐng)求,與服務(wù)器確認(rèn)該資源是否被更改,如果未被更改,則使用緩存。

          no-store: 不允許緩存,每次都要去服務(wù)器上,下載完整的響應(yīng)。(安全措施)

          public : 緩存所有響應(yīng),但并非必須。因?yàn)閙ax-age也可以做到相同效果

          private : 只為單個(gè)用戶緩存,因此不允許任何中繼進(jìn)行緩存。(比如說CDN就不允許緩存private的響應(yīng))

          maxage : 表示當(dāng)前請(qǐng)求開始,該響應(yīng)在多久內(nèi)能被緩存和重用,而不去服務(wù)器重新請(qǐng)求。例如:max-age=60表示響應(yīng)可以再緩存和重用 60 秒。

          禁止百度自動(dòng)轉(zhuǎn)碼

          說明:用于禁止當(dāng)前頁面在移動(dòng)端瀏覽時(shí),被百度自動(dòng)轉(zhuǎn)碼。雖然百度的本意是好的,但是轉(zhuǎn)碼效果很多時(shí)候卻不盡人意。所以可以在head中加入例子中的那句話,就可以避免百度自動(dòng)轉(zhuǎn)碼了。

          <meta http-equiv="Cache-Control" content="no-siteapp" />

          (4)expires(網(wǎng)頁到期時(shí)間)

          說明:用于設(shè)定網(wǎng)頁的到期時(shí)間,過期后網(wǎng)頁必須到服務(wù)器上重新傳輸。

          <meta http-equiv="expires" content="Sunday 26 October 2016 01:00 GMT" />

          (5) refresh(自動(dòng)刷新并指向某頁面)

          說明:網(wǎng)頁將在設(shè)定的時(shí)間內(nèi),自動(dòng)刷新并調(diào)向設(shè)定的網(wǎng)址。

          <meta http-equiv="refresh" content="2;URL=http://www.xxx.com/"> //意思是2秒后跳轉(zhuǎn)到PHP中文網(wǎng)

          (6) Set-Cookie(cookie設(shè)定)

          說明:如果網(wǎng)頁過期。那么這個(gè)網(wǎng)頁存在本地的cookies也會(huì)被自動(dòng)刪除。

          <meta http-equiv="Set-Cookie" content="name, date"> //格式

          <meta http-equiv="Set-Cookie" content="User=Lxxyx; path=/; expires=Sunday, 10-Jan-16 10:00:00 GMT"> //具體范例

          總結(jié):meta標(biāo)簽的自定義屬性實(shí)在太多了。所以只總結(jié)了一些常用的,希望對(duì)大家有所幫助。


          主站蜘蛛池模板: 无码午夜人妻一区二区三区不卡视频| 亚洲色偷偷偷网站色偷一区| 国产天堂在线一区二区三区| 精彩视频一区二区三区| 鲁丝片一区二区三区免费| 日韩福利视频一区| 日韩一区二区在线观看视频| 一区二区三区内射美女毛片| 韩国福利一区二区美女视频| 国模丽丽啪啪一区二区| 手机福利视频一区二区| 麻豆AV一区二区三区久久| 久久精品一区二区影院| 色国产在线视频一区| 麻豆AV一区二区三区久久| 中文字幕一区二区区免| 亚洲av乱码一区二区三区按摩 | 无码人妻一区二区三区一| 国模大尺度视频一区二区| 国产精品成人99一区无码| 制服丝袜一区在线| 国精产品一区一区三区免费视频| 成人精品一区久久久久| 国产精品高清视亚洲一区二区| 亚洲一区二区三区在线观看精品中文| 波多野结衣一区二区免费视频| 亚洲.国产.欧美一区二区三区| 日韩精品一区二区三区色欲AV| 精品一区二区三区免费视频 | 亚洲男女一区二区三区| 亚洲一区无码中文字幕| 久久久人妻精品无码一区| 制服美女视频一区| 国产精品亚洲专区一区 | 无码人妻久久一区二区三区免费| 国产精品免费大片一区二区| 变态调教一区二区三区| 精品国产免费一区二区| 无码中文字幕人妻在线一区二区三区 | 无码精品人妻一区二区三区AV| 韩国精品福利一区二区三区|