![]() |
CSS units are used to define the size and layout of elements on a web page. They are used in various properties such as width, height, and font sizes, and choosing the right unit can be challenging. This article will discuss the differences between px, %, and em units in CSS, and provide examples to illustrate their appropriate usage. Understanding CSS Units: px, %, and em1. Pixels (px)Pixels (px) are an absolute unit representing a single pixel on the user’s device screen. They provide precise control over the size of elements. Example: The below CSS properties change a button width to 100 pixels and height to 40 pixels. button {
width: 100px;
height: 40px;
} Usages
Percentage (%)Percentage (%) is a relative unit representing a percentage of the containing element’s size. It is useful for creating responsive designs. Example: Sets the container to 80% width of its parent and adds 10% margin on all sides .container {
width: 80%;
margin: 10%;
} Usages
3. emThe em unit is a relative unit based on the font size of the element’s parent element. One em equals the parent’s font size. Example: (Sets the h1 heading to twice the size of the parent element’s font size) h1 {
font-size: 2em;
} Usages
Example: The example showcases the units responsive behavior.
Output: ![]() Output Choosing the appropriate CSS unit is essential for creating flexible and responsive web designs. Pixels (px) offer precise control, percentages (%) allow for responsive layouts, and em units provide scalable typography. Understanding the differences and applications of these units will help you make informed decisions in your web design projects. Experiment with the examples provided to see how each unit behaves in different scenarios and enhance your CSS skills. |
Reffered: https://www.geeksforgeeks.org
CSS |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |