</> Code Corner

CSS · CSS Tutorial · Lesson 21

CSS Max-width

Use max-width for flexible layouts.

Setting width on a block element prevents it from stretching, but on a small screen it causes a horizontal scrollbar. max-width fixes this: the element shrinks on small screens.

Example

<style>
.w { width: 500px; margin: auto; border: 3px solid #73AD21; margin-bottom: 10px; }
.mw { max-width: 500px; margin: auto; border: 3px solid #73AD21; }
</style>
<div class="w">width: 500px — causes scrolling on small screens</div>
<div class="mw">max-width: 500px — shrinks on small screens</div>

Keep reading “CSS Max-width”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