2013-05-02 58 views
0
<style type="text/css"> 
.x-container img { 
    float: left; 
    margin-right: 15px; 
} 
.content img{ 
    clear:both; 
} 
</style> 

<div class="x-container span-18"> 
    <div class="content"> 
     <img src="image.jpg" id="disneland-img"> 
    </div> 
    <h2>Hello</h2> 
</div> 

我有像上面這樣的html。我期待在.content imgclear:both將覆蓋float:left.x-container img,但它不是。爲什麼我的CSS沒有被覆蓋?

我該如何做到這一點?

+3

您的圖片既浮動又不允許其他浮動元素位於其旁邊。你的定義不會相互覆蓋。 您可能希望爲h2,pheraps設置清除屬性? – Renan 2013-05-02 13:33:31

+1

**不同的** CSS屬性不會互相覆蓋。 – MMM 2013-05-02 13:38:44

回答

0

floatclear是兩個不同的CSS聲明。他們不會互相推翻。 (雖然他們與/相互影響當然)。如果你要替換自己的一流的float:left可以簡單的寫:

.content img{ 
    float:none; 
} 

清除浮動是另一回事,你需要使用clear在兄弟元素上,或者在父元素上放置一個overflow:auto|hidden或應用所謂的「clear-fix」方法。

.clearfix:after { 
    content:""; 
    display:table; 
    clear:both; 
} 
+0

謝謝Christoph!直接回答! – Uda 2013-05-02 13:45:50

+0

不客氣;) – Christoph 2013-05-02 13:46:42

1

用戶clearfix類圖像上的直接,這將做所有的工作 把div上的clearfix類與內容類。

<div class="x-container span-18"> 
    <div class="content clearfix "> 
    <img src="image.jpg" id="disneland-img"> 
    </div> 
<h2>Hello</h2> 
</div> 

.clearfix:before, .clearfix:after { 
    content: ""; 
    display: table; 
} 
.clearfix:after { 
    clear: both; 
} 
.clearfix { 
    zoom: 1; 
} 

http://jsfiddle.net/wikijames/TVm6K/

+0

這也解決了這個問題,謝謝! – Uda 2013-05-02 13:46:43

+0

歡迎您:) @Uda – 2013-05-02 13:53:02

0

這應該修復它。

.x-container img { 
    float: left; 
    margin-right: 15px; 
} 
.x-container h2 { 
    display: block; 
    clear: both; 
} 
相關問題