CSS Viewport Items

CSS Viewport items have been round for the previous few years, and by time, I see them getting used an increasing number of by builders. Their profit lies in offering us with a strategy to dimension issues in a fluid and dynamic approach, with out the necessity to use JavaScript. Additionally, it’s simple to offer a fallback in the event that they fail.

On this article, we’ll study CSS viewport items and use them, together with some use-cases and options for frequent points. Let’s begin and dig in!


In keeping with the CSS spec, viewport-percentage items are relative to the scale of the preliminary containing block, which is the foundation aspect of an internet web page.

The viewport items are: vw, vh, vmin, and vmax.

Viewport Width

The vw unit represents a proportion of the foundation aspect width. One vw is the same as 1% of the viewport width.

We’ve a component with the next CSS:


When the width of the viewport is 500px, the 50vw might be calculated as beneath:

width = 500*50% = 250px

Viewport Top

The vh unit represents a proportion of the foundation aspect peak. One vh is the same as 1% of the viewport peak.

We’ve a component with the next CSS:


When the peak of the viewport is 290px, the 70vh might be calculated as beneath:

peak = 290*70% = 202px

That’s it. Let’s transfer to a distinct sort of viewport items!

vmin Unit

The vmin represents the worth of the minimal of the width and peak of the viewport. If the viewport width is larger than its peak, then the worth might be calculated based mostly on the peak.

Let’s take the next instance.

We’ve a cell in panorama mode, and a component has the vmin unit. In that case, the worth might be calculated based mostly on the viewport peak, as a result of it’s lower than the width.

