Anonymous white space between two divs

Here is the fiddle,

I have two divs , one for the title and the other for the main body. Inside the div , which is for the title, I have another div , and that div was floated left . Because of this, a gap has occurred between the two divs (between the div for the title and the div for the main body).

[Note: see #DivLogo in the css section of the script]

My question is why this problem comes up due to swimming.?

+4
source share
5 answers

This is an artifact of having your #DivHeader inline-block . Change it to display: block and set overflow: hidden; to contain floats. See http://jsfiddle.net/jhcUb/

+8
source

To apply

 float:left; 

to #DivHeader to contain its descendants

remove

 position:relative; 

from #DivMain

see fiddle

+4
source

Why inline block behavior

Your original CSS is as follows:

 #DivHeader { position:relative; display:inline-block; width:100%; font-family: Century Gothic, sans-serif; background-color:#3b5998; } 

There are two #DivHeader , one of them floats, and one is absolutely positioned and as such does not contribute to the height of #DivHeader .

Suppose you set display: block , you would see a red background, which was written in the upper left corner of the page, and you would not see the title.

This is because you set #DivMain to position: relative , so you created a new stacking context that is drawn on top of previously displayed elements.

If you set position: static to #DivMain , you will see the contents of the header from the floating element.

So, returning to #DivHeader using display: inline-block , the element now recognizes a space in your HTML document, creates an anonymous inline field, which means that #DivHeader now has content. Since there is a space after the float, this means that the clearance resulting from the float contributes to the built-in block, and you see the background color, etc.

Fixing the problem is easy enough, on DivHeader install overflow: auto and display: block .

One more thing, if you did not swim #DivLogo , you would not see the problem, because the larger font size in the logo elements would move the baseline so that the white space recognized by the built-in block would not be the case.

Pretty little here!

+3
source

Take a look at http://jsfiddle.net/y6hSV/3/

HTML

 <div id="DivHeader"> <div id="DivLogo"> <span class="Logo" id="SpLogo">Header</span><br/> <span class="Logo" id="SpSlogan">{sub header}</span> </div> <div id="DivPrabhu"> <a href="http://google.com">Who?</a> <a href="https://www.facebook.com">Connect</a> <a href="#">Feedback/Bugs</a> </div> </div> <div id="DivMain"> Some text to have layout </div> 

CSS

 html,body { margin:0; padding:0; } #DivHeader { float: left; width:100%; font-family: Century Gothic, sans-serif; background-color:#3b5998; } #DivLogo { position:relative; float:left; /*please remove this property and run*/ padding-bottom:10px; padding-top:10px } .Logo { margin-left:20px; margin-right:20px; color:#fff; } #SpLogo { font-size:35px; font-weight:bold; } #SpSlogan { font-size:20px; } #DivPrabhu{ float: right; margin-top: 30px; font-size:13px; font-family: 'lucida grande',tahoma,verdana,arial,sans-serif; } #DivPrabhu a{ text-decoration:none; color:#fff; margin-left:15px; } #DivPrabhu a:hover{ text-decoration:underline; } #DivMain{ height:600px; width; 100%; background-color:red; } 
0
source

Adding top: -5px; in #DivMain solves your problem !! :)

See the script here .

0
source

Source: https://habr.com/ru/post/1486900/


All Articles