简体   繁体   English

3个自适应DIV框并排放置-不在一起

[英]3 Responsive DIV Boxes Side by Side - Not Staying Together

Hi i need help figuring this out. 嗨,我需要帮助弄清楚这一点。 I have 3 divs that need to stay side by side regardless of how big or small the screen is, but the problem is that, once the screen's width reaches below 400px, then the last div goes under the others. 我有3个div,无论屏幕大小是多少,都需要并排放置,但是问题是,一旦屏幕的宽度达到400px以下,最后一个div就会移到其他div之下。 How can I make them stay inline and at the same time responsive and centered without getting crazy with media queries? 我如何才能让他们保持内联,同时保持响应和集中注意力,而不会因媒体查询而发疯? Please help. 请帮忙。 HERE'S A FIDDLE 这里是一块

CSS: CSS:

.box{
    background-color: coral;
    width: 30%;    
    float:left;
    margin:10px;
    border-radius:5px;
}
.text{
    padding: 10px;
    color:white;
    font-weight:bold;
    text-align:center;
}

HTML: HTML:

<div class="box">
    <div class="text">Text</div>
</div>
<div class="box">
    <div class="text">Text</div>
</div>
<div class="box">
    <div class="text">Text</div>
</div>

One way to fix this would be to wrap the divs in a container, and give that container a white-space:nowrap;text-align:center rule. 解决此问题的一种方法是将div包装在一个容器中,并给该容器一个white-space:nowrap;text-align:center规则。 Then change the divs from floating to display:inline-block; 然后将div从float更改为display:inline-block; .

jsFiddle example jsFiddle示例

.box {
    background-color: coral;
    width: 30%;
    display:inline-block;
    margin:10px 0;
    border-radius:5px;
}
.text {
    padding: 10px 0;
    color:white;
    font-weight:bold;
    text-align:center;
}
#container {
    white-space:nowrap;
    text-align:center;
}

For a safer responsive layout, work with display:table on a wrapper div, and change the box to display:table-cell . 为了获得更安全的响应式布局,请在包装div上使用display:table ,然后将框更改为display:table-cell For the padding, add a middle div, and set the width in percentual value. 对于填充,添加中间div,然后以百分比值设置宽度。 Also, you won't even need to set the box width. 另外,您甚至不需要设置框的宽度。

http://jsfiddle.net/32hcm/9/ http://jsfiddle.net/32hcm/9/


HTML: HTML:

<div class="wrapper">
    <div class="box">
        <div class="text">Text</div>
    </div>
    <div class="middle"></div>
    <div class="box">
        <div class="text">Text</div>
    </div>
    <div class="middle"></div>
    <div class="box">
        <div class="text">Text</div>
    </div>
</div>

CSS: CSS:

.box{
    background-color: coral;
    display: table-cell;
    border-radius:5px;
}
.text{
    color:white;
    font-weight:bold;
    text-align:center;
    padding: 10px 0;
}

.wrapper {
    display: table;
    width: 100%;
}

.middle {
    display: table-cell;
    width: 10%;
}

The problem is with your fixed margin of 10px . 问题是您的固定边距为10px Change it to percent value, and adjust the width percentual also, and it will work fine. 将其更改为百分比值,然后也调整百分比宽度,它将正常工作。

.box{
    background-color: coral;
    width: 28%;    
    float:left;
    margin:1%;
    border-radius:5px;
}
.text{
    padding: 10px 0;
    color:white;
    font-weight:bold;
    text-align:center;
}

http://jsfiddle.net/32hcm/5/ http://jsfiddle.net/32hcm/5/

Use table-cell 使用表格单元

and have a container to be set to 100%: 并将容器设置为100%:

.core {width: 100%; display: table; border-spacing: 10px;}

.box{
    background-color: coral;
    width: 32.03125%; 
    float:none;
    display: table-cell;
    border-radius:5px;
}

FIDDLE 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM