国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

18143453325 在線咨詢 在線咨詢
18143453325 在線咨詢
所在位置: 首頁(yè) > 營(yíng)銷(xiāo)資訊 > 網(wǎng)絡(luò)營(yíng)銷(xiāo) > CSS常用代碼使用技巧大全

CSS常用代碼使用技巧大全

時(shí)間:2022-05-29 17:48:01 | 來(lái)源:網(wǎng)絡(luò)營(yíng)銷(xiāo)

時(shí)間:2022-05-29 17:48:01 來(lái)源:網(wǎng)絡(luò)營(yíng)銷(xiāo)

這幾天,一直忙于幫公司做新的網(wǎng)站,在設(shè)計(jì)和修改新模板頁(yè)面的過(guò)程中,需要不停的調(diào)試CSS樣式,有的部分還需要重新來(lái)寫(xiě)。為了便于以后再次使用,我特意將在網(wǎng)頁(yè)制作過(guò)程中常使用的CSS代碼的使用技巧整理成文章的形式,在億企邦上跟大家分享一下,一是便于以后自己在做站的時(shí)候,使用起來(lái)比較方便;二是希望能對(duì)正在搭建網(wǎng)站的站長(zhǎng)們有所幫助。

1、文字的水平居中

將一段文字置于容器的水平中點(diǎn),只要設(shè)置text-align屬性即可:

text-align:center;

2、容器的水平居中

先為該容器設(shè)置一個(gè)明確寬度,然后將margin的水平值設(shè)為auto即可。

#container {
  width:760px;
  margin:0 auto;
  }

3、文字的垂直居中

單行文字的垂直居中,只要將行高與容器高設(shè)為相等即可。

比如,容器中有一行數(shù)字。

<div id="container">1234567890</div>

然后CSS這樣寫(xiě):

#container {height: 35px; line-height: 35px;}

如果有n行文字,那么將行高設(shè)為容器高度的n分之一即可。

4、容器的垂直居中

比如,有一大一小兩個(gè)容器,請(qǐng)問(wèn)如何將小容器垂直居中?

<div id="big">
  <div id="small">
  </div>
  </div>

首先,將大容器的定位為relative。

#big{
  position:relative;
  height:480px;
  }

然后,將小容器定位為absolute,再將它的左上角沿y軸下移50%,最后將它margin-top上移本身高度的50%即可。

#small {
  position: absolute;
  top: 50%;
  height: 240px;
  margin-top: -120px;
  }

使用同樣的思路,也可以做出水平居中的效果。

5、圖片寬度的自適應(yīng)

如何使得較大的圖片,能夠自動(dòng)適應(yīng)小容器的寬度?CSS可以這樣寫(xiě):

img {max-width: 100%}

但是IE6不支持max-width,所以遇到IE6時(shí),使用IE條件注釋,將語(yǔ)句改寫(xiě)為:

img {width: 100%}

6、3D按鈕的CSS代碼

要使按鈕具有3D效果,只要將它的左上部邊框設(shè)為淺色,右下部邊框設(shè)為深色即可。對(duì)于顏色的選取大家可以通過(guò)億企邦的RGB顏色查詢工具:http://www.mahaixiang.cn/zyxz/zxgj/RGByscxb.html來(lái)進(jìn)行選取。

#button {
  background: #888;
  border: 1px solid;
  border-color: #999 #777 #777 #999;
  }

7、font屬性的快捷寫(xiě)法

font快捷寫(xiě)法的格式為:

body {
  font: font-style font-variant font-weight font-size line-height font-family;
  }

所以:

body {
  font-family: Arial, Helvetica, sans-serif;
  font-size: 13px;
  font-weight: normal;
  font-variant: small-caps;
  font-style: italic;
  line-height: 150%;
  }

可以被寫(xiě)成:

body {
  font: italic small-caps normal 13px/150% Arial, Helvetica, sans-serif;
  }

8、link狀態(tài)的設(shè)置順序

link的四種狀態(tài),需要按照下面的前后順序進(jìn)行設(shè)置:

a:link
  a:visited
  a:hover
  a:active

9、IE條件注釋

你可以利用條件注釋,設(shè)置只對(duì)IE產(chǎn)生作用的語(yǔ)句:

<!--[if IE]>
  <link rel="stylesheet" type="text/css" href="ie-stylesheet.css" />
  < ![endif]-->

還可以區(qū)分各種不同的IE版本:

<!--[if IE 6]> - targets IE6 only -->
  <!--[if gt IE 6]> - targets IE7 and above -->
  <!--[if lt IE 6]> - targets IE5.5 and below -->
  <!--[if gte IE 6]> - targets IE6 and above -->
  <!--[if lte IE 6]> - targets IE6 and below -->

