N
N
Nikolai Chuprik2018-03-18 13:00:57
css
Nikolai Chuprik, 2018-03-18 13:00:57

Why is there no simple center positioning?

The question has long occupied me. Why is there not one simple directive for positioning a block in the center (vertically and horizontally) (okay, let's say two)? The center location is what a person encounters immediately in the html layout. Why is it necessary to use all sorts of "recipes" with negative indents, flaxes and other garbage? Why is the world like this? What are the reasons?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
ink, 2018-03-18
@AskMy

like this no:

display:flex;
justify-content:center;
align-items:center;

this will center any child block vertically and horizontally: https://codepen.io/topicstarter/pen/MVbYMg
Second option with positioning: https://codepen.io/topicstarter/pen/QmGbWP

D
Dima Polos, 2018-03-18
@dimovich85

So that life does not seem like a raspberry))
Now it already exists, so they wrote to you about flexes.
If you want to center an element with one line, give it margin: auto, or as mentioned above, the difference is that justify-content and align-items are on the parent, and margin auto is on the child.

I
Ivan Bogatov, 2018-03-18
@Malekith

In addition to all of the above, IT development is just wonderful in that previously written code can be used so many times. Write once any centering (at least light flex, hol add hardcore 10 lines) and use everywhere.

L
Lynn "Coffee Man", 2018-03-18
@Lynn

By and large, because html / css was originally invented for documents and vertical centering was not necessary.
And for horizontal there have always been a bunch of ways starting with the center tag

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question