Two divs floating left and right: How can I keep them on the same level when a page resizes?

我怕爱的太早我们不能终老 提交于 2019-12-10 19:17:29

问题


Given the following HTML:

<!DOCTYPE html>
<html>
<body>
  <div id="container">
    <div id="left" style="float: left; background-color: #777; width: 500px;">
      Here is some content. Blah blah blah etc.
      <div style="height: 50px;">Some more content</div>
      And finally some more.
    </div>
    <div id="right" style="float: right; background-color: #aaa; width: 500px;">
      Here is some content. Blah blah blah etc.
      <div style="height: 50px;">Some more content</div>
      And finally some more.
    </div>
    <div style="float: clear;"></div>
  </div>
</body>
</html>

What can I do to div#container or another tag to prevent div#right from moving under div#left when I resize the browser window such that the windows width is smaller then 1000px?


回答1:


Set a min-width to your container:

#container {
  min-width: 1000px;
}



回答2:


Give the #container DIV a min-width: 1000px property, that way the parent will always be wide enough to hold both the children side-by-side.




回答3:


Container div not closed in your code. please check it once, and apply min-width:1000px;




回答4:


http://www.w3schools.com/css/default.asp I learned all of my CSS here, there is a section on positioning that is quite helpful I suggest reading it for what your working on now as well as future projects.

Try:

#container {
   position: relative;
}


来源:https://stackoverflow.com/questions/4082894/two-divs-floating-left-and-right-how-can-i-keep-them-on-the-same-level-when-a-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!