vertical-align – CSS-Tricks https://css-tricks.com Tips, Tricks, and Techniques on using Cascading Style Sheets. Mon, 02 Aug 2021 14:45:40 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://i0.wp.com/css-tricks.com/wp-content/uploads/2021/07/star.png?fit=32%2C32&ssl=1 vertical-align – CSS-Tricks https://css-tricks.com 32 32 45537868 Deep dive CSS: font metrics, line-height and vertical-align https://css-tricks.com/deep-dive-css-font-metrics-line-height-vertical-align/ Fri, 24 Feb 2017 11:34:06 +0000 http://css-tricks.com/?p=251863 Vincent De Oliveira has written an epic post that details pretty much everything you might ever want to know about the line-height and vertical-align properties. If you’ve ever had trouble aligning things next to text or wondered why two fonts …


Deep dive CSS: font metrics, line-height and vertical-align originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
Vincent De Oliveira has written an epic post that details pretty much everything you might ever want to know about the line-height and vertical-align properties. If you’ve ever had trouble aligning things next to text or wondered why two fonts look so wildly different from one another then this post is certainly for you.

To Shared LinkPermalink on CSS-Tricks


Deep dive CSS: font metrics, line-height and vertical-align originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
251863
Centering in CSS: A Complete Guide https://css-tricks.com/centering-css-complete-guide/ https://css-tricks.com/centering-css-complete-guide/#comments Tue, 02 Sep 2014 16:56:39 +0000 http://css-tricks.com/?p=181375 Centering things in CSS is the poster child of CSS complaining. Why does it have to be so hard? They jeer. I think the issue isn’t that it’s difficult to do, but in that there so many different ways of …


Centering in CSS: A Complete Guide originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
Centering things in CSS is the poster child of CSS complaining. Why does it have to be so hard? They jeer. I think the issue isn’t that it’s difficult to do, but in that there so many different ways of doing it, depending on the situation, it’s hard to know which to reach for.

So let’s make it a decision tree and hopefully make it easier.

I need to center…

Horizontally

Is it inline or inline-* elements (like text or links)?

You can center inline elements horizontally, within a block-level parent element, with just:

.center {
  text-align: center;
}

This will work for inline, inline-block, inline-table, inline-flex, etc.

Is it a block level element?

You can center a block-level element by giving it margin-left and margin-right of auto (and it has a set width, otherwise it would be full width and wouldn’t need centering). That’s often done with shorthand like this:

.center-me {
  margin: 0 auto;
}

This will work no matter what the width of the block level element you’re centering, or the parent.

Note that you can’t float an element to the center. There is a trick though.

Is there more than one block level element?

If you have two or more block-level elements that need to be centered horizontally in a row, chances are you’d be better served making them a different display type. Here’s an example of making them inline-block and an example of flexbox:

Unless you mean you have multiple block level elements stacked on top of each other, in which case the auto margin technique is still fine:

Vertically

Vertical centering is a bit trickier in CSS.

Is it inline or inline-* elements (like text or links)?
Is it a single line?

Sometimes inline / text elements can appear vertically centered, just because there is equal padding above and below them.

.link {
  padding-top: 30px;
  padding-bottom: 30px;
}

If padding isn’t an option for some reason, and you’re trying to center some text that you know will not wrap, there is a trick were making the line-height equal to the height will center the text.

.center-text-trick {
  height: 100px;
  line-height: 100px;
  white-space: nowrap;
}
Is it multiple lines?

Equal padding on top and bottom can give the centered effect for multiple lines of text too, but if that isn’t going to work, perhaps the element the text is in can be a table cell, either literally or made to behave like one with CSS. The vertical-align property handles this, in this case, unlike what it normally does which is handle the alignment of elements aligned on a row. (More on that.)

If something table-like is out, perhaps you could use flexbox? A single flex-child can be made to center in a flex-parent pretty easily.

.flex-center-vertically {
  display: flex;
  justify-content: center;
  flex-direction: column;
  height: 400px;
}

Remember that it’s only really relevant if the parent container has a fixed height (px, %, etc), which is why the container here has a height.

If both of these techniques are out, you could employ the “ghost element” technique, in which a full-height pseudo-element is placed inside the container and the text is vertically aligned with that.

