溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

書寫CSS時需要的七個方面指的是什么

發(fā)布時間:2021-11-18 09:28:36 來源:億速云 閱讀:116 作者:柒染 欄目:web開發(fā)

今天就跟大家聊聊有關(guān)書寫CSS時需要的七個方面指的是什么,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

書寫CSS時注意的七個方面

隨著CSS網(wǎng)頁布局的應(yīng)用越來越廣泛,更多的CSSer開始書寫CSS,如何才能寫出高效規(guī)范的CSS代碼呢,今天向大家介紹,必須要注意的七個方面:

一、使用外聯(lián)樣式替代行間樣式或者內(nèi)嵌樣式

◆不推薦使用行間樣式

XML/HTML代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">     <html xmlns="http://www.w3.org/1999/xhtml">     <head>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />     <title>Page title - book.chinaz.comtitle>     head>       <body>     <p style="color: red"> ... p>       body>       html>

◆不推薦使用內(nèi)嵌樣式

XML/HTML代碼

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"    "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en">       <head>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />     <title>Page title - book.chinaz.comtitle>       <style type="text/css" media="screen">     p { color: red; }       style>       head>       <body>... body>       html>

◆推薦使用外聯(lián)樣式

XML/HTML代碼

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"    "http://www.w3.org/TR/html4/strict.dtd">     <html lang="en">     <head>       <meta http-equiv="content-type" content="text       <title>Page title - book.chinaz.comtitle>       <link rel="stylesheet" href="name.css" type="text/css" media="screen" />       < /head>       <body> ... body>       html>

二、建議使用 link 引入外部樣式表

為了兼容老版本的瀏覽器,建議使用 link 引入外部樣式表的方來代替 @import導(dǎo)入樣式的方式.

譯者注: @import是CSS2.1提出的所以老的瀏覽器不支持。

@import和link在使用上會有一些區(qū)別, 利用二者之間的差異,可以在實際運用中進行權(quán)衡。

關(guān)于@import和link方式的比較在52CSS.com上有幾篇文章可以拓展閱讀。

◆不推薦@import導(dǎo)入方式

XML/HTML代碼

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"    "http://www.w3.org/TR/html4/strict.dtd">       <html lang="en">     <head>       <meta http-equiv="content-type" content="text       <title>Page title - 52css.comtitle>       <style type="text/css" media="screen">       @import url("styles.css");      style>     head>     <body> ... body>     html>

◆推薦引入外部樣式表方式

XML/HTML代碼

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"    "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head>     <meta http-equiv="content-type" content="text       <title>Page title - blog.huangchao.orgtitle>     <link rel="stylesheet" href="name.css" type="text/css" media="screen" />     head>       <body> ... body>       html>

三、使用繼承

低效率的

CSS代碼

p{ font-family: arial, helvetica, sans-serif; }      #container { font-family: arial, helvetica, sans-serif; }      #navigation { font-family: arial, helvetica, sans-serif; }      #content { font-family: arial, helvetica, sans-serif; }      #sidebar { font-family: arial, helvetica, sans-serif; }      h2 { font-family: georgia, times, serif; }

高效的

CSS代碼

body { font-family: arial, helvetica, sans-serif; }       body { font-family: arial, helvetica, sans-serif; }      h2 { font-family: georgia, times, serif; }

四、使用多重選擇器

低效率的

CSS代碼

h2 { color: #236799; }       h3 { color: #236799; }      h4 { color: #236799; }      h5 { color: #236799; }

高效的

CSS代碼

h2, h3, h4, h5 { color: #236799; }

五、使用多重聲明

低效率的

CSS代碼

p { margin: 0 0 1em; }      p { background: #ddd; }      p { color: #666; }

譯者注: 對于十六進制顏色值,個人偏向于色值不縮寫且英文字母要大寫的方式.

高效的

CSS代碼

p { margin: 0 0 1em; background: #ddd; color: #666; }

六、使用簡記屬性

低效率的

CSS代碼

body { font-size: 85%; font-family: arial, helvetica, sans-serif; background-image: url(image.gif);   background-repeat: no-repeat; background-position: 0 100%; margin-top: 1em; margin-right: 1em;   margin-bottom: 0; margin-left: 1em; padding-top: 10px; padding-right: 10px; padding-bottom: 10px;   padding-left: 10px; border-style: solid;   border-width: 1px; border-color: red; color: #222222;

高效的

CSS代碼

body { font: 85% arial, helvetica, sans-serif; background: url(image.gif) no-repeat 0 100%;   margin: 1em 1em 0; padding: 10px; border: 1px   solid red; color: #222; }

 七、避免使用 !important

慎用寫法

CSS代碼

#news { background: #ddd !important; }

特定情況下可以使用以下方式提高權(quán)重級別

CSS代碼

#container #news { background: #ddd; }      body #container #news { background: #ddd; }

看完上述內(nèi)容,你們對書寫CSS時需要的七個方面指的是什么有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

css
AI