我有兩個不同的div,一個漂浮在左邊,一個漂浮在右邊。它們比整個頁面小得多(每個大約400x200),因此兩者相距甚遠,擁抱頁面的邊緣。我怎樣才能讓他們在中心相鄰?我試圖設置邊距爲自動和約20px,但它並沒有改變任何東西..CSS float「擁抱中心」?
回答
給他們一個父div「margin:0 auto; width:1000px;」
<div style="margin:0 auto; width:1000px;">
<div style="float:left">Left</div>
<div style="float:right">Right</div>
</div>
或者,如果你希望他們緊挨着對方:
<div style="margin:0 auto;">
<div style="float:left">Left</div>
<div style="float:left">Right</div>
</div>
你測試了這個嗎?這不是OP之後的事情。 – j08691 2012-04-09 19:48:52
@ j08691當然,我測試了它,這是我如何解釋他的請求。 – 2012-04-09 19:50:59
這是inline-block
工作!
<div id="container">
<div id="left">Left!</div>
<div id="right">Right!</div>
</div>
#container{
text-align:center;
width:100%;
height:300px;
border:1px solid black
}
#left{
border:3px solid blue;
height:100px;
width:100px;
margin:auto;
display:inline-block;
}
#right{
border:3px solid red;
height:100px;
width:100px;
margin:auto;
display:inline-block;
}
添加一個包裝DIV他們倆身邊。在包裝上設置寬度並將邊距頂部和底部設置爲0,將左側和右側設置爲自動。然後設置兩個浮動div的寬度以適合包裝,例如50%會使它們的寬度相同。
嘗試使用的z-index和位置絕對,或相對
繼承人的鏈接,幫助你的了
<div style="position: absolute; left: 610px; top: 80px; height: 400px; width: 100px; padding: 1em;">layer stuff</div>
http://www.yourhtmlsource.com/stylesheets/csslayout.html
甚至沒有關閉。 – j08691 2012-04-09 19:49:36
考慮到這在我的機器上正常工作,請關閉我 – 2012-04-09 20:17:07
你可以用一個wrapper div來完成這個任務,並將子div設置爲內聯塊。
CSS:
#a, #b{
border: 1px solid #999;
width: 100px;
display:inline-block;
}
#container {
text-align:center;
}
HTML:
<div id="container">
<div id="a">a</div>
<div id="b">b</div>
</div>
- 1. 在css中將float設置爲中心
- 2. 在擁抱中啓用-98?
- 3. iOS Autolayout和擁抱內容
- 4. 擁抱`/`與類型推斷
- 5. 擁抱XML中的DRY原理
- 6. flex中的擁抱大括號
- 7. 如何從forEach退出擁抱方法?
- 8. UIView的擁抱使用自動佈局
- 9. StackView沒有拉伸,擁抱內容
- 10. Android WebView CSS float
- 11. css float問題
- 12. CSS Float解釋
- 13. css force float div
- 14. CSS Clearing float
- 15. CSS clear float
- 16. css float/positioniong
- 17. 在Internet Explorer中的CSS Float
- 18. CSS的float導致中間
- 19. CSS float下和左
- 20. css float問題ie7
- 21. CSS的float弊端
- 22. CSS的float聲明
- 23. 中心與CSS
- 24. CSS DIV中心
- 25. 如何從擁抱包裝中引用新實例`this`?
- 26. 如何在Ubuntu中安裝擁抱編譯器?
- 27. asp.net 2.0核心擁有
- 28. CSS Float左側問題
- 29. CSS的float和填充
- 30. 混淆關於CSS的float
在http://jsfiddle.net/上重新創建您的問題並將其粘貼到您的問題中。 – 2012-04-09 19:47:20