Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place two divs next to each other?

Tags:

html

css

Consider the following code:

#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>

I would like the two divs to be next to each other inside the wrapper div. In this case, the height of the green div should determine the height of the wrapper.

How could I achieve this via CSS ?

like image 981
Misha Moroshko Avatar asked Apr 27 '11 11:04

Misha Moroshko


People also ask

How do I arrange divs side by side?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.

How do I put 4 divs next to each other?

you can use flex property for, Display:flex apply flex layout to the flex items or children of the container only. So, the container itself stays a block level element and thus takes up the entire width of the screen. ...

How do you put boxes next to each other in HTML?

Instead of using float:left, you can use: "display:inline-block", this will put the div next to each other.


1 Answers

Float one or both inner divs.

Floating one 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 */ } 

or if you float both, you'll need to encourage the wrapper div to contain both the floated children, or it will think it's empty and not put the border around them

Floating both divs:

#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 */ } 
like image 149
clairesuzy Avatar answered Sep 20 '22 05:09

clairesuzy