考虑下面的代码:

#wrapper { width: 500px; border: 1px solid black; } #first { width: 300px; border: 1px solid red; } #second { border: 1px solid green; } <div id="wrapper"> <div id="first">Stack Overflow is for professional and enthusiast programmers, people who write code because they love it.</div> <div id="second">When you post a new question, other users will almost immediately see it and try to provide good answers. This often happens in a matter of minutes, so be sure to check back frequently when your question is still new for the best response.</div> </div>

我希望这两个div在包装器div中彼此相邻。在这种情况下,绿色div的高度应该决定包装器的高度。

我如何通过CSS实现这一点?


当前回答

#wrapper { width: 1200; border: 1px solid black; position: relative; float: left; } #first { width: 300px; border: 1px solid red; position: relative; float: left; } #second { border: 1px solid green; position: relative; float: left; width: 500px; } <div id="wrapper"> <div id="first">Stack Overflow is for professional and enthusiast programmers, people who write code because they love it.</div> <div id="second">When you post a new question, other users will almost immediately see it and try to provide good answers. This often happens in a matter of minutes, so be sure to check back frequently when your question is still new for the best response.</div> </div>

其他回答

你可以使用CSS float属性将元素放在一起:

#first {
float: left;
}
#second {
float: left;
}

你需要确保包装div允许浮动的宽度,和空白等设置正确。

这是正确的CSS3答案。希望这能对你有所帮助 我真的很推荐你去看这本书:https://www.amazon.com/Book-CSS3-Developers-Future-Design/dp/1593272863其实我现在已经通过看这本书做出了这个解决方案。: D

#wrapper{ display: flex; flex-direction: row; border: 1px solid black; } #first{ width: 300px; border: 1px solid red; } #second{ border: 1px solid green; } <div id="wrapper"> <div id="first">Stack Overflow is for professional and enthusiast programmers, people who write code because they love it.</div> <div id="second">When you post a new question, other users will almost immediately see it and try to provide good answers. This often happens in a matter of minutes, so be sure to check back frequently when your question is still new for the best response.</div> </div>

尝试使用下面的代码更改来将两个div放在彼此前面

#wrapper {
  width: 500px;
  border: 1px solid black;
  display:flex;
}

JSFiddle link

浮动一个或两个内部div。

浮动一个div:

#wrapper {
    width: 500px;
    border: 1px solid black;
    overflow: hidden; /* will contain if #first is longer than #second */
}
#first {
    width: 300px;
    float:left; /* add this */
    border: 1px solid red;
}
#second {
    border: 1px solid green;
    overflow: hidden; /* if you don't want #second to wrap below #first */
}

或者如果你浮动两个,你需要鼓励包装器div包含两个浮动的子元素,否则它会认为它是空的,不会在它们周围设置边框

浮动两个div:

#wrapper {
    width: 500px;
    border: 1px solid black;
    overflow: hidden; /* add this to contain floated children */
}
#first {
    width: 300px;
    float:left; /* add this */
    border: 1px solid red;
}
#second {
    border: 1px solid green;
    float: left; /* add this */
}

#wrapper { width: 1200; border: 1px solid black; position: relative; float: left; } #first { width: 300px; border: 1px solid red; position: relative; float: left; } #second { border: 1px solid green; position: relative; float: left; width: 500px; } <div id="wrapper"> <div id="first">Stack Overflow is for professional and enthusiast programmers, people who write code because they love it.</div> <div id="second">When you post a new question, other users will almost immediately see it and try to provide good answers. This often happens in a matter of minutes, so be sure to check back frequently when your question is still new for the best response.</div> </div>