如何对齐3个div左中右? [重复]

时间:2021-01-08 22:22:14

This question already has an answer here:

这个问题在这里已有答案:

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>

5 个解决方案

#1


16  

Add a wrapper div and 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

DEMO

#2


2  

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;
}

#3


2  

<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>

#4


0  

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>

#5


0  

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

DEMO

#1


16  

Add a wrapper div and 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

DEMO

#2


2  

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;
}

#3


2  

<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>

#4


0  

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>

#5


0  

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

DEMO