Dimensions, Overflow and Visibility
Learning Objectives
- Control width, height, minimum and maximum dimensions.
- Understand auto sizing and percentage dimensions.
- Manage overflow and scrolling.
- Distinguish display, visibility and opacity.
1. Width and Height
.card {
width: 300px;
min-height: 150px;
max-width: 100%;
}Width and height control an element's dimensions. Min and max constraints restrict the resulting size. Auto allows the browser's sizing rules to determine a dimension.
2. Responsive Dimensions
.container {
width: 90%;
max-width: 1100px;
margin-inline: auto;
}
img {
max-width: 100%;
height: auto;
}Percentages depend on the relevant containing block or sizing rules. A percentage height may not behave as expected when the containing block's height is indefinite.
3. Overflow
.box { overflow: visible; }
.clip { overflow: hidden; }
.scroll { overflow: auto; }
.always-scroll { overflow: scroll; }- visible: Overflow can extend beyond the box.
- hidden: Overflow is clipped, without ordinary user scrolling.
- auto: Scrollbars or other scrolling mechanisms appear when needed.
- scroll: The element provides scrolling even when content currently fits, subject to platform behavior.
- clip: Clips overflow without creating a scroll container.
4. Visibility and Display
.removed { display: none; }
.invisible { visibility: hidden; }
.transparent { opacity: 0; }display: none removes the element's box from layout. visibility: hidden preserves its layout space. opacity: 0 makes it transparent but does not by itself prevent interaction or remove it from accessibility APIs.
5. Aspect Ratio
.thumbnail {
width: 100%;
aspect-ratio: 16 / 9;
object-fit: cover;
}The aspect-ratio property provides a preferred width-to-height ratio. object-fit controls how replaced content such as an image fits its box.
6. Min and Max Functions
.content {
width: min(90%, 70rem);
margin-inline: auto;
}
.title {
font-size: clamp(1.5rem, 4vw, 3rem);
}min(), max() and clamp() allow values to respond to constraints and viewport sizes.
Practice Exercise
Create a responsive image card with max-width, aspect-ratio, object-fit and overflow handling. Compare display: none, visibility: hidden and opacity: 0.