.aspect {
padding-top: 10vmin;
padding-bottom: 10vmin;

Right here is how vmin might be calculated:

As you may guessed it, the end result might be calculated because the beneath:

padding-top = (10% of peak) = 10% * 164 = 32px
padding-bottom = (10% of peak) = 10% * 164 = 32px

vmax unit

It’s the other of vmin. The worth is calculated based mostly on the utmost width and peak.

Let’s take the next instance.

.aspect {
padding-top: 10vmax;
padding-bottom: 10vmax;

The end result might be calculated because the beneath:

padding-top = (10% of width) = 10% * 350 = 35px
padding-bottom = (10% of width) = 10% * 350 = px

How Viewport Items Are Completely different From Percentages?

Viewport items are based mostly on the foundation aspect of the web page, whereas the proportion is predicated on the container they’re in. For that purpose, they’re totally different from one another and each has its use circumstances.

Use Instances For Viewport Items

Within the following sections, I’ll discover some use circumstances for viewport items and implement them in your undertaking.

Font Measurement

CSS viewport items are excellent for responsive typography. For instance, we will use the next for an article title:

font-size: 5vw;

The title’s font-size will enhance or shrink based mostly on the viewport width. It’s like giving a font dimension of 5% of the viewport width. Nevertheless, it could be tempting to only use it with out correct testing. Let’s see the video beneath:

Discover that the font dimension grew to become very small in cell sizing, that is dangerous for accessibility and consumer expertise. So far as I do know, the minimal font dimension on a cell machine shouldn’t transcend 14px. Within the GIF, the font dimension went beneath 10px.

To unravel that challenge, we have to give the title a minimal font dimension that it could actually’t go beneath it. CSS calc() to the rescue!

font-size: calc(14px + 2vw);

The calc() CSS perform may have a base 14px worth, and it’ll add 2vw to it. With that in hand, the font-size worth gained’t change into too small.

One other essential factor to think about is how the font dimension will behave on massive screens, for instance, a 27” iMac. What is going to occur? Nicely, you guessed it. The font dimension went to round 95px, which an enormous worth. To forestall that, we should always use media queries at sure breakpoints and alter the font-size.

@media (min-width: 1800px)

By resetting the font-size, we will make sure that the scale gained’t go too enormous. You may additionally want so as to add a number of media queries, however it’s as much as you and depends upon the context of your undertaking.


Full Display screen Sections

Typically, we want a bit to take 100% of the viewport peak. This is named ** full-screen sections**. To take action, we will use the viewport peak unit.


By including peak: 100vh, we will make sure that the part peak will take 100% of the viewport. Additionally, I added some flexbox to deal with centering the content material horizontally and vertically.


On a big display, you may discover that web site content material is brief, and the footer shouldn’t be caught to the underside. That’s regular, and it’s not thought-about as a foul follow. Nevertheless, there may be room for enhancement. Think about the next determine that represents the difficulty:

To unravel that, we have to give the principle content material a peak that is the same as viewport peak – (header + footer). It’s tough to do this dynamically, however with CSS viewport items, it’s fairly simple.

1. First Answer: Calc and Viewport Items

If the header and footer peak is fastened, it’s attainable to mix each CSS calc() perform with viewport items. Right here is how:

peak: 70px;


This answer shouldn’t be assured to work on a regular basis, particularly for the footer. In my profession, I haven’t used a footer with a set peak, as a result of it’s not doable, for instance, on totally different display sizes.

By including the 100vh as a peak for the physique aspect, we will then use flexbox to make the principle aspect take the remaining area.

physique {
min-height: 100vh;
show: flex
flex-direction: column;

/* This can make the principle aspect take the remaining area dynamically */
flex-grow: 1;

With that, the difficulty is fastened and we’ve got a sticky footer whatever the content material size.


Responsive Components

I stumbled upon this text whereas researching, and I actually appreciated it, so I’ll re-take the use-case and clarify it my approach.

Suppose that we’ve got a portfolio to showcase our responsive design work, and we’ve got three units (cell, pill, and laptop computer). Every machine incorporates a picture. The purpose is to make this 100% responsive in CSS.

Through the use of CSS grid and viewport items, we will make this absolutely responsive and dynamic.

class=“machine laptop computer”>

class=“machine cell”>

class=“machine pill”>

Discover that viewport items are used within the grid-* properties. They’re additionally used for the border, border-radius, and different properties. All of this can end in a fluid design.

show: grid;
grid-template-columns: repeat(20, 5vw);
grid-auto-rows: 6vw;

place: relative;
z-index: 1;
grid-column: 2 / span three;
grid-row: three / span 5;


.laptop computer

/* Viewport items are used for the underside, left, proper, peak, and border-radius. Is not that cool? */
.laptop computer:after
content material:“”;
backside: -3vw;
left: -5.5vw;
proper: -5.5vw;
peak: 3vw;
background-color: #a9B9dd;
border-radius: zero zero 1vw 1vw;


Breaking Out Of The Container

I’ve observed a use case which is most fitted for editorial layouts. A baby aspect that takes 100% width of the viewport, even when its dad or mum width is proscribed. Think about the beneath determine:

To realize an identical impact, we will use viewport items and a few positioning properties. Right here is the CSS:


Let’s break it down so we perceive how all of this works little by little.

1. Including width: 100vw

Crucial step, which is able to give the picture a width equal to 100% of the viewport.

2. Including margin-left: -50vw

To middle the picture, we might want to give it a detrimental margin with half of the viewport width.

three. Including left: 50%

Lastly, we might want to push the picture to the proper with a worth of 50% of its dad or mum width.


Vertical and Horizontal Spacing

One other attention-grabbing use-case that I considered is utilizing viewport items for spacing between parts. This can be utilized with values like margin, high, backside, and grid-gap. When used, the spacing might be based mostly on the viewport width or peak, which could be helpful to make a format extra dynamic.

For modals, we have to push them from the highest of the viewport. Typically, I made this with the highest property and I exploit both proportion or pixel worth. Nevertheless, with viewport items, we will add a spacing that may change based mostly on the viewport’s peak.


See the video beneath for the ultimate end result.


A web page header is a bit that acts as an introduction for a web page. It usually has a title and an outline, and amongst that there’s both a set peak or padding for the highest and backside edges. We’re within the padding factor.

For instance, right here is how the CSS seems to be:


@media (min-width: 800px)

The vertical padding is small on cell, and it will get bigger on higher viewports. What about utilizing viewport items? Let’s discover that.

padding-top: 10vh;
padding-bottom: 10vh;

.page-header h2
margin-bottom: 1.5vh;

I used vh unit for the padding of the web page header, and the margin beneath the title. Discover how the spacing adjustments!


Grid of Gadgets

One other use case for the dynamic spacing is a grid of things. It might for something, like a grid of articles, providers part.. and many others. For our case, we’ll check out use that for an article grid.

Through the use of viewport items inside grid-gap, we will get the specified end result. Discover that I used CSS calc() perform as nicely. The purpose of utilizing calc() is to have a base vertical and horizontal hole.

show: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
grid-gap: calc(14px + 1vh) calc(14px + zero.5vw);


Vmin and Vmax Use Instances

Whereas researching about use-cases for these two values, I didn’t discover something however this one from CSS-Tips.

The use-case is in regards to the high and backside padding for a web page header aspect. The padding is commonly diminished when the viewport is small (cell). Through the use of vmin, we will have a fluid high and backside padding based mostly on the viewport smaller dimension (width or peak).



4 years in the past, I wrote an article about vertical media queries. I need to make clear this matter because it’s associated to this text that you just’re studying now

Full Top Sections in Panorama Mode

Through the use of vertical media queries, we will test if the consumer telephone or pill is in panorama mode. Whether it is, then it gained’t make sense to have a bit taking the total peak of the viewport through the use of peak: 100vh.

To unravel that challenge, we will do the next:

@media (min-height: 400px)

Or we will use the orientation media question:

@media (orientation: panorama)

Side Ratio

We will use vw unit to create responsive parts that keep its facet ratio whatever the viewport dimension. Let’s discover that.

We might want to resolve on the facet ratio we want. For this instance, I’ll use 9/16.

/* 9/16 * 100 */
peak: 56.25vw;


Utilizing Viewport Items For Graphical Components

I’m undecided if my naming for that is right, however I hope you’ll get my level from the next visible examples.

Are you aware that high border which is many of the web sites use nowadays? Principally, its shade is similar because the model shade – which is able to give some persona.

Let’s assist that the preliminary worth for the border is 3px. Easy methods to convert that fastened worth to a viewport factor? Nicely, right here is calculate the vw equal of it.

vw = (Pixel Worth / Viewport width) * 100

The viewport width is used to estimate the ratio between the pixel worth and the specified vw unit.

For our instance, right here is how the highest border added:


In my case, the viewport width is 1440 (This isn’t a set quantity, change it with your individual)

vw = (four / 1440) * 100 = zero.277


Even higher, we will use a base pixel worth, and the viewport unit will be an addition.


Part Numbering

For part gadgets with a counter or an icon, we will leverage the utilization of viewport items. See the mockup beneath:

We’ve a circle containing a quantity, and there may be textual content content material. Each of them should be sized fluidly with viewport items. Let’s discover how!

1. Utilizing a fluid worth for the flex, width, and peak properties.
2. The road-height is used to middle the textual content vertically
three. Font dimension
four. The spacing between the circle and the textual content

.level:earlier than
flex: zero zero calc(24px + 4vw); /* [1] */
width: calc(24px + 4vw); /* [1] */
peak: calc(24px + 4vw); /* [1] */
line-height: calc(24px + 4vw); /* [2] */
font-size: calc(14px + 1vw); /* [3] */
margin-right: calc(10px + zero.5vw); /* [4] */


Viewport Items Watcher

I made a device to assist me in checking the present computed worth parts with viewport items. Think about the beneath:

I added the category identify and property for every aspect, and on viewport resize, the values are up to date dynamically. That is a lot better than inspecting DevTools and going to the computed for every particular aspect.

Attempt it your self

There’s a frequent challenge in cell which circumstances a scroll, even when 100vh is used. The reason being that the deal with bar peak is in view. Louis Hoebregts wrote an article about that with a easy and straightforward repair.

peak: 100vh; /* Fallback for browsers that don’t assist Customized Properties */
peak: calc(var(–vh, 1vh) * 100);

// First we get the viewport peak and we a number of it by 1% to get a worth for a vh unit
let vh = window.innerHeight * zero.01;
// Then we set the worth within the –vh customized property to the foundation of the doc
doc.documentElement.fashion.setProperty(‘–vh’, `$px`);


Accessibility Is Necessary

When utilizing viewport items extensively in a undertaking, like for constructing an entire format with it, accessibility is essential.
When a format is zoomed in/out, the weather with viewport items gained’t scale as you may anticipate. This can trigger a problem to customers who depend on zoom to browse the online.

As per this tweet by Sāra Soueidān, utilizing viewport items alone for font sizing is dangerous for accessibility. It’s higher to mix them with fastened values, just like the beneath:


..and it is going to be good to go. Please make it possible for utilizing viewport items is appropriate for what you’re doing, and check nicely.

I discovered this little px to vw converter which could make it easier to in your undertaking.

Do you might have a great tool? Please let me know at @shadeed9

The Finish

That’s a wrap. Do you might have a remark or a suggestion? Please be at liberty to ping me on @shadeed9.

Leave a Reply