Last week we experimented with HTMl and dipped our toes into CSS and styles. In your final projects, much of your coding time will be spent trying to get your site to look the way you want it to. If you have a firm understanding of CSS (and of where to look for more info when you’re having trouble!), this will be a lot easier.

You already know about the Codeacademy CSS offerings. Two other helpful tutorial sites are shayhowe and Learn Layout, to which I’ll be referring later; and there are many other resources available on the web.

CSS Basics

CSS is the Cascading Style Sheets Language; it is a powerful tool for controlling the appearance of web pages (and sometimes other documents, too). It works in the following way:

a web page links to a stylesheet

the stylesheet instructs the browser as to how to display various kinds of web pages

the browser then renders the page according to those instructions.

Sometimes when you have a really slow Internet connection, you will see a very ugly, cluttured web page before a smooth, clean, modern-looking website loads. That’s because your browser has loaded the web page but is having trouble accessing the stylesheet. E.g., here is the New York Times with and without CSS:

So, while HTML is responsible for the structure and content of a website, CSS is responsible for its presentation.

CSS Selectors

When you look at a CSS file, you will see it is divided into a bunch of stanzas, like this. Each of these stanzas is called a “selector statement”:

There are plenty of other properties that can be set, in much the same way.

Display: Block vs. Inline

Box Model

One important notion to understand in CSS is the so-called “Box Model”, which accounts for the distribution of whitespace around elements. A lot of the grief you will encounter when designing websites will come back to the box model, so it’s important to learn it:

For each element, you have the actual content, which is surrounded by the padding, surrounded in turn by the border, and once again surrounded by the margin. So for instance, try this code in JSBin:

<div class="fancy"> will look a lot bigger than <div class="simple">, even though they are nominally the same size!

There are two ways to deal with this:

subtract the size of the padding and border from your width values

use the new “box-sizing:border-box” attribute. This will do all the math for you.

Positioning

This is a difficult subject. When you are dealing with lots of different screen sizes and resolutions, and different devices with different fonts installed, etc., it is not trivial to position every element precisely where you want it. You will find a lot of your teeth-gnashing time is spent trying to get various columns of content to line up pretty, center themselves, etc.

Static positioning is the default. A statically-positioned element is said to be “unpositioned”.

Relatively positioned elements are displaced relative to the position they “ought” to be occupying (according to the defaults). But meanwhile, the space it “ought” to be occupying is still considered “taken” by the browser, which won’t put anything else in that space unless you force it to do so.

fixed elements have their position fixed to a spot on the screen (which is called “the viewport” in CSS talk). This is great when you want a fixed header or footer.

absolutely-positioned elements are like fixed elements, only they’re positioned relative to the closest positioned ancestor, usually an element with a position property value of “relative”.

Understanding this well involves fiddling a lot with code; rather than make a bunch of fiddly exercies myself, I’ll direct you the codeacademy positioning exercies, which have a great help system that makes things a little easier.

Positioning 2: Float

So, that’s one way to position elements. Another is to use the float property, which imagines the page flowing like water. The element will “float” left or right, and everything else will flow around it. To stop the flowing – that is, to require the next element to appear below a floated element – that next element will need to apply the clear property, which stops the float.

Changes Comin’ Round Real Soon

All of what you just learned is in the process of changing dramatically. Two new new CSS modules, flexbox and makes a lot of this stuff much easier, but takes some practice to understand.

It turns out that the theme framework we use may be converting to flexbox very soon. So, here is a very quick introduction to flexbox. You may want to look at this cheatsheet, this slightly more verbose one, or this very detailed specification. Meanwhile, there are lots of other new CSS features coming along, which will be supported by more and more browsers as we move forwards. We will keep an eye out for those as we continue; meanwhile, if you want to, you can check out transitions and learn a little bit about animations.