10、IE6專用語(yǔ)句

IE專用語(yǔ)句方法一:

由于IE6不把html視為文檔的根元素,所以利用這一點(diǎn),可以寫(xiě)出只有IE6才能讀到的語(yǔ)句:

/* the following rules apply only to IE6 */
  * html{
  }
  * html body{
  }
  * html .foo{
  }

IE7專用語(yǔ)句則要寫(xiě)成

/* the following rules apply only to IE7 */
  *+html .foo{
  }

IE專用語(yǔ)句方法二:

除了IE6以外,所有瀏覽器都不能識(shí)別屬性前的下劃線。而除了IE7之外,所有瀏覽器都不能識(shí)別屬性前的*號(hào),因此可以寫(xiě)出只有這兩個(gè)瀏覽器才能讀到的語(yǔ)句:

.element {
  background: red; /* modern browsers */
  *background: green; /* IE 7 and below */
  _background: blue; /* IE6 exclusively */
  }

11、CSS的優(yōu)先性

如果同一個(gè)容器被多條CSS語(yǔ)句定義,那么哪一個(gè)定義優(yōu)先呢?

基本規(guī)則是:

行內(nèi)樣式 > id樣式 > class樣式 > 標(biāo)簽名樣式

比如,有一個(gè)元素:

<div id="ID" class="CLASS" style="color:black;"></div>

行內(nèi)樣式是最優(yōu)先的,然后其他設(shè)置的優(yōu)先性,從低到高依次為:

div < .class < div.class < #id < div#id < #id.class < div#id.class

12、IE6的min-height

IE6不支持min-height,有兩種方法可以解決這個(gè)問(wèn)題:

方法一:

.element {
  min-height: 500px;
  height: auto !important;
  height: 500px;
  }

共有三條CSS語(yǔ)句,第一句是針對(duì)其他瀏覽器設(shè)置最小高度,第三句是針對(duì)IE設(shè)置最小高度,第二句則是讓其他瀏覽器覆蓋第三句的設(shè)置。

方法二:

.element {
  min-height: 500px
  _height: 500px
  }

_height只有IE6能讀取。

13、font-size基準(zhǔn)

瀏覽器的缺省字體大小是16px,你可以先將基準(zhǔn)字體大小設(shè)為10px:

body {font-size:62.5%;}

后面統(tǒng)一采用em作為字體單位,2.4em就表示24px。

h1 {font-size: 2.4 em}

14、Text-transform和Font Variant

Text-transform用于將所有字母變成小寫(xiě)字母、大寫(xiě)字母或首字母大寫(xiě):

p {text-transform: uppercase}
  p {text-transform: lowercase}
  p {text-transform: capitalize}

Font Variant用于將字體變成小型的大寫(xiě)字母(即與小寫(xiě)字母等高的大寫(xiě)字母)。

p {font-variant: small-caps}

15、CSS重置

這個(gè)方法通常用來(lái)清除瀏覽器內(nèi)置的默認(rèn)樣式。通常,所有的字體和line-height都設(shè)置為100%,margin、padding設(shè)置為0px,去掉border,列表也無(wú)樣式。

比如我們最常見(jiàn)到的css重置代碼:

body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,form,fieldset,input,textarea,p,th,td {
  margin:0;
  padding:0;
  }
  table {
  border-collapse:collapse;
   border-spacing:0;
  }
  img {
  border:0;
  }
  ol,ul {
   list-style:none;
  }
  h1,h2,h3,h4,h5,h6 {
   font-size:100%;
  font-weight:normal;
  }

16、用圖片充當(dāng)列表標(biāo)志

默認(rèn)情況下,瀏覽器使用一個(gè)黑圓圈作為列表標(biāo)志,可以用圖片取代它:

ul {list-style: none}
  ul li {
  background-image: url("path-to-your-image");
  background-repeat: none;
  background-position: 0 0.5em;
  }

17、透明容器設(shè)置

將一個(gè)容器設(shè)為透明,可以使用下面的代碼:

.element {
  filter:alpha(opacity=50);
  -moz-opacity:0.5;
  -khtml-opacity: 0.5;
  opacity: 0.5;
  }

在這四行CSS語(yǔ)句中,第一行是IE專用的,第二行用于Firefox,第三行用于webkit核心的瀏覽器,第四行用于Opera。

18、CSS三角形

如何使用CSS生成一個(gè)三角形?

先編寫(xiě)一個(gè)空元素

<div class="triangle"></div>

