Moby Week 6 Demo

Download the demo files

  1. Download the zip folder containing the Moby demo files from the Week 6 page of the class website. It should be called moby-start-wk6.zip.
  2. Unzip the folder into TempMedia

Inside the moby-start-wk6 folder you should see a very basic, one page website.

This page is an excerpt from Moby Dick with some notes on the site. The footer contains a few links.

Set up some very basic site styles

  1. Spend some time familiarizing yourself with the html in index.html
  2. Link both the stylesheets to index.html as external stylesheets.
  3. Create some basic type styles for the site in styles.css:
    • Pick a font stack (at least 2 typefaces in addition to a CSS fallback keyword)
    • Set the type size to 1 em and the leading to 1.4
    • Set the h1 type size to 4 em and the leading 1
    • Set the h2 type size 2.25 em and the top margin to 1em
    • Set the h3 type size 1.5 em and the top and bottom margins to 1em
    • Set the bottom margins to 1 em for p, ul, and ol

View screen shot of site »

Stop here, wait for more demonstrations

If you have extra time, start picking a color scheme. There will be background colors for the page, aside, and footer. Also think of a text color as well as a secondary text color.

Basic Page Layout

The most common way to lay out a page in CSS is by using floats. Crafty use of floating allows you to create columns in your page.

When creating a layout in CSS, always specify a width on each element you float. This keeps the layout simple and avoids common browser bugs.

Dimensions for our demo

Start by adding background colors to each major section. You can use the colors listed or a color of your own choice.

This should help you see each section clearly.

View screen shot of site »

Float the aside

The aside block should be a column on the right. Let's give it a width and float it to the right.

aside {
  float: right;
  width: 30%;
}

This should move all the aside content all the way to the right of the page.

View screen shot of site »

Main Content

Notice how the main content now stretches underneath the end of the aside. Our content box is flowing around the floated content. In this case, this effect is not desirable. We want our aside to fill the whole height of the page.

We can add a width to the article block to keep it from flowing around the aside.

article {
   width: 60%;
}

View screen shot of site »

In this case, we have the aside floated to the right. What if we change our minds and want the aside on the left instead?

We could reverse the float by changing the direction our aside is floated.

aside {
  float: left;
}

View screen shot of site »

Notice how the content doesn't move to the right of our aside. This is because even though our aside is floated, it's first in the source order of our HTML file, causing our content to go below it.

In order for our content to display on the right side, we have to float it to the right.

article {
  float: right;
  width: 60%
}

View screen shot of site »

Now that we've added a float to both the main content and aside, when our page is wide enough, the footer sneaks to the top of the page.

This is because all of the content above the footer has now been moved out of the normal document flow.

We can fix this in several ways.

  • Float everything
  • Add a clear to the footer

Both approaches work.

Add a clear to the footer

footer {
  clear: both;
}

This clears floats on both the right and the left of the element.

View screen shot of site »

Float everything

Instead of using the clear property, you can float the footer and give it a width to stretch across the page.

Make sure to remove the clear property.

footer {
  float: left;
  width: 100%;
}

View screen shot of site »

Breathing Room

Everything on our page is hitting the edges of the browser window. We need some breathing room around our pages. Add some margins.

body {
  margin: 5%;
}

That gave us space around our page, but also messed up the footer. Our footer background should stretch to the edge of the window

We need to add a margin to the main content and aside, but not the footer. This is easiest to accomplish by wrapping the main content and aside into one element that we can style.

divs to the rescue

Since we do not currently have an HTML element that contains both the article and the aside. We need to add something do to this to our HTML. This is the perfect opportunity to use a div, because we only need the element as a target for our CSS.

  1. Add a div to the HTML that wraps around both the article and aside, but not the footer. Give the div a class of container.
  2. Remove the 5% margin from the body element.
  3. Add a 5% margin to the container class.
.container {
  margin: 5%;
}

Swap content and aside

Adjust the CSS floats so that the content is on the left and the aside is on the right.

Adjust padding on major elements

Adjust your page so that the aside is on the right.

Add some padding and margins our major elements.

footer {
  color: white;
  padding: 50px 5% 25px 5%;
  margin-top: 100px;
}

aside {
  padding: 1em 3em;
}

View screen shot of site »

Notice how the width of the sidebar is now much wider than it was before. This is because of how the default CSS box model works. The default box model is content-box.

We have added padding to our layout elements: aside and footer. This increases the total width of the footer and aside boxes (padding gets added to content width in the content-box model). Now the total width of our boxes is over 100% of the page width, causing our content to drop down the page.

Messing with the content-box

Using wrapper divs (the "old" way)

One way to work around this is to ensure that we never declare padding or margins on a floated container. We can avoid this by using “wrapper” divs around each of our floated elements.

We can then float the wrappers and apply visual styles to the actual containers.

The HTML:

<div class="aside-wrapper">
  <aside>
    <h3>Notes</h3>
    <ul>
      [...]
    </ul>
  </aside>
</div>

