Large full-screen sections are quite a common element on nowadays websites, especially on landing pages acting as a hero banner. They take the full height and full width of the viewport with the intent of bringing the focus in a single portion of the page. Here we will be explaining how to achieve just that.
Defining our sections in HTML
Let’s start by adding a couple of section elements inside a the
Making sections become full-screen
Making sections full-height and full-width can be done in two ways:
- Using viewport units (
vw) - May feel the most intuitive out of the two. In fact, the viewport units were introduced to do just these kinds of things.
- Using implicit units like percentages. (
%). They used to be used more in the past when viewport units were not a thing or were not widely supported.
Using viewport units
First, we will style our sections so that they scale to the height of the viewable area. We will use the
vh unit, which stands for "viewport height". The
vh unit is roughly 1% of the viewport's height. And what is the "viewport"? It is the visible area of your website in the browser. To make it take 100% of the vertical space, we will set
100vh, meaning, the section will take a minimum height of 100% of the viewport height.
min-height instead of
height because this way each section can, if required, have a bigger height, while if you used just
height any content that scales beyond the viewport height would be overflowing the container.
It is important to note that if you intend to support legacy browsers, then you might want to use percentage units instead. Take a look at the following chart to verify if the browsers you intend to support are listed in green:
Using percentage units
The main difference between the percentage unit and the viewport units is that the percentage units rely on the height and width values of the parent. This means that all the parent elements must also scale to the full height and width if the children underneath wish to. This is why the
main tags have
width set to
100%. This will force each parent in the hierarchy to take up the full available height and width of the viewport.
Centering content vertically and horizontally
You will very probably want to have some centered content on your section. If not text or images, probably a container element.
We will be using the
align-items property to vertically center whatever content we have. Then, to center them horizontally we'll be using
And to make those two properties work, we'll
display: flex in order to turn each
section into a flex container.
Styling sections individually
To make each section distinguishable from the other, we will use the
nth-child selector to set a background and a color. You can achieve the same thing by using a class for each section too if you prefer so.
With that, you should now be able to confidently create fullscreen sections with HTML and CSS. If you want to see how it looks like, take a look at the following CodePen and while you're at it play around with it:
And here we have the same but using a percentage instead of
If you're interested in creating fullscreen websites, then you can take a look at fullPage.js. As the name suggests, fullPage.js revolves around the idea of using full-screen sections, but on top of that, it allows you to add some nifty effects like snap scrolling or horizontal scrolling with little to no effort on your part. Easy to configure and with tends of options that will make your page look quite unique.
Join 2,000+ readers and learn something new every month!