.ghost-center {
  position: relative;
}
.ghost-center::before {
  content: " ";
  display: inline-block;
  height: 100%;
  width: 1%;
  vertical-align: middle;
}
.ghost-center p {
  display: inline-block;
  vertical-align: middle;
}
Is it a block-level element?
Do you know the height of the element?

It’s fairly common to not know the height in web page layout, for lots of reasons: if the width changes, text reflow can change the height. Variance in the styling of text can change the height. Variance in the amount of text can change the height. Elements with a fixed aspect ratio, like images, can change height when resized. Etc.

But if you do know the height, you can center vertically like:

.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  height: 100px;
  margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */
}
Is the element of unknown height?

It’s still possible to center it by nudging it up half of it’s height after bumping it down halfway:

.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}
Do you care if the element stretches the height of the container?

If you don’t, you just need the content inside vertically centered, using tables or CSS display to make elements into tables can do the trick.

Can you use flexbox?

No big surprise, this is a lot easier in flexbox.

.parent {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

You can also get centering in flexbox using margin: auto; on the child element.

Both Horizontally & Vertically

You can combine the techniques above in any fashion to get perfectly centered elements. But I find this generally falls into three camps:

Is the element of fixed width and height?

Using negative margins equal to half of that width and height, after you’ve absolutely positioned it at 50% / 50% will center it with great cross-browser support:

.parent {
  position: relative;
}

.child {
  width: 300px;
  height: 100px;
  padding: 20px;

  position: absolute;
  top: 50%;
  left: 50%;

  margin: -70px 0 0 -170px;
}
Is the element of unknown width and height?

If you don’t know the width or height, you can use the transform property and a negative translate of 50% in both directions (it is based on the current width/height of the element) to center:

.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
Can you use flexbox?

To center in both directions with flexbox, you need to use two centering properties:

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}
Can you use grid?

This is just a little trick (sent in by Lance Janssen) that will pretty much work for one element:

body, html {
  height: 100%;
  display: grid;
}
span { /* thing to center */
  margin: auto;
}

Conclusion

You can totally center things in CSS.


Centering in CSS: A Complete Guide originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
https://css-tricks.com/centering-css-complete-guide/feed/ 58 181375
Centering in the Unknown https://css-tricks.com/centering-in-the-unknown/ https://css-tricks.com/centering-in-the-unknown/#comments Sat, 29 Oct 2011 19:58:12 +0000 http://css-tricks.com/?p=14745 When it comes to centering things in web design, the more information you have about the element being centered and its parent element, the easier it is. So what if you don’t know anything? It’s still kinda doable.

Not too…


Centering in the Unknown originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
When it comes to centering things in web design, the more information you have about the element being centered and its parent element, the easier it is. So what if you don’t know anything? It’s still kinda doable.

Not too hard: Known Child

If you know the height and width of both the element to be centered and its parent element (and those measurements won’t change, i.e. not fluid width environment) one foolproof way to center the element is just to absolute position it with pixel values so it looks perfectly centered.

Let’s say you know the exact width and height of the element you are centering, but the parent element can change in height and width.

You absolutely position the element to be centered and set the top and left values to 50% and the margin top and left values to negative half of the elements height and width. That was a tounge twister, so check this out.

Harder: Unknown Child

The hard comes in when you don’t know the dimensions of the element to be centered.

What do we know? Nothing! When do we know it? Now!

The grossest way to handle it is literally tables:

<table style="width: 100%;">
  <tr>
     <td style="text-align: center; vertical-align: middle;">
          Unknown stuff to be centered.
     </td>
  </tr>
</table>

If you are worried about the semantics of that, you could attempt to match it to your content.

<div class="something-semantic">
   <div class="something-else-semantic">
       Unknown stuff to be centered.
   </div>
</div>

And get the same result as the tables like:

.something-semantic {
  display: table;
  width: 100%;
}
.something-else-semantic {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

CSS tables might be fine for you. Or it might not. Tables do render a bit differently than just a regular block-level div does. For instance the 100% width thing. A table will only stretch to be as wide as it needs to for the content inside it whereas by default a block level element will expand to the width of its parent automatically. Also, god help you if you need other content inside that div that you want to position or otherwise not act as a table-cell.

Michał Czernow wrote in to me with an alternate technique that is extremely clever and accomplishes the same thing. If we set up a “ghost” element inside the parent that is 100% height, then we vertical-align: middle both that and the element to be centered, we get the same effect.

See what we did there?

So does that ghost element need to be an un-semantic element? Nope, it can be a pseudo element.

/* This parent can be any width and height */
.block {
  text-align: center;

  /* May want to do this if there is risk the container may be narrower than the element inside */
  white-space: nowrap;
}
 
/* The ghost, nudged to maintain perfect centering */
.block:before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  margin-right: -0.25em; /* Adjusts for spacing */
}

