Html 如何将 3 个 div 左右对齐?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15134389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:58:42  来源:igfitidea点击:

How to align 3 divs left-center-right?

htmlcss

提问by membersound

How can I align 3 divs in one line left-center-right without having to define explicit sizes?

如何在一行中左中右对齐 3 个 div 而不必定义明确的大小?

Left should be aligned most to the left edge, and right to the right edge.

左应该最对齐到左边缘,右对齐到右边缘。

The following does not work:

以下不起作用:

<div style="float: left;">
    left
</div>
<div style="float: right;">
    right
</div>
<div style="margin: 0 auto;">
    center
</div>

回答by Sowmya

Add a wrapper divand give text-align:center

添加一个包装器div并给出text-align:center

CSS

CSS

.wrap{
        text-align:center
    }

HTML

HTML

<div class="wrap">
<div class="left">
    left
</div>
<div class="right">
    right
</div>
<div class="center">
    center sdv dg sdb sdfbh sdfhfdhh h dfh
</div>
    </div>

DEMO

演示

回答by zkanoca

<div style="width:100%;margin:0 auto; padding: 0">
     <div style=" float:left;width:32%;border: thin solid black">
         left
     </div>
     <div style=" float:left;width:32%;border: thin solid black">
         center
     </div>
     <div style=" float:left;width:32%;border: thin solid black">
          right 
     </div>
 </div>
 <div style="clear:both">
 </div>

回答by chriz

Heres an example of how to do this by placing the floats in the correct order.

这是一个如何通过以正确的顺序放置浮点数来做到这一点的示例。

jsFiddle Example

jsFiddle 示例

<div class="square" style="float: left;">left</div>
<div class="square" style="float: right;">right</div>
<div class="square" style="margin:0 auto  !important;">center</div>


.square {
width:50px;
height:50px;
background-color:#ff0000;
text-align:center;
border: 1px solid #000;
}

回答by Rohan Kumar

Try this

尝试这个

CSS

CSS

div{width:33%;}

HTML

HTML

<div style="float: left;border:1px solid red;">
    left
</div>
<div style="float: right;border:1px solid green;">
    right
</div>
<div style="margin: 0 auto;border:1px solid blue;">
    center
</div>

回答by ravy amiry

It's not possible actually do it, without knowing about the content and layout pattern. But for a begining point, you can try this:

在不了解内容和布局模式的情况下,实际上是不可能做到的。但作为一个起点,你可以试试这个:

HTML:

HTML:

<div class="clearfix holder">
    <div class="column left">
        Some Contents Here...
    </div>
    <div class="column middle">
        Some Contents Here...
    </div>
    <div class="column right">
        Some Contents Here...
    </div>
</div>

CSS:

CSS:

.clearfix:before,
.clearfix:after {
    content: " ";
    display: table;
}
.clearfix:after {
    clear: both;
}
.clearfix {
    *zoom: 1;
}
.holder{
    text-align:center;
}
.column{
    display:inline-block;
    *display:inline;
    *zoom:1;
    width:auto;
}
.left{
    background-color:#ff0;
    float:left;
}
.middle{
    background-color:#f0f;
    margin:0 auto;
}
.right{
    background-color:#0ff;
    float:right;
}

DEMO

演示