Last Updated on September 14, 2023 by Mayank Dham
Sizing is an important aspect of web design that can have a significant impact on the overall design of a website. CSS includes a number of sizing properties that web designers can use to control the size of HTML elements such as text, images, and containers. Understanding these properties is essential for developing a responsive website that looks great on any device, be it a desktop, tablet, or mobile phone. This article will go over the various CSS sizing properties and their importance in web design. We will also provide tips and best practices for effectively utilizing these properties to create beautiful and responsive websites.
Types of Sizes in CSS
- px
- em
- rem
- vh and vw
Px in CSS
Pixels are a fixed unit of measurement, meaning that they don’t scale proportionally with the size of the browser window or device screen. This can make it difficult to create responsive websites that look great on any device, as elements sized in pixels may become too large or too small on different screens.
CODE
< !DOCTYPE html>
< html>
< head>
< style>
h1 {
font-size: 40px;
}
h2 {
font-size: 30px;
}
p {
font-size: 14px;
}
< /style>
< /head>
< body>
< h1>This is heading 1
< h2>This is heading 2
< p>This is a paragraph.
< /body>
< /html>
OUTPUT
In this example, you can see the different sizes of the font by giving different pixel sizes.
When using pixels in CSS, it’s important to keep in mind that different devices have different pixel densities. This means that a pixel on one device may not be the same size as a pixel on another device. As a result, elements sized in pixels may appear smaller or larger on different devices, even if they have the same physical screen size.
em
The "em" unit is a relative unit of measurement in CSS that is based on the font size of the element it is applied to. One "em" is equal to the font size of the parent element.
For example, if the font size of a paragraph is 16 pixels, then one "em" is equal to 16 pixels.
The "em" unit is particularly useful because it allows you to create layouts that are based on the size of the text. This means that if the user changes the font size of their browser, the layout will adjust accordingly.
CODE
< !DOCTYPE html>
< html>
< head>
< style>
h1 {
font-size: 3em;
}
h2 {
font-size: 2em;
}
p {
font-size: 1em;
}
< /style>
< /head>
< body>
< h1>This is heading 1
< h2>This is heading 2
< p>This is a paragraph.
< /body>
< /html>
OUTPUT
CSS rem
The css rem unit is used to define the size of an element relative to the root element, which is typically the HTML element. The css rem unit is short for "root em" and it is similar to the "em" unit, which is also a relative unit, but with one important difference: "em" is relative to the font size of the parent element, while css rem is relative to the font size of the root element.
CODE
< !DOCTYPE html>
< html>
< head>
< style>
h1 {
font-size: 3rem;
}
h2 {
font-size: 2rem;
}
p {
font-size: 1rem;
}
< /style>
< /head>
< body>
< h1>This is heading 1
< h2>This is heading 2
< p>This is a paragraph.
< /body>
< /html>
OUTPUT
vh and vw
In CSS, the "vh" and "vw" units are used to define dimensions relative to the viewport size of the user’s browser window.
"vh" stands for "viewport height" and represents 1% of the height of the viewport. This unit is often used for setting the height of elements, such as sections or containers, in a way that adapts to the height of the user’s screen.
"vw" stands for "viewport width" and represents 1% of the width of the viewport. This unit is often used for setting the width of elements, such as images or containers, in a way that adapts to the width of the user’s screen.
Conclusion
We learned about the sizing property and saw different sizes to change the font size, as well as what different units mean and why they should be used, in this article. Some of them, such as em and rem units, are flexible and change with the layout of the screen, whereas px is rigid and does not change with the layout of the screen.
Frequently Asked Questions (FAQs)
Here are some of the frequently asked questions on sizing in CSS.
Q1. How to set the size of an HTML element using CSS?
You can set the size of an HTML element in CSS using properties like width, height, max-width, max-height, min-width, and min-height. These properties allow you to define the dimensions of an element in various units, such as pixels, percentages, or viewport units.
Q2. What are the differences between using absolute and relative units for sizing in CSS?
Absolute units, like pixels (px), provide a fixed size regardless of the parent container or device screen. Relative units, such as percentages (%) and viewport units (vw, vh), scale based on the parent container’s size or the viewport dimensions. Using relative units can help create responsive designs that adapt to different screen sizes.
Q3. How to center an element both horizontally and vertically in CSS?
To center an element horizontally, you can use margin: 0 auto;. To center an element both horizontally and vertically, you can use techniques like Flexbox or CSS Grid. For Flexbox, you can use display: flex; on the parent container and align-items: center; justify-content: center; to center the child element. For CSS Grid, you can use the grid property and place-items: center; on the parent container.
Q4. What is the box-sizing property in CSS, and how does it affect sizing?
The box-sizing property determines how an element’s total size is calculated. The default value is content-box, which calculates size based on the content, excluding padding and borders. Setting it to border-box includes padding and borders in the size calculation, making it easier to control the size of elements in a predictable way.
Q5. How to make an element responsive to different screen sizes using CSS?
To make an element responsive, use relative units like percentages or viewport units (vw and vh) for sizing properties. Additionally, you can use media queries to apply different styles based on the screen’s width or height. This allows you to create designs that adapt to various devices, such as desktops, tablets, and mobile phones.