Test your skills: Sizing

The aim of this skill test is to assess whether you understand the different ways of sizing items in CSS.

Note: Click "Play" in the code blocks below to edit the examples in the MDN Playground. You can also copy the code (click the clipboard icon) and paste it into an online editor such as CodePen, JSFiddle, or Glitch. If you get stuck, you can reach out to us in one of our communication channels.

Task 1

In this task, you have two boxes. The first should be sized so that the height will be at least 100 pixels tall, even if there is less content that would cause it to grow to that height. However, the content should not overflow if there is more content than fits in 100 pixels. Test this box by removing the content from the HTML to make sure you still get a 100 pixel tall box even with no content.

The second box should be fixed at 100 pixels tall, so that content will overflow if there is too much.

Two boxes one with overflowing content

Try to update the code below to recreate the finished example:

html
<div class="box box1">
  <p>
    Veggies es bonus vobis, proinde vos postulo essum magis kohlrabi welsh onion
    daikon amaranth tatsoi tomatillo melon azuki bean garlic. Gumbo beet greens
    corn soko endive gumbo gourd.
  </p>
</div>

<div class="box box2">
  <p>
    Veggies es bonus vobis, proinde vos postulo essum magis kohlrabi welsh onion
    daikon amaranth tatsoi tomatillo melon azuki bean garlic. Gumbo beet greens
    corn soko endive gumbo gourd.
  </p>
</div>
css
.box1 {
  /* Add styles here */
}

.box2 {
  /* Add styles here */
}
Click here to show the solution

There are two boxes, the first should be given a minimum height, in which case it will expand to take the additional content but if you remove some content, the box will be at least as tall as the min-height. The second is given a fixed height which will cause content to overflow.

css
.box1 {
  min-height: 100px;
}

.box2 {
  height: 100px;
}

Task 2

In this task, you have a box, which contains another box. Your task is to make the inner box 60% of the width of the outer box. The value of the box-sizing property is set to border-box, which means that the total width includes any padding and border. You should also give the inner box padding of 10% using the width (or inline size) as the size from which that percentage is calculated.

Your final result should look like the image below:

A box with another box nested inside

Try to update the code below to recreate the finished example:

html
<div class="box">
  <div class="inner">Make me 60% of my parent's width.</div>
</div>
css
* {
  box-sizing: border-box;
}
.inner {
  /* Add styles here */
}
Click here to show the solution

Make the box 60% of the container and give it 10% of padding on all sides. All elements already have box-sizing: border-box to save you from worrying about which width you are using:

css
* {
  box-sizing: border-box;
}
.inner {
  width: 60%;
  padding: 10%;
}

Task 3

In this task, you have two images in boxes. One image is smaller than the box, the other is larger and breaking out of the box. If you imagine that the box is responsive and therefore could grow and shrink, which property would you apply to the image so that the large image shrinks down into the box but the small image does not stretch.

Your final result should look like the images below:

Two boxes with images in

Try to update the code below to recreate the finished example:

html
<div class="box">
  <img
    alt="A pink star"
    src="https://mdn.github.io/shared-assets/images/examples/star-pink_256x256.png" />
</div>

<div class="box">
  <img
    alt="Hot air balloons flying in clear sky, and a crowd of people in the foreground"
    src="https://mdn.github.io/shared-assets/images/examples/balloons.jpg" />
</div>
css
img {
  /* Add styles here */
}
Click here to show the solution

The example has an image which is breaking out of the box and one which is smaller than the box, you need to use max-width set to 100% to cause the larger image to grow only as large as the box. If you use width: 100%, the small image will stretch.

css
img {
  max-width: 100%;
}

See also