Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
When building a website, one of your daily tasks is deciding how big or small your cards, sidebars, images, and text containers should be. 📐
Should a profile card have a fixed width of 300px? What happens when a student opens that same page on a small smartphone with a narrow 360px screen? Does the box spill off the screen causing an ugly horizontal scrollbar?
In this lesson, you will master the dimension properties that make websites flexible, modern, and mobile-friendly:
width and heightmax-width and min-widthmin-height and the danger of fixed heightsmargin: 0 autoTo visualize the difference between fixed width and max-width, think of your school lunchbox:
+-------------------------------------------------------------------------+ | FIXED WIDTH vs RESPONSIVE MAX-WIDTH | +-------------------------------------------------------------------------+ | 1. FIXED WIDTH (width: 400px) --> RIGID STEEL LUNCHBOX | | • Takes up exactly 400px of space on every single screen. | | • On a 360px mobile screen, it overflows the bag and tears the zip! | | | | 2. RESPONSIVE (width: 100%; max-width: 400px) --> ELASTIC CLOTH POUCH | | • On a wide study table, it expands comfortably up to 400px max. | | • In a narrow school bag, it smoothly compresses to fit inside! | +-------------------------------------------------------------------------+
width and height (Basic Dimensions)The width and height properties set the horizontal and vertical size of an element.
px): Fixed absolute size (width: 300px;).%): Relative to the size of the parent container (width: 50%;).height ⚠️Here is a golden rule followed by professional frontend developers:
height on containers that hold text!On a wide laptop monitor, 3 sentences of text might fit neatly in a single horizontal line inside a height: 60px box. But when a user views that same website on their mobile phone, the screen is narrow! The 3 sentences wrap into 6 lines. If the box is locked at height: 60px, the text will spill out of the box and overlap other elements below it!
height: auto) or Use min-height!By default, block elements have height: auto. This allows the box to expand downwards naturally as more content is added!
If you want a box to have a minimum starting size, use min-height:
max-width (The Secret to Responsive Layouts ⭐)max-width defines the maximum upper limit for how wide an element can stretch.
The container expands up to 900px and stops. This prevents lines of text from stretching all the way across the monitor, which is tiring for human eyes to read!
Because width: 100% is active, the container seamlessly shrinks down to 768px.
The container smoothly shrinks to 360px without causing any horizontal scrollbars!
margin: 0 autoHow do you take a container with a max-width and place it in the exact middle of the screen?
In CSS, the standard trick is: margin: 0 auto;
auto Work?When you give an element a defined width (like 800px) and set its horizontal margins to auto, the browser calculates the remaining empty space on the screen, divides it equally into two halves, and assigns equal margins to both the left and right sides. The element ends up perfectly centered!
+-------------------------------------------------------------+ | SCREEN (1400px) | | | | [ margin-left: auto ] +---------------+ [ margin-right ]| | (300px) | CARD (800px) | (300px) | | +---------------+ | +-------------------------------------------------------------+
min-width (Preventing Elements from Collapsing)min-width sets the smallest possible width an element can shrink to.
A common example is a website action button:
Even if the button text is only two letters (OK), the button will maintain a comfortable, clickable width of at least 120px.
| Property | Meaning | Common Use Case |
|---|---|---|
width | Exact width of the element | Fixed icons, grid columns |
max-width | Element cannot grow wider than this limit | Responsive containers, images (max-width: 100%) |
min-width | Element cannot shrink narrower than this | Action buttons, form inputs |
height: auto | Height adjusts naturally to fit all content | Text cards, article bodies |
min-height | Element cannot shrink shorter than this | Hero banners, empty dashboard widgets |
margin: 0 auto | Centers a block element horizontally | Main page layouts, modal dialogs |
| What Beginners Do (Don't ❌) | What You Should Do Instead (Do ✅) | Why? |
|---|---|---|
Setting a fixed height: 100px on a card with dynamic text. | Use min-height: 100px; or height: auto;. | Fixed height causes text overflow and overlapping on mobile screens. |
Using width: 800px without max-width: 100%. | Use width: 100%; max-width: 800px;. | Fixed width breaks mobile screens smaller than 800px. |
Trying to center an element with margin: 0 auto without setting a width or max-width. | Always define a width (e.g. max-width: 600px). | Without a defined width, block elements naturally stretch to 100%, leaving no empty space to balance! |
width sets fixed or percentage horizontal size.height on text containers; prefer height: auto or min-height.width: 100% and max-width: ... creates responsive containers that adapt to any screen size.margin: 0 auto centers a block element horizontally by splitting remaining space equally between left and right.min-width guarantees an element will never shrink below a usable size.Test your understanding with these multiple-choice questions:
height: 80px;)?margin: 0 auto;, what does the value auto accomplish?width or max-width for margin: 0 auto to work?min-height: 250px; on a dashboard card guarantee?responsive-card.html.In the next lesson, we will continue your web styling journey with The Box-Sizing Property: border-box vs content-box (The CSS Box Model).
👉 Continue to Next Lesson: The Box-Sizing Property: border-box vs content-box →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| The CSS Box Model: Content, Padding, Border, Margin | The Box-Sizing Property: border-box vs content-box |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.