The Box Model Challenge

At this poing you should be able to create a simple HTML document, link it to external stylesheets, commit your work, and push your work up to GitHub. If you are unable to do this please review the previous posts. Hopefully you have taken things a little further, and spent some time playing with HTML/CSS. If not then get ready, because you are about to!

The Box Model

Each element in an HTML document is represented by a rectangular box. This box can be provided with a host of properties via CSS. The most notable (and the ones we will focus on for now) include:

  • width / height

  • background color / images

  • borders

  • margins

  • padding

These properties can be visualized in Figure 1. The background property is represented by the green color.

Box Model
Figure 1. The Box Model

It is very helpful to keep this image in mind when working with elements on the page, especially as positioning and alignment become increasingly important. Something that may not be immediately apparent is that when we say, "each element in the HTML document is represented by the box model," this includes elements that contain other elements. This outer element is often called a wrapper, and its children, the elements it contains, will appear in the content area shown in Figure 1.

Often we will use wrappers to help with positioning and grouping of its child elements on the page. This allows us to have discrete sections. Think header, footer, sidebar, etc. Very often this parent (wrapper) element will be the <div>.

Before folks start lighting the torches, I should mention there are many more potential wrappers, especially for big site sections. HTML5 has specific wrappers for a lot of the site sections you may build out like <header>, <footer>, <nav>, and even <section>. I’m blatantly ignoring them for now, and only focusing on the more generic <div> to try and avoid information overload. Don’t worry, HTML5 will be adressed once everyone has their legs, so to speak.

A simple example may help clarify some this box model and wrapper business:

HTML:

  <div class="call-to-action">
    <h2>Step Right Up!</h2>
    <p>Hot Chili here!</p>
    <p>Come get your piping hot <strong>Texas Style</strong> Chili!</p>
    <a href="https://www.youtube.com/watch?v=eM1pnZ5omyw" class="button" target="_blank">BWAH?!</a>
  </div>

CSS:

.call-to-action {
  width: 250px;
  margin: 50px auto 0;
  border: 10px solid #CCCCCC;
  background-color: #EFEFEF;
  padding: 25px;
  overflow: auto;
}

.call-to-action h2 {
  margin-top: 0;
}

a.button {
  background: #AAFFAA;
  border: 2px solid #00AA00;
  color: #005500;
  font-weight: bold;
  text-decoration: none;
  padding: 7px;
  float: left;
}

This renders to something like this.

This is a good time to mention a very special tool that’s just sitting in your browser. If you have not discovered this yet, you should know about the developer tools. When you are viewing the rendered output of the above example in your browser, right click on an area of interest (try the larger grey box) and select inspect element. A Window pane will appear at the bottom of the browser. This is FULL of useful information! On the left you can see the HTML, and on the right the correspending CSS. Hover over the HTML elements on the left, and you can see a visual representation of the properties of the element (margins, padding, etc). Click on an HTML element on the left, and the corresponding CSS will be displayed on the right. You can even edit the CSS here and see how it changes how the document renders. This works very well in Firefox and Chrome, and I have it open constantly when I am doing front-end work. I cannot stress enough how valuable this tool is, and I highly suggest you use it as we progress. Many of the examples going forward I will simply link to, and leave the inspection of it as an exersise for the reader. Further, you can find all of the code for the live examples I am linking to in this repository.

So back to the above example. The a element with the class button is a very simple example of working with the box model properties. No width and height are defined, so default to auto. This means the box will expand or contract to fit the stuff in the content area. A Background and a border is given, with some colors. Notice how the background also fills the padding area? Also note the shorthand for the border definition. There is also a curious float: left, we’ll ignore that for just a second. Our little a.button is wrapped by a div with the class call-to-action. So a.button is a child element of div.call-to-action. Notice how we have added some of our own box model properties to call-to-action, and the child elements all appear in the content section of its box model. Note the shorthand for margins here, padding is similar.

Take some time and really study this. Replicate the code on your local, and mess around with it. What happens if you add more content to the call-to-action wrapper? What happens when the content in the a tag is longer. Play with the CSS and see what variations you can come up with, and how things change.

Floating

We’ve been talking about the box model to build to the idea of positioning. Up to this point we have been dealing with what is called normal flow. Elements are laid out vertically in the same order they are in the HTML document. Sometimes this is all you need, and can work out somewhat nicely (as seen in the previous example.) However this can be pretty limiting, and we will often want more control of how things are positioned when rendered. There are a couple of scemes available to us to accomplish this, but for now we will focus on the float positioning sceme.

