Want to know the reason for this behavior.
CSS
div {
display: inline-block;
margin-right: 2px;
width: 20px;
background-color: red;
}
Empty div
<div style="height:20px;"></div>
<div style="height:40px;"></div>
<div style="height:60px;"></div>
<div style="height:80px;"></div>
behavior: element increases from bottom to top (height)
div with text
<div style="height:20px;">20</div>
<div style="height:40px;">30</div>
<div style="height:60px;">40</div>
<div style="height:80px;">50</div>
behavior: element increases from top to bottom (height)
see it in action: http://jsfiddle.net/8GGYm/
Basically it got to do with the way that vertical-align: is calculated. So if you put vertical-aling:bottom; attribute in the css then you will notice it will be the same with and without text.
you can read the this for more details.
When the div has no content, padding is not drawn in the box (i.e. when when 0, if there is content, the browser calculates where the padding would be). so there is a little difference in calculating with and without text.
Hope this is helpfull.
please see here: http://jsfiddle.net/dd24z/. By default text is vertical-align: top, but you can change that behavior:
div {
display: inline-block;
margin-right: 2px;
width: 20px;
background-color: red;
vertical-align: bottom;
}
http://www.w3.org/TR/2008/REC-CSS2-20080411/visudet.html#line-height
'vertical-align': baseline
Align the baseline of the box with the baseline of the parent box. If the box doesn't have a baseline, align the bottom of the box with the parent's baseline.
Add
vertical-align: bottom;
to your CSS. Hope it works as you want.
I guess this can be explained by the text alignment, independently from divs.
Text, when placed in a div, is vertically aligned to top-left by default. Those divs without text align beside each other (inline-block) expanding the page downwards. If you add another div, you'll see the second header going further down.
<h1>Empty div</h1>
Some text
<div style="height:20px;"></div>
<div style="height:40px;"></div>
<div style="height:60px;"></div>
<div style="height:80px;"></div>
continuing here
<h2>Div with text</h2>
Some text
<div style="height:20px;">20</div>
<div style="height:40px;">40</div>
<div style="height:60px;">60</div>
<div style="height:80px;">80</div>
continuing here
...
div {
display: inline-block;
margin-right: 2px;
width: 20px;
background-color: red;
}
Fiddle
In the above fiddle, you can see that the text line is the "guideline".
Maybe this is the explanation: once the divs have text in them, they will align it with the surrounding text and, if inexistent, then they align their bottom line.
I'm sorry, maybe not very clear but I hope you understand my view.
Related
I have 3 divs like so:
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
with the following CSS:
div {
border: 1px solid black;
display: inline-block;
height: 100px;
width: 100px;
}
When the divs are empty, this code works fine. All divs align along the same horizontal plane. But! When I put any content in 1 or 2 divs, the divs with the content move down about 90% of the height:
<div class="div1">X</div>
<div class="div2">Y</div>
<div class="div3"></div>
Divs 1 and 2 are now spaced down in comparison to the normally aligned div 3. The plot really thickens when I add content to the final div:
<div class="div1">X</div>
<div class="div2">Y</div>
<div class="div3">Z</div>
Now all three divs are properly aligned at page top again. Not sure what's happening here or the proper work around?
This is happening because the default vertical-align for a inline block element is baseline*.
This image from CSS Tricks helps to demonstrate the baseline of text:
As you can see, the baseline isn't how far down the text goes, it is the line that the text is aligned on. With vertical-align:baseline, the div with no content aligns with the baseline created by the <div>'s with content.
This image may help you visualize what's happening(or, you can play with the jsfiddle):
To make all your <div>'s align, no matter the content, set vertical-align:top;:
div {
border: 1px solid black;
display: inline-block;
height: 100px;
width: 100px;
vertical-align:top;
}
This article also helps explain vertical-align some more
* W3 Specs
I want to gain a deeper understanding of how line-height works, in particular with centering text vertically within a fixed height element. In the example link below, you can see that setting the line-height to be equal to the container's height will center items as long as the items are all of the same size. When you put two different sized spans in, then the larger one will be centered, while the other will not be. I am at a loss to explain this, since both the large and small elements work separately. How can I accomplish having a large and small span to be centered (using line-height)? (and why is this behavior happening in the first place?)
.header {
background: grey;
width: 100%;
height: 65px;
}
.big, .small {
line-height: 65px;
}
.big {
font-size: 1.5em;
}
.
<div class="header">
<span class="big">A</span>
<span class="small">B</span>
</div>
<br/>
<div class="header">
<span class="small">A</span>
<span class="small">B</span>
</div>
<br/>
<div class="header">
<span class="big">A</span>
<span class="big">B</span>
</div>
JS Fiddle Example
This happens, because default vertical-align is baseline. If you switch it to vertical-align:middle you get the expected behaviour.
Edit for clarification:
As span's are inline elements they share the same baseline, regardless of the font size you are using. Vertical alignment of text is baseline initially, but by setting it to middle, you force these inline elements to align vertically centered.
More info on vertical align here: https://developer.mozilla.org/en/docs/Web/CSS/vertical-align
You need vertical-align: middle; . default value is baseline.
.big, .small {
line-height: 150px;
vertical-align: middle;
}
JSFiddle
I am having an issue properly aligning a div element.
For example: at my site:
http://www.journeywithandrew.com/messel-pit-fossil-site-unesco-site/
you can see there is an intro, and then a 'key facts' box.
What I want is for that 'key facts' box to be brought up. So the last line in the box is inline with the last line of the intro text. And of course for the intro text to wrap around the 'key facts' box. So really what I want is to bottom-right align that box. Hope this makes sense..
Tried to make a negative top margin on the 'key facts' box but that doesn't displace the intro text. Just makes it overlay the text.
Any suggestions?
I think this FIDDLE will help you figure out where to put the box.
(Trial and error)
Depending on the width of the page-wrap or screen, the text position might change.
CSS
.page-wrap {
width: 600px;
}
.spacerdiv {
height: 100px;
}
.stuff {
float: right;
width: 100px;
height: 100px;
line-height: 100px;
text-align: center;
border: 1px solid black;
border-radius: 5px;
}
You have the following general format:
<div class="entry-content">
<p>Some words</p>
<div class="keyfactsunescowrapper">
<!-- This block -->
</div>
</div>
The paragraph tag, being a block level element, pushes the next content to a new line. The float respects that and does not push itself up.
However, you can put the Key facts block before the paragraph(s) and problem solved. Since it is floated right, it allows the paragraph to flow on the same line and the paragraph won't push it to a new line since the paragraph is after it.
<div class="entry-content">
<div class="keyfactsunescowrapper">
<!-- This block -->
</div>
<p>Some words</p>
</div>
So here are two identical divs:
HTML
<div id="left"></div>
<div id="right"></div>
CSS
#left, #right
{
width: 100px;
height: 40px;
border: 1px solid gray;
display: inline-block;
}
These render just fine, as two identical boxes side-by-side (fiddle: http://jsfiddle.net/URy59/).
But with text in one div, and none in the other, they're misaligned! (fiddle: http://jsfiddle.net/URy59/1/)
This...
<div id="left"></div>
<div id="right">Right</div>
...results in:
This behaviour is reproducible using <span> as well.
What causes this, and why? What's a good solution to this?
The short answer: set the vertical-align property to top.
The longer answer: An inline element's default vertical alignment is baseline. When your divs have no content, they line up fine. However when you added the text, the browser then will move the div downward so that the text sits on the baseline:
By changing the alignment to top, you align the divs the way you need.
jsFiddle example
You need to vertically align your elements:
#left, #right {
...
vertical-align: top;
}
JSFiddle demo.
On this page there's a #container div that has a white background. This white background doesn't appear behind the 5 floated boxes (with titles "Latest", Music Festivals, Alerts, etc.) even though those boxes are children of #container and don't specify a background colour of their own, why?
The parent container does not expand to fit floated elements. You need a way to "clear" the float to end the floating after the child elements. See this page:
http://www.quirksmode.org/css/clearing.html
you should add an <div style="clear:both"></div> after last floated element, so that your floated elements affect on increasing your #container block's height. otherwise they "fall out" from your container box.
You just need to clear your floats after using your .box class:
.box {
float: left;
width: 30%;
text-align: justify;
margin-right: 25px;
}
Here's the simplest way possible:
<div id="main">
<div class="box"></div>
<div class="box"></div>
<br style="clear: both; display: block;" />
</div>