Skip to content

Popup Elements

Lecture notes: content and decorative images, float, clearfix, text wrapping and element positioning

HTML & CSS — Course for BeginnersLecture 8 of 1911 min29 views

1. Content vs decorative image

There are two ways to add an image to a page — via HTML and via CSS. They are not interchangeable.

MethodTechnologyWhen to use
<img>HTMLThe image is part of the content — a product, an avatar, a photo of an apartment
background-imageCSSThe image is decorative — a header background, an abstract texture
Tip

How to determine the type: mentally remove the image from the page. If important information is lost (a product photo, an apartment photo) — it is content → <img>. If nothing essential changed — it is decoration → background-image.

2. Background image — the basic set of properties

For decorative backgrounds (a hero banner, a header with a photo) almost always the same set is used:

CSS
.hero {
    background-image: url("img/hero.jpg");
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
    background-color: #000;
}
PropertyValueWhat it does
background-imageurl("...")Attaches the image
background-repeatno-repeatPrevents the image from tiling
background-positioncenterCenters it horizontally and vertically
background-sizecoverFills the block completely while preserving proportions
background-color#000A fallback background if the image fails to load
Attention

Always add background-color! If the image fails to load or loads slowly — the text will remain readable (white text on a black background, not white on white).

cover vs contain

ValueBehaviorWhen to use
coverFills the entire block, crops the excessBackground images for banners, headers
containFits fully, crops nothingIcons in pseudo-elements, logos

3. position: absolute — behavior and properties

When an element is given position: absolute, it radically changes its behavior:

  • Width and height are calculated from the amount of content (not 100% of the parent)
  • The element becomes invisible to its siblings and to the parent element — as if it were not in the markup
  • The parent does not account for the absolute element when calculating its own dimensions
  • Neighboring elements take its place
  • The element is placed on top of regular blocks

Controlling position: top, right, bottom, left

An absolute element is moved using four properties:

CSS
.element {
    position: absolute;
    top: 0;     /* 0 from the top edge */
    left: 20px;  /* 20px from the left edge */
}
CombinationResult
top: 0; left: 0;Top-left corner
top: 0; right: 0;Top-right corner
bottom: 0; left: 0;Bottom-left corner
bottom: 0; right: 0;Bottom-right corner

Stretching an absolute element

If the width is not set explicitly, but left: 0 and right: 0 are specified — the element will stretch to the full available width. The same works vertically with top: 0 and bottom: 0:

CSS
.overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
Tip

If the width is set (for example, width: 300px) together with left: 0; right: 0 — the element will not stretch, but will be pinned to the left edge. The right value will be ignored.

4. position: relative — preserving behavior

Unlike absolute, the value position: relative does not change the standard behavior of a block:

  • A block element remains block, an inline one remains inline
  • The element occupies its place in the document flow, as before
  • Siblings and the parent account for it in their calculations

Two main purposes of position: relative

  1. Activating z-index — without position the z-index property does not work
  2. Positioning context — a parent element with position: relative becomes the reference point for child elements with position: absolute

5. Positioning context (relative + absolute)

An element with position: absolute calculates its top/left/right/bottom from the nearest parent element with position: relative (or absolute, fixed).

CSS
/* Parent — the reference point */
.parent {
    position: relative;
}

/* Child — positioned relative to .parent */
.child {
    position: absolute;
    top: 0;
    right: 0;
}
Attention

Without position: relative on the parent the absolute element is positioned relative to the first screen (the viewport). On scroll it will "drift away" together with the visible area — this is a typical mistake.

The context lookup algorithm:

  1. Check the immediate parent — does it have position: relative?
  2. If not — check the next parent up the DOM
  3. If no parent has position — positioning happens relative to the viewport

6. z-index — the stacking order

The z-index property controls which element is displayed on top of another when they overlap.

  • A higher value — the element is closer to the user (on top of the others)
  • Works only with elements that have position set (relative, absolute, fixed)
  • Without z-index — the order is determined by the position in the HTML (an element lower in the code — higher in the stack)
  • With an equal z-index — the HTML-order rule applies
CSS
/* Without z-index: the third element is on top of all */
.box-1 { position: absolute; }
.box-2 { position: absolute; }
.box-3 { position: absolute; }

/* With z-index: the first element is on top of all */
.box-1 { position: absolute; z-index: 2; }

7. The background-darkening technique (overlay)

A classic task: a background banner with darkening and white text on top. The darkening must be done via CSS, not in a graphics editor — because the image may be dynamic (changed via the CMS).

The idea

  1. Create an ::after pseudo-element
  2. Stretch it over the whole block via position: absolute
  3. Set a semi-transparent black background via rgba()
  4. Bring the text to the front via z-index

Full example

CSS
/* HTML: <section class="hero"><div class="hero__content">...</div></section> */

/* 1. Parent block */
.hero {
    position: relative;         /* context for ::after */
    background-image: url("img/hero.jpg");
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
    background-color: #000;
}

/* 2. Darkening layer */
.hero::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
}

/* 3. Content on top of the darkening */
.hero__content {
    position: relative;         /* activates z-index */
    z-index: 1;                 /* above ::after */
    color: #fff;
}
Tip

Why ::after and not a separate <div>? An empty block with no content is not needed in the HTML. A pseudo-element is the ideal tool for decorative layers.

8. text-transform — changing the case via CSS

A heading in capital letters should be written as ordinary text in the HTML, and transformed via CSS:

CSS
.hero__title {
    text-transform: uppercase;
}
ValueResultExample
noneNo change (default)Hello World
uppercaseAll capsHELLO WORLD
lowercaseAll lowercasehello world
capitalizeEach word capitalizedHello World
Attention

