Learn HTML and CSS: Images that scale
Images that scale
Walter had a drawing made of his workbench. On the laptop it looks lovely, but on his phone it sticks out of the page on the right, and he has to scroll sideways to see the radio.
Never wider than the parent
By default an image is as wide as the file or the width attribute says, 800px here. On a phone it then sticks out of the page.
With max-width: 100% it never gets wider than its parent: on a narrow screen it shrinks, on a wide screen it stays 800px. With height: auto the height shrinks along, otherwise it gets squashed.
width: 100% looks the same, but also stretches a small image to the full width, and then it gets blurry. Change the width under the page and watch the drawing follow.
img { max-width: 100%; height: auto;}Try it yourself
Let the drawing scale. On a phone it stands above the text; from 700px the drawing and the text stand side by side, in two equally wide columns with 16px of space.
/* Your CSS goes here. */<link rel="stylesheet" href="style.css"><main> <img class="photo" src="werkbank.svg" alt="A workbench with tools" width="800" height="400"> <p class="slogan">Bring your broken things: we fix them together.</p></main>body { margin: 0; font-family: system-ui, sans-serif; background: #f4f0e8; color: #2a2622;}header { padding: 12px 16px; background: #1f4e5f; color: #ffffff;}header h1 { margin: 0; font-size: 1.5rem;}nav a { padding: 6px 10px; border-radius: 999px; color: #ffffff; text-decoration: none;}main { padding: 0 16px 24px;}.slogan { margin: 16px 0 0; max-width: 40rem;}.benches { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; margin: 16px 0;}.button { display: inline-block; margin-top: 12px; padding: 10px 18px; border-radius: 999px; background: #d1495b; color: #ffffff; font-weight: 700; text-decoration: none;}/* A coloured edge per bench and a tag with a hole: only for the look. */.bench { padding: 14px 16px 16px; border-top: 4px solid #f2a541; border-radius: 14px; background: #ffffff; box-shadow: 0 1px 2px rgb(42 38 34 / 0.08), 0 8px 18px -12px rgb(42 38 34 / 0.35);}.bench h2 { margin: 0 0 6px; font-size: 1.1rem;}.bench p { margin: 0; color: #5b544c;}.bike { border-top-color: #2a9d8f;}.electrics { border-top-color: #f2a541;}.sewing { border-top-color: #d1495b;}.computer { border-top-color: #5b6cae;}.tag { display: inline-block; margin-bottom: 8px; padding: 3px 10px 3px 16px; border-radius: 4px 999px 999px 4px; background: radial-gradient(circle at 7px 50%, #ffffff 0 2.5px, transparent 3px), #ffd166; color: #3d2c00; font-size: 0.75rem; font-weight: 700;}form { display: grid; gap: 6px; max-width: 28rem; margin: 16px 0;}label { margin-top: 8px; font-weight: 600;}input,select,textarea { padding: 8px 10px; border: 1px solid #b9b0a3; border-radius: 8px; background: #ffffff; color: #2a2622; font: inherit;}button { justify-self: start; margin-top: 12px; padding: 10px 18px; border: 0; border-radius: 999px; background: #1f4e5f; color: #ffffff; font: inherit; font-weight: 700;}footer { padding: 12px 16px; background: #1f4e5f; color: #ffffff;}Ready for more?
The full lesson has 5 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson