This can actually be done with 2 lines now!
.center {
display: grid;
place-items: center;
}This can actually be done with 2 lines now!
.center {
display: grid;
place-items: center;
} <td valign="middle">
worked flawlessly.Of course maybe there is some good explanation... I certainly never contributed to the CSS spec.
The flex-box approach is the most intuitive in my opinion. The flex display-type basically means you want to distribute the area of a box among its immediate child elements, and control how the children and aligned relative to the container and each other. Centering is naturally part of this.
Using grid is IMHO less intuitive, since a grid with a single cell seem just as much a hack as a table with a single cell.
While it's a ton of markup, tables are build upon a few easy-to-remember concepts and you can do anything (including vertically align without googling!). I'm still constantly looking up flex and grid rules.
That said, I did move on.
foo { display: table-cell; vertical-align: middle; }
And it has also worked a lot longer than the flex-approach. But now when flex is available it seem the more appropriate tool for the job.Source: https://developer.mozilla.org/en-US/docs/Web/CSS/place-items
I think there's a lot of confusion for most people between justify-content/justify-items and align-content/align-items
MDN also has a list of shorthand properties if someone didn't know what 'shorthand property' really meant: https://developer.mozilla.org/en-US/docs/Web/CSS/Shorthand_p...
But yeah, the discoverability or suggestion-to-refactor-something-to-shorthand is typically non-existent in CSS. Lists like these are really the only way I discover CSS stuff.
They have the best CSS completion I’ve ever seen, and it includes live (and cached) mdn documentation for each rule. It also suggests shorthand collapses as a feature
https://www.smashingmagazine.com/2020/01/understanding-css-g...
1: https://preset-env.cssdb.org/ 2: https://preset-env.cssdb.org/features#place-properties
I’ve seen this said, and for the life of me, I can’t find an actual reference for it.
At least it’s first-hand.
A good use case for flex would be a list of tags, that need to wrap.
The notable exception is IE 11…though even then, with a bit of autoprefixer magic, you may be able to use this.
I do use grid when I need it, but I don't see the point of using grid in places where flexbox will do. Granted this is on the verge of not mattering at all anymore, but I just don't see the point in using something less compatible when you don't have to.
But as I stated in elsewhere in this thread, grid is not a replacement for flexbox. There is overlap, they both have their strengths.
- Particularly in Firefox they are much slower. When grids start to pile up on a page, performance goes down the drain
- Implicit row-sizing is not really a thing in Safari or iOS -> https://stackoverflow.com/questions/44770074/css-grid-row-he...
Quote from that SO post: "This is not a Safari bug. It's just a different interpretation of the spec.
When dealing with percentage heights, some browsers (like Safari) adhere to the traditional interpretation of the spec, which requires a defined height on the parent.
In other words, a percentage height on an in-flow element will be recognized only when the parent has a defined height.
Some browsers, such as Chrome and Firefox, have moved past this interpretation and now accept flex and grid heights as an adequate parent reference for a child with a percentage height.
But Safari is stuck in the past. This doesn't mean it's wrong, invalid or a bug."
Just worth noting, as I had a lot of fun with that one.
place-content will position all the children to the center, which is generally what's desired here.
place-items, would make each child element center itself within its own grid square
.center {
display: table-cell;
vertical-align: center;
} CENTER{LINE-HEIGHT:100VH}
https://jsfiddle.net/pnyr1u8d/actually, neither of your versions work without specifying a document height. (or outer container)
.center {
display: grid;
place-items: center;
}
https://jsfiddle.net/a67gzsjf/something like this would work