Master width, height, and min/max constraints to control element sizing
Understanding how to control the width and height of elements is essential for creating responsive, well-structured layouts. CSS provides multiple properties to set exact sizes, minimum and maximum constraints, and responsive dimensions.
The width property sets the width of an element's content area.
/* Absolute units */
.box {
width: 300px; /* Pixels */
}
/* Relative units */
.container {
width: 50%; /* Percentage of parent */
width: 20em; /* Relative to font size */
width: 30rem; /* Relative to root font size */
}
/* Viewport units */
.hero {
width: 100vw; /* 100% of viewport width */
width: 50vw; /* 50% of viewport width */
}
/* Auto (default) */
.element {
width: auto; /* Browser calculates width */
}
The height property sets the height of an element's content area.
/* Absolute units */
.box {
height: 200px; /* Pixels */
}
/* Relative units */
.sidebar {
height: 100%; /* 100% of parent height */
}
/* Viewport units */
.hero {
height: 100vh; /* 100% of viewport height */
height: 50vh; /* 50% of viewport height */
}
/* Auto (default) */
.content {
height: auto; /* Height based on content */
}
For percentage heights to work, the parent element must have an explicit height set. Otherwise, use vh units or set parent heights.
The max-width property sets the maximum width of an element. The element can be smaller but never larger than this value.
/* Responsive container */
.container {
width: 100%;
max-width: 1200px; /* Never wider than 1200px */
margin: 0 auto; /* Center it */
}
/* Responsive images */
img {
max-width: 100%; /* Never overflow parent */
height: auto; /* Maintain aspect ratio */
}
/* Flexible box */
.card {
width: 90%;
max-width: 400px; /* Cap at 400px */
}
The min-width property sets the minimum width of an element. The element can be larger but never smaller than this value.
/* Prevent element from being too narrow */
.button {
min-width: 120px; /* At least 120px wide */
padding: 10px 20px;
}
/* Flexible with minimum */
.sidebar {
width: 20%;
min-width: 250px; /* Never narrower than 250px */
}
/* Input fields */
input[type="text"] {
width: 100%;
min-width: 200px; /* Minimum usable width */
}
The max-height property sets the maximum height of an element.
/* Scrollable container */
.comments {
max-height: 400px;
overflow-y: auto; /* Add scrollbar if needed */
}
/* Dropdown menu */
.dropdown {
max-height: 300px;
overflow: auto;
}
/* Modal content */
.modal-body {
max-height: 80vh; /* Max 80% of viewport height */
overflow-y: auto;
}
The min-height property sets the minimum height of an element.
/* Full viewport height minimum */
.hero {
min-height: 100vh; /* At least full screen height */
}
/* Flexible section */
.section {
min-height: 400px; /* Minimum height for content */
padding: 40px;
}
/* Card with minimum height */
.card {
min-height: 200px; /* Never shorter than 200px */
}
You can combine multiple dimension properties for flexible and responsive designs.
.container {
width: 90%; /* Flexible width */
max-width: 1200px; /* Don't get too wide */
min-width: 320px; /* Don't get too narrow */
margin: 0 auto; /* Center horizontally */
}
.card {
width: 100%;
max-width: 400px;
min-height: 300px;
max-height: 600px;
overflow: auto; /* Scroll if content exceeds max-height */
}
img {
width: 100%; /* Fluid width */
max-width: 100%; /* Never overflow container */
height: auto; /* Maintain aspect ratio */
}
.hero {
width: 100vw;
min-height: 100vh; /* At least full screen */
max-height: 1200px; /* Cap at 1200px */
}
The aspect-ratio property maintains a specific width-to-height ratio.
/* Square */
.square {
aspect-ratio: 1 / 1;
width: 200px; /* Height will be 200px */
}
/* Widescreen (16:9) */
.video {
aspect-ratio: 16 / 9;
width: 100%;
}
/* Portrait (3:4) */
.card {
aspect-ratio: 3 / 4;
width: 300px; /* Height will be 400px */
}
.main-content {
width: 90%;
max-width: 1140px;
min-width: 320px;
margin: 0 auto;
padding: 20px;
}
.card {
width: 100%;
max-width: 350px;
min-height: 400px;
padding: 20px;
margin: 10px;
}
.sidebar {
width: 250px;
min-width: 200px;
height: 100vh;
overflow-y: auto;
}
.button {
padding: 12px 24px;
min-width: 120px;
height: 44px; /* Touch-friendly size */
}
.modal {
width: 90%;
max-width: 600px;
min-width: 300px;
max-height: 90vh;
overflow-y: auto;
}
Q1: Which property prevents an element from becoming too wide?
Q2: What does 100vh represent?
Q3: How do you make an image responsive?
Q4: What is the purpose of min-height?
Q5: Which unit is relative to the root font size?
max-width: 100%; height: auto;width: 90%; max-width: 1200px; margin: 0 auto;height: 100vh or min-height: 100vh