{"id":5156,"date":"2021-07-05T02:00:00","date_gmt":"2021-07-05T00:00:00","guid":{"rendered":"https:\/\/alvarotrigo.com\/epa\/how-to-create-websites-fullscreen-sections-html-and-css\/"},"modified":"2025-05-09T13:51:54","modified_gmt":"2025-05-09T11:51:54","slug":"how-to-create-websites-fullscreen-sections-html-and-css","status":"publish","type":"post","link":"https:\/\/alvarotrigo.com\/blog\/how-to-create-websites-fullscreen-sections-html-and-css\/","title":{"rendered":"How to create a website with fullscreen sections using CSS and HTML"},"content":{"rendered":"\n<p>Large full-screen sections are quite a common element on nowadays websites, especially on landing pages acting as a <a href=\"https:\/\/alvarotrigo.com\/blog\/hero-slider\/\">hero slider<\/a>. 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.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"defining-our-sections-in-html\">Defining our sections in HTML<\/h2>\n\n\n<p>Let\u2019s start by adding a couple of section elements inside a the <code>&lt;main&gt;<\/code> wrapper:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\">    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Hello!<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Toodles~<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">section<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">main<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"making-sections-become-full-screen\">Making sections become full-screen<\/h2>\n\n\n<p>Making sections full-height and full-width can be done in two ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using viewport units (<code>vh<\/code> and <code>vw<\/code>) &#8211; May feel the most intuitive out of the two.  In fact, the viewport units were introduced to do just these kinds of things.<\/li>\n\n\n\n<li>Using implicit units like percentages. (<code>%<\/code>). They used to be used more in the past when viewport units were not a thing or were not widely supported.<\/li>\n<\/ul>\n\n\n<h2 class=\"wp-block-heading\" id=\"using-viewport-units\">Using viewport units<\/h2>\n\n\n<p>First, we will style our sections so that they scale to the height of the viewable area. We will use the <code>vh<\/code> unit, which stands for &#8220;viewport height&#8221;. The <code>vh<\/code> unit is roughly 1% of the viewport&#8217;s height. And what is the &#8220;viewport&#8221;? It is the visible area of your website in the browser. To make it take 100% of the vertical space, we will set <code>min-height<\/code> to <code>100vh<\/code>, meaning, the section will take a minimum height of 100% of the viewport height.<\/p>\n\n\n\n<p>We set <code>min-height<\/code> instead of <code>height<\/code> because this way each section can, if required, have a bigger height, while if you used just <code>height<\/code> any content that scales beyond the viewport height would be overflowing the container.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">body<\/span> {\n  <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span>;\n}\n\n<span class=\"hljs-selector-tag\">section<\/span> {\n  <span class=\"hljs-attribute\">font-family<\/span>: sans-serif;\n  <span class=\"hljs-attribute\">min-height<\/span>: <span class=\"hljs-number\">100vh<\/span>;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>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:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-src=\"https:\/\/alvarotrigo.com\/blog\/assets\/imgs\/viewport_units_compatibility_chart.jpeg\" alt=\"Viewport units compatibility chart\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" \/><\/figure>\n\n\n<h2 class=\"wp-block-heading\" id=\"using-percentage-units\">Using percentage units<\/h2>\n\n\n<p>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 <code>html<\/code>, <code>body<\/code> and <code>main<\/code> tags have <code>height<\/code> and <code>width<\/code> set to <code>100%<\/code>. This will force each parent in the hierarchy to take up the full available height and width of the viewport.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">html<\/span>,\n<span class=\"hljs-selector-tag\">body<\/span> {\n  <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span>;\n  <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100%<\/span>;\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\n}\n\n<span class=\"hljs-selector-tag\">main<\/span> {\n  <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100%<\/span>;\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\n}\n\n<span class=\"hljs-selector-tag\">section<\/span> {\n  <span class=\"hljs-attribute\">font-family<\/span>: sans-serif;\n  <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">100%<\/span>;\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"centering-content-vertically-and-horizontally\">Centering content vertically and horizontally<\/h2>\n\n\n<p>You will very probably want to have some centered content on your section. If not text or images, probably a container element.<\/p>\n\n\n\n<p>We will be using the <code>align-items<\/code> property to vertically center whatever content we have. Then, to center them horizontally we&#8217;ll be using <code>justify-content: center<\/code>.<\/p>\n\n\n\n<p>And to make those two properties work, we&#8217;ll  <code>display: flex<\/code> in order to turn each <code>section<\/code> into a flex container.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">section<\/span> {\n  <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">120px<\/span>;\n  <span class=\"hljs-attribute\">display<\/span>: flex;\n  <span class=\"hljs-attribute\">justify-content<\/span>: center;\n  <span class=\"hljs-attribute\">align-items<\/span>: center;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<h2 class=\"wp-block-heading\" id=\"styling-sections-individually\">Styling sections individually<\/h2>\n\n\n<p>To make each section distinguishable from the other, we will use the <code>nth-child<\/code> 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.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">section<\/span><span class=\"hljs-selector-pseudo\">:nth-child(1)<\/span> {\n  <span class=\"hljs-attribute\">background<\/span>: orange;\n  <span class=\"hljs-attribute\">color<\/span>: white;\n}\n\n<span class=\"hljs-selector-tag\">section<\/span><span class=\"hljs-selector-pseudo\">:nth-child(2)<\/span> {\n  <span class=\"hljs-attribute\">background<\/span>: cyan;\n  <span class=\"hljs-attribute\">color<\/span>: white;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>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&#8217;re at it play around with it:<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_mdWdPmv\" data-src=\"\/\/codepen.io\/anon\/embed\/mdWdPmv?height=450&amp;theme-id=1&amp;slug-hash=mdWdPmv&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed mdWdPmv\" title=\"CodePen Embed mdWdPmv\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n\n<p>And here we have the same but using a percentage instead of <code>vh<\/code>  units.<\/p>\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_WNpNwOv\" data-src=\"\/\/codepen.io\/anon\/embed\/WNpNwOv?height=450&amp;theme-id=1&amp;slug-hash=WNpNwOv&amp;default-tab=css,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed WNpNwOv\" title=\"CodePen Embed WNpNwOv\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n<p>If you&#8217;re interested in creating fullscreen websites, then you can take a look at <a href=\"https:\/\/alvarotrigo.com\/fullPage\/\">fullPage.js<\/a>. 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.<\/p>\n\n\n\n<p>\n    <a href=\"https:\/\/alvarotrigo.com\/fullPage\/\">\n        <video width=\"680\" height=\"382\" controls muted autoplay loop playsinline>\n        <source src= \"https:\/\/cdn.jsdelivr.net\/gh\/alvarotrigo\/blog-assets\/videos\/fullPage_intro.webm\" type=\"video\/webm\"\/>\n        <source src= \"https:\/\/cdn.jsdelivr.net\/gh\/alvarotrigo\/blog-assets\/videos\/fullPage_intro.mp4\" type=\"video\/mp4\" \/>\n        Your browser does not support the video tag.\n        <\/source><\/source><\/video>\n    <\/a>\n<\/p>\n\n\n\n<p>And if you want to keep on improving your fullscreen website, adding a <a href=\"https:\/\/alvarotrigo.com\/blog\/sticky-navbar\/\">sticky or fixed navbar<\/a> is definetely the way to go. And if you want to consider adding videos, check out <a href=\"https:\/\/alvarotrigo.com\/blog\/how-to-create-a-fullscreen-video-with-css-and-html\/\">how to create a fullscreen video with CSS and HTML<\/a> or <a href=\"https:\/\/alvarotrigo.com\/blog\/how-to-create-a-youtube-video-background-with-css\/\">how to add Youtube videos as a background with CSS<\/a>.<\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"related-articles\">Related articles<\/h2>\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/alvarotrigo.com\/blog\/standard-webpage-sizes\/\">Standard Website Sizes<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Create full-screen sections using CSS and HTML in a matter of minutes. Use any: viewport units or percentage.<\/p>\n","protected":false},"author":1,"featured_media":5155,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[106],"tags":[9,25,10,43],"class_list":["post-5156","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-css","tag-css","tag-html","tag-javascript","tag-slider"],"acf":[],"_links":{"self":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/comments?post=5156"}],"version-history":[{"count":5,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5156\/revisions"}],"predecessor-version":[{"id":17897,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5156\/revisions\/17897"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media\/5155"}],"wp:attachment":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media?parent=5156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/categories?post=5156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/tags?post=5156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}