幾個常見CSS錯誤和解決辦法

來源:互聯網
上載者:User

標籤:style   blog   http   os   io   使用   strong   ar   for   

1.IE6下,當float存在時,margin雙倍的問題

 

解決方案:加display:inline;

例:

#content {
 

   float:left;
    width:500px;
    padding:10px 15px;
    margin-left:20px;
   display:inline;
}

2.克服盒子模型的hack

原寫法:

#main-div{width: 150px;border: 5px;padding: 20px;}
修正後
#main-div1{width: 150px;}#main-div1 div{border: 5px;padding: 20px;}
例如:
<div id="main-div">1</div>
<div id="main-div1"><div>2</div></div>
見圖:
兩者區別:1是把padding加到父層裡,DIV的寬度是150+20+20
2是把PADDING加到子層裡,div的寬度是150.
我習慣是用1的方法去自己計算寬高.
3,4.最小高度(最小寬度)IE不識別的解決方案
.container {width:20em;padding:0.5em;border:1px solid #000;min-height:8em;height:auto;}* html .container {height: 8em;}
5.整塊元素置中對齊的方法
body{text-align: center;}#container{text-align: left;width: 960px;margin: 0 auto;}
先定義BODY中的文字整個置中text-align:center,再定義其中需要靠左對齊的子項目text-align:left;
6.垂直置中的CSS
#wrapper {        width:530px;        height:25px;        background:url(container.gif) no-repeat left top;        padding:0px 10px;}#wrapper p {        line-height:25px;}
適用單行文字,將height設成line-height同值
7.固定寬度下幾個LI元素float橫排時,超出寬度限定的解決方案
css:
ul.listing{   margin:0;   width:400px; border:1px solid #000;} ul.listing li{   list-style-type:none;   float:left;   margin:0 20px 0 0;   width:83px; border:1px solid red;}  
ul.listing1{   margin:0 0 0 -20px;   width:420px; border:1px solid #000;} 
ul.listing1 li{   list-style-type:none;   float:left;   margin:0 0 0 20px;   width:83px; border:1px solid red;} 
例子:
<ul class="listing">
<li>第一句</li>
<li>第二句</li>
<li>第三句</li>
<li>第四句</li>
</ul>
</div>
<div>
<ul class="listing1">
<li>第一句</li>
<li>第二句</li>
<li>第三句</li>
<li>第四句</li>
</ul>
圖示:
原理:
用負margin的方法
9.定位z-index
z-index起作用的前提是,必須有position:absolute, position:fixed or position:relative這三種中的任一種屬性.
css:
.relativeblock1 {        position:relative;        width:200px;        height:80px;        z-index:51;}.absoluteblock1 {        position:absolute;        left:10px;        top:90px;        width:40px;        height:40px;        z-index:1;}.relativeblock2 {        position:relative;        width:200px;        height:80px;        z-index:50;}例:
<div class="relativeblock1">        <div class="absoluteblock1"></div></div><div class="relativeblock2"></div>見圖:
按CSS來說,relativeblock2應該會蓋上absoluteblock1,但實際正相反,是因為
absoluteblock1的優先順序別高於relativeblock2,他繼承了absoluteblock1的z-index,雖然他自身的z-index值為1,但實際為51.1,當然高於relativeblock2的z-index:50.
10.相對定位relative和絕對位置absolute的區別
 CSS:
#redSquare{position: relative;bottom: 40px;right: 40px;}圖例:

 
11.絕對位置的應用
css:
#hang_tab {position: absolute;top: 7px;left: 0px;width: 157px;height: 93px;}
圖例:

 
13.正確消除浮動(float)的方法
css:
#container:after{        content: ".";        display: block;        height: 0;        clear: both;        visibility: hidden;}#container{display: inline-block;}* html #container{height: 1%;}#container{display: block;}
例圖:

 
經實現,用:after屬性是為了消除float浮動(針對FIREFOX使用)時父層無法正確識別高度,複雜且不易記不,優點是不需要再寫<div style="clear:both"></div>這樣一個空層來撐起高度。
15.如何處理圓角
最簡單的方法是用一張足夠大的圖,然後我該圓角標註
html:
<div class="roundBox">  <p>beautifully-encapsulated paragraph</p>  <div class="boxBottom"></div></div>
css:
.roundBox {  background:transparent url(roundBox.gif) no-repeat top left;  width:340px;  padding:20px;}.roundBox .boxBottom {  background:white url(roundBox.gif) no-repeat bottom left;  font-size:1px;  line-height:1px;  height:14px;  margin:0 -20px -20px -20px;}
執行個體:http://www.askthecssguy.com/examples/notchedcorners/index.html
17.用label標籤做表單(非table)
精華html:
<form method="post" action="contactengine.php">
    <label for="Name">Name:</label>
    <input type="text" name="Name" id="Name" />
.....
</form>
css:
label {        float: left;        text-align: right;        margin-right: 15px;        width: 100px;}
執行個體:http://css-tricks.com/nice-and-simple-contact-form/
另一個:http://www.box.net/shared/1zbtouuwws
20.用有背景圖的hr做分隔線,適用所有瀏覽器的方法.因為hr自身的樣式是一根黑線,所以讓他自身的樣式 隱藏,然後新寫一個class="hr",讓hr聽他父層的話
經驗證這樣寫無意義,對於目前中國的網站來說,極少用到HR.

=========================================================
The CSS Class
====

幾個常見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.