我想有3个div对齐在一个容器div,就像这样:

[[LEFT]       [CENTER]        [RIGHT]]

容器div是100%宽(没有设定宽度),中心div在调整容器大小后应该保持在中心。

所以我设置:

#container{width:100%;}
#left{float:left;width:100px;}
#right{float:right;width:100px;}
#center{margin:0 auto;width:100px;}

但它变成了:

[[LEFT]       [CENTER]              ]
                              [RIGHT]

任何建议吗?


当前回答

我喜欢我的杠铃紧而有活力。这是css3和HTML 5

First, setting the Width to 100px is limiting. Don't do it. Second, setting the container's width to 100% will work ok, until were talking about it being a header/footer bar for the whole app, like a navigation or credits/copyright bar. Use right: 0; instead for that scenario. You are using id's (hash #container, #left, etc) instead of classes (.container, .left, etc), which is fine, unless you want to repeat your style pattern elsewhere in your code. I'd consider using classes instead. For HTML, no need to swap order for: left, center, & right. display: inline-block; fixes this, returning your code to something cleaner and logically in order again. Lastly, you need to clear the floats all up so that it doesn't mess with future <div>. You do this with the clear: both;

总结:

HTML:

<div class="container">
  <div class="left"></div>
  <div class="center"></div>
  <div class="right"></div>
  <div class="clear"></div>
</div>

CSS:

.container {right: 0; text-align: center;}

.container .left, .container .center, .container .right { display: inline-block; }

.container .left { float: left; }
.container .center { margin: 0 auto; }
.container .right { float: right; }
.clear { clear: both; }

如果使用HAML和SASS,则有加分项;)

HAML:

.container
  .left
  .center
  .right
  .clear

萨斯:

.container {
  right: 0;
  text-align: center;

  .left, .center, .right { display: inline-block; }

  .left { float: left; }
  .center { margin: 0 auto; }
  .right { float: right; }
  .clear { clear: both; }
}

其他回答

Float属性实际上并不用于对齐文本。

此属性用于将元素添加到右侧、左侧或中心。

Div > Div{边框:1px纯黑色;} < html > < div > < div风格= "浮动:左”>第一个< / div > < div风格= "浮动:左”>第二< / div > < div风格= "浮动:左”>第三< / div > < div风格= "浮动:对" >第一个< / div > < div风格= "浮动:对" >第二< / div > < div风格= "浮动:对" > 3 < / div > < / div > < / html >

左输出为[First][second][Third]

右输出为[Third][Second][First]

这意味着float => left属性将把你的下一个元素添加到上一个元素的左边,right也是如此

此外,你还必须考虑父元素的宽度,如果子元素的宽度之和超过了父元素的宽度,那么下一个元素将在下一行添加

< > html < div style =教室:100% > < div style =花车:左派;教室:50%第一> < / div > < div style =花车:左派;教室:50% >第二个< / div > < div style = "漂浮:左派;第三教室:50% > < / div > < / div > < / html >

(第一次)(第二次)

(三)

所以你需要考虑所有这些方面来得到完美的结果

可能的答案,如果你想保持HTML的顺序而不使用flex。

HTML

<div class="a">
  <div class="c">
    the 
  </div>
  <div class="c e">
    jai ho 
  </div>
  <div class="c d">
    watsup
  </div>
</div>

CSS

.a {
  width: 500px;
  margin: 0 auto;
  border: 1px solid red;
  position: relative;
  display: table;
}

.c {
  display: table-cell;
  width:33%;
}

.d {
  text-align: right;
}

.e {
  position: absolute;
  left: 50%;
  display: inline;
  width: auto;
  transform: translateX(-50%);
}

码笔链接

如果左边、中间和右边的div有不同的宽度,你可以这样完成:

  #container {
    position: relative;
    width: 100%;
    text-align: center;
  }

  #left {
    position: absolute;
    left: 0px;
  }

  #right {
    position: absolute;
    right: 0px;
  }

  #center {
    display: inline-block;
  }

如果你的中心DIV是文本,你不需要#center CSS。

使用CSS3 Flexbox可以很容易地做到这一点,这个功能将在未来被几乎所有浏览器使用(当<IE9完全死亡时)。

查看浏览器兼容性表

HTML

<div class="container">
  <div class="left">
    Left
  </div>
  <div class="center">
    Center
  </div>
  <div class="right">
    Right
  </div>
</div>

CSS

.container {
  display: flex;
  flex-flow: row nowrap; /* Align on the same line */
  justify-content: space-between; /* Equal margin between the child elements */
}

Output: .container { display: flex; flex-flow: row nowrap; /* Align on the same line */ justify-content: space-between; /* Equal margin between the child elements */ } /* For Presentation, not needed */ .container > div { background: #5F85DB; padding: 5px; color: #fff; font-weight: bold; font-family: Tahoma; } <div class="container"> <div class="left"> Left </div> <div class="center"> Center </div> <div class="right"> Right </div> </div>

#warpcontainer  {width:800px; height:auto; border: 1px solid #000; float:left; }
#warpcontainer2 {width:260px; height:auto; border: 1px solid #000; float:left; clear:both; margin-top:10px }