Hero Section HTML CSS Text Animation

This hero section HTML CSS tutorial shows you how to build a full-screen centered hero with a large serif heading and a short description. The text enters with a soft fade, blur, and slide animation using CSS keyframes, and no JavaScript. I also cover 100dvh, clamp(), text-wrap: balance, logical properties, and prefers-reduced-motion, so the hero stays accessible and responsive.

Final preview:

A point of view. Not just a presence.

We turn what makes you, you into brands and digital experiences that feel like no one else.

Skip to view source code


Steps for Hero Section HTML CSS Text Animation:

1. Set Up the Section and Base Styles

Link the Instrument Serif and DM Sans fonts from Google Fonts in the head.

<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,100..1000;1,9..40,100..1000&family=Instrument+Serif:ital@0;1&display=swap" rel="stylesheet">

Let’s start with a main element, and inside it a section with the class hero-section. Then add a .section-container div. The aria-labelledby attribute points to the heading id, so screen readers announce the section by its title.

<main>
    <section class="hero-section" aria-labelledby="hero-title">
        <div class="section-container"></div>
    </section>
</main>

Then reset the margins and the padding, and define the heading font and two colors as CSS variables on :root. Set DM Sans as the font family on the body.

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --heading-font: "Instrument Serif", serif;

    --color-bg-primary: hsl(180 100% 25.1%);

    --color-text-primary: hsl(46 59% 91%);
}

body {
    font-family: "DM Sans", sans-serif;
}

Note:
main. It marks the primary content of the page. Use it only once per page, since screen reader users can jump straight to it.

Next, give the section a teal background and a warm cream text color. Center the text, and set the minimum block size to 100dvh, so the hero fills the screen. Then set the align content property to center, so the text sits in the vertical middle. Finally, center the container with the margin inline property set to auto and a max inline size.

.hero-section {
    text-align: center;
    background-color: var(--color-surface);
    color: var(--color-text-primary);
    padding-block: 8.75rem;
    padding-inline: 5%;
    min-block-size: 100dvh;
    align-content: center;

    .section-container {
        max-inline-size: 50.25rem;
        margin-inline: auto;
    }
}

Note:
dvh. On phones, the browser toolbar grows and shrinks as you scroll. 100vh ignores that, so the page can overflow. 100dvh is the dynamic viewport height, so it adjusts as the toolbar moves.
min-block-size. It is the logical version of min-height. I use “minimum” so the hero can still grow if the content gets taller than the screen.

2. Add the Heading

Now add the h1 inside the container. Each sentence sits in its own span with the class line, so we can control the line breaks and style them separately.

<h1 id="hero-title">
    <span class="line">A point of view.</span>
    <span class="line">Not just a presence.</span>
</h1>

Use clamp() on the font size, so it scales smoothly from phones to large screens. Add a tight line height and a negative letter spacing, since large serif text looks better when it is a little compressed. Set the display property to block on each line, so every sentence gets its own row. Then make the second line italic using :nth-child(2).

.hero-section h1 {
    font-size: clamp(3.25rem, calc(1rem + 8.33vw), 8.5rem);
    font-weight: 400;
    line-height: .97;
    letter-spacing: -0.0448em;
    font-family: var(--heading-font);

    .line {
        display: block;

        &:nth-child(2) {
            font-style: italic;
        }
    }
}

Note:
why span and not br? A span set to display: block keeps the text as one heading for screen readers, and it also lets you style each line on its own.

Output:

A point of view. Not just a presence.

3. Add the Description

After the heading, add a paragraph.

<p>We turn what makes you, you into brands and digital experiences that feel like no one else.</p>

Use clamp() on the top margin, so the gap grows with the screen. Then set the max inline size to 35ch, which keeps the line short and easy to read. Center the paragraph with the margin inline property set to auto. Finally, set the text wrap property to balance, so the lines break evenly.

.hero-section p {
    margin-block-start: clamp(1.5rem, calc(1rem + 2.22vw), 3rem);
    font-size: 1rem;
    line-height: 1.65;
    max-inline-size: 35ch;
    margin-inline: auto;
    text-wrap: balance;
}

