Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no easy was to vertically center in css?

Tags:

css

web

I was searching around for a way to vertically center a div in a container. I found a few different ways, but all of them seemed to be very "hacky".

My question is, why is there not just a css property, such as align-vertical that can simply be set to center to center the content? It seems like adding this to css would make so many things much easier.

I am assuming there must be a reason why something like this is not implemented, and I would like to hear if anyone has any idea why.

like image 929
eeze Avatar asked Apr 18 '18 03:04

eeze


People also ask

How do I center an image vertically in CSS?

To center an image vertically, you can wrap it in a block element like a div and use a combination of the CSS position property, the left and top properties, and the transform property.

Why is text-align center not working?

This is because text-align:center only affects inline elements, and <aside> is not an inline element by default. It is a block-level element. To align it, we will have to use something other than text-align , or turn it into an inline element.


2 Answers

It's because how browsers traditionally work.

In a browser, by default, the content scrolls vertically. The viewport width is well defined (width of the device), but the viewport height can be one or two times the height of the device, or can even be infinite (as in infinite scrolling).

Traditionally blocks were meant to be horizontally oriented. You place a div and it's automatically occupying 100% of the width of the parent. But its height value is contrained to its content.

If you do

.mydiv {
  background: red;
  width: 100%;
  height: 100%
}

Nothing changes, since divs have already 100% of width, and it can't calculate the height, since it doesn't know how far the viewport will go. You need to add:

html, body {
  height: 100%;
}

to tell the browser to use the device height as the 100% value.

That's why horizontal center is easy: you know what the boundaries are, and how to center the element. But vertical center is more complicated, that's why (before flexbox), you need to resort to absolute positioning or hacks.

Flexbox allows you to render content horizontally or vertically, so it's prepared to handle centering along two axes.

If you want to read more about this, I suggest the spec:

  • Visual formatting model
  • Visual formatting model details
like image 104
Jorjon Avatar answered Oct 19 '22 18:10

Jorjon


#outerDiv{
  display:flex;
  width:100%;
  height:200px;
  background:#ccc;
  align-items:center;
}
#innerDiv {
  background:#aaa;
  width:80%;
  margin:0 auto;
}
<div id="outerDiv"><div id="innerDiv">Hello</h1></div>

Run the script and the div remain in the center.
You can mix and match the combination like this. Earlier you need to play with the height of the parent container and self height.
But with flex it becomes easy.

like image 1
manish kumar Avatar answered Oct 19 '22 16:10

manish kumar