[英]How to horizontally align columns within a div
我有三個<figure>
標記,我想在我的container
div中將它們水平對齊。 我嘗試了使用float left,right和margin auto的方法,但是沒有用。 我該怎么辦?
* { margin: 0; padding: 0; } .container { width: 100%; } .col { width: 33.33%; float: left; }
<div class="container"> <figure class="col"> <img src="http://placehold.it/300x300"> </figure> <figure class="col"> <img src="http://placehold.it/300x300"> </figure> <figure class="col"> <img src="http://placehold.it/300x300"> </figure> </div>
您只需要overflow:auto;
為您的container
類。
將解決方案發布到您的代碼和更新的代碼中,可以看到區別。 例如添加了border:1px solid red;
看看區別。
解
* { margin: 0; padding: 0; } .container { width: 100%; border:1px solid red; overflow:auto; /*added*/ } .col { width: 33.33%; float: left; }
<div class="container"> <figure class="col""> <img src="http://placehold.it/300x300"> </figure> <figure class="col""> <img src="http://placehold.it/300x300"> </figure> <figure class="col"> <img src="http://placehold.it/300x300"> </figure> </div>
您的代碼段
* { margin: 0; padding: 0; } .container { width: 100%; border:2px solid red; } .col { width: 33.33%; float: left; }
<div class="container"> <figure class="col""> <img src="http://placehold.it/300x300"> </figure> <figure class="col""> <img src="http://placehold.it/300x300"> </figure> <figure class="col"> <img src="http://placehold.it/300x300"> </figure> </div>
要將圖像放在div中,請嘗試以下操作:
img {
display: block;
margin-left: auto;
margin-right: auto;
}
這是您修改過的Codepen: http ://codepen.io/anon/pen/xwwdmP
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.