An Interest In:
Web News this Week
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
Discover CSS Fundamentals 1
Recently creating a simple page with a flex box layout, I missed some basic concepts of CSS and it goes wrong at the first place.
To get my head around this and learn CSS from the ground, I created some experimental codepens to learn CSS by doing.
I share these here, because I hope it is also useful for you and encourage to make some experiments on your on. In the end there is no magic with CSS, it is based on fundamentals, you should remember during styling.
All tag's fell in one of the two categories inline or block, nothing more. So if you know how these two are handled during layout, you are almost done.
So let's begin with the most simple element, the span
.
An inline element like span
takes only the space it needs in both directions and is never put on a new line.
A block element like div
is always put onto a new line and takes all space horizontally and needed space vertically.
When we nest div
s this does not change. The outer div
only occupies as much space vertically he needs to layout its children, not more.
You can see the green border becomes bigger for the nested div
s sharing a border.
So lets see what happens, when we create a simple flex box layout, centering the children.
It works as expected, the div
s are centered horizontally.
No let's try the opposite direction, column mode.
What I want to get is both divs centered in the page, but I get no centering at all.
But why is that, there is enough room to center them?
There are centered, centered in the surrounded container.
And the container only takes the space he needed to layout the div
s inside, nothing more. So there are in perfect center.
So to see a centering, we have to give the container more space vertically.
And if we want them in the center of the browser window, we can use the viewport unit, means set the height to 100% of the viewport height, with height: 100vh;
.
I hope this helps one or the other coder outside.
Regards.
Original Link: https://dev.to/dirkecker/discover-css-fundamentals-1-4315
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To