CSS
π¨ CSS Width Property
The width property in CSS is used to define the horizontal size of an element.
It controls the width of the content area of an element.
β οΈ By default, width does not include:
- Padding
- Border
- Margin
These are added separately through the CSS Box Model.
π Basic Syntax
width: value;
πΉ Common Width Values
The width property can use several different value types.
1. auto
auto is the default value.
The browser automatically calculates the width based on:
- The parent container
- The content size
- The element type
π Example
div {
width: auto;
}
π‘ The element automatically adjusts its width.
2. Fixed Length Values
You can set a fixed width using units such as:
- px
- em
- rem
- cm
- in
π Example
div {
width: 300px;
}
β The element width becomes exactly 300px.
3. Percentage %
Percentage values are calculated relative to the parent element.
This is very useful for responsive layouts.
π Example
div {
width: 50%;
}
β The element takes up half of its parentβs width.
4. initial
Resets the property to its default browser value.
π Example
div {
width: initial;
}
5. inherit
The element inherits the width value from its parent.
π Example
.container {
width: 80%;
}
.box {
width: inherit;
}
β .box inherits the 80% width from .container.
π‘ Width Property Example
π HTML + CSS Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Width Property</title>
<style>
.container {
width: 800px;
background-color: lightgray;
}
.box1 {
width: auto;
background-color: lightcoral;
}
.box2 {
width: 400px;
background-color: lightblue;
}
.box3 {
width: 50%;
background-color: lightgreen;
}
</style>
</head>
<body>
<div class="container">
<div class="box1">width: auto</div>
<div class="box2">width: 400px</div>
<div class="box3">width: 50%</div>
</div>
</body>
</html>
π Explanation
| Element | Width Behavior |
|---|---|
| Color type .box1 | Automatic width |
| Color type .box2 | Fixed width (400px) |
| Color type .box3 | 50% of parent width |
π§± min-width
The min-width property sets the minimum width an element can have.
Even if the screen becomes smaller, the element will not shrink below this value.
π Syntax
min-width: value;
π Example
div {
min-width: 300px;
}
β The element will never become narrower than 300px.
π‘ Usage Example
<div style="min-width:300px; background:lightblue;">
This box will never be narrower than 300px.
</div>
π§± max-width
The max-width property sets the maximum width an element can have.
Even on very large screens, the element will not exceed this size.
π Syntax
max-width: value;
π Example
div {
max-width: 800px;
}
β The element will never grow wider than 800px.
π‘ Usage Example
<div style="max-width:800px; background:lightcoral;">
This box will not exceed 800px in width.
</div>
βοΈ Using width, min-width, and max-width Together
These properties are commonly combined in responsive web design.
They allow layouts to remain flexible while staying within size limits.
π Examples
.container {
width: 80%;
min-width: 300px;
max-width: 1200px;
background-color: lightgreen;
}
π Explanation
- Width is 80% of the page
- Minimum width is 300px
- Maximum width is 1200px
This creates a responsive but controlled layout.
π‘ Practical Responsive Example
π HTML + CSS Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Min Width and Max Width</title>
<style>
.container {
width: 80%;
min-width: 300px;
max-width: 1000px;
background-color: lightgray;
padding: 20px;
margin: auto;
}
p {
background-color: lightblue;
padding: 10px;
}
</style>
</head>
<body>
<div class="container">
<p>
This box expands and shrinks with the screen size,
but never becomes smaller than 300px
or larger than 1000px.
</p>
</div>
</body>
</html>
