Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to space the children of a div with css?

Tags:

css

I want a gap of say 30px; between all children of my div. E.g if I have:

<div id="parent">        <img ... />    <p>...</p>    <div>.......</div> </div> 

I want all of them to have a space of 30px; between them. How can I do this with CSS?

like image 305
Ali Avatar asked Jun 28 '11 13:06

Ali


People also ask

How do you put a space between divs in CSS?

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

How do you put a space in a CSS content?

The break tag is meant for single line breaks, and not more than one in a row. If you want to add extra whitespace between pieces of text, use CSS padding and margins instead for cleaner code. Or, you can use an HTML <p> tag, as we'll see next.

How do you put space between images in CSS?

You can either use the margin or its shorthand properties to add space between the images. For the horizontal spacing, you can use the margin-left and margin-right. While for the vertical spacing you can use the margin-top and margin-bottom properties.


1 Answers

For an unknown amount of children you could use.

#parent > * {     margin: 30px 0; } 

This will add a top and bottom margin of 30px to all direct children of #parent.

But img is not displaying as block default, so you may use:

#parent > * {     display: block;     margin: 30px 0; } 

Vertical margins of block elements will be collapsed. But you will have margins at top and bottom of your parent div. To avoid that use the following code:

#parent > * {     display: block;     margin-top: 30px; }  #parent > *:first-child {     margin-top: 0px; } 

This will only add top margin and removes that top margin for the first element.

like image 51
DanielB Avatar answered Sep 23 '22 04:09

DanielB