简体   繁体   English

HTML,在同一行的两边对齐两个元素

[英]HTML, align two elements on both sides of the same line

不使用表格,如何在同一行中对齐两个元素(一个在左边,另一个在右边)?

HTML content: HTML 内容:

<div class='container'>
  <div class="align-left">left</div>
  <div class="align-right">right</div>
</div>

Style as shown:样式,如下所示:

.container{ width:100%; }    
.align-left{ float: left;width:50%; }
.align-right{ float: right;width:50%; }

Using float: right;使用浮动:对; and float:left;和浮动:左;

If you want them on the same line, you can use INLINE or INLINE-BLOCK.如果您希望它们在同一行,您可以使用 INLINE 或 INLINE-BLOCK。

 #element1 { display: inline-block; margin-right: 10px; width: 200px; background-color: red; } #element2 { display: inline-block; width: 200px; background-color: red; }
 <div id="element1">element 1 markup</div> <div id="element2">element 2 markup</div>

try this:试试这个:

<div class="align-left">
  left
</div>
<div class="align-right">
  right
</div>

and the css:和CSS:

.align-left{
   float: left;
 }

.align-right{
   float: right;
 }

see example here在这里查看示例

I have made an example for you to understand the elements a bit better.我做了一个例子让你了解元素的好一点。 With using a float you can 'move'(float) elements to a specific side (left or right).与使用浮动,你可以“移动”(浮动)的元素,以一个特定的侧面(左或右)。

Depending on what you want, you can float everything to the left, which makes every element 'stick' to the other element as long as the width's of the elements do not exceed the parent element width.这取决于你想要什么,你可以浮动的一切到左边,这使得每一个元素“粘”到其它元件,只要宽度的不超过父元素宽度的元素。 Otherwise they will 'fall' under the element to fit the width.否则会“下降”的元素,以适应宽度。 For instance this code where the container has a width of 600px.例如,这代码,其中所述容器具有的600像素的宽度。 Each class 'test' has a width of 250px.每一类“测试”具有250像素的宽度。 This will result in 2 elements next to each other, but the other one will fall under it.这将导致在2个元素彼此相邻,但其他一个将属于它。

<div class='container green'>
  <div class='test big blue'>a</div>
  <div class='test big red'>b</div>
  <div class='test big yellow'>c</div>
</div>

http://jsfiddle.net/qBR9M/4/ http://jsfiddle.net/qBR9M/4/

The best way to play with this is the give your elements a background-color, so you can see how the respond.玩这个最好的办法是给你的元素的背景色,所以你可以看到如何响应。

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

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