My smiling face

Paul Dynowski

Fledgeling Web Developer, All-Around Swell Guy

Facebook Twitter GitHub LinkedIn Email

CSS and the Art of Webpage Styling

The devil doesn't wear Prada, the devil uses absolute positioning

Nov 6, 2015

Welcome to this week's technical blog, all about CSS. In theory, I was supposed to select one of four questions to blog about, but, dammit, I learned all this stuff, so you're going to see me show it off. Here are some quick links to further down the page, should you wish to see one specific item: the box model, use of classes vs. IDs, positioning, and inline vs. inline-block.

What is the difference between margin, border, and padding?

Ah, the box model. Defining the simple way to style an HTML element since...(a quick Wikpedia search tells me) December 16, 1996, when the CSS1 recommendation was first adopted by the World Wide Web Consortium. So, let's take a look at this bad boy:

The Box Model

Now, let's break this image down a little. We can immediately see that, moving outward from the actual content of the element, the padding, border, and margin are, essentially, different layers that can be defined to change the look of an element. The padding and the margin are doomed to remain forever invisible, except for the effect that they have on the placement of elements on the page. The border, however, can be made gloriously visible, in any of many styles (solid, dashed, dotted, etc.). Now, let's play with these things a little. We'll start with a basic text-containing div, with visible border, like so:

#base-div { display: inline-block; background-color: #666; }

Base Div

Now, let's add a visible border, so we can see other changes more easily:

#bordered-div { display: inline-block; background-color: #666; border: 5px solid #0ff; }

Bordered Div

Now, add some padding (say, 10 pixels) to that div:

#padded-div { display: inline-block; background-color: #666; padding: 10px; border: 5px solid #0ff; }

Padded Div

So, we can see that padding increases the space between the content of the element and the border of the element. In this case, we increased the padding equally around the content with the property "padding: 10px"

Next, and finally, we'll look at what happens when we add a margin to the base div. So, we're going to remove the padding, but add 20px of margin, instead.

#margin-div { display: inline-block; background-color: #666; margin: 20px; border: 5px solid #0ff }

Margined Div

So, we see what that has done to our super-fancy text div. The spacing all the way around the border has grown larger - the margin has moved the div down and to the right, and would have moved any elements below or to the right of the div further in that direction, clearing the margin of the element.

So, to make a long story short: (TOO LATE!): the content and the border are the (potentially) visible parts of an element. Padding controls the spacing between the content and the border. Margin controls the spacing between the element and another element. Remember, also, that all three of the properties can be controlled separately for each side of the element.

Return to top

What are the best practices associated with using classes vs IDs?

So. Now we know a little bit about the box model of the elements. The question remains, then, how do we get the styling onto the elements. Generally, when you define an element in your HTML, you can add class or ID attributes to the tag. Then, instead of adding styling via the general tag, you can choose to use the class or ID to add a more specific styling to certain elements. IDs have a limitation, though: they can only be used once per webpage. This means, however, that if you apply styling to an ID, it is very specific - it will apply only to that single element. So, if you have several elements you wish to style in the same manner, you may wish to employ a class - if you apply styling to a class, any element tagged with that class will obtain that styling. To summarize: if you only need to use it once, throw it in an ID; if you'd like to be able to reuse the styling, make it a class.

Return to top

What are the differences between relative, absolute, fixed, and static positioning?

Now you've got your elements, and you've got them styled just like you want. How can you make them go where you want them to? Positioning! So, first, a quick listing/definition of the types of positioning, followed by some code examples.

Now, some examples. First, a statically-positioned div (we're looking at the cyan square on the gray field, here)
.static-div { display: inline-block; background-color: #0ff; height: 5em; width: 5em; }
This is where the HTML interpreter chose to place the cyan square, with no modifications - a static positioning. Next, we'll move on to a relative positioning:
.relative-div { display: inline-block; background-color: #0ff; height: 5em; width: 5em; position: relative; top: 5em; left: 25%; }
So, with relative positioning, you can see that the cyan square has moved relative to where the static positioning would have placed it - in this case, shifted down and to the right. Now, an example of absolute positioning:
.absolute-div { display: inline-block; background-color: #0ff; height: 5em; width: 5em; position: relative; top: 2.5em; left: 50%; }
It should be noted that the background divs have had their position defined (ie, they're not static) so that the absolute positioning of the cyan square would be relative to that background - the nearest ancestor that had been manually positioned.

Finally, we'll look at a fixed div.
.fixed-div { position: fixed; bottom: 0px; left: 0px; height:10em; width: 10em; color: #220679; }
You may have noticed that there's a little cyan square in the bottom left corner of the screen, and it's been there no matter where you are on the page. (Note that the background-color property has been removed from the code sample so that the rest fo the properties all fit within the div). That's how fixed positioning works - it tells the element to go stick to one place in the viewing window and stay there, no matter what.

Return to top

What does it mean to display inline vs using inline block?

And finally, we get to some general display options for your HTML elements. The major options are block, inline, and the bastard child of the two, inline-block.

So, now, to a display of the types. All of the divs are defined in CSS as 5x5 blocks, with a margin of 0.5em.
First up, block:
Block
Block
Note that the elements are stacked, not displayed horizontally.
On to inline:
Inline
Inline

Note that, in this case, the height and width definitions have no effect - the elements take up as much room as they need to inline, and that's it. Note also that the inline element allows the next element to be displayed next to it, on the same line.
Finally, inline-block:
Inline-block
Inline-block

Note that now, again, the height and width definitions are respected as in a block display declaration. Note also that, like an inline declaration, the elements are allowed to display next to each other on a single line.

Return to top

And, that, finally, thankfully, is what I've learned about that in the past week. I hope you've found this entertaining and informative, or at the very least informative. See you next week.