The Wayback Machine - https://web.archive.org/web/20240930005229/https://www.geeksforgeeks.org/how-to-set-viewport-height-width-in-css/
Open In App

How to Set Viewport Height & Width in CSS ?

Last Updated : 02 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Set viewport height and width in CSS is essential for creating responsive and visually appealing web designs. We’ll explore the concepts of setting viewport height and width by using various methods like CSS Units, Viewport-Relative Units, and keyframe Media Queries.

Setting viewport height and width using CSS Units

CSS units such as pixels (px), percentages (%), and ems (em) allow to specify fixed or relative dimensions for elements based on the viewport size.

Example: Setting viewport height and width using CSS Units.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Using CSS Units</title>
    <style>
        .container {
            text-align: center;
        }

        .box {
            height: 200px;
            margin: 20px auto;
            text-align: center;
        }

        .px-div {
            background-color: #ff0000;
            width: 300px;
        }

        .percent-div {
            background-color: #00ff00;
            width: 20%;
        }

        .em-div {
            background-color: #0000ff;
            width: 20em;
        }
    </style>
</head>

<body>
    <h3 class="container">
      Setting viewport height and width using CSS Units
      </h3>
    <div class="box px-div">
      Setting viewport height and width using Px
      </div>
    <div class="box percent-div">
      Setting viewport height and width using %
      </div>
    <div class="box em-div">
      Setting viewport height and width using em
      </div>
</body>

</html>

Output:

z34p1
Output

Setting viewport height and width using Viewport-Relative Units

This approach ensures that elements scale proportionally with the viewport, offering a flexible and intuitive way to create responsive layouts. Viewport-relative units, including vh (viewport height) and vw (viewport width), enable developers to define element dimensions relative to the size of the viewport.

