Great question. A lot of people want to use float to center elements. There's a float:left
and a float:right
, so there must be a float:center
; isn't there?
No. There is no float:center
.
To center things, we use either
margin:auto
which is appropriate
for block elements where we want to center the
block, not necessarily the things within it. This almost
always means adjusting the width of the block
element.
text-align:center
, which is appropriate
for inline elements, including lines of text, though I
personally think this is often ugly. But it's simple and
effective if there's only one inline element you want to
center.
We have to understand the flow, which is the term for how words and such flow like liquids into their containers.
the float
attribute temporarily removes the element
(image) from the flow, allowing the words to move up, and
then shoves it back in, forcing the words to flow around the
image.
when to use position:relative vs position:absolute
These work together to create a coordinate system where you can position things like in high school math class.
We put position:relative
on the ancestor
and position:absolute
on the descendants.
The descendants will also have top:y
and left:x
.
An ancestor is some element that surrounds the elemement we are talking about. Suppose we had:
The main
element is an ancestor of all the
things it contains: the h1
and both section
elements, including all the things inside them.
Similarly, each section
is an ancestor of the things
inside them, like the h2
and the paragraphs.
There are lots of possible ancestors. Most will have the
default position
property, which
is position:static
.
The first one (going upwards) that isn't is the one that we are referring to.
We use CSS, like this:
Sure; we saw some examples today. Basically, if the web page gets larger, what happens to the extra space? Distributed equally? Or differently?
Good question. No, you can also specify, say, a percentage. If I have four flex
children, and I want them all to be equal size, I might
use flex: 1 1 25%
, saying that the starting size for each
is 25% of the width. Each is allowed to grow (equally) or shrink
(equally), so they'll always be the same width.
Did the example above help?
Yes, we will. Plus, you'll use them in the next assignment.
Can we talk more about inline blocks, like visually.
Let's draw some pictures of inline-block elements, say for a photo gallery.
W3Schools has some good examples of inline-blocks
html, body { height: 100%; } body { display: flex; flex-direction: column; }
That says that the html and body (the outermost tags) take up 100% of the window. So, no scroll bars.
We can use a comma between two selectors to select both. So, instead of:
I can say:Those mean the same thing.