Consider our above example. Looks pretty good right? Nice little element centered on the page and what not. But say we want 3 call to action blocks all side by side. What to do? Well first we should add a couple more call-to-action blocks to our HTML. Something like:

<div class="call-to-action">
  <h2>HEYEAYEA!</h2>
  <p>"And he tries!"</p>
  <p>"<strong>Oh my god do I try!</strong> I try all the time... In this institution."</p>
  <a href="https://www.youtube.com/watch?v=eh7lp9umG2I" class="button" target="_blank">NYAH!</a>
</div>

<div class="call-to-action">
  <h2>Is that a pop-tart?</h2>
  <p>I hope no one has a heart attack.</p>
  <a href="https://www.youtube.com/watch?v=QH2-TGUlwu4" class="button" target="_blank">NYAN!</a>
</div>

Now looking at the browser we see our 3 blocks centered, one after another, down the page. If we adjust the CSS on the call-to-action element to something like:

.call-to-action {
  width: 250px;
  margin: 50px 25px 0 0;
  border: 10px solid #CCCCCC;
  background-color: #EFEFEF;
  padding: 25px;
  overflow: auto;
  float: left;
}

We see something very different! The call-to-action blocks line up horizontally. Notice how the right margin of 25px spaces them apart from one another. To get things to center up again, we will need to wrap all of our call-to-action elements in a new div and perform our little margin trick. Play with these floating elements on your local for a while. Change stuff up and see how it effects the float, if at all. Here is a cleaned up version of floating three elements horizontally. Be sure to check out the code for this. There are a couple of "tricks" in there. Pay close attention to widths of wrappers containing floating elements, otherwise you risk elements wrapping (popping down to the next line.)

Floating elements can also be used in conjunction with non-floated elements. This is often used where we want normal flow content to wrap around an element. Inspect this simple example to see this in action. The fill-murray element is the only thing floated here, again note the margins used for spacing.

The Challenge

Some Housekeeping

Before I lay down the details of this challenge, I think we should do a bit of housekeeping of the basic-html repository you have been working with. Rather than having to create a repository for every challenge, I think it may be best just to organize things in folders within a single repository. The structure should be something like my examples repository. Navigate to your repository folder in terminal. First create a folder to preserve the work you have done, call it whatever you would like. Move all of the files you have been working with (except for the README.md) into this newly created folder using the mv command. Now create a folder called box-model-challenge. This is the folder you will use to contain the HTML and CSS for this challenge. It will probably be helpful to have a root index.html file as well. Create it with your editor in the base repository directory (the same place as your readme), have it include a simple list of links to the folders you have just created. See the root file in my examples repository. When you are all done and you perform and ls from your repository directory, you should see something like:

$> ls
previous-work  box-model-challenge  index.html  README.md

If everything looks good do a git add -A, commit with a thoughtful message, then push up to gh-pages.

Here We Go

The objective of this challenge is to recreate Figure 2. in your newly created box-model-challenge folder. Please use and external stylesheet, linked appropriately.

Box Model Challenge
Figure 2. The Challenge

If you click the image it will link you to the full size. A few details:

  • The width of the centered content area is 1000px

  • Remeber the auto margin trick

  • Remeber that the browser will add defaul styles (mostly margins) to certain elements including <body>

  • The header and footer background color is #EEEEEE (but you can make it whatever you would like)

  • The header and footer border size is 3px and its color #CCCCCC

  • paragraph margins are left to browser default

  • margins are mostly 20px with a couple of exceptions

  • borders around floating elements are 2px and have a color of #CCCCCC

  • tip: add overflow: auto to elements that have floating children

  • The green "buttons" should be links (like in the above examples)

  • you can use fill murray or a similar service for the image, or provide your own!

Things don’t have to match exactly, but try to get it close. I don’t care as much about colors or content, so you can make that watever you would like. I’m looking to see that you can implemente some floating elements, structure an HTML document reasonably, and write some fairly clean CSS. Not to mention solving the puzzle of creating a layout to match this one.

You may feel like you’ve been throwin in the deep end a bit. That’s okay! Take your time. If you feel stuck review what we have covered so far, do some googling and find some supplemental material, and of course feel free to ask questions. If I find that everyone is struggling with this I will post some follow ups with more clarification. Otherwise, good luck and have fun!

comments powered by Disqus