CSS

CSS (Cascading Style Sheets) is the language that brings web pages to life with colors, layouts, and animations. It works alongside HTML to create visually stunning and responsive designs. Let's explore the power of CSS.

CSS Heigth / Width

31 March 2025 | Category:

CSS height and width properties define the dimensions of an element. You can set these values in absolute (px, cm, etc.) or relative (%, vw, vh, etc.) units.


1️⃣ Setting Width & Height

The width and height properties control the size of an element.

Example:

div {
    width: 200px;
    height: 100px;
    background-color: lightblue;
}

📌 This makes the div 200px wide and 100px tall.


2️⃣ Different Units for Width & Height

CSS allows multiple units to define height & width:

UnitDescriptionExample
pxFixed pixelswidth: 300px;
%Relative to parentwidth: 50%;
vwViewport widthwidth: 50vw;
vhViewport heightheight: 70vh;
emRelative to font-sizewidth: 10em;
remRelative to root font-sizeheight: 5rem;
autoAdjusts based on contentheight: auto;
min-contentShrinks to the smallest content sizewidth: min-content;
max-contentExpands to fit contentwidth: max-content;
fit-contentShrinks or expands to fit contentwidth: fit-content;

3️⃣ width: auto vs Fixed Width

🔹 auto: The element expands based on content
🔹 Fixed width (px, %, etc.): The element stays fixed

Example:

div {
    width: auto;
} 

📌 The div will stretch based on its content.


4️⃣ min-width, max-width, min-height, max-height

These properties control how much an element can shrink or grow.

PropertyDescription
min-widthMinimum width an element can shrink to
max-widthMaximum width an element can grow to
min-heightMinimum height an element can shrink to
max-heightMaximum height an element can grow to

Example:

div {
    min-width: 200px;
    max-width: 500px;
    min-height: 100px;
    max-height: 300px;
}

📌 The div won’t be smaller than 200px or bigger than 500px in width.


5️⃣ height: 100% and width: 100%

When you set width: 100%, it takes the full width of the parent.

Example:

.container {
    width: 100%;
    height: 100%;
}

📌 This will fill its parent’s height & width.

🚨 Note: For height: 100% to work, the parent must have a defined height.


6️⃣ Viewport Units (vh and vw)

Viewport units make elements responsive.

UnitMeaning
1vw1% of viewport width
1vh1% of viewport height

Example:

.fullscreen {
    width: 100vw;
    height: 100vh;
}

📌 This creates a full-screen element.


7️⃣ box-sizing: border-box;

By default, padding and borders increase the total size of an element.

Example:

div {
    width: 200px;
    padding: 20px;
}

📌 Total width = 200px + 20px (left) + 20px (right) = 240px

✅ To keep the width fixed, use:

div {
    width: 200px;
    padding: 20px;
    box-sizing: border-box;
}

📌 Now the width remains 200px, and padding is included inside.


8️⃣ max-width: 100% for Responsive Images

To make an image resize automatically:

img {
    max-width: 100%;
    height: auto;
}

📌 This ensures images never overflow their container.


9️⃣ CSS Grid & Flexbox Effects

In Flexbox and CSS Grid, height & width behave differently.

Flexbox Example:

.container {
    display: flex;
}
.flex-item {
    flex: 1; /* Takes available space */
    min-width: 200px;
}

CSS Grid Example:

.grid-container {
    display: grid;
    grid-template-columns: 1fr 2fr 1fr;
}

📌 1fr means fractional width of available space.


🔟 CSS Width & Height Cheat Sheet

PropertyEffect
width: 50%;50% of parent’s width
width: 100vw;Full viewport width
min-width: 200px;Won’t shrink below 200px
max-width: 600px;Won’t grow beyond 600px
height: auto;Adjusts to content height
box-sizing: border-box;Includes padding in width

🎯 Conclusion

width & height define an element’s size
auto adjusts height dynamically
min/max-width & min/max-height control size limits
vw & vh make elements responsive
box-sizing: border-box; keeps dimensions predictable