<div class="footer-wrapper">
  <footer>
    <p>Text experts...</p>
    [...]
  </footer>
</div>

The CSS:

.aside-wrapper {
  float: right;
  width: 30%;
}
aside {
  padding: 1em 3em;
}

.footer-wrapper {
  float: left;
  width: 100%;
}
footer {
  color: white;
  padding: 50px 5% 25px 5%;
  margin-top: 100px;
}

This neatly avoids any spacing issues with content-box and also minimizes the math we do. I'm sure most of you can appreciate that. The downside is that it also includes extra markup in our HTML.

View screen shot of site »

Using the border-box reset (the "new" way)

Unless we need to support older browsers, we can now just change the CSS box model we're using to the more intuitive border-box.

When using border-box, the total width of the element is the same as the content width. In practice, border-box will usually cause element widths to behave how we naturally expect them to.

The "border-box reset" is a few rules that cause the default box-sizing of your elements to use border-box. It also attempts to maintain compatibility with other modules that might expect the older content-box setting.

  1. Remove the wrapper divs from your HTML file.
  2. Combine the wrapper styles back into their respective aside and footer selectors in your CSS.
  3. Add the border-box reset to the top of your stylesheet. (http://www.simmons.edu/~grovesd/comm244/notes/week6/box-sizing-property)
html {
  box-sizing: border-box;
}

*, *:before, *:after {
  box-sizing: inherit;
}

View screen shot of site »

Image resizing and aligning

Image Size

A screen shot of the image overlapping the sidebar when it's too big to fit into the page width

When we make our page small enough, our first image breaks the page layout. It can no longer fit into 60% of the page width, so the image overlaps the other content. Obviously this can't happen on a site.

We need to ensure that the image never becomes wider than the container it is supposed to fit into. We can do this with the max-width property.

img {
  max-width: 100%;
}

View screen shot of site »

Image Alignment

Our images are all on the left and breaking up the text flow. We want them to float to one side or the other and flow inline with the text. Floating the img does this.

img {
  float: right;
}

View screen shot of site »

Now the images are flowing inline with the text, but now every image is floated to the right. We probably don't want this.

We want to be able to decide if an image should float, and also to what side. The best approach is to use a generic class. We could also use the class on other elements, like a future pull quote.

  1. Remove the float: right; rule you just added
  2. Create a new class selector called 'align-right' and add a property of float with a value of right.
  3. In your HTML, add the align-right class to the second image on the page.

You should now see the second image aligning to the right of the content while the final two are still on the left.

We should also add some margins to our class so that the image doesn’t bump against the text.

  1. Add a margin on the left side of 1.5em
    .align-right {
      float: right;
      margin-left: 1.5em;
    }
  2. Create a second class selector called 'align-left' that has similar settings except aligns to the left.
  3. Apply either class to each image in the HTML to float it.
  4. Do not apply a class to first image. It should not be floated at all.

View screen shot of site »

Aside

Aside heading

  1. Add a bottom border to the aside heading. It should be 2px thick, solid, and use a transparent black color ( rgba(0,0,0,.4) )
  2. Adjust the line-height to make the border move closer to the heading.

Aside Text

In our aside, we’ve used an unordered list to display each definition. Each definition label is simply marked with strong.

<li><strong>Full Title</strong> Moby-Dick; or The Whale</li>
<li><strong>Author</strong> Herman Melville</li>
<li><strong>Type of Work</strong> Novel</li>

strong is an inline element, so our list items each displays in a line, with the label and definition on the same line. If we wanted the labels to display on their own lines, we could change the HTML to use a block-level element to make our term labels each be on their own lines, but that sounds like a lot of work to change all the HTML.

It's much easier to use the display property in CSS to change the behavior of strong.

aside strong {
  display: block;
}

Now each term label displays on its own line.

  1. Make the term labels bold.
  2. Add a margin to to top of each term label for added definition between terms.
  3. Make the font-size of the term label 1.2em for added emphasis.

At this point, it may feel like all of the sidebar text is a bit large. Since we’re using em’s, which are relative units, we can actually just scale down all of the text of the aside in one declaration. Remember, ems are calculated relative to the size of their parent containers.

aside {
  font-size: 90%;
}

View screen shot of site »

Drop Caps

We can create a drop cap effect at the start of each section by using advanced CSS selectors.

Combine the :first-of-type and :first-letter selectors to target the first letter of the first paragraph in each section.

Once you’ve figured out the selector, you'll need to fine tune the drop cap display. The key properties to adjust are:

  • font-size
  • line-height
  • float
  • padding

This will take some playing around to get the effect you want. I suggest using the Web Inspector to get the effect right.

Think about other visual effects your drop cap might have.

Add other effects

Using the notes from today’s class, add other effects to the page. Include:

  • box-shadow
  • text-shadow
  • border-radius

Also work on refining the typography and colors.

Upload to your web space

When you have completed all steps, upload the entire project folder to your Simmons web space. Link to it from your class website as part of the class 6 activity.