Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

div displaying a margin

Tags:

html

css

margin

i am trying to make a game board using divs with different background-colors so i wrote some simple css (i am just figuring out how to do it at this stage, and i wrote the divs in manually - so im not including the html/js)

.grass {
    background-color: oliveDrab;
}

.dirt {
    background-color: sandyBrown;
}

div{
    height: 25px;
    width: 25px;
    display: inline-block;
    margin: 0;
}

when i rendered it, and there was a margin. like so:

margin added

so i checked firebug and it shows margin,border, and padding is 0!!

no margin in style

DEMO fiddle.

i am being led to feel i am missing some extremely basic css knowledge here.

like image 327
Math chiller Avatar asked Dec 09 '22 12:12

Math chiller


2 Answers

Inline and inline-block level elements are sensitive to white space.

Here's a jsFiddle example of your demo with the white space in the first line removed.

jsFiddle example

<div id = "board" class = "grass"></div><div id = "board" class = "grass"></div><div id = "board" class = "grass"></div><div id = "board" class = "grass"></div><div id = "board" class = "grass"></div><div id = "board" class = "grass"></div>
like image 94
j08691 Avatar answered Dec 30 '22 06:12

j08691


It is because there are inline-block elements.

Either remove the white space in your markup, add a negative margin, or using float elements as opposed to display:inline-block.

Here is another example:

Probably the best solution.

body {
    width:150px;
}
div {
    float:left;
}

jsFiddle with negative margins

jsFiddle with white space removed

like image 27
Josh Crozier Avatar answered Dec 30 '22 06:12

Josh Crozier