Note:
text-wrap: balance. It spreads the words evenly across the lines, so you never get one lonely word on the last line.

Output:

A point of view. Not just a presence.

We turn what makes you, you into brands and digital experiences that feel like no one else.

4. Animate the Text In

Finally, let’s add a gentle entrance. The text starts invisible, slightly blurred, and shifted down. Then a keyframe animation moves it to its normal state. As a result, the heading and description fade and float into place. Set a delay on each one, so the paragraph arrives after the heading.

@media (prefers-reduced-motion: no-preference) {

    .hero-section h1,
    .hero-section p {
        opacity: 0;
        filter: blur(0.5rem);
        transform: translateY(0.5em);
        animation: text-enter 1.2s cubic-bezier(0.16, 1, 0.3, 1) both;
    }

    .hero-section h1 {
        animation-delay: 0.5s;
    }

    .hero-section p {
        animation-delay: 0.8s;
    }

    @keyframes text-enter {
        to {
            opacity: 1;
            filter: blur(0);
            transform: translateY(0);
        }
    }
}

Note:
reduced motion. I wrapped everything in prefers-reduced-motion: no-preference. So people who turned off animations on their device see the text right away, and never get stuck with invisible text.
both. The animation-fill-mode: both keeps the starting styles during the delay, and the ending styles afterwards. Without it, the text would flash visible before the delay ends.
cubic-bezier(). It defines the easing curve. This one starts fast and slows down gently at the end, which feels smooth and natural.

Output:

A point of view. Not just a presence.

We turn what makes you, you into brands and digital experiences that feel like no one else.


Final Source Code for Hero Section HTML CSS Text Animation:

HTML

<main>
    <section class="hero-section" aria-labelledby="hero-title">
        <div class="section-container">
            <h1 id="hero-title">
                <span class="line">A point of view.</span>
                <span class="line">Not just a presence.</span>
            </h1>
            <p>We turn what makes you, you into brands and
                digital experiences that feel like no one else.</p>
        </div>
    </section>
</main>

CSS

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --heading-font: "Instrument Serif", serif;

    --color-bg-primary: hsl(180 100% 25.1%);

    --color-text-primary: hsl(46 59% 91%);
}

body {
    font-family: "DM Sans", sans-serif;
}

.hero-section {
    text-align: center;
    background-color: var(--color-bg-primary);
    color: var(--color-text-primary);
    padding-block: 8.75rem;
    padding-inline: 5%;
    min-block-size: 100dvh;
    align-content: center;

    .section-container {
        max-inline-size: 50.25rem;
        margin-inline: auto;
    }
}

.hero-section h1 {
    font-size: clamp(3.25rem, calc(1rem + 8.33vw), 8.5rem);
    font-weight: 400;
    line-height: .97;
    letter-spacing: -0.0448em;
    font-family: var(--heading-font);

    .line {
        display: block;

        &:nth-child(2) {
            font-style: italic;
        }
    }
}


.hero-section p {
    margin-block-start: clamp(1.5rem, calc(1rem + 2.22vw), 3rem);
    font-size: 1rem;
    line-height: 1.65;
    max-inline-size: 35ch;
    margin-inline: auto;
    text-wrap: balance;
}

@media (prefers-reduced-motion: no-preference) {

    .hero-section h1,
    .hero-section p {
        opacity: 0;
        filter: blur(0.5rem);
        transform: translateY(0.5em);
        animation: text-enter 1.2s cubic-bezier(0.16, 1, 0.3, 1) both;
    }

    .hero-section h1 {
        animation-delay: 0.5s;
    }

    .hero-section p {
        animation-delay: 0.8s;
    }

    @keyframes text-enter {
        to {
            opacity: 1;
            filter: blur(0);
            transform: translateY(0);
        }
    }
}

If you have any doubts or stuck somewhere, you can reach out through Coding Yaar's Discord server.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Most Voted
Newest Oldest
0
Would love your thoughts, please comment.x
()
x