Fog of details ahead!
Do not 'learn off'.
Look-up when needed.
block
, if it wasn't already;
<section id="seashells">
<h2>Seashells</h2>
<p>
A seashell is a hard, protective outer
layer created by an animal that lives
in the sea.
<img src="seashell.jpg" alt="..." />
The shell is part of the body of the
animal. Empty seashells are often found
washed up on beaches by beachcombers.
The shells are empty because the animal
has died and the soft parts have been
eaten by another animal or have
decomposed.
</p>
</section>
img {
float: left;
}
Text is from Wikipedia
shape-outside
property whose values include circle
,
ellipse
, inset
(= rectangle) or polygon
.
<section id="seashells">
<h2>Seashells</h2>
<div id="shape" role="presentation">
</div>
<p>
A seashell is a hard, protective
outer layer created by an animal
that lives in the sea. The shell is
part of the body of the animal.
Empty seashells are often found
washed up on beaches by
beachcombers. The shells are empty
because the animal has died and the
soft parts have been eaten by
another animal or have decomposed.
</p>
</section>
#shape {
float: left;
height: 10em;
width: 10em;
}
with this:
#shape {
float: left;
height: 10em;
width: 10em;
shape-outside: circle();
}
The div
spoils the HTML.
To avoid this, people use CSS generated-content:
<section id="seashells">
<h2>Seashells</h2>
<p>
A seashell is a hard, protective
outer layer created by an animal
that lives in the sea. The shell is
part of the body of the animal.
Empty seashells are often found
washed up on beaches by
beachcombers. The shells are empty
because the animal has died and the
soft parts have been eaten by
another animal or have decomposed.
</p>
</section>
#seashells::before {
content: "";
float: left;
height: 10em;
width: 10em;
shape-outside: circle();
}
An image is another way to avoid using a div
.
<section id="seashells">
<h2>Seashells</h2>
<img src="seashell.png" alt="" />
<p>
A seashell is a hard, protective
outer layer created by an animal
that lives in the sea. The shell is
part of the body of the animal.
Empty seashells are often found
washed up on beaches by
beachcombers. The shells are empty
because the animal has died and the
soft parts have been eaten by
another animal or have decomposed.
</p>
</section>
#seashells img {
float: left;
shape-outside: circle();
}
If the image has an alpha channel, then you can even use that to create the shape.
<section id="seashells">
<h2>Seashells</h2>
<img src="seashell.png" alt="" />
<p>
A seashell is a hard, protective
outer layer created by an animal
that lives in the sea. The shell is
part of the body of the animal.
Empty seashells are often found
washed up on beaches by
beachcombers. The shells are empty
because the animal has died and the
soft parts have been eaten by
another animal or have decomposed.
</p>
</section>
#seashells img {
float: left;
shape-outside: url("seashell.png");
}
CSS positioning allows you to precisely position an element.
However, avoid CSS Positioning! (Mostly.)
position
property specifies how an element will be positioned:
static
,
relative
,
absolute
,
fixed
,
sticky
.
top
, right
, bottom
, left
, z-index
top
and bottom
, then bottom
is ignored; if
you set both left
and right
, then right
is ignored.)
This is the default: normal layout.
The offset properties have no effect: the element remains in its normal position.
This is for 'nudging' the element.
The offset properties are how much to move relative to the element's original position, e.g.:
top: 2em; |
nudge the element down from where it was |
top: -2em; |
nudge the element up from where it was |
But the element is still in flow, so it leaves a gap behind.
<p id="container">
This paragraph contains
<em id="phrase">an element</em>
which we will position in various ways.
</p>
#phrase {
position: relative;
top: 2em;
left: 3em;
}
The element is removed from the flow
(it is out of flow so no gap left behind).
It is positioned relative to a 'containing element':
either its nearest ancestor that is not static
, if there is one,
else the viewport (i.e. roughly speaking, the screen/browser window).
The offset properties are now the distance from the edge of the containing element.
top: 2em; |
place it 2em down from the top of the containing element |
In this one, the element will be positioned relative to the viewport:
<p id="container">
This paragraph contains
<em id="phrase">an element</em>
which we will position in various ways.
</p>
#phrase {
position: absolute;
top: 2em;
left: 3em;
}
To make absolute positioning work as we want,
we often need to use relative positioning on its parent
but without changing the parent's position!
<p id="container">
This paragraph contains
<em id="phrase">an element</em>
which we will position in various ways.
</p>
#container {
position: relative;
}
#phrase {
position: absolute;
top: 2em;
left: 3em;
}
Fixed positioning is like absolute positioning but always relative to the viewport.
<p id="container">
This paragraph contains
<em id="phrase">an element</em>
which we will position in various ways.
</p>
#phrase {
position: fixed;
top: 2em;
left: 3em;
}
Sticky positioning is sensitive to the user's scroll position.
The element is positioned normally until it gets to a specific distance from the edges of
the viewport.
At that point, the element behaves as if it we had used fixed positioning.
<h1>A heading</h1>
h1 {
position: sticky;
top: 0;
left: 0;
}
Generally, avoid CSS Positioning!
Web developers used to lay out their whole web page using positioning.
Don't do this. There are now better alternatives.
However, there are some fun things we can do with positioning.
You could position text over an image.
<figure id="censoredfigure">
<img src="naked_man.jpg" alt="..." />
<strong id="censoredmessage">CENSORED</strong>
</figure>
#censoredfigure {
width: fit-content;
position: relative;
}
#censoredmessage {
color: red;
background-color: white;
border: 2px solid red;
position: absolute;
top: 45%;
left: 25%;
transform: rotate(-30deg);
}
Fixed headers and footers: only the main will scroll.
<header>
…
</header>
<main>
…
</main>
<footer>
…
</footer>
header {
position: fixed;
top: 0;
left: 0;
}
footer {
position: fixed;
bottom: 0;
left: 0;
}