March 5, 2012

Nested Divs Spacing

Question by user596075

I have a parent div and two nested divs. I have the nested divs set as display: table-cell. How can you set spacing between those nested divs? I’ve tried margin, but that didn’t do anything (the jsFiddle has margin set, yet with no effect).

jsFiddle showing my question/problem

Answer by sandeep

Give display: inline-table; instead of display: table-cell. Write like this

.child {
    width: 100px;
    height: 100px;
    display: inline-table;
    margin-left: 100px;
}

Check this http://jsfiddle.net/cZptA/9/

Answer by Starx

It is possible and very simple

Since you have specified the child divs to behave as table-cell, by default they will be no spacing between them just like a regular table. So, there is nothing wrong with you code.

If you really want your divs to behave as tables. Your parent div should have border-spacing for the spacing between the table-cells.

.parent {
    width: 400px;
    height: 400px;
    background-color: red;
    border-spacing: 10px;
}

Demo

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!