Why Does Vertical-Align: Text-Top Make Element Go Down

Why does vertical-align: text-top make element go down?

Ref: https://www.w3.org/TR/CSS2/visudet.html#line-height

To understand this you need to first consider the definition of text-top:

The following values only have meaning with respect to a parent inline element, or to the strut of a parent block container element.

In the following definitions, for inline non-replaced elements, the box used for alignment is the box whose height is the 'line-height' (containing the box's glyphs and the half-leading on each side, see above).

Then

text-top

Align the top of the box with the top of the parent's content area

So we need to identify the top of the box and the top of the parent's content area

If we add some decorations, we can easily identify them

body {
font-family: sans-serif;
font-size: 30px;
}

p {
background: yellow;
line-height: 50px;
background:
linear-gradient(blue,blue) 0 7px/100% 2px no-repeat
yellow;
}

p span {
background:green;
}

.three {
vertical-align: text-top;
background:red;
}


.block {
display: inline-block;
width: 20px;
height: 20px;
background: pink;
border: 2px solid black;
vertical-align: text-top;
}
<div>
<p><span class="one">I'm</span> <span class="two">on the</span> <span class="three">yellow</span> <span>background</span> <span class="block"></span></p>
</div>

Why is vertical-align:text-top; not working in CSS

The vertical-align attribute is for inline elements only. It will have no effect on block level elements, like a div. Also text-top only moves the text to the top of the current font size. If you would like to vertically align an inline element to the top just use this.

vertical-align: top;

The paragraph tag is not outdated. Also, the vertical-align attribute applied to a span element may not display as intended in some mozilla browsers.

Why is vertical-align: middle not working on my span or div?

Using CSS3:

<div class="outer">
<div class="inner"/>
</div>

Css:

.outer {
display : flex;
align-items : center;
}

use "justify-content: center;" to align elements horizontally

Note: This might not work in old IE's

Vertical-align not working as expected

I highly recommend reading this vertical-align article to gain in-depth understanding of the property.

How does the vertical-align property work?

Simply said: vertical-align is only active/valid when the element it is applied to has display: inline-block or ìnline, which for example is useful if you want to align a bunch of images at their top border: You define them as inline-blocks and apply vertical-align: top to them

Here is an example:

.wrapper {

height: 250px;

border: 1px solid green;

}

.wrapper>img {

display: inline-block;

vertical-align: top;

}
<div class="wrapper">

<img src="https://placehold.it/120x40">

<img src="https://placehold.it/70x140">

<img src="https://placehold.it/80x60">

<img src="https://placehold.it/60x140">

</div>

Does vertical align CSS property ever work?

because vertical-align only applies to inline level and table-cell elements. Both div and p are block level elements.

Applies to inline-level and table-cell elements. It also applies to
::first-letter and ::first-line.

MDN Source

With that in mind and using your example make your div a table and your p a table-cell

div {

height: 200px;

width: 500px;

background: red;

text-align: center;

display: table

}

p {

vertical-align: middle;

display: table-cell;

}
<div>

<p>

yo bro

</p>

</div>

How can I vertically align elements in a div?

Wow, this problem is popular. It's based on a misunderstanding in the vertical-align property. This excellent article explains it:

Understanding vertical-align, or "How (Not) To Vertically Center Content" by Gavin Kistner.

“How to center in CSS” is a great web tool which helps to find the necessary CSS centering attributes for different situations.


In a nutshell (and to prevent link rot):

  • Inline elements (and only inline elements) can be vertically aligned in their context via vertical-align: middle. However, the “context” isn’t the whole parent container height, it’s the height of the text line they’re in. jsfiddle example
  • For block elements, vertical alignment is harder and strongly depends on the specific situation:
    • If the inner element can have a fixed height, you can make its position absolute and specify its height, margin-top and top position. jsfiddle example
    • If the centered element consists of a single line and its parent height is fixed you can simply set the container’s line-height to fill its height. This method is quite versatile in my experience. jsfiddle example
    • … there are more such special cases.

Vertical Align Not Taking Effect

Two things:

  1. Add line-height: 40px to your .favouriteLink a rule. I picked that since it matches the height you set for the button.

  2. Add the rule .favouriteLink img {vertical-align: middle;}. If that’s not quite where you want it, use length-based offsets like vertical-align: -9px or some such.

How to align content of a div to the bottom

Relative+absolute positioning is your best bet:

#header {
position: relative;
min-height: 150px;
}

#header-content {
position: absolute;
bottom: 0;
left: 0;
}

#header, #header * {
background: rgba(40, 40, 100, 0.25);
}
<div id="header">
<h1>Title</h1>
<div id="header-content">And in the last place, where this might not be the case, they would be of long standing, would have taken deep root, and would not easily be extirpated. The scheme of revising the constitution, in order to correct recent breaches of it, as well as for other purposes, has been actually tried in one of the States.</div>
</div>


Related Topics



Leave a reply



Submit