css技巧總結

來源:互聯網
上載者:User

標籤:art   device   sid   play   min   over   http   ul li   現象   

一.自由布局的相關技巧

1.盡量不用指定像素寬度(width:300px)

用百分比或者auto(width:90%或者width:auto)

2.相對大小字型

{font-size:0.875em}

small元素的大小是預設大小的0.875倍,即14像素(14/16=0.875) 預設是16px

3.流動布局

float:left; clear:both的活用

4.選擇載入CSS

<link rel="stylesheet" type="text/css" media="screen and (max-device-width: 400px)" href="tinyScreen.css" />

<link rel="stylesheet" type="text/css" media="screen and (min-width: 400px) and (max-device-width: 600px)" href="smallScreen.css" />

@media screen and (max-device-width:400px) {

    .column {float:none;width:auto;}

  #sidebar {display:none;}

}

5.圖片的自動縮放

這隻要一行CSS代碼:

img { max-width: 100%;}

這行代碼對於大多數嵌入網頁的視頻也有效,所以可以寫成  

img, object { max-width: 100%;}

老版本的IE不支援max-width,所以只好寫成:

img { width: 100%; }

此外,windows平台縮放圖片時,可能出現映像失真現象。這時,可以嘗試使用IE的專有命令

img { -ms-interpolation-mode: bicubic; }

二.常用CSS技巧

1.容器的垂直置中

small要在big容器裡垂直置中

 
  1. <div id="big">  
  2.    <div id="small">  
  3.    </div>  
  4. </div>  
  1. div#big{  
  2.   position:relative;  
  3.   height:480px;  
  4. }  
  5. div#small {  
  6.   position: absolute;  
  7.   top: 50%;  
  8.   height: 240px;  
  9.   margin-top: -120px;  
  10. }  

裡面有2個技巧:

 

1.position:absolute定位的話會尋找第一個外層position為absolute或者是relative的。

2.內部元素如果要置中只要top父元素的50%,然後margin-top反方向自己的50%的高度就可以了,尤其是布局類似tabel最為有效

 

2.用icon來替代ul li的小黑點

 

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

 

3.去除float脫離文檔流的方法

詳細可以查看http://www.ruanyifeng.com/blog/2009/04/float_clearing.html

主要方法大致是在float元素後面加入clear:both;

或者在父級元素上加入style="overflow:hidden",也會自動消除子項目的float的脫離文檔流的行文

 

三.CSS特效

1.CSS使button有3D效果

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

2.CSS3浮水印字

 

.element {   color: #222;    text-shadow: 0px 2px 3px #555;  } 

css技巧總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.