Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set space between divs

I have two divs like this:

<section id="main">         <div id="left">             <asp:ContentPlaceHolder ID="left" runat="server" />         </div>         <div id="right">             <asp:ContentPlaceHolder ID="right" runat="server" />         </div> </section> 

And here is my css:

#left {     float: left;     margin-right: 17px;  }  #right {     float: right; } 

I want the space between the divs to be 40px. I tried adding padding, margin and width in my css, but I think it didn't set the padding to correct 40px. How to do it?

like image 982
petko_stankoski Avatar asked Apr 07 '12 08:04

petko_stankoski


People also ask

How do I add space between divs?

Use the margin property. I put stye=“margin-top: 50px;” in the second div.

How do you put a space between two boxes in HTML?

The simplest way to add a space in HTML (besides hitting the spacebar) is with the non-breaking space entity, written as &nbsp; or &#160;. Multiple adjacent non-breaking spaces won't be collapsed by the browser, letting you “force” several visible spaces between words or other page elements.


2 Answers

For folks searching for solution to set spacing between N divs, here is another approach using pseudo selectors:

div:not(:last-child) {   margin-right: 40px; } 

You can also combine child pseudo selectors:

div:not(:first-child):not(:last-child) {   margin-left: 20px;   margin-right: 20px; } 
like image 72
David Salamon Avatar answered Oct 02 '22 23:10

David Salamon


Float them both the same way and add the margin of 40px. If you have 2 elements floating opposite ways you will have much less control and the containing element will determine how far apart they are.

#left{     float: left;     margin-right: 40px; } #right{    float: left; } 
like image 26
benni_mac_b Avatar answered Oct 02 '22 22:10

benni_mac_b