[英]Centering boxes inside a div (image+text)
I'd like to center three boxes like this: https://drive.google.com/file/d/0B4gPO2Q50KsZcGxrbGEySFV5eU0/view?usp=sharing 我想像这样将三个框居中: https : //drive.google.com/file/d/0B4gPO2Q50KsZcGxrbGEySFV5eU0/view?usp=sharing
What I want to achieve is to show the boxes one over the another in mobile devices but I do not know how to do so. 我想要实现的是在移动设备中一个接一个地显示盒子,但我不知道该怎么做。
I am using this code: 我正在使用此代码:
div#alex_box { border: solid 1px #aaa; overflow: hidden; } div.img_home { margin: 5px; padding: 5px; border: 1px solid #0000ff; height: auto; width: auto; float: left; text-align: center; } div.img_home img { display: inline; margin: 5px; border: 1px solid #ffffff; } div.img_home a:hover img { border: 1px solid #0000ff; } div.img_desc_home { text-align: center; font-weight: normal; width: 120px; margin: 5px; }
<div id="alex_box"> <div class="img_home"> <a target="_blank" href=""><img src="http://dev.lifeonbikes.com/wp-content/uploads/2015/01/depositphotos_18467679-Travel-car-illustration.jpg" alt="primera imagen"></a> <div class="img_desc_home">Texto primera imagen</div> </div> <div class="img_home"> <a target="_blank" href=""><img src="http://dev.lifeonbikes.com/wp-content/uploads/2015/01/images.jpg" alt="segunda imagen"></a> <div class="img_desc_home">Texto segunda imagen</div> </div> <div class="img_home"> <a target="_blank" href=""><img src="http://dev.lifeonbikes.com/wp-content/uploads/2015/01/bus-icon120413.png" alt="tercera imagen"></a> <div class="img_desc_home">Texto tercera imagen</div> </div> </div>
Try this zoom out and in to see the change Fiddle 尝试缩小并放大查看小提琴
@media screen and (max-width: 2000px) and (min-width: 300px){ img { height:100px; } div#alex_box { border: solid 1px #aaa; overflow: hidden; } div.img_home { margin: 5px; padding: 5px; border: 1px solid #0000ff; height: auto; width: auto; float: left; text-align: center; } div.img_home img { display: inline; margin: 5px; border: 1px solid #ffffff; } div.img_home a:hover img { border: 1px solid #0000ff; } div.img_desc_home { text-align: center; font-weight: normal; width: 120px; margin: 5px; } }
<div id="container"> <div id="alex_box"> <div class="img_home"> <a target="_blank" href=""><img src="http://dev.lifeonbikes.com/wp-content/uploads/2015/01/depositphotos_18467679-Travel-car-illustration.jpg" alt="primera imagen"></a> <div class="img_desc_home">Texto primera imagen</div> </div> <div class="img_home"> <a target="_blank" href=""><img src="http://dev.lifeonbikes.com/wp-content/uploads/2015/01/images.jpg" alt="segunda imagen"></a> <div class="img_desc_home">Texto segunda imagen</div> </div> <div class="img_home"> <a target="_blank" href=""><img src="http://dev.lifeonbikes.com/wp-content/uploads/2015/01/bus-icon120413.png" alt="tercera imagen"></a> <div class="img_desc_home">Texto tercera imagen</div> </div> </div> </div>
You can achieve it using margin-left: auto;margin-right: auto;
您可以使用
margin-left: auto;margin-right: auto;
within your parent div : 在您的父div中 :
div#alex_box {
border: solid 1px #aaa;
margin-left: auto;
margin-right: auto;
overflow: hidden;
padding:10px;
display:table;
}
I also added display:table;
我还添加了
display:table;
to the parent div and display:table-cell;
到父div并
display:table-cell;
for each child div , and it works fine and it's responsive too. 对于每个子div ,它都可以正常工作,并且响应速度也很快。
This is the working Fiddle . 这是工作中的小提琴 。
EDIT: 编辑:
To separate boxes with spaces you just need to add margin:5px;
要用空格分隔框,您只需要添加
margin:5px;
to their CSS: Take a look at this Fiddle . 他们的CSS:看看这个Fiddle 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.