/* The element to be centered, can also be of any width and height */ 
.centered {
  display: inline-block;
  vertical-align: middle;
  width: 300px;
}
View Demo

I’d like to tell you the ghost element technique is way better and should be the go-to centering technique for the ages. But in reality, it’s just about the same as the table trick. The browser support for this is essentially everything and IE 8+. IE 7 doesn’t support pseudo elements. But it doesn’t support CSS tables either, so it’s a horse apiece. If IE <= 7 support is needed, it's <table> time (or use an equally un-semantic <span> or something for the ghost element).

This stuff isn’t brand new territory. Gary Turner wrote about it like 5 years ago. But I credit Michał for doing it with a pseudo element and making it the most semantic approach yet.

Note: The 0.25em nudge-back is a little janky. To do it perfectly, you could set font-size: 0; on the parent and then notch the font size back up inside the content container.


Centering in the Unknown originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
https://css-tricks.com/centering-in-the-unknown/feed/ 86 14745
vertical-align https://css-tricks.com/almanac/properties/v/vertical-align/ https://css-tricks.com/almanac/properties/v/vertical-align/#comments Wed, 07 Sep 2011 01:59:38 +0000 http://css-tricks.com/?page_id=14132 The vertical-align property in CSS controls how elements set next to each other on a line are lined up.

img {
  vertical-align: middle;
}

In order for this to work, the elements need to be set along a baseline. As …


vertical-align originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
The vertical-align property in CSS controls how elements set next to each other on a line are lined up.

img {
  vertical-align: middle;
}

In order for this to work, the elements need to be set along a baseline. As in, inline (e.g. <span>, <img>) or inline-block (e.g. as set by the display property) elements.

The valid values are:

  • baseline – This is the default value.
  • top – Align the top of the element and its descendants with the top of the entire line.
  • bottom – Align the bottom of the element and its descendants with the bottom of the entire line.
  • middle – Aligns the middle of the element with the middle of lowercase letters in the parent.
  • text-top – Aligns the top of the element with the top of the parent element’s font
  • text-bottom – Aligns the bottom of the element with the bottom of the parent element’s font.
  • sub – Aligns the baseline of the element with the subscript-baseline of its parent. Like where a <sub> would sit.
  • super – Aligns the baseline of the element with the superscript-baseline of its parent. Like where a <sup> would sit.
  • length – Aligns the baseline of the element at the given length above the baseline of its parent. (e.g. px, %, em, rem, etc.)

You can see examples of each here:

Check out this Pen!

A common use case is lining up an avatar with a username. To get them centered along a line, you’d use vertical-align: middle;. Although note that it centers the text according to its tallest ascender and deepest descender.

Each element lines up according to the line you’ve set, which doesn’t change from element to element. So, you can mix-and-match which elements have which value – the elements don’t affect each other.

Note that vertical-align is useful on table-cell elements as well, aligning the content within them. Sticking to top, middle, and bottom is the best bet though, as the other values have inconsistent cross-browser results.

More Information

  • What is vertical-align?
  • This property does not allow you to “vertically center” an element within another element. Flexbox is more of the proper tool there. However, there is a trick involving a pseudo “ghost” element that can allow this to work.
  • MDN

Browser Support

Chrome Safari Firefox Opera IE Android iOS
Any Any Any 4+ 4+ Any Any
Fairly consistent across browsers old and new, assuming the font is the same.

Note that some replace elements (e.g. <textarea>) are inline, but their baseline isn’t specified, so behavior may vary from browser to browser.


vertical-align originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
https://css-tricks.com/almanac/properties/v/vertical-align/feed/ 15 14132
Absolute Center (Vertical & Horizontal) an Image https://css-tricks.com/snippets/css/absolute-center-vertical-horizontal-an-image/ https://css-tricks.com/snippets/css/absolute-center-vertical-horizontal-an-image/#comments Fri, 04 Sep 2009 23:03:43 +0000 http://css-tricks.com/?page_id=3856 CSS background-image Technique:
html { 
   width:100%; 
   height:100%; 
   background:url(logo.png) center center no-repeat;
}