Do not write "IN CAPS" in the HTML! HTML describes content, not styling. Use text-transform in the CSS.

9. Social-media block — the correct markup

A block with links to social networks is a list of links. The icons are decorative images, not content.

Markup

HTML
<address class="social">
    <ul class="social__list">
        <li class="social__item">
            <a href="https://facebook.com/…" class="social__link social__link--facebook">
                <span class="social__text">Facebook</span>
            </a>
        </li>
        <li class="social__item">
            <a href="https://instagram.com/…" class="social__link social__link--instagram">
                <span class="social__text">Instagram</span>
            </a>
        </li>
        <li class="social__item">
            <a href="https://youtube.com/…" class="social__link social__link--youtube">
                <span class="social__text">YouTube</span>
            </a>
        </li>
    </ul>
</address>

Why exactly this way?

  • <address> — a semantic tag for contact information (address, phone, social networks). Not for "Share" buttons
  • Two classes on each link: a shared one (social__link) — for common styles; a unique one (social__link--facebook) — for the individual icon and color
  • Text in a <span> — visually hidden, but available to screen readers and search engines

CSS — two classes

CSS
/* Common styles — same size, shape, background */
.social__link {
    display: inline-block;
    width: 40px;
    height: 40px;
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
    background-color: #3b5998;  /* base color */
}

/* Unique styles — icon and color */
.social__link--facebook  { background-image: url("img/fb.svg"); }
.social__link--instagram {
    background-image: url("img/ig.svg");
    background-color: #e1306c;
}
.social__link--youtube {
    background-image: url("img/yt.svg");
    background-color: #ff0000;
}
Tip

The advantage of the "two classes" pattern: the common code is gathered in one place. If the icon size changes — we edit only .social__link, not each network separately.

10. The <address> tag — the default style

The <address> tag has a default style: font-style: italic. If you do not need italics — reset it:

CSS
address {
    font-style: normal;
}
Attention

A common mistake: developers forget about the italics of <address> and waste time searching for the reason the text became slanted.

11. margin vs padding — when to use which

SituationPropertyExample
Spacing between neighboring (parallel) elementsmarginThe distance between menu items, between cards
Spacing from parent to child (internal)paddingThe padding inside a header, the content's offset from a section's edge
Example

A site header: the logo on the left, the menu on the right. The offset from the top/bottom of the header to the content is padding-top / padding-bottom for the <header>. The spacing between menu items is margin-left or margin-right for each <li>.

Tip

An important nuance: if a block has a background color or image — only padding will be "inside" the background. margin will remain outside the background. In such cases the choice is obvious.

12. Typical use cases for position: absolute

position: absolute is not for placing the main content on a page. The main elements should be regular blocks with margin, padding, Flexbox, or Grid.

Typical use cases for absolute:

  • A darkening overlay — a mask on top of the background (section 7)
  • An element that protrudes beyond its own block (for example, a photo that "peeks out" from a section)
  • Decorative icons — added via ::before/::after, positioned absolutely
  • Badges and labels — "New", "-20%" on the corner of a product card
  • Dropdown menus — they should not shift the main content

13. Working with layouts — tools and measurement

To build markup from a layout you need to be able to measure distances, colors, and sizes. Modern tools:

ToolTypeNotes
FigmaOnline editorThe most convenient for a markup developer: runs in the browser, automatically shows distances, colors, fonts
PhotoshopDesktopOpens PSD files. Requires manual measurement with a ruler and guides
ZeplinOnline serviceAccepts layouts from Figma, Sketch, Adobe XD. Convenient for collaboration between designers and developers

The basics of working with PSD in Photoshop

  • Units of measurement: in the settings set pixels (Edit → Preferences → Units & Rulers)
  • Open the .psd file specifically, not .jpg/.png — only PSD has layers
  • Layers: each element of the layout is a separate layer. The Layers panel is on the right
  • Auto-select: the tool in the top-left corner — you click an element in the layout, and its layer is highlighted
  • Guides: drag them from the ruler onto the layout for precise measurements
  • Ruler tool: measures the distance between two guides with pixel precision
Tip

Fractional values when measuring (for example, 900.33 px) — always round them. Pixels are indivisible: 900.33 = 900 px.

Attention

Distances between text elements cannot be measured perfectly. Every font has internal spacing (it depends on font-size and line-height). For pixel precision use the pixel perfect technique — overlaying the layout onto the markup.

14. Layer types in Photoshop

TypeMarkerPurpose
TextThe letter TAny text in the layout
ImageA thumbnailPhotos, illustrations, icons
BackgroundA colored squareA color fill
MaskA black-and-white rectangleCrops or modifies another layer
Smart ObjectAn icon with a little squareA group of layers collapsed into one. A double click opens its contents

15. Summary table

TopicKey point
Content vs decoration<img> — content; background-image — decoration
Background setbackground-image + no-repeat + center + cover + background-color
cover vs containcover fills the block (crops); contain fits fully
position: absoluteRemoves from the flow, positioned via top/left/right/bottom
position: relativeDoes not change behavior; activates z-index; creates a context for absolute
z-indexControls the stacking order; requires position
Overlay (darkening)::after + absolute + rgba(); the text is brought forward via relative + z-index: 1
text-transformuppercase / lowercase / capitalize / none
Social blockA list of links; the text is hidden; the icons via CSS; <address> for contacts
<address>Semantics of contact information; by default font-style: italic
margin vs paddingmargin — between neighbors; padding — from parent to child
Figma / PhotoshopFigma — the most convenient; PSD — open in Photoshop, units — pixels

More interesting articles

Read the blog
01 / 06