Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove "whitespace" between div element

Tags:

This is my HTML code

<div id="div1">     <div></div><div></div><div></div><br/><div></div><div></div><div></div> </div> 

My CSS:

#div1 {     width:150px;height:100px;white-space:nowrap;     border:blue 1px solid;padding:5px; } #div1 div {     width:30px;height:30px;     border:blue 1px solid;     display:inline-block;     *display:inline;zoom:1;     margin:0px;outline:none; } 

If I insert the <!DOCTYPE html> before the <html> tag, the page will look like this:

enter image description here

But if I remove the <!DOCTYPE html> tag, the 'whitespace' between the two lines will be remove enter image description here

But I'd like to use <!DOCTYPE html> tag, it's recommend, but I can't find any CSS rule that can remove that whitespace, I have used margin:0;outline:none; etc... but it not work , anyone help me. Thanks!( I'm not good at English ...)

like image 759
phibao37 Avatar asked Aug 29 '13 10:08

phibao37


People also ask

Why is there whitespace between divs?

You get whitespace there because you have whitespace inbetween the divs. Whitespace between inline elements is interpreted as a space. However, this is a bad way to do what you want to do. You should float the elements if thats what you want to do.

How do I get rid of the white space between images in HTML?

HTML. Line Height Property: The CSS line-height-property can be used to set the height of the line, whose value is set to normal, by default. By setting the height of the container at 0%, the white space can be removed.

How do I get rid of white space in HTML css?

We can also remove white space by setting parent element font-size to 0 and child elements font-size to 17px .


2 Answers

The cleanest way to fix this is to apply the vertical-align: top property to you CSS rules:

#div1 div {    width:30px;height:30px;    border:blue 1px solid;    display:inline-block;    *display:inline;zoom:1;    margin:0px;outline:none;    vertical-align: top; } 

If you were to add content to your div's, then using either line-height: 0 or font-size: 0 would cause problems with your text layout.

See fiddle: http://jsfiddle.net/audetwebdesign/eJqaZ/

Where This Problem Comes From

This problem can arise when a browser is in "quirks" mode. In this example, changing the doctype from:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 

to

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN"> 

will change how the browser deals with extra whitespace.

In quirks mode, the whitespace is ignored, but preserved in strict mode.

References:

html doctype adds whitespace?

https://developer.mozilla.org/en/Images,_Tables,_and_Mysterious_Gaps

like image 98
Marc Audet Avatar answered Oct 31 '22 19:10

Marc Audet


Add line-height: 0px; to your parent div

jsfiddle: http://jsfiddle.net/majZt/

like image 23
Simon Staton Avatar answered Oct 31 '22 20:10

Simon Staton