CSS
CSS Font Size - px, em, rem, vw, vh
In CSS, there are several units you can use to define font sizes. Each unit has its own characteristics and use cases. Here’s an overview of some common units for specifying font sizes:
¶Pixels (px):
- Pixels are an absolute unit of measurement and are commonly used for font sizing.
- 1 pixel is equal to one dot on the screen.
- Font sizes in pixels don’t change with the user’s settings, making them less flexible for accessibility.
- Example:
p {
font-size: 16px;
}
¶Ems (em):
- The “em” unit is relative to the font size of the parent element. If no parent size is set, it’s relative to the browser’s default font size (usually 16px).
- Ems can be useful for creating scalable and responsive designs.
- Example:
p {
font-size: 1.5em; /* 1.5 times the parent's font size */
}
¶Rems (rem):
- Similar to “em,” but it’s relative to the root element’s font size (usually the
<html>
tag). - Using “rem” can make it easier to maintain consistent typography across the entire page.
- Example:
p {
font-size: 1.5rem; /* 1.5 times the root font size */
}
¶Viewport Width (vw):
- “vw” units are relative to the width of the viewport, or the visible area of the browser window.
- It’s often used for responsive typography to make font sizes adjust to the screen width.
- Example:
h1 {
font-size: 5vw; /* 5% of the viewport width */
}
¶Viewport Height (vh):
- “vh” units are similar to “vw” but are relative to the viewport’s height instead of width.
- This can be useful for creating font sizes that depend on the screen height.
- Example:
h2 {
font-size: 8vh; /* 8% of the viewport height */
}
Here’s a simple HTML and CSS example using these units:
Copy code
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-size: 16px;
}
p {
font-size: 1.5em;
}
h1 {
font-size: 3rem;
}
h2 {
font-size: 4vw;
}
h3 {
font-size: 6vh;
}
</style>
</head>
<body>
<h1>Heading 1 (3rem)</h1>
<h2>Heading 2 (4vw)</h2>
<h3>Heading 3 (6vh)</h3>
<p>Paragraph (1.5em)</p>
</body>
</html>
In this example, font sizes for headings and paragraphs are set using various units. The actual font sizes will depend on the base font size of the <body>
element and the viewport size.
How To Work CSS Backgrounds - background-color,
CSS Position - static, relative, absolute, sticky, fixed
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development