Html 同一行上 3 个 div 的水平对齐方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17278714/
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 10:25:23  来源:igfitidea点击:

horizontal alignment of 3 divs on the same line

csshtml

提问by tuseau

I'm trying to get 3 divs to appear on the same line in the formation: left - centered - right

我试图让 3 个 div 出现在队形的同一条线上:左 - 居中 - 右

For example, one div left-aligned, the next one centered, and the last one right-aligned.

例如,一个 div 左对齐,下一个居中,最后一个右对齐。

Does anyone know how to do this? I have 2 div left and right aligned, but if I introduce a centered div in the middle it moves the rightmost div onto a new line.

有谁知道如何做到这一点?我有 2 个 div 左右对齐,但是如果我在中间引入一个居中的 div,它会将最右边的 div 移动到一个新行上。

回答by Brian Lewis

.left-col {
    float: left;
    width:25%;
}
.center-col {
    float: left;
    width: 50%;
}
.right-col {
    float: left;
    width: 25%;
}

<div class="left-col">purple</div>
<div class="center-col">monkey</div>
<div class="right-col">dishwasher</div>

回答by Iradiel90

One easy way to do that is using any framework. For example: 960gs -> http://960.gs/.

一种简单的方法是使用任何框架。例如: 960gs -> http://960.gs/

With this framework you can set columns in your page. Example here: http://960.gs/demo.html

使用此框架,您可以在页面中设置列。这里的例子:http: //960.gs/demo.html

回答by G-Cyrillus

you may use : display:flex;

你可以使用:display:flex;

A tool to help you produce the code and undertand how it works : http://the-echoplex.net/flexyboxes/

帮助您生成代码并了解其工作原理的工具:http: //the-echoplex.net/flexyboxes/

example in your case it could be :

例如,在您的情况下,它可能是:

.flex-container {
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-box-direction: normal;
    -moz-box-direction: normal;
    -webkit-box-orient: horizontal;
    -moz-box-orient: horizontal;
    -webkit-flex-direction: row;
    -ms-flex-direction: row;
    flex-direction: row;
    -webkit-flex-wrap: nowrap;
    -ms-flex-wrap: nowrap;
    flex-wrap: nowrap;
    -webkit-box-pack: justify;
    -moz-box-pack: justify;
    -webkit-justify-content: space-between;
    -ms-flex-pack: justify;
    justify-content: space-between;
    -webkit-align-content: stretch;
    -ms-flex-line-pack: stretch;
    align-content: stretch;
    -webkit-box-align: start;
    -moz-box-align: start;
    -webkit-align-items: flex-start;
    -ms-flex-align: start;
    align-items: flex-start;
    }

.flex-item:nth-child(1) {
    -webkit-box-ordinal-group: 1;
    -moz-box-ordinal-group: 1;
    -webkit-order: 0;
    -ms-flex-order: 0;
    order: 0;
    -webkit-box-flex: 0;
    -moz-box-flex: 0;
    -webkit-flex: 0 1 auto;
    -ms-flex: 0 1 auto;
    flex: 0 1 auto;
    -webkit-align-self: auto;
    -ms-flex-item-align: auto;
    align-self: auto;
    }

.flex-item:nth-child(2) {
    -webkit-box-ordinal-group: 1;
    -moz-box-ordinal-group: 1;
    -webkit-order: 0;
    -ms-flex-order: 0;
    order: 0;
    -webkit-box-flex: 0;
    -moz-box-flex: 0;
    -webkit-flex: 0 1 auto;
    -ms-flex: 0 1 auto;
    flex: 0 1 auto;
    -webkit-align-self: auto;
    -ms-flex-item-align: auto;
    align-self: auto;
    }

.flex-item:nth-child(3) {
    -webkit-box-ordinal-group: 1;
    -moz-box-ordinal-group: 1;
    -webkit-order: 0;
    -ms-flex-order: 0;
    order: 0;
    -webkit-box-flex: 0;
    -moz-box-flex: 0;
    -webkit-flex: 0 1 auto;
    -ms-flex: 0 1 auto;
    flex: 0 1 auto;
    -webkit-align-self: auto;
    -ms-flex-item-align: auto;
    align-self: auto;
    }

/*
    Legacy Firefox implementation treats all flex containers
    as inline-block elements.
*/

@-moz-document url-prefix() {
.flex-container {
    width: 100%;
    -moz-box-sizing: border-box;
    }

}

other way to fake an horizontal-align-content:justify(this is made up rule ). http://codepen.io/gcyrillus/pen/Babcs

另一种伪造 an 的方法horizontal-align-content:justify(这是由规则构成的)。 http://codepen.io/gcyrillus/pen/Babcs

with little less CSS to adapt for older IES

用更少的 CSS 来适应旧的 IES

.justify {
  text-align:justify;
  line-height:0;
}
.justify:after, .justify span.ie {
  content:'';
  display:inline-block;
  width:100%;
  vertical-align:top;
}
.justify > div {
  text-align:left;
  line-height:1.2em;
  display:inline-block;
  *display:inline;
  zoom:1;
  width:50%;
  border:solid;
}
.justify > div:nth-child(odd) {
  width:20%;
}

Float and display:table have already been discussed :)

Float 和 display:table 已经讨论过了 :)

回答by Mr_Green

You can do something like this:

你可以这样做:

body {                    /* or parent element of below child elements */
    position: relative;
}
.left-col {
    float: left;
    width:25%;
    height:100px;
    background-color:blue;
    position:absolute;
    top:0;
    left: 0;
}
.center-col {
    width: 10%;
    height:100px;
    margin: 0 auto;
    background-color: cornflowerblue;
    text-align:center;
}
.right-col {
    width: 25%;
    height:100px;
    background-color:green;
    text-align: right;
    position:absolute;
    top:0;
    right: 0;
}

Working Fiddle

工作小提琴

Care must be taken that the sum of the widths of the three elements must not be greater than 100%. if you want to use borders for these elements then create child elements for each element and give position: absoluteto them. Something like this:

必须注意三个元素的宽度总和不得大于 100%。如果您想为这些元素使用边框,则为每个元素创建子元素并赋予position: absolute它们。像这样的东西:

.childDiv{
    position: absolute;
    top: 2px;                 /* acts as border-top for parent element*/
    bottom: 2px;              /* border-bottom */
    left: 2px;                /* border-left */
    right: 2px;               /* border-right */
    background-color: black;  /* acts as border-color for parent element */
}

回答by CarlosB

So you want to make a table with div's? :)

所以你想用div制作一张桌子?:)

ok it has to be something like:

好的,它必须是这样的:

<div style="float:left; width:20%;"></div>
<div style="float:left; width:20%;"></div>
<div style="float:left; width:20%;"></div>

this should be on a css rule though has you probably will want to change the width of each div

这应该是一个 css 规则,尽管你可能想要改变每个 div 的宽度