teaching machines

CS 318: Lab 7 – Box Model

February 19, 2018 by . Filed under cs318, lectures, spring 2018.

Dear students,

Let’s stop a moment to reflect upon what we have done so far this semester:

Where are we headed next? Layout. Most everything we’ve done so far has followed the natural document flow, which fills all our elements in from left-to-right, top-to-bottom. We need to break out of this.

Our first step in doing so is to discuss HTML’s box model. In this model, the spatial footprint of every element is determined by a few properties:

Let’s experiment with a few of these properties. I’m going to right-click and Inspect this element with my browser’s developer tools. Then we’ll tweak these properties directly and see what impact they have.

The border, margin, and padding do not need to be uniform. Each side can have a different value. They can be set individually:

margin-top: 30px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 0px;

Or all at once:

// the order is like a clock: 12-3-6-9
margin: 30px 20px 10px 0px;

If you want the same values on the vertical sides and the horizontal sides, you can use an even more compressed form:

// the order is vertical horizontal.
margin: 30px 10px;

Block elements respond as you’d probably expect to these properties, but phrase elements behave differently. For example, the height property has no effect on phrase elements. Same with the top and bottom margin. In general, vertical spacing through these properties is ignored for phrase elements. Top and bottom padding will reveal the phrasal content’s background color, but it won’t affect vertical spacing.

There are several strange quirks about the box model that you should be aware of—eventually. We’ll settle for learning one of these today. When you specify width and height, you are specifying the width and height of the content, not including the padding, border, and margin. This is often not so natural. When calculating layouts, we tend to be more concerned about the dimensions of the framed box—not just the content. We can make CSS behave like this by setting the box-sizing property for all elements:

* {
  box-sizing: border-box;
}

The default is content-box. I encourage you to automatically add this rule to the top of every CSS file you ever create.

Our lab exercises for today will give you a chance to explore the box model.

Here’s your TODO list for next time:

See you then!

Sincerely,

Lab

We have a few goals for today’s lab:

Your task is to create several independent pages with no overarching story.

Boxing

Your first task is to reverse engineer a few boxes whose properties have been customized. Consider the following box:

It is produced with this HTML:

<div class="outerbox"><div id="box0" class="innerbox">I am content.</div></div>

And this CSS:

body {
  background-color: gray;
}

.outerbox {
  margin: 10px;
  background-color: goldenrod;
  display: inline-block;
}

.innerbox {
  display: inline-block;
  background-image: linear-gradient(salmon, salmon), linear-gradient(#1E90FF, #1E90FF);
  background-clip: content-box, padding-box;
}

#box0 {
}

Create files boxes.html and boxes.css. Paste the code given above in these files. These rules color the margin box goldenrod, the padding box blue, and the content box salmon.

Copy and paste the outer-inner div combo to create four more boxes. Change their ids to box1, box2, etc. Add rules for these ids to style the boxes like the ones shown below. Do not modify the existing rules; just add new ones.

Be observant. The relative sizes are more important than the exact sizes. Different browsers handled dotted borders differently. Some show squares, some circles. There’s not much you can do about that.

Countries

Your second task is to create a page of several country “stamps.” Something like this:

We won’t spell out every individual step you should take to achieve this, but we do provide a rough checklist. You will almost certainly need to consult the MDN documentation to understand a few CSS properties.

Ask questions if your results aren’t matching up with the example.

Publish and Validate

Commit and push your work. Verify that all pages have been uploaded by visiting https://USERNAME.github.io/cs318/lab07 in your browser.

Validate your pages with the W3C CSS Validator. Paste in the URLs to your pages, one at a time, and address all concerns raised by the validator. Fix your changes on the local machine, commit, and push until the validator reports no errors or warnings.