CSS + Inline Image Technique:

img {
   position: absolute;
   top: 50%;
   left: 50%;
   width: 500px;
   height: 500px;
   margin-top: -250px; /* Half the height */
   margin-left: -250px; /* Half 


Absolute Center (Vertical & Horizontal) an Image originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
CSS background-image Technique:
html { 
   width:100%; 
   height:100%; 
   background:url(logo.png) center center no-repeat;
}

CSS + Inline Image Technique:

img {
   position: absolute;
   top: 50%;
   left: 50%;
   width: 500px;
   height: 500px;
   margin-top: -250px; /* Half the height */
   margin-left: -250px; /* Half the width */
}

Table technique:

html, body, #wrapper {
   height:100%;
   width: 100%;
   margin: 0;
   padding: 0;
   border: 0;
}
#wrapper td {
   vertical-align: middle;
   text-align: center;
}
<table id="wrapper">
  <tr>
    <td><img src="logo.png" alt="" /></td>
  </tr>
</table>

Absolute Center (Vertical & Horizontal) an Image originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
https://css-tricks.com/snippets/css/absolute-center-vertical-horizontal-an-image/feed/ 95 3856
Vertically Center Multi-Lined Text https://css-tricks.com/vertically-center-multi-lined-text/ https://css-tricks.com/vertically-center-multi-lined-text/#comments Wed, 13 May 2009 13:29:41 +0000 http://css-tricks.com/?p=2767 If you only have a single word or a single line of text, there is a clever way to vertically center it in a block with CSS. You set the line-height of that text to be equal to the height


Vertically Center Multi-Lined Text originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
If you only have a single word or a single line of text, there is a clever way to vertically center it in a block with CSS. You set the line-height of that text to be equal to the height of the box. Works great, but is a major fail if that text needs to wrap.

A “speech bubble” is a classic example of somewhere we might want text to be centered both horizontally and vertically and be adaptable to multiple lines. There is a little, fairly simple CSS trick for this, using CSS tables. Here is the outcome:

View DemoDownload Files

The HTML is nothing fancy. The “area” is just the region we are dealing with, where we can set position: relative; so that we can absolutely position the text are inside the bubble.

<div class="area">
      <div class="bubble">
          <p>To look best, text should really be centered inside this bubble both vertically and horizontally.</p>
      </div>
</div>

The “bubble” we’ll set to display: table;, which really doesn’t do much by itself, but then we can set the <p> element inside to be a table-cell, which allows us to use the vertical-align property on it.

.area { 
  width: 300px; 
  height: 300px; 
  background: url(../images/abe-bg.png) no-repeat; 
  position: relative;
}

.bubble { 
  position: absolute; 
  left: 93px; 
  top: 21px; 
  width: 135px; 
  height: 84px; 
  display: table; 
}

.bubble p {
  display: table-cell; 
  vertical-align: middle; 
  text-align: center; 
}

Does the trick beautifully I think. This current version of CSS-Tricks has a little twitter speech bubble down by the footer I used this for.

What about IE <= 7 ?!

IE 8 is supporting CSS tables, but IE 7 and below do not. Too bad, so sad. Instead you get this:

… could be worse. I was hoping the Dean Edwards ie8.js would solve this but no dice (yet).

UPDATE 1

Boris Kuzmic comments below a perfect solution to make IE 6 and 7 work perfectly:

<!--[if lt IE 8]>
<style>
.bubble { position: absolute; left: 93px; top: 21px; width: 135px; height: 84px; text-align: center;}
.bubble p { position: relative; font-size: 11px;
margin-top: expression(this.offsetHeight < this.parentNode.offsetHeight ? parseInt((this.parentNode.offsetHeight - this.offsetHeight) / 2) + "px" : "0");
}
</style>
<![endif]-->

UPDATE 2

Boris again with a way to make the IE expression not leak memory (this way it only needs to be evaluated once, not continuously run).

.bubble p { position: relative; font-size: 11px;
margin-top: inherit;
*clear: expression(
style.marginTop = "" + (offsetHeight < parentNode.offsetHeight ? parseInt((parentNode.offsetHeight - offsetHeight) / 2) + "px" : "0"),
style.clear = "none", 0
);
}

