Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make two divs side by side using inline-block?

Tags:

How can I make the divs side by side and one of the div ('contentwrapper') be responsive to the browser resizing.

HMTL

<div id="maincontainer"> <div id="leftcolumn">&nbsp;</div>  <div id="contentwrapper">&nbsp;</div> </div> 

CSS

#maincontainer {     width:100%;     height: 100%; }  #leftcolumn {     display:inline-block;     width: 100px;     height: 100%;     background: blue; }  #contentwrapper {     display:inline-block;     width:100%;     height: 100%;     background-color: red; } 

JSFIDDLE http://jsfiddle.net/A5HM7/

like image 564
user2133606 Avatar asked Dec 26 '13 23:12

user2133606


People also ask

How do I align two divs side by side?

To position the divs side by side, we are using the float property to float each . float-child element to the left. Since they are both floating to the left, they will display side by side if there's enough space for both to fit.

Can 2 divs be in the same line?

By default, if we have multiple div tags it will be displayed one below the other. In othere words, each div will be displayed in a separate line. To display multiple div tags in the same line, we can use the float property in CSS styles. The float property takes left, right,none(default value) and inherit as values.

How do I make a div inline?

Users can see that all div elements inside the parent div are displaying inline. Approach 2: In this approach, we will apply display: inline-block property to all the div which we need to display inline. The display:inline-block property will set the all div elements side by side.

How do I vertically align two divs?

To align two <div> elements vertically in Bootstrap 3, you can try using the CSS Flexible Box Layout. In the example below, we display the needed row as a flex container box with the CSS display property and then, align the flex-items (columns) vertically with the align-items property.


2 Answers

<style>   #maincontainer {     width:100%;     height: 100%;   }    #leftcolumn {     float:left;     display:inline-block;     width: 100px;     height: 100%;     background: blue;   }    #contentwrapper {     float:left;     display:inline-block;     width: -moz-calc(100% - 100px);     width: -webkit-calc(100% - 100px);     width: calc(100% - 100px);     height: 100%;     background-color: red;   } </style> 

enter image description here

enter image description here

like image 169
Asraful Haque Avatar answered Oct 16 '22 13:10

Asraful Haque


Ok, so I think this will be the quickest fix for you. You already have a great html structure but I am going to narrow it down more for you. Here is the JsFiddle.

With your code:

#maincontainer {     width:100%;     height: 100%; } 

I have made a minor adjustment like so:

#maincontainer {     width:100%;     height: 100%;     display:inline-block;//added this } 

and then I also restructured two other things like so:

#leftcolumn {     float:left;//added this     width: 100px;     height:100%;     background: blue; } #contentwrapper {     float:right;//added this     width:100%;     height: 100%;     background-color: red; } 

Now in this JsFiddle, I have appropriately created a specific width, so you can always change that. Please keep in mind that if you use 100% as a width, and try to stick something else in that same line, it will automatically create two lines such like so:

#leftcolumn {     display:inline-block;<-- changed this above.     width: 100px;<----This won't work with the below     height: 100%;     background: blue; }  #contentwrapper {     display:inline-block;<---- changed this above.     width:100%;<---- This won't work with the above     height: 100%;     background-color: red; } 

But if you restructure that to be more like this:

#leftcolumn {     display:inline-block;     width: 10%;<---This will work with the below     height: 100%;     background: blue; }  #contentwrapper {     display:inline-block;     width:90%;<---This will work with the above.     height: 100%;     background-color: red; } 

A few things to note, I did add in a height with the JsFiddle so that I could see the actual dimensions and I also added in width for the exact reason. Something to note that can really help out with implementations and the basic "why does this work" is this.

Comment below if something doesn't work for you :)

like image 26
BuddhistBeast Avatar answered Oct 16 '22 14:10

BuddhistBeast