然后,將它四個(gè)邊框中的三個(gè)邊框設(shè)為透明,剩下一個(gè)設(shè)為可見(jiàn),就可以生成三角形效果:

.triangle {
  border-color: transparent transparent green transparent;
  border-style: solid;
  border-width: 0px 300px 300px 300px;
  height: 0px;
  width: 0px;
  }

19、禁止自動(dòng)換行

如果你希望文字在一行中顯示完成,不要自動(dòng)換行,CSS命令如下:

h { white-space:nowrap; }

20、用圖片替換文字

有時(shí)我們需要在標(biāo)題欄中使用圖片,但是又必須保證搜索引擎能夠讀到標(biāo)題,CSS語(yǔ)句可以這樣寫(xiě):

h1 {
  text-indent:-9999px;
  background:url("h1-image.jpg") no-repeat;
  width:200px;
  height:50px;
  }

21、獲得焦點(diǎn)的表單元素

當(dāng)一個(gè)表單元素獲得焦點(diǎn)時(shí),可以將其突出顯示:

input:focus { border: 2px solid green; }

22、!important規(guī)則

多條CSS語(yǔ)句互相沖突時(shí),具有!important的語(yǔ)句將覆蓋其他語(yǔ)句。由于IE不支持!important,所以也可以利用它區(qū)分不同的瀏覽器。

h1 {
  color: red !important;
  color: blue;
  }

上面這段語(yǔ)句的結(jié)果是,其他瀏覽器都顯示紅色標(biāo)題,只有IE顯示藍(lán)色標(biāo)題。

23、CSS提示框

當(dāng)鼠標(biāo)移動(dòng)到鏈接上方,會(huì)自動(dòng)出現(xiàn)一個(gè)提示框。

<a class="tooltip" href="#">鏈接文字 <span>提示文字</span></a>

CSS這樣寫(xiě):

a.tooltip {position: relative}
  a.tooltip span {display:none; padding:5px; width:200px;}
  a:hover {background:#fff;} /*background-color is a must for IE6*/
  a.tooltip:hover span{display:inline; position:absolute;}

24、固定位置的頁(yè)首

當(dāng)頁(yè)面滾動(dòng)時(shí),有時(shí)需要頁(yè)首在位置固定不變,CSS語(yǔ)句可以這樣寫(xiě):

body{ margin:0;padding:100px 0 0 0;}
  div#header{
  position:absolute;
  top:0;
  left:0;
  width:100%;
  height:<length>;
  }
  @media screen{
  body>div#header{position: fixed;}
  }
  * html body{overflow:hidden;}
  * html div#content{height:100%;overflow:auto;}

IE6的另一種寫(xiě)法(用于固定位置的頁(yè)腳):

* html #footer {
  position:absolute;
  top:expression((0-(footer.offsetHeight)+(document.documentElement.clientHeight?document.documentElement.clientHeight : document.body.clientHeight)+(ignoreMe = document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop))+'px');
  }

25、在IE6中設(shè)置PNG圖片的透明效果

.classname {
  background: url(image.png);
  _background: none;
  _filter:progid:DXImageTransform.Microsoft.AlphaImageLoader
  (src='image.png', sizingMethod='crop');
  }

26、各類(lèi)瀏覽器的專用語(yǔ)句

/* IE6 and below */
  * html #uno { color: red }

/* IE7 */
  *:first-child+html #dos { color: red }

/* IE7, FF, Saf, Opera */
  html>body #tres { color: red }

/* IE8, FF, Saf, Opera (Everything but IE 6,7) */
  html>/**/body #cuatro { color: red }

/* Opera 9.27 and below, safari 2 */
  html:first-child #cinco { color: red }

/* Safari 2-3 */
  html[xmlns*=""] body:last-child #seis { color: red }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
  body:nth-of-type(1) #siete { color: red }

/* safari 3+, chrome 1+, opera9+, ff 3.5+ */
  body:first-of-type #ocho { color: red }

/* saf3+, chrome1+ */
  @media screen and (-webkit-min-device-pixel-ratio:0) {
  #diez { color: red }
  }

/* iPhone / mobile webkit */
  @media screen and (max-device-width: 480px) {
  #veintiseis { color: red }
  }

/* Safari 2 - 3.1 */
  html[xmlns*=""]:root #trece { color: red }

/* Safari 2 - 3.1, Opera 9.25 */
  *|html[xmlns*=""] #catorce { color: red }

/* Everything but IE6-8 */
  :root *> #quince { color: red }

/* IE7 */
  *+html #dieciocho { color: red }

/* Firefox only. 1+ */
  #veinticuatro, x:-moz-any-link { color: red }