Example: Setting viewport height and width using Viewport-Relative Units.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Setting viewport height and width 
             using Viewport-Relative Units
      </title>
    <style>
        body,
        html {
            height: 100%;
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: flex-start;
        }

        .container {
            margin-top: 20px;
        }

        .box {
            width: 50vw;
            height: 50vh;
            background-color: #ff0000;
        }

        h3 {
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="container">
        <h3>Setting viewport height and width 
              using Viewport-Relative Units
          </h3>
        <div class="box"></div>
    </div>
</body>

</html>

Output:

z341
Output

Setting viewport height and width using CSS Media Queries

In this approach, the .box element has a default height of 500px and adjusts to 50px on screens smaller than 768px using CSS media queries, demonstrating how to set viewport height and width dynamically based on screen size.

Example: Setting viewport height and width using CSS Media Queries.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Media Queries</title>
    <style>
        .box {
            width: 80%;
            height: 500px;
            background-color: #0000ff;
            margin-left: 10vw;
        }

        @media screen and (max-width: 768px) {
            .box {
                height: 50px;
            }
        }

        h3 {
            text-align: center;
        }
    </style>
</head>

<body>
    <h3>Setting viewport height and width
          using CSS Media Queries
      </h3>
    <div class="box"></div>
</body>

</html>

Output:

1


Similar Reads

HTML width/height Attribute vs CSS width/height Property
In HTML 5, few elements follow the width and height attributes and maximum elements do not follow this attribute. Like img, iframe, canvas, and svg kind of elements follow the width and height attributes but div, span, article and section type of elements don't follow them.The width and height attributes are affected in img, svg tags, those are wea
3 min read
How to fill the height of the viewport with tailwind CSS ?
Tailwind CSS is a popular utility-first CSS framework that makes designing and building responsive websites easier. Filling the height of the viewport with a specific element or container is a common design requirement. This is simple to accomplish with Tailwind CSS, and we will look at various approaches in this article. Syntax: Before we dive int
4 min read
How to Set Calc Viewport Units Workaround in CSS ?
In the HTML webpage, when applying values to the CSS properties, the calculations are performed by using calc() function. Syntax: element { // CSS property property : calc( expression) } Properties: The calc() function takes parameters in the form of a single expression. The value becomes the result of the expression. Even the expression be the com
2 min read
How to set width and height of background image in percent with respect to parent element in CSS ?
If we are using an image as a child and want to set the height and width in percentage then we need to set the parent element with some fixed size. Approach 1: Here we will use CSS inside the tags which are also known as inline CSS.For the parent div we will give a fixed size by giving height: 500px and width: 40% according to screen size, and we w
2 min read
How to set height equal to dynamic width (CSS fluid layout) ?
To create a fluid layout in CSS, set an element's height to the same value as its dynamic width. This makes the layout more adaptable and responsive since the element's height will automatically change depending on the viewport's width. It is possible to determine the element's height by using the padding by setting the height attribute to 0. Using
2 min read
How to Set Height Equal to Dynamic Width (CSS fluid layout) in JavaScript ?
A key component of constructing a contemporary website in web development is developing an adaptable and dynamic layout. Setting the height of an element equal to its dynamic width, often known as a CSS fluid layout, is a common difficulty in accomplishing this. When the screen size changes and you want to keep an element's aspect ratio, like that
4 min read
How to Set Width and Height of Span Element using CSS ?
The <span> tag is used to apply styles or scripting to a specific part of text within a larger block of content. The <span> tag is an inline element, you may encounter scenarios where setting its width and height becomes necessary. This article explores various approaches to set the width and height of a <span> element using CSS.
2 min read
How to set the Width and Height in Tailwind CSS ?
Setting the width and height in Tailwind CSS involves using utility classes like w-[size] and h-[size]. These classes allow you to easily define the width and height of elements in your project. Setting WidthTo define the width of an element, employ the w-[size] classes, replacing [size] them To establish an element's heightwith options like 1/2, f
1 min read
New CSS Viewport Units (vi, vb, vmax, vmin)
The original viewport units, such as vw (viewport width) and vh (viewport height), have been widely used to create responsive layouts that adapt to different screen sizes. Using the vh unit on mobile is buggy because the viewport size won’t include the browser’s address bar UI. However, the newly introduced units, namely vmin, vmax, vi and vb bring
3 min read
How does the SVG Viewbox & Viewport work in CSS ?
SVG (Scalable Vector Graphics) is a format used to create graphics on the web that can scale without losing quality. Within SVG, two important concepts are the viewBox and viewPort, which play crucial roles in how SVG elements are displayed and scaled. Table of Content What is SVG ViewBox?What is SVG ViewPort?How Do They Work Together?Employing Ext
5 min read
How to change background-color on a specific wider viewport in CSS ?
The purpose of this article is to change the background-color on a specific wider viewport in CSS. In simple words specify some width and if the width satisfies the condition then it does not change its view but if the with not satisfy the condition then it is going to change its view. Media rule: This rule is used in media queries to apply differe
2 min read
Difference Between css(‘width’) and width() methods In jQuery
In jQuery, we have two ways to change the width of any HTML element. The jQuery css() method and width() method, below both the methods are described properly with the example. jQuery CSS('width') Method: It is a method present in jQuery which is used to get or set the property on the matched element. It is a property used to get or set the width o
3 min read
How to add Background-Color for Text Width Instead of Entire Element Width using CSS ?
In this article, we will see how to add background color for the text width instead of the entire element width using CSS. The display: inline or display: inline-block property is used to set the background color of the text width. Syntax: display: inline; display: inline-block; Approach: We will create an element and then apply the display: inline
2 min read
How to make a grid with two columns where 1st column has 20% of width and 2nd one 80% width in Tailwind CSS ?
Tailwind CSS simplifies the process of making complex UI designs. It has a lot of predefined styles in form of classes, which can be applied to any HTML element in the code. Also, one advantage of Tailwind CSS is that during the production build, it analyzes the HTML components, and removes any unused CSS, hence decreasing the overall size of the C
4 min read
What is the difference between 'width: 100%' and 'width: 100vw' in CSS ?
The CSS width Property is used to set the width of the text and images. The width can be assigned to the text and images in the form of pixels(px), percentages (%), centimeters (cm), etc. The width: 100% sets an element's width relative to its containing block, making it responsive within its parent. The width: 100% responds to the container's size
1 min read
Primer CSS Layout width and height
Primer CSS is a free open-source CSS framework that is built with the GitHub design system to provide support to the broad spectrum of Github websites. It creates the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approa
5 min read
CSS Height and Width
Height and Width in CSS are used to set the height and width of boxes. Their values can be set using length, percentage, or auto. Width and HeightThe width and height properties in CSS are used to define the dimensions of an element. The values can be set in various units, such as pixels (px), centimeters (cm), percentages (%), etc. Example: This e
4 min read
CSS Media Queries - max-width or max-height
CSS media queries can help you apply various styles depending on the user's device or viewport properties. You may make responsive designs with them, which change to accommodate various screen sizes, orientations, and device capabilities. The @media rule in CSS is used to write media queries, which can target a wide range of characteristics, includ
3 min read
How to Increase Height and Width of Date and Time Fields in Tailwind CSS?
Tailwind CSS is a utility-first CSS framework that enables developers to design web interfaces quickly. By applying pre-defined classes we can control the appearance of the HTML elements without writing custom CSS. This article will focus on adjusting the size of the date and time fields to fit the design requirements. Below are the approaches to i
2 min read
How to expand floated child div's height to its parent's height in CSS ?
Making the height of the floating child div equal to the height of the parent div is referred to as expanding the height of the floated child div to the height of the parent div. Approach 1: Using the CSS "clearfix" method is the first approach to increase a floated child div's height to match its parent's height. When an element is floated, it is
4 min read
How to set the height and width of background image inline style in react?
In React, one can create an object with styling information like background image, height, width, etc and refer to the object in the style attribute in the HTML element. Since the inline CSS is written in a JavaScript object, properties with two names, like background-color, must be written with camel case syntax. Syntax : class MyHeader extends Re
2 min read
How to set the height and width of the video player in HTML5 ?
In this article, we will learn to set the width and height of the video player in HTML5. The following are the two scenarios The width and height of the video are fixed.Make the video respond to your screen width. Approach: The HTML5 width and height attributes of the video tag are used to set the height and width of the video player respectively.
2 min read
How to Set the height and width of the Canvas in HTML5?
The canvas element is a part of HTML5 which allows us for dynamic, scriptable rendering of 2D shapes and bitmap images, facilitating the creation of games, graphs, animations, and compositions. To set the height and width of the canvas in HTML5, we can utilize the height and width attributes within the <canvas> tag, specifying pixel values to
2 min read
How to Set Table Cell Width & Height in HTML ?
In HTML, we can control the width and height of table cells using various approaches. This article discusses different methods to set the width and height of table cells in HTML. Table of Content Using inline StyleUsing width and height attributesUsing Inline StyleIn this approach, we are using the inline style to set the width and height of table
2 min read
How to set the width and height of an image using HTML ?
The height and width of an image can be set using the height and width attributes in HTML. Setting these attributes ensures that images display correctly and consistently, contributing to a stable page layout and improved user experience. Importance of Setting Image DimensionsSetting the height and width of images is important for maintaining a sta
2 min read
How to Set NextJS Images with auto Width and Height?
Next.js provides a powerful Image component that automatically optimizes images on-demand for a faster load time and better performance. The Image component includes features such as lazy loading, responsive design, and more. In this article, we will learn how to Set NextJS Images with auto Width and Height. Steps to Create NextJS ApplicationStep 1
3 min read
How to Set Height and Width of a Chart in Chart.js ?
Chart.js is a popular JavaScript library that allows developers to create interactive and visually appealing charts for web applications. One of the key aspects of chart customization is controlling its dimensions. In this article, we'll explore how to set the height and width of a Chart.js chart. Chart.js CDN link <script src="https://cdnjs.clo
4 min read
How to dynamically set the height and width of a div element using jQuery ?
Set the height of a div element using jQueryThe content height of a div can be dynamically set or changed using height(), innerHeight(), and outerHeight() methods depending upon the user requirement. If the user wants to change the content height of a div dynamically, it includes changing the actual height, actual height with padding, and actual he
7 min read
How to get card vertically centered between search bar and bottom of the viewport?
To center an HTML element horizontally you need to specify the width of the parent element. Similarly to center an HTML element vertically you need to specify the height of the parent element. Again to center an element horizontally as well vertically you need to specify the height and width of the parent element. Now specifying the height and widt
3 min read
How to get the dimensions of the Viewport using AngularJS ?
In this article, we will see how to get the dimensions of the Viewport in AngularJS. The Viewport is the visible area for the users on the web page that varies with different screen-width, depending upon the devices used. to get the dimension of the Viewport, the clientWidth, and innerWidth properties can be utilized. The DOM clientWidth Property i
2 min read