UPDATE 3

James John Malcolm chimes in with another technique for IE. It’s slightly less semantic (requires and extra div), but it needs no expression.

First wrap the inside <p> in a new <div> and then:

<!--[if lt IE 8]>
<style>
.bubble div { position: absolute; top:50%;}
.bubble div p {position: relative; top: -50%}
</style>
<![endif]–>

UPDATE 4

Another method from Andy Howard.


Vertically Center Multi-Lined Text originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
https://css-tricks.com/vertically-center-multi-lined-text/feed/ 63 2767
What is Vertical Align? https://css-tricks.com/what-is-vertical-align/ https://css-tricks.com/what-is-vertical-align/#comments Mon, 20 Apr 2009 22:03:27 +0000 http://css-tricks.com/?p=2597 CSS has a property called vertical align. It can be a bit confusing when you first learn about it, so I thought we could help explain what it is for and some use cases.

The basic usage is like …


What is Vertical Align? originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
CSS has a property called vertical align. It can be a bit confusing when you first learn about it, so I thought we could help explain what it is for and some use cases.

The basic usage is like this:

img { 
  vertical-align: middle;
}

Notice in this usage case, it is being applied to the img element. Images are naturally inline elements, meaning they sit right inline with text if they are able to. But what does “sit inline” mean exactly? That is where vertical-align comes in.

The valid values are: baseline, sub, super, top, text-top, middle, bottom, text-bottom, length, or a value in percentage.

The confusion, in my opinion, sets in when people try to use vertical-align on block-level elements and get no results. If you have a small div inside a larger div and want to vertically center the smaller one within, vertical-align will not help you.

Baseline

The default value of vertical-align (if you declare nothing), is baseline. Images will line up with the text at the baseline of the text. Note that descenders on letters dip below the baseline. Images don’t line up with the lowest point of the descenders, that isn’t the baseline.

Middle

Perhaps the most common use for vertical-align is setting it to ‘middle’ on icon-size images. The results are generally consistent cross-browser:

The browser does the best job it can centering the pixel height of the text with the pixel height of the image:

Be aware that if the image is larger than the current font-size and line-height, it will push the following lines down if need be:

Text-bottom

Different from the baseline of type, is the bottom of text, where the descenders go down to. Images can be aligned with this depth as well:

Text-top

Opposite of text-bottom, is text-top, the highest point of the current font-size. You can align to this as well. Note that the current font, Georgia, probably has some ascenders taller than those represented here hence the small gap.

Top & Bottom

Top and Bottom work similarly to text-top and text-bottom, but they are not constrained by text but rather by anything at all on that line (like another image). So if there were two images on the same line, of different heights and both larger than the text on that line, their tops (or bottoms) would align regardless of that text size.

Sub & Super

These values stand for superscript and subscript, so elements aligned with these methods align themselves as such:

Vertical Align on Table Cells

Unlike images, table cells default to middle vertical alignment:

If you would rather see that text aligned to the top or bottom of the cell when it needs to stretch beyond the height that it needs, apply top or bottom vertical alignment:

When using vertical-align on table cells, sticking with top, bottom, and middle is your best bet. None of the other values make a whole lot of sense anyway and have unpredictable cross-browser results. For example, setting a cell to text-bottom aligns the text to the bottom in IE 6, and to the top in Safari 4. Setting it to sub causes middle alignment in IE 6 and top in Safari 4.

vertical-align and inline-block

Images, while technically inline-level elements, behave more like inline-block elements. You can set their width and height and they obey that, unlike most inline elements.

Inline-block elements behave just like images with vertical align, so refer to the above. However, be aware that not all browsers treat inline-block elements the same way, so vertical-align may be the least of your worries. That’s another story though….

Deprecated as an attribute

Occasionally you will see “valign” used on table cells to accomplish vertical alignment. e.g. <td valign=top>. It should be noted that this attribute is deprecated and should not be used. There really isn’t any reason for it anyway as you can do it with CSS anyway.

More Information


What is Vertical Align? originally published on CSS-Tricks, which is part of the DigitalOcean family. You should get the newsletter.

]]>
https://css-tricks.com/what-is-vertical-align/feed/ 68 2597