/* Firefox 3.0+ */
  #veinticinco, x:-moz-any-link, x:default { color: red }

/***** Attribute Hacks ******/

/* IE6 */
  #once { _color: blue }

/* IE6, IE7 */
  #doce { *color: blue; /* or #color: blue */ }

/* Everything but IE6 */
  #diecisiete { color/**/: blue }

/* IE6, IE7, IE8 */
  #diecinueve { color: blue/9; }

/* IE7, IE8 */
  #veinte { color/*/**/: blue/9; }

/* IE6, IE7 -- acts as an !important */
  #veintesiete { color: blue !ie; } /* string after ! can be anything */

27、容器的水平和垂直居中

HTML代碼如下:

<figure class='logo'>
  <span></span>
  <img class='photo'/>
  </figure>

CSS代碼如下:

.logo {
  display: block;
  text-align: center;
  display: block;
  text-align: center;
  vertical-align: middle;
  border: 4px solid #dddddd;
  padding: 4px;
  height: 74px;
  width: 74px;
  }
  .logo * {
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  }
  .logo .photo {
  height: auto;
  width: auto;
  max-width: 100%;
  max-height: 100%;
  }

28、CSS陰影

外陰影:

.shadow {
  -moz-box-shadow: 5px 5px 5px #ccc;
  -webkit-box-shadow: 5px 5px 5px #ccc;
  box-shadow: 5px 5px 5px #ccc;
  }

內(nèi)陰影:

.shadow {
  -moz-box-shadow:inset 0 0 10px #000000;
  -webkit-box-shadow:inset 0 0 10px #000000;
  box-shadow:inset 0 0 10px #000000;
  }

29、取消IE文本框的滾動(dòng)條

textarea { overflow: auto; }

30、圖片預(yù)加載

所謂的圖片預(yù)加載技術(shù),也就是加載一個(gè)圖片的時(shí)候,為了減輕網(wǎng)頁(yè)的壓力,將圖片衰減后先顯示出來(lái),然后再慢慢加載,你就會(huì)看到一個(gè)圖片由模糊突然變得清淅起來(lái),這就是預(yù)加載的一種。這段代碼就完成了圖片的預(yù)加載功能,你可以點(diǎn)擊一個(gè)小圖然后看到大圖慢慢變得清淅,直至加載完成。

在此特意在億企邦上跟大家分享一段比較常規(guī)的圖片預(yù)加載代碼:

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  <title>圖片預(yù)加載</title>
  <style type="text/css">
  <!--
  body{background:#000; color:#FFFFFF; }
  .big-img{height: 390px; text-align:center; }
  ol,ul{list-style:none}
  ul{width: 632px; height: 56px; margin:10px auto; }
  li{float:left; width: 200px; height: 53px; margin-right:10px; }
  ul li img{ cursor:pointer}
  -->
  </style>
  </head>
  <body>
  <div class="big-img"><img id="showImg" width="500" height="375" src="http://www.mahaixiang.cn/uploads/allimg/1310/1-13101911245Ec.jpg" alt="" /></div>
  <ul id="imgList">
  <li><img src="http://www.mahaixiang.cn/uploads/allimg/1310/1-13101911245Ec.jpg" /></li>
  <li><img src="http://www.mahaixiang.cn/uploads/allimg/130301/1IK11242-0.jpg" /></li>
  </ul>
  <script type="text/javascript">
  var imgL=document.getElementById("imgList").getElementsByTagName("img");
  var imgURL="www.mahaixiang.cn/uploads/allimg/1310/";
  var bigImg=new Array("1-13101911245Ec.jpg","1IK11242-0.jpg");
  var imgshow=new Image();
  for (var i = 0; i < imgL.length; i++) {
  (function() {
  var p = i
    imgL[i].onclick = function() {
   document.getElementById("showImg").src = this.src;
  imgshow.src = imgURL + bigImg[p];
   imgshow.onload = function() {
  document.getElementById("showImg").src = this.src;
   }
  }
   })()
  }
  </script>
  </body>
  </html>

了解了這么多的代碼使用技巧,那么,作為SEOer的我們有該如何對(duì)這些代碼進(jìn)行優(yōu)化,減輕網(wǎng)站頁(yè)面的負(fù)載呢?關(guān)于這個(gè)問(wèn)題大家可以查看億企邦上的《網(wǎng)頁(yè)制作中常用的20個(gè)優(yōu)化CSS代碼技巧》一文,進(jìn)行具體的了解。

關(guān)鍵詞:技巧,使用

74
73
25
news

版權(quán)所有? 億企邦 1997-2022 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