Tag: css



CSS Font Weights Explained

Those not too hip with typography might at one point wonder, “Why are the font weights on a ranking scale from 100 to 900?” Where are those numbers coming from? It was actually an interesting questions brought up to me recently that I hadn’t thought much of. After all, no one’s breaking the internet over not knowing why font-weight can take a numerical value from 0 to 900 and why anyone would want to use the numerical scale over the (arguably) more intuitive, lighter, normal, bold, bolder scale.

There are a surprising, delightful and numerous amount of things about CSS that harkens back to design theory and borrows from typography principles and font-weight’s number system is a pale echo of a type family’s numbering system. Before I get to explaining the numbers, we have to explore a few terms related to typography first.

A type family (or font-family as far as CSS properties go) is a typeface and all of its total variants. Where you might have Helvetica identified as a type family. Inside that family you’ll have members like Helvetica Roman, Helvetica Italic, Helvetica Bold.

Variants within a type family are different styles of that same typeface. For example, the Italics for a typeface is an example of a variant. Small caps style within a type family is another example of a variant. Within CSS, we declare different variants for our font-families by using font-variant.

Weights within a type family are generally considered how thick or thin a typeface appears. On the lowest end of the scale, you have the thinnest of thin characters. On the other side, you have big, bold and thick characters. As you’ve probably guessed by now, font-weight is what you generally use to affect the weight of your web typefaces. And it is with font-weight that we’re going to dive into the numbering system.

The Typography Numbering System

Most large type families have a numbering system that dates back to some of the earliest examples of variant and weight-rich typefaces such as Univers and Helvetica. These typefaces typically have family members that run the gamut of thick, thin, condensed, wide, and so on. So a type numbering system was devised to easily catalog and describe the various members of–let’s say, Univers. Where you could say, Univers Normal, you could also say Universe 430. Why does 430 mean normal?

  1. The first digit identifies the weight of a typeface.
  2. The second digit identifies its amount of compression (ex. condensed, ultrawide, etc).
  3. The third digit signifies the stance (roman or italic).

Typically in the numbering system, 400 is identified as “normal” weight, 30 signifies normal compression, and 0 would mean that the typeface is roman or upright. So that is what we mean by Univers 430. It is of the type family, Universe. It is normal in weight, normal in compression and upright roman in stance.

Another example might be Times New Roman 631. This would mean a Bold and Italic Times New Roman. 600 typically represents bold, 30 would indicate normal compression and the 1 identifies it as an italic variant.

Last example, Helvetica 620. This is Helvetica bold, upright and condensed. The 600 indicates Bold, the 2 is meant to represent condensed, and the 0 identifies this as an upright variant.

So that’s the typography numbering system in a nutshell. Many designers typically prefer to identify the typefaces by name–because many of us are not creatures particularly fond of nondescript numbering systems. I don’t blame you, I’m not hip on it most of the time either. But even I have to admit to its simplified, condensed elegance. However, it should be more obvious now how and where font-weight gets its numbers and considering how many web fonts have large swaths of weights, why not keep in mind what the numbers mean?

100 – The lightest of the light.
200 – Light.
300 – Book.
400 – Normal.
500 – Semibold.
600 – Bold.
700 – Extra Bold.
800 – Ultra Bold.
900 – Black.

Maybe some day CSS spec will catch up to the type family numbering system and we’ll be declaring our font-families as Proxima Nova 431. For more fabulous insights on typography as well as a quick and useful read about typefaces, check out Erik Spiekermann and E.M. Ginger’s, Stop Stealing Sheep & Find Out How Type Works (2nd Edition).

Resources

Fontfeed, It’s All in the (Type) Family
Great resource that showcases a more visual chart of the original font numbering system with Univers’ two digit example. Also takes a jab at Helvetica and I’m always for a bit of that.



Coding a 1 to 6 Column Layout in HTML & CSS

One of the consistent things I notice while teaching web is that a lot of students understandably have a hard time lining up columns, setting up to create columns and understanding the box model and how it relates to positioning columns in HTML and CSS. Since this seems to be a pretty common hurdle for students I encounter, I think it’s safe to say that a lot of people just learning HTML and CSS for the first time find the topic of creating a 1 to 6 column layout to be challenging and confusing too. I also haven’t seen much in the way of online courses for beginners that really digs into using columns, so here’s my take on it.

The Premise

This is going to be a straight up no responsive code tutorial that will assume your layout has 960px of width to work with. This means we’re not worried about how the columns will scale or how they’ll look on a tablet or a phone. I’m only focusing on fixed widths there to keep things simple. Here’s some basic knowledge you should have first:

  • How to set up an HTML document and connect a CSS Stylesheet to it.
  • An understanding of <div> tags and some other basic HTML tags such as <p>, <a>, <strong>, <em>, etc.
  • A basic understanding of CSS and how CSS Rules are written (ex. the difference between declaring a class or ID selector).

Even if you’re missing some of the stuff in the above list, don’t worry. I’ll try to cover as much as I can as we go along. At this stage, you should also have a code editor that you prefer to use. I tend to recommend either Brackets or Sublime Text for HTML and CSS.

The Box Model and Thinking in Rows & Columns

The Box Model is the basic diagram of how browsers interpret elements on a typical website. It also tells you, the coder, how various components of an element can be manipulated. This is the box model:

boxmodel

The element or grey box on the inside represents your content, in other words, your text, images and anything else you place on the screen is considered to be the element. Just outside of the element, the padding wraps around it. Padding moves the element away from the edges of the box container. Outside of padding is border, it resides between padding and the outermost box model component, border is typically used to–well, create a border around your element and its padding. Finally, on the outside is margin. Margin is used to nudge elements away from each other. Every time you have to move one element around the screen, you are probably going to want to use Margin. Margins are also what we’re, incidentally, going to be using to create our columns today.

I want you to take a look at this next diagram which illustrates how columns and rows are laid out, this diagram goes up to 4 columns, but you can pretty much extrapolate, 5, 6, 7, 8, etc. column layouts from this:

columnrowdiagramRows go down, columns go across. Easy. The spacing in between each box in the above diagram represents margins. Before we get to writing our code, remember that when we want to space elements away from other elements, or move elements around on the screen, we tend to use margins.

Setting Up the HTML File

Below, I have a bare bones HTML file with a bunch of dummy text that we’re going to use for our layout. We’ll be building all our columns on the same page to also give you some practice using floats. Fun, huh? I’ve outputted an HTML page for you to download here: Columns Demo Dropbox (HTML/CSS)

Or you can just copy and paste these into your code editor, this is your HTML page to start:

There shouldn’t be anything too surprising to you at this point. We have a basic HTML document set up, within the head tags, I have declared a title for this page and used to associate our HTML document with our Stylesheet. style.css will power how our columns will be laid out, it is sitting in a folder called /css. Within the rest of the HTML, I have a bunch of comments to tell you where we’re going to start dealing with certain columns, and under each comment is dummy text set inside of tags. All the elements that are visible are wrapped inside of .main. It is always a good idea to wrap your site inside of a container such as .main so you have a consistent basis to declare widths, margins, padding, and border. Typically this containing element has the general width of your website, everything falling inside of it therefore takes the containing element’s width and basis their own widths, margins, padding and borders on it. This should become more evident as we work through the demo. This is your CSS document to start, place your CSS document into a folder called /css:

In our CSS file, we’re importing Droid Serif from Google Web Fonts on Line 3, and setting up basic styling for the body and .main elements from Line 5 – 16. If some of that seems unfamiliar to you, don’t stress about it. This is about columns, and those elements and properties are just there to make our file look a little nicer so we aren’t staring at an unformatted document. If you’re going to be re-using this demo for one of your projects, I suggest removing my styling from Line 1 – 16 so you start off with a blank slate.

Go ahead and open up your HTML file in a browser and take a look at it. Our masterpiece should resemble something like this:

See the Pen EjOwYb by Khanh (@ironion) on CodePen.

OK, let’s make our first column happen.

The One Column Layout

We’ll start off easy. Let’s assume you have a one column layout. We’re going to go into our HTML first, and write in a <div> for our first column. On Line 12 of your HTML document, append the code that is there with the following:

<div class="column-1">
<p>Sleep on keyboard. Poop in litter box, scratch the walls cough furball. Sleep on dog bed, force dog to sleep on floor pelt around the house and up and down stairs chasing phantoms poop on grasses and love to play with owner's hair tie so lounge in doorway. Kick up litter chase laser meow all night having their mate disturbing sleeping humans and mark territory.</p>
</div>

Make sure you leave the comment on Line 11 alone, all we’re doing here is wrapping our paragraph in a <div> and declared a class of .column-1 to it. Once you’ve done this, head into your style.css document and under the comment /* One Column */, declare the following CSS Rule:

.column-1 {
  width: 960px;
  background-color: #b3c8cd;
}

So nothing should be shocking you at the moment, we’ve set our single column to be the same width as our .main container. Therefore, we can see, when we preview the page, that our first div will stretch from one side of the .main container to the other side. Now, we’re going to want to modify this a little bit, because the vast majority of the time, you don’t want your content to be touching the edges of your containing <div>. Let’s amend the CSS code for .column-1 a little, to make it look like this:

.column-1 {
  width: 960px;
  background-color: #b3c8cd;
  padding: 20px;
}

Take a look at your page in a browser now, you’re going to notice that our box, which fit so neatly into .main before, now falls outside of it, the instant we added padding:

Adding padding: 20px broke .column-1 out of .main.

Adding padding: 20px broke .column-1 out of .main.

Think back to the box model for a bit, remember that padding is used to move content away from the edges of a container. When we added padding to our .column-1 element, we’re actually adding 20px to the left, right, top and bottom of the container. That means we actually added 40px worth of spacing to our element which was already 960px. So, to resolve the issue with our padding screwing up our element, we need to remove the 40px we used for padding from .column-1’s total width of 960px. Doing so will give us this CSS:

.column-1 {
  width: 920px;
  background-color: #b3c8cd;
  padding: 20px;
}

And .column-1 will fall neatly back into .main:

If you add padding or border to your container, you will need to make room for it by subtracting from the total width it's supposed to take up.

If you add padding or border to your container, you will need to make room for it by subtracting from the total width it’s supposed to take up.

So, you might be wondering, “If I added a border to my element, do I also have to account for that border in the total width? Yes, you do. Let’s say you added a 5px border around your content. That means you need to remove 10px from your total width and will end up with 910px of total width if you want your container to fall within .main. Heck, let’s do that right now:

.column-1 {
  width: 910px;
  background-color: #b3c8cd;
  padding: 20px;
  border: 5px solid #557f84;
}

Check it out and see how we removed 10px (5px for left and right sides of the container) and our element still falls well within our .main container. Let’s move on to creating a simple two column layout now.

The Two Column Layout

In your HTML document, and under the Two Column Layout comment, append the code with this:

  <div class="column-2-left">
            <p>Sleep on keyboard. Poop in litter box, scratch the walls cough furball. Sleep on dog bed, force dog to sleep on floor pelt around the house and up and down stairs chasing phantoms poop on grasses and love to play with owner's hair tie so lounge in doorway. Kick up litter chase laser meow all night having their mate disturbing sleeping humans and mark territory.</p>
  </div>
  <div class="column-2-right">
            <p>Get video posted to internet for chasing red dot mew but jump off balcony, onto stranger's head. Meow spread kitty litter all over house and plan steps for world domination yet favor packaging over toy, spot something, big eyes, big eyes, crouch, shake butt, prepare to pounce then cats take over the world hate dog. Chew iPad power cord if it fits.</p>
  </div>

We’re declaring two divs and wrapping our two paragraphs with them. The first div wraps the first paragraph and we gave it a class of .column-2-left. The second div we gave a class of .column-2-right. The first paragraph now contained inside of .column-2-left will be sitting on the left side of the .main container, and the second paragraph inside of .column-2-right will be sitting on the right side of the .main container. The Logical Order of Things Now, it’s pretty obvious to some, but it needs to be mentioned that you should set up your HTML elements to contain content in a logical order. Typically that means in the order that you would normally read a page out of a book, from left to right, top to bottom. That means that if you had two paragraphs you want to display inside of columns, that the first one should be set on the left because people are likely to read that one first, and the second paragraph should be set on the right because most people are going to read it second. With that having been said, let’s dive into style.css. Add this code under the Two Column comment in style.css:

.column-2-left {
  width: 430px;
  background-color: #e0ddcd;
  padding: 20px;
  border: 5px solid #938d6d;
}

.column-2-right {
  width: 430px;
  background-color: #e0ddcd;
  padding: 20px;
  border: 5px solid #938d6d;
}

When you preview that in a browser, it’s going to look something like this:

Two columns stacked on top of each other.

Two columns stacked on top of each other.

Those two columns are stacked one on top of the other, which is probably not what you’re looking for. The reason why this is happening is because of the concept of Block and Inline elements in HTML. Block elements can be considered as elements that take up the entire width of a container. These elements will always try to stack versus flow. Inline elements will flow with the text and content that you place on the page. Block elements, such as <div>, <h1>, etc. will not automatically sit side-by-side, even when you’ve created enough space for them to do so. You need to manually tell them to sit side-by-side by using one of several techniques. The most commonly seen is the CSS property: float. Float takes four values: left, right, none, and inherit. We’re mostly concerned with float: left and float: right today, as these will allow us to position our columns side-by-side instead of the mess you see in the image above. I want you to apply floats to .column-2-left and .column-2-right:

.column-2-left {
  width: 430px;
  background-color: #e0ddcd;
  padding: 20px;
  border: 5px solid #938d6d;
  float: left;
}

.column-2-right {
  width: 430px;
  background-color: #e0ddcd;
  padding: 20px;
  border: 5px solid #938d6d;
  float: right;
}

Check out our layout in a browser now and you should be seeing something like this:

Much better.

Much better.

Sometimes what you see above will work perfectly fine for you, sometimes, you want a bit of margin in between those two containers so they’re not touching each other. I’m going to apply a 20px margin between the above containers. Remember, every time we add margin, border, or padding to a container, we have to take away from its width. Otherwise, it’ll fall outside of its container. So, since we want 20px of space between .column-2-left and .column-2-right, I need to subtract 10px of width from both of them, then declare margin-right: 10px on .column-2-left and margin-left: 10px on .column-2-right. Just like this:

.column-2-left {
 width: 420px;
 background-color: #e0ddcd;
 padding: 20px;
 border: 5px solid #938d6d;
 float: left;
 margin-right: 10px;
 }
.column-2-right {
 width: 420px;
 background-color: #e0ddcd;
 padding: 20px;
 border: 5px solid #938d6d;
 float: right;
 margin-left: 10px;
 }

Notice I removed 10px from the width on both of the columns to account for the margin we just added? You should see something like this now:

I trimmed some of the body copy to keep my columns a little neater. Feel free to remove or add copy as needed if you're crazy about keeping things the same relative size like I am.

Looking good.

Let’s create a three column layout now!

The Three Column Layout

Let’s tackle our three column layout now. You should be somewhat familiar with the drill, let’s append our HTML and add in our <div> tags for our columns. One for each paragraph, first paragraph is left, second is in the middle and third is going to be on the right. Under the Three Column Layout comment change what is there with this:

<div class="column-3-left">
            <p>Sleep on keyboard. Sleep on dog bed, force dog to sleep on floor pelt around the house and up and down stairs chasing phantoms poop on grasses and love to play with owner's hair tie so lounge in doorway. Kick up litter chase laser.</p>
  </div>
            
  <div class="column-3-middle">
            <p>Get video posted to internet for chasing red dot mew but jump off balcony, onto stranger's head. Meow spread kitty litter all over house and plan steps for world domination yet favor packaging over toy, spot something, big eyes.</p>
  </div>
            
  <div class="column-3-right">
            <p>Shake treat bag stand in front of the computer screen mew play riveting piece on synthesizer keyboard intently sniff hand rub face on everything meowing non stop for food. Destroy the blinds. Parts with tongue then lick owner's face.</p>
  </div>

Diving into our CSS now, add this code under the Three Column comment:

.column-3-left {
  width: 270px;
  background-color: #d5edcd;
  padding: 20px;
  border: 5px solid #67a155;
  float: left;
}

.column-3-middle {
  width: 270px;
  background-color: #d5edcd;
  padding: 20px;
  border: 5px solid #67a155;
  float: left;
}

.column-3-right {
  width: 270px;
  background-color: #d5edcd;
  padding: 20px;
  border: 5px solid #67a155;
  float: right;
}

In the above code, we’re floating the left and middle columns to the left, and the last column to the right. Despite what most would assume, there is no such thing as float: middle. Which means dealing with columns in the middle will have to be settled with margins while using either float: left or float: right. Let’s add in our margins, but let’s think about it a little first. We want 20px of spacing between each column. That means, that .column-3-left is going to declare margin-right: 10px, and .column-3-right is going to declare margin-left: 10px. That leaves .column-3-middle which will need to declare margin-left: 10px and margin-right: 10px in order to cover the space on both sides of it. Also, don’t forget to remove the appropriate width from each of your columns when you’re declaring a margin between them:

.column-3-left {
  width: 256.65px;
  background-color: #d5edcd;
  padding: 20px;
  border: 5px solid #67a155;
  float: left;
  margin-right: 10px;
}

.column-3-middle {
  width: 256.65px;
  background-color: #d5edcd;
  padding: 20px;
  border: 5px solid #67a155;
  float: left;
  margin-left: 10px;
  margin-right: 10px;
}

.column-3-right {
  width: 256.65px;
  background-color: #d5edcd;
  padding: 20px;
  border: 5px solid #67a155;
  float: left;
  margin-left: 10px;
}

Yes, you’re looking at a decimal pixel set of columns up there (also called a floating value pixel) so that all three will fill the width for a .main with 960px of width. That’s how the more precise math breaks down for this particular layout. Now, this is what works if we were really concerned with having the exact same width for each column in a fixed width layout. Half-pixels or floating value pixel can be problematic because they can sometimes cause unintended results, usually while working with images, printing and some tranformations in CSS. So, let’s try to adjust our values a little to avoid the use of the floating value pixels. Normally what I would do is one of three things: 1) Remove the floating value pixels and allow the extra pixels in spacing in the margin 2) apply the full pixels to the left and right columns, 3) add two full pixels to the center column, like this:

.column-3-left {
  width: 256px;
  background-color: #d5edcd;
  padding: 20px;
  border: 5px solid #67a155;
  float: left;
  margin-right: 10px;
}

.column-3-middle {
  width: 258px;
  background-color: #d5edcd;
  padding: 20px;
  border: 5px solid #67a155;
  float: left;
  margin-left: 10px;
  margin-right: 10px;
}

.column-3-right {
  width: 256px;
  background-color: #d5edcd;
  padding: 20px;
  border: 5px solid #67a155;
  float: left;
  margin-left: 10px;
}

It’s not too noticeable to the vast majority of users that the center column is two pixels wider than the left and right columns and you avoid using a decimal value for your pixel widths–if that was a concern. Should you be worried about floating value pixels? For a fixed width layout, that all depends considering that a fixed width layout will always force you to either round up your pixels or settle for the floating value pixels so it becomes a question of how precise you want to be, or if you can settle for knowing your columns may have varying widths by a couple of pixels in certain circumstances, if you need precisely the same width columns and don’t want to use floating value pixels, allow for the spacing in your margins. For responsive layouts you are likely to be using more precise values and can allow the browser to automatically calculate values for you using the calc() function in CSS. So for now, don’t stress too much about using decimal values vs. evening out the values. All right, that’s enough of that. Let’s move on to our four column layout now.

The Four Column Layout

You should know the drill now, this time we have two middle columns, one left and one right. The second and third paragraph under the Four Column Layout comment should be wrapped using a <div> with the same class of .column-4-middle. So under the comment Four Column Layout, add this:

  <div class="column-4-left">
            <p>Sleep on keyboard. Poop in litter box, scratch the walls cough furball. Sleep on dog bed, force dog to sleep on floor pelt around the house and up and down stairs chasing phantoms poop on grasses and love to play with owner's hair tie so lounge in doorway.</p>
  </div>
  
  <div class="column-4-middle">
            <p>Get video posted to internet for chasing red dot mew but jump off balcony, onto stranger's head. Meow spread house and plan steps for world domination yet favor packaging over toy, big eyes, crouch, shake butt. Chew iPad power cord if it fits.</p>
  </div>
            
    <div class="column-4-middle">
            <p>Shake treat bag stand in front of the computer screen mew play riveting piece on synthesizer keyboard intently sniff hand rub face on everything meowing non stop for food. Destroy the blinds. Make meme, make cute face then lick owner's face.</p>
  </div>
            
  <div class="column-4-right">
            <p>Leave dead animals as gifts play time, or scamper, sweet beast. Why must they do that cat snacks damn that dog leave dead animals as gifts sleep nap so stretch. Roll on the floor purring your whiskers off paw at your fat belly. Put toy mouse in food bowl.</p>
  </div>

We’re reusing the column-4-middle class for the second and third paragraphs. This is because I anticipate those two will have pretty much the exact same styling and width values. So it doesn’t make sense to declare a different class for each because that would be extra work to style two classes in exactly the same way versus if we just reused the one class to save ourselves some. Going into our CSS, here’s what we’re going to end up with:

.column-4-left {
  width: 175px;
  background-color: #f5d7ce;
  padding: 20px;
  border: 5px solid #cb785e;
  float: left;
  margin-right: 10px;
}

.column-4-middle {
  width: 175px;
  background-color: #f5d7ce;
  padding: 20px;
  border: 5px solid #cb785e;
  float: left;
  margin-left: 10px;
  margin-right: 10px;
}

.column-4-right {
  width: 175px;
  background-color: #f5d7ce;
  padding: 20px;
  border: 5px solid #cb785e;
  float: right;
  margin-left: 10px;
}

Three CSS Rules to style four columns is a bargain. Outside of that, there’s nothing very surprising here so long as we got our one, two and three column layouts working out, the four column version won’t have any curve balls for us. The only thing to note is the re-use of the middle column CSS Rule to save on time and code we have to write. Let’s head on over and deal with five columns now.

The Five Column Layout

All right, moving things a long a little faster, here’s our HTML. Again, we’re going to be reusing the column-5-middle class because all of those should have the same styling and widths, therefore, no reason to write a different class for each:

  <div class="column-5-left">
            <p>Kick up litter chase laser meow all night having their friend disturbing the sleeping humans and mark this territory.</p>
  </div>
            
  <div class="column-5-middle">
            <p>World domination yet favor packaging over toy, spot something, big eyes, big eyes, purr, crouch, shake butt.</p>
  </div>
            
  <div class="column-5-middle">
            <p>Rub face on everything meowing non stop for food. Destroy the blinds. Make meme, make cute face then lick owner's face.</p>
    </div>
  
    <div class="column-5-middle">
            <p>Rub face on everything meowing non stop for food. Destroy the blinds. Swish tail, pounce and roll then lick owner's face.</p>
    </div>
            
  <div class="column-5-right">
            <p>Why must they do that cat snacks sleep nap so stretch. Mark territory. Chase mice. Roll on the floor purring your whiskers off paw.</p>
  </div>

Here’s our CSS. Nothing surprising again, we’re just setting up more columns now and adjusting the values of our widths to account for padding, border and our margins:

.column-5-left {
  width: 126px;
  background-color: #e7e6f8;
  padding: 20px;
  border: 5px solid #c2beeb;
  float: left;
  margin-right: 10px;
}

.column-5-middle {
  width: 126px;
  background-color: #e7e6f8;
  padding: 20px;
  border: 5px solid #c2beeb;
  float: left;
  margin-left: 10px;
  margin-right: 10px;
}

.column-5-right {
    width: 126px;
  background-color: #e7e6f8;
  padding: 20px;
  border: 5px solid #c2beeb;
  float: right;
  margin-left: 10px;
}

The bottom of your document should look something like this now:

Three, four and five column layout.

Three, four and five column layout.

Now, the six column layout should be a snap at this point. Or at least you should be able to suss it out for yourself. Go ahead and try it out on your own before moving onto the answer below. If it worked, great!

The Six Column Layout

Most of the layouts I’ve encountered don’t go further than six columns, but you never know when you might encounter a seven, eight, nine, ten, etc. column design. The principles we’ve gone over in this tutorial would work for all of them. Just remember to adjust your values based on how much width you have, use your floats, and never write more classes than absolutely necessary. Here’s our HTML for the six column layout:

  <div class="column-6-left">
    <p>Chasing phantoms and love to play with owner's hair.</p>
  </div>

  <div class="column-6-middle">
    <p>World domination yet favor packaging over toy.</p>
  </div>

  <div class="column-6-middle">
    <p>Rub face on everything meowing for food all the time.</p>
  </div>

  <div class="column-6-middle">
    <p>Purr and roll around batting toy. Destroy the blinds.</p>
  </div>

  <div class="column-6-middle">
    <p>Make meme, make cute face bathe self then lick owner.</p>
  </div>

  <div class="column-6-right">
    <p>Why must they do that cat snacks sleep nap so stretch.</p>
  </div>

And here’s our CSS:

.column-6-left {
  width: 94px;
  background-color: #d7ede9;
  padding: 20px;
  border: 5px solid #5e9d92;
  float: left;
  margin-right: 10px;
}

.column-6-middle {
  width: 93px;
  background-color: #d7ede9;
  padding: 20px;
  border: 5px solid #5e9d92;
  float: left;
  margin-left: 10px;
  margin-right: 10px;
}

.column-6-right {
  width: 94px;
  background-color: #d7ede9;
  padding: 20px;
  border: 5px solid #5e9d92;
  float: right;
  margin-left: 10px;
}

Note I allowed for additional width on my left and right column. Just to demonstrate that you can allow for additional spacing between margins in order to void floating value pixels:

.column-6-left {
  width: 93px;
  background-color: #d7ede9;
  padding: 20px;
  border: 5px solid #5e9d92;
  float: left;
  margin-right: 11px;
}

.column-6-middle {
  width: 93px;
  background-color: #d7ede9;
  padding: 20px;
  border: 5px solid #5e9d92;
  float: left;
  margin-left: 10px;
  margin-right: 10px;
}

.column-6-right {
  width: 93px;
  background-color: #d7ede9;
  padding: 20px;
  border: 5px solid #5e9d92;
  float: right;
  margin-left: 11px;
}

All in all, we should have something that looks like this when we’re all done: See the Pen VLVMZE by Khanh (@ironion) on CodePen.

Here’s our finalized HTML:

And our finalized CSS:

If you’d like to download the completed version of this demo here you go: Columns Demo Finished (HTML/CSS)

A Note About Clearing Your Floats

Also keep in mind that our layout doesn’t take into consideration the need to clear your floats when you use them. Clearing occurs after floating as it allows you to remove the float property from subsequent elements that may not need it. Failing to clear floats will cause elements following your floated elements to attempt to sit themselves in the rift of space created by your float properties. To declare a clear, you call the CSS clear property with a value of both. Like this:

.footer {
clear: both;
}

In the above code, your clear: both; declaration will effectively tell the element with a class of .footer to clear and disregard all the floats you declared on the elements that preceded it. In days gone by (read: a couple of years ago), people still created elements specifically geared towards clearing. Nowadays, may coders clear the element following the float if they don’t need the float to affect it.

Hooray! We’re done with our layout, now you should have six rows, each with a varying amount of columns that fall neatly into a 960px width total container. There are column generators and other generation resources you could use for this, but it’s better to learn by writing things from scratch before we resort to automation. With CSS3 out, there are some alternative and neat ways to create columns too, such as using the CSS column property, which I’ll cover in another tutorial. With that having been said, I’ve linked to an older generator that still deals with fixed width columns and another site that will detail how floating and clearing works for positioning elements and more.

Resources

Page Column Generator
Shaye Howe, Positioning Content



Using CSS Attributes Selectors

A CSS Attribute Selector, in its most simple format looks like this: selector[attribute]

Its purpose is exactly as it sounds, it allows you to select an element based upon its attribute and using css attributes selectors is exactly what we’re talking about today. While you might be wondering how or why that’s useful, given that most attributes within HTML can already be selected by just declaring that attribute, the CSS Attribute Selector can extend your selection abilities as well as allow you to specify precise values and custom attributes. That, and the most basic Attribute Selector indicated above is far from the only one that exists out there. In this tutorial, we’re going to explore the different types of attribute selectors and the interesting and useful implementations for each.

Selector[Attribute]

This attribute selector works with an element selector and is typically expressed inside of square brackets. Usually you use this to select the attribute of the element instead of the assigned value. An example:

HTML:

<p>Warm Kitty, Soft Kitty,</p>
 <p>little ball of fur.</p>
 <p title="kitty">Sleepy Kitty, Happy Kitty,</p>
 <p>purr, purr, purr.</p>

CSS:

p[title] {
 color: #de858a;
 font-style: italic;
 font-weight: 700;
 }

In the above code, we declare a <p> with a title attribute (you can check out other types of attributes available in HTML here: MDN, HTML Attribute Reference). Our title attribute takes “kitty” as its value, but that isn’t relevant yet here. Within our CSS, we target the span attribute using p as our general selector and declare [title] immediately after it. We style, therefore, only the line of text containing <p> with an attribute of title. It should look like this:

See the Pen General Attributes Selector by Khanh (@ironion) on CodePen.

Selector[Attribute=”Value”]

This more specialized attribute selector targets a specific attribute assigned to an element with an equally specific value assigned to the attribute. For example: HTML:

<p>Warm Kitty, Soft Kitty,</p>
    <p>little <a href="#ball">ball of fur</a>.</p>
    <p>Sleepy Kitty, <a href="#happy">Happy Kitty</a>,</p>
    <p>purr, purr, purr.</p>

CSS:

a[href="#ball"] {
  color: #de858a;
  font-weight: 700;
  text-transform: uppercase;
  text-decoration: none;
}

In the code above, we declared an href attribute with a value of #ball (which, in our example, won’t seem to do anything when clicked). In our CSS, we declared a as the element selector and specified [href] as the attribute and go on to further specify that href should have a value of #ball before assigning the styling. Note that there is another <a> with an href attribute in our HTML, but it is unaffected. Check out the attribute selector with value example:

See the Pen Attribute With Value Selector by Khanh (@ironion) on CodePen.

Selector[Attribute^=”Value”]

This attribute selector will locate any attribute beginning with whatever the value is specified as. This is useful for detecting things such as keywords within your copy, links, images, downloads, etc. because it looks for whatever the value is at the send of a string. A string is a statement, sentence, a URL, a series of characters like “Hello, how are you?”, or “http://www.google.com” or “3479 Winding Hollow Way”. Here’s an example of usage:

HTML:

<p>Warm Kitty, Soft Kitty,</p>
<p>little <a href="https://en.wikipedia.org/wiki/Warm_Kitty">ball of fur</a>.</p>
<p>Sleepy Kitty, Happy Kitty,</p>
<p>purr, purr, purr.</p>

CSS:

a[href^="https"]:after {
  font-family: FontAwesome;
  top: 0;
  left: -5px;
  content: " \f0a9";
}

The above code declares an <a> with a link directed to the Warm Kitty Poem Wikipedia page. In the CSS, we are using the caret character to indicate that we want CSS to look for anything with a value of “https” if it appears first in a string of an href attribute. Some of you may recognize caret by its other name, the circumflex. Here’s what this one looks like:

See the Pen Attribute Selector Beginning of String by Khanh (@ironion) on CodePen.

Selector[Attribute$=”Value”]

Here’s a good one that’s very useful for targeting specific file types that happen to be linked or loaded through HTML. I’ve seen this one used a lot to indicate different download types (something I really should write into my own site). The $ tells CSS to look for the value at the end of a string. Here’s its implementation:

HTML:

<p>Warm Kitty, Soft Kitty,</p>
    <p>little <a href="http://www.google.com">ball of fur</a>.</p>
    <p>Sleepy Kitty, Happy Kitty,</p>
    <p><a href="#.pdf">purr, purr, purr.</a></p>

CSS:

a[href$=".pdf"]:after {
  font-family: FontAwesome;
  top: 0;
  left: -5px;
  content: " \f019";
}

Up there, we declared a PDF to be downloaded when someone clicks on the purr, purr, purr portion in HTML. Within our CSS, we target specifically the links that end with .pdf. Notice, when you run it, that it doesn’t detect the link that ends with .jpg. Here it is in action:

See the Pen Attribute Selector End of String by Khanh (@ironion) on CodePen.

 Selector[Attribute*=”Value”]

The use of the asterisk here detects if the value appears anywhere within the attribute. You can use this one whenever you might want to highlight a specific word, link or other element. Like this: HTML:

 <p>Warm Kitty, <span title="great to snuggle">Soft Kitty</span>,</p>
    <p>little <span title="cats are like this">ball of fur</span>.</p>
    <p>Sleepy Kitty, Happy Kitty,</p>
    <p><span title="cats always do this">purr, purr, purr.</span></p>

CSS:

span[title*="cat"] {
  color: #663750;
  font-weight: 700;
  border-bottom: 2px solid #663750;
}

See how only the span titles with an instance of the word “cat” are selected and subsequently affected? Here’s the visual:

See the Pen Attribute Selector Contains A Value by Khanh (@ironion) on CodePen.

There are a couple more attribute selectors that I rarely ever use, but exist within CSS. I’ll go over these now.

Selector[Attribute|=”Value”]

The bar targets attributes that contain the value immediately followed by a dash. For example: cart-port, red-wine, salad-dressing. This might be useful if you have attributes that are required to have dashes in them, such as file names and certain Classes and Methods in some frameworks. Here’s an example of this one: HTML:

<p>Warm Kitty, <span title="great to snuggle">Soft Kitty</span>,</p>
    <p>little <span title="cats are like this">ball of fur</span>.</p>
    <p>Sleepy Kitty, Happy Kitty,</p>
    <p><span title="cats-always do this">purr, purr, purr.</span></p>

CSS:

span[title|="cats"] {
  color: #663750;
  font-weight: 700;
  border-bottom: 2px solid #663750;
}

And here it is in action:

See the Pen Attribute Selector Contains A Value by Khanh (@ironion) on CodePen.

Selector[Attribute~=”Value”]

The tilde will target attributes that contain the value in a list, even if that value appears in a space separated list. Like this:

HTML:

<p>Warm Kitty, <span title="great to always snuggle">Soft Kitty</span>,</p>
    <p>little <span title="cats enjoy this">ball of fur</span>.</p>
    <p>Sleepy Kitty, Happy Kitty,</p>
    <p><span title="cats always do this">purr, purr, purr.</span></p>

CSS:

span[title~="always"] {
  color: #663750;
  font-weight: 700;
  border-bottom: 2px solid #663750;
}

Up there, we have the word ‘always’ appearing in the title attribute in two spans. When we run the code, we see that it searches, locates and highlights those spans with always, regardless of where always is on a list. I have a hard time coming up with a reasonable situation to use this one, but if you have any ideas, feel free to let me know! Here it is in an example:

See the Pen Attribute Selector Contains A Value by Khanh (@ironion) on CodePen.

And there you have it, Attribute Selectors in a nutshell. A lot of the time, many of these selections can be done in Javascript. I’m sure the debate is still out there, but I’m of the opinion that CSS is faster and puts less load on the CPU. So any time I can use CSS to accomplish something of equivalent function instead of JS, I tend to take that route.

Resources

30 CSS Selectors You Must Memorize, TutsPlus
MDN, Attribute Selectors
CSS Tricks, [Attribute]



CSS: Using Transform: Translate() for Animations and Position

The CSS Translate Transformation function comes in three flavors: translate(), translateX() and translateY(). It can be used to move elements around on your screen, either statically for positioning or coupled with a transition to create attention-grabbing effects. I’ve seen translate used a lot on images and sections to showcase a pull up or pull out type of effect, similar to this:

See the Pen PureCSS Pull Up by Khanh (@ironion) on CodePen.

Hooray, a pull up.

The Translate() Function

The CSS Transform property can be used with a bunch of different CSS functions to generate a variety of effects. In addition to the translate functions, you can also skew, rotate, scale and even perform some 3D functions. A typical transform property with a translate function is written like this:

.sandwich {
transform: translate(valueX, valueY);
}

In the above declaration, we’re using the transform property on the .sandwich class. The transform property takes a function as a value, that function is translate(). Within the translate function, there are two parameters, the first is a value for the X-Axis, the second is a value for the Y-Axis. Written in a functional declaration, it would look like this:

.sandwich {
width: 400px;
height: 400px;
background: #333;
transform: translate(50px, 80px);
}

The above code will statically position .sandwhich 50px from the left, and 80px from the top of its container. It’s more likely that you’ll be using translate in conjunction with transition to create an animation on mouse hover, or when detecting active, etc. than using translate for static positioning. Here’s what it would look like if we were to use translate() with a transition on hover:

.sandwich {
width: 400px;
height: 400px;
background: #333;
transition: all 0.5s ease-in-out;
}
.sandwich:hover {
transform: translate(50px, 80px);
}

In the above code, when a user hovers over .sandwhich, it will move to the right by 50px, and down by 80px. You can think of the values as adding space away from the top, left point of an element. So if we were to declare translate(100px, 400px), that means it will move the element 100px away from the left-most origin point, and 400px away from the top-most origin point.

The TranslateX() & TranslateY() Functions

TranslateX() and TranslateY() work relatively the same as Translate(), only instead of taking two parameters, they will work perfectly fine with just one. You enter in the parameter you want to move the element by either just the X-Axis or the Y-Axis. It probably goes without saying that you use TranslateX() to move elements on the X-Axis and TranslateY() to move elements on the Y-Axis. Here’s TranslateX() in action:

.oranges {
width: 400px;
height: 400px;
background: #888;
transition: all 0.5s ease-in-out;
}
.oranges:hover {
transform: translateX(50px);
}

The above code will move the element with the .oranges class 50px to the right upon a user hovering their mouse over the element. You can always move content to the left instead of the right by using negative values:

.oranges {
width: 400px;
height: 400px;
background: #888;
transition: all 0.5s ease-in-out;
}
.oranges:hover {
transform: translateX(-150px);
}

The above code will move the element with the .oranges class 150px to the left upon the user hovering their mouse over the element. Now, you don’t necessarily have to use pixels with translate, it will work perfectly fine with other types of values too such as percentages, viewport values and even ems. Here’s TranslateY() in action using percentages:

.apples {
 width: 400px;
 height: 400px;
 background: #7c7c7c;
 transition: all 0.5s ease-in-out;
 }
.apples:hover {
 transform: translateY(15%);
 }

We’re moving the element with the class of .apples down by 15% in the above code upon user mouse hover. And whatever we move down, we can also move up using negative values:

.apples {
 width: 400px;
 height: 400px;
 background: #7c7c7c;
 transition: all 0.5s ease-in-out;
 }
.apples:hover {
 transform: translateY(-25%);
 }

We’re moving .apples up by 25% up there upon user mouse hover. Play around with the values a little and see what you can get out of it. There are, of course, equivalent ways to perform translate using Javascript/jQuery too, but I always find CSS to be more straightforward when it comes to these matters. Here’s a CodePen illustrating the three Translate() functions for your perusal:

See the Pen Translate() Demo by Khanh (@ironion) on CodePen.

Styling a CSS Button Click Animation with Translate()

There’s a lot you can do with translate() and one of the more interesting things is creating a button that animates a press when the user clicks on it. For this tutorial, we’re going to use the active pseudo class to detect user clicks, and translate() to animate the button press. Let’s get started by defining our HTML:

<button>Click Here</button>

We’re creating an HTML button element and inside of that element, we have the text “Click Here”, simple enough. The CSS is where we get a little code heavy. Because button elements come pre-styled already with a lot of options that we might not necessarily want, we’re going to overwrite a lot of it so our button looks a little cleaner and more modern. I’m going for this look:

button

So we’re going to use this CSS on our button to get it styled properly:

 @import url(https://fonts.googleapis.com/css?family=Patua+One);

button {
  display: block;
  margin: 0 auto;
  border: none;
  border-radius: 5px;
  box-shadow: 0px 10px 0px #e88372;
  font-family: 'Patua One', serif;
  text-align: center;
  color: #ffffff;
  font-size: 20px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  text-decoration: none;
  background: #faab89;
  padding: 10px 20px 10px 20px;
  width: 300px;
  transition: all 0.1s ease-in-out;
}

That’s a lot, I know. I’m building a pretty fancy button here so there’s a lot of components that will go into it. Here’s what I’m doing with the button above…

First I import my Google Fonts. Then set my button element to display: block so I can center it using margin: 0 auto on the screen. After that, I remove the default border from the button by declaring border: none, and add some slightly rounded corners using border-radius. I generate a solid shadow using the box-shadow property, normally you will want to write in browser prefixes for border-radius and box-shadow, but I trimmed that code to keep things brief.

After that, we should be in more familiar territory, I’m setting the typeface, the size of the text, the color, aligning the text in the center, forcing the text to be uppercase, removing any text decorations that it might be tempted to add, and adjusting the letterspacing because I didn’t like how it looked initially.

After that I set the color of the button using background, adjust the padding to what I like, set the width of the button, the default cursor when someone hovers over it, and finally, I apply a transition property. The one declarations that you should pay attention to most above is the box-shadow declaration. We’re going to modify that a little bit, so that when a user clicks on the button, we’ll reduce the distance of the box-shadow and make it look like the button is being pressed down.

Most of that you should be familiar with, so let’s put in our active pseudo class and animate this thing upon user click. Funny enough, we already wrote most of our code above, and it doesn’t get any more complicated than that because here’s our translate animation:

button:active {
  box-shadow: 0 1px 0 #e88372;
  transform: translateY(10px);
}

First thing I do is declare the active pseudo class on button. This detects when a user has clicked on the button element then runs our code. Within the declarations, the first thing I did was modify box-shadow, specifically its distance value, changing it from 10px to 1px. This will cause the solid shadow we have under our button to scale down from 10px to 1px upon click and therefore simulating an animated button press.

Next the transform property calls the translateY() function to move the button down by 10px. This is beneficial to us because our original box-shadow was 10px in distance, moving it down 10px lets us keep the button in alignment with the scaling down of the box-shadow so we don’t get any overlapping during the transition. When you’re done, you should have a completed button that looks like this and animates a button press upon clicking:

See the Pen PureCSS Animated Button Click by Khanh (@ironion) on CodePen.

So there you have it, the translate function in CSS used to move elements around and animate a pretty cute button upon click!

Resources

MDN Transform Property Documentation



Wrapping Text With CSS Shapes (Shape-Outside)

CSS Shapes is a relatively new technology being developed by the team at Adobe Platform that not a whole lot of websites use yet, but will undoubtedly become very popular when more browsers support it with consistency. It involves Wrapping text with CSS Shapes. This allow you to flow text and other forms of content along the edges of things like curves, circles, ellipses. This allows designers to break away from the constraint of the rectangle. Flowing content along a curve or shape has been possible in print layouts for a very long time, but up until now was a pain to get working when it came to interactive design.

Browser Support

At the moment, CSS Shapes are limited to browsers at or later than Chrome 37, Safari 7.1, Android Browser 40, iOS Safari Browser 8 and Opera Mobile 24. Adobe has created a Shapes demo on Codepen, so if you were wondering if your browser supports Shapes, you can test it out quickly by going here: codepen.io/adobe/full/Brtdz

If your browser has CSS Shapes support, you should see something like this:

from: codepen.io/adobe/full/Brtdz

from: codepen.io/adobe/full/Brtdz

See how the text flows along the edge of the pottery piece? Pretty neat. I’m looking forward to being able to use Shapes once support becomes more widespread. For now, we’ll go over how to implement CSS Shapes so once all the browsers catch up, you can jump right in. By the way, you can check out up-to-date browser support information for CSS Shapes at: caniuse.com

Demo Files

You are going to need some demo files for this. You can always create your own, but I’ve gone ahead and made some to save you some time. You can grab those demo files by clicking here: Download Demo Files

A completed set of demo files will be provided at the end of this tutorial.

The CSS Shapes Property & Functions

The CSS Shapes property is shape-outside. Where the property will expect functions, typically in the form of shapes such as circles, ellipses or polygons. A typical CSS Shapes property declaration looks like this:

.my-circle {
 shape-outside: circle(50%);
 background-color: #3ea7c2;
 width: 400px;
 height: 400px;
 float: left;
 }

In the above code, you have a class element selector identified as .my-circle. You’re also going to be floating your shape to the left of the container or screen and your element has a width and height of 400px. Ordinarily, without shapes, what you would get is a box that is 400px in height and width, floating to the left and has a blue color. Those things you should already be familiar with. The CSS Shapes property comes into play with the shape-outside declaration. Let’s break it down:

shape-outside calls the CSS property that will define the shape you wish to use in order to flow content outside of the shape.

The shape-outside property will expect the following functions:

circle()
ellipse()
inset()
polygon()

circle() expects the parameters of radius, circle-x, and circle-y. Radius determines how big your circle is, in the code snippet above, we identified the Radius as 50%. In this context, it defines 50% of the width of 400px and the height of 400px. Circle-X and Circle-Y determine the center point of the circle. You can move the center point around to allow for some overlapping effects.

ellipse() expects the parameters of radius-x, radius-y, circle-x, circle-y. Because ellipses are not perfectly round, you need two radii to be defined. One for its x-axis and the other for its y-axis. The other two parameters of circle-x and circle-y are the same as they were for the circle() function, in that they define the center point for the ellipse.

inset() expects a top, right, bottom and left value with a border-radius. Inset is a little confusing at the moment because its functions are already covered by traditional uses of floats, padding and margin. It allows you create rectangle shapes that you will use to wrap content around. I won’t be covering inset much in this tutorial because (at the time of this writing) you are still better off using more traditional and familiar methods to deal with all the features that inset() allows.

polygon() expects parameters of x1, y1, x2, y2, x3, y3, … Because polygons deal with more unusual shapes than circles, ellipses and rectangles, they can accept as many x,y coordinates as you need to define the shape. So you could have a super complicated shape in the form of a tree and still be able to define the essential shape to wrap your text. Typically when defining a polygon, you need a minimum of three points or vertices to define a triangle. There is no maximum, but keeping polygons simple when all you need to do is wrap text will save you from headaches in the long run.

Using CSS Shapes Demo – Circle()

I’ve set up some files we can use for this demo and you can grab them by clicking here. Nothing very complicated, just some images, html files and css files inside of different folders. One folder for each of the four demos we’ll be doing in this tutorial. If you’d rather not use my demo files, here’s the HTML to start off for the circle demo (you will need to do the file setup yourself:

<img class="my-circle" src="circle.png">
 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

We’re going to create a circle shape first to flow some text around it. Open up the circle-shape folder and open the HTML and CSS files in your favorite editor. This is probably the easiest shape to do because it takes in the fewest parameters. So, taking a look at our HTML, we have this defined for our circle demo in HTML.

Note that the image has a class of my-circle which we will be using to define the shape later on. The above HTML shouldn’t be new to most of you, I’m just throwing an image on the screen with a paragraph of dummy text. Let’s take a look at the CSS. You should have this:

.my-circle {
float: left;
}

I have defined a class of my-circle and set it to float to the left. Now, let’s add in our shape declarations. I’m going to start by defining the height and width I want my shape to be. Because my image is 400px by 400px, that’s what I’ll also set my height and width to. If you had a circle image of 235px width by 235px height, you would then probably want to define 235px for width and 235px for height for instance. So now you should have this:

.my-circle {
float: left;
width: 400px;
height: 400px;
}

We don’t have much of a shape yet, so let’s define our shape-outside property so that text will start wrapping:

.my-circle {
shape-outside: circle(50%);
width: 400px;
height: 400px;
float: left;
}

In the above shape-outside declaration, I defined the property shape-outside because I want to wrap the text outside of the shape instead of inside. I’ve defined the shape as a circle so I use the circle() function and set its radius to 50%, which means it starts in the center and will use that point to draw a circle that will have a total width of 400px by 400px. Why use 50% instead of 100%? Because at 100%, you will start in the center of a circle and it will draw out on all sides by 400px and you will end up with a 800px by 800px circle. Instead, we use 50% so the center point will only draw out by 200px and you will then end up with 400px by 400px worth of circle. Remember, the radius starts in the center of the circle. I know how elementary that is, but it’s super easy to forget stuff when we’re focused on learning something new.

So, if everything went off without a hitch, you should see something similar to this:

I won't win any beauty contests, but it works!

I won’t win any beauty contests, but it works!

If you don’t see what I have above, check your syntax and make sure you’re using a browser that supports CSS Shapes. Not all browsers do yet as of this writing so if the shapes aren’t working, think about looking at your demo file in another browser such as Chrome or a recent version of Safari.

Great! We got CSS Shapes working for a circle! Let’s move onto the Ellipse now.

Using CSS Shapes Demo – Ellipse()

Working with the Ellipse() function over the Circle() function isn’t much different, you’re still essentially dealing with a pretty simple shape, only this time, it might take an additional parameter. Open up the ellipse-shape folder you downloaded earlier. Here’s what I have for my HTML for the ellipse demo:

<img class="my-ellipse" src="ellipse.png">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>

The only major point of interest up there is the changing of the image file name, the class to my-ellipse and the additional paragraph of dummy text. Otherwise, it should all be the same. Let’s go into our CSS where I have this:

.my-ellipse {
width: 335px;
height: 494px;
float: left;
}

So I’ve gone ahead and defined the width and height of my ellipse. Note that it’s two rather awkward numbers, but that’s OK. You don’t need perfectly round numbers for shapes to work and many times you won’t be dealing with perfect numbers, though doing so would make things a little easier. Now, even though we have an ellipse instead of a circle, this would still yield acceptable results:

.my-ellipse {
shape-outside: ellipse(50%);
width: 335px;
height: 494px;
float: left;
}

You define shape-outside so the text wraps on the outside of the ellipse and then we declare 50% and let the browser figure out the math by allowing it to divide the width and height by half and draw the ellipse based upon the values we set for those two properties. You will get the text to wrap around the ellipse just fine. But let’s say you want to do things the manual way and clearly define the Circle-X and Circle-Y parameters of the ellipse() function. What we would do is manually divide the width and height ourselves and plug them in like this:

.my-ellipse {
shape-outside: ellipse(167.5px 247px);
width: 335px;
height: 494px;
float: left;
}

This will allow you to define an ellipse around the image without using a straight percentage should you ever have the need to. If everything went well, you’ll get this:

ellipse-shape

Yep, that’s text wrapping around an ellipse all right.

Using CSS Shapes Demo – Polygon()

Polygons are exciting because you’re not just limited to using circles or ellipses, now you can theoretically wrap things around any crazy shape you want. Let’s open up the polygon-shape folder. CSS Shapes open up opportunities for designers to do things that up until now, they’ve only been able to accomplish within the realm of print design–or with a massive amount of time and frustration in code. Anyway, my HTML for the polygon() demo:

<img class="my-polygon" src="polygon.png">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</section>

Straightforward enough by now. Here’s my CSS:

.my-polygon {
width: 382px;
height: 300px;
float: left;
}

OK, let’s apply a CSS Shape to this polygon. Now the image itself isn’t a simple shape like a circle, ellipse or rectangle. It’s got an irregular shape that we want to wrap text around. This means we’re going to have to figure out the points in our coordinate system that we’ll need to declare in order to make the browser understand the shape its working with here.

Polygon() should be used when you don’t have a simple shape to work with and depending upon how detailed you want to get, you may be defining a lot of points. Thankfully, our shape is relatively simple, if we were to boil it down, we could probably define three vertices and be done with it. So before we can write our CSS Shape property, we should determine where our points are. Let’s say you were using the example image provided in the demo files:

382x300

382×300

We can see that the shape is irregular above so using polygon() is the right way to go. So when we go about determining the points of the above image, we’d look in the three major vertices. There is one at the very top, one in the middle and one at the bottom and they are all on the right side. Like this:

poly-ex1

The red dots are the points that you want to determine.

Keep in mind that you can’t get away with defining just three vertices otherwise you have a perfect triangle. What we need to do is define the entire shape. So we need the two points on the left side as well:

That's a total of five points you need to determine.

That’s a total of five points you need to determine.

Now, finding out the points is actually pretty easy. Remembering what order they go in is the harder part. There a multitude of ways to find out the coordinate points of your image. For simplicity’s sake, you can just open up the image in an image editor like Photoshop and locate the points in there using the Info panel. There may be other ways to locate points that are easier and more efficient, but let’s just keep things simple for now.

So when all is said and done, I have the following point values for my sample image, point of origin is in the upper left corner:

poly-ex3

The coordinates are listed X,Y. If were to extrapolate this into pixel coordinates for our demo, it would look like this: 0px 0px, 380px 0px, 303px 167px, 335px 300px, 0px 300px

Now that we have the coordinates for our image, we’ll extrapolate them into a parameter. Polygon parameters list X and Y pairs without a comma in between the two partners of the pair. But when you want to list a new pair, you will need to separate the pairs with a comma. It’s easier to show this than explain it in writing so your polygon() parameters should look like this:

0px 0px, 380px 0px, 303px 167px, 335px 300px, 0px 300px

See how each pair is listed next to each other with just a space to separate them? Then between each pair, there is a comma to denote a new pair is being declared. The above parameters are pulled directly from the image. We start putting in the points starting in the upper left corner and move around the shape in a clockwise fashion. So we start in the upper left, go to the upper right, then the point in the middle on the right, then the point in the lower right, then finally the point in the lower left. In our CSS Shapes code, the final declaration for polygon() should look like this:

.my-polygon {
shape-outside: polygon(0px 0px, 380px 0px, 303px 167px, 335px 300px, 0px 300px);
width: 382px;
height: 300px;
float: left;
}

If all goes well, you should have something that looks like this:

poly-ex4

It’s beautiful! Sorta…

Wrapping It Up

So that was fun and pretty cool. The major thing to keep in mind is that CSS Shapes are still in development and aren’t supported by all major browsers yet. So while this will become something totally awesome and useful in the future, you should avoid using it in production code. There’s no harm in fiddling around with it though and still using it in your projects provided you have a fallback so users who don’t have browser support for shapes don’t end up staring at a mad ugly website.

If you’re looking for the completed demo files, you can find them by click here: Download Completed Demo Files

Resources

Now, if you just want to draw a custom shape and don’t necessarily have an image in mind, check out some of these neat tools for creating CSS Shapes:

Coveloping CSS Shapes Generator
CSS3 Shapes – CSS Portal

And some more CSS Shapes resources:

A List Apart, CSS Shapes 101
CSS Shapes Polyfill



Anatomy of a CSS Rule

One of the basic skills in CSS is knowing the anatomy of a CSS Rule. A CSS Rule is a chunk of code (written in CSS) that styles certain components in HTML, typically its element, class or ID. There are other ways to style HTML components, but they’re a little beyond the scope of this tutorial. The following is a walkthrough of the basic anatomy of a CSS Rule.

cssanatomy1

The above CSS Rule is a very basic block with an HTML Element selector and a declaration block. The Selector highlighted in red is typically the HTML component that you want to target with your CSS to style. In this case, we’re targeting “p”, which is usually considered to be the <p> or paragraph tags in an HTML document. Other types of HTML Elements include <h1> to <h6>, <span>, <ul>, <li>, etc.

The declaration portion of the above diagram represents the Declaration block. Declarations are typically the properties and value pairs that are used to describe specific aspects of the selector that we want to change. In the above example, we’re setting the size of the font to 1.4em (16pt or 22px), and the color to #888888, which is a medium grey color written in HEX.

So those are the two basic components of a CSS Rule, but there’s more to it. Check this out…

cssanatomy2

This is the usual CSS Rule Anatomy diagram you’re going to see. Here we’ve got the Selector and the Declaration as we saw above. Except now we have two other components within the Declaration block. Property in orange represents the type of setting we want to change using our CSS. Types of properties depend upon the type of Selector we’ve targeted as some are compatible with certain properties while others are not. The Selector we’ve used in this instance is a class selector called “.roses”. The dot infront of “roses” is what gives it away as a class, possibly applied to a DIV element like this:

<div class="roses"><p>A Dozen Roses</p></div>

Since we know in our HTML that .roses is a class of a div, we know that we’re dealing with a Block element, and therefore, can use typical Block properties on it such as setting its background color. Some properties work whether you’re dealing with a Block or Inline element.

In blue we have the Value which represents what the property should be set to. For example, background color is being set to #f8f8f8 which is a very light grey. And the font-family property is set to Helvetica, Arial, Sans-Serif fonts. Properties in CSS usually need an accompanying value. Otherwise, the properties won’t know what settings they should apply and will therefore do nothing.

You may have noticed that there are two Curly Braces (I call them fancy brackets personally, but in the interest of not confusing anyone…), they wrap around the Declaration block. This is typically done so CSS understands where to start the declarations for a particular Selector and when it should stop. You will also notice Semicolons after each declaration is completed. This is again done so CSS knows when to stop applying values to a specific property. Finally, you’ll notice the Colon after the property, this is so CSS knows you’re done writing the property and are going to move onto writing in the values for that property.

OK, let’s take a look at one last example…

cssanatomy3

The above example is the same as the previous one except the Selector is now an <a> instead of a class. Our Selector also as a second component to it highlighted in purple. This is a Pseudo-Class. Pseudo-Classes are used to detect the state of a selector. For example, :hover is used to detect when a user hovers their cursor over the selector. There are other types of Pseudo-Classes that are used to wait for user responses too such as :focus which waits for a user to select something. Some Pseudo-Classes aren’t specifically waiting for user input and are instead used to target specific elements within HTML such as :first-child, which locates the first member of a selector type.

So there you have it, a run-through of a CSS Rule’s anatomy, plus some bonus tidbits.

Resources

MDN Pseudo-Classes Documentation



CSS Tutorial: Simplifying :nth-child

I’m going to take a moment to explain a CSS concept that tripped me up when I first heard about it and started using it: nth-child. This is a really basic and quick overview of the nth-child class so it won’t go into any specifics or lots of details about the philosophy behind the pseudo class.

It does assume that you already understand basic CSS terminology (ex. you know what a pseudo class is, you understand how to write CSS). So without further ado.

nth-child sounds like a B-List horror or sci-fi movie, but it is actually a CSS concept that allows us to select multiple elements at once by using numbers and some simple algebraic equations. The :nth-child pseudo class allows you to target specific items, elements and even a chain of elements based on a little bit of math. For now, let’s focus on the simplest nth-child declaration:

ul li:nth-child(4) {
	color: #89f89f;
}

The CSS above uses nth-child to search for the fourth list or LI item and make only that fourth item the color of #89f89f. You can change the number 4 to any other number. For example if you changed the number from 4 to 5, and it will change only the fifth item to become the #89f89f color instead.

Moving on to more complicated nth-child classes we need to do a bit of light math. Most notably; algebra. nth-child pseudo classes allow us to target specific elements inside of another element as we saw above, but it can also allow us to target chains of elements. For example:

ul li:nth-child(3n+1) {  
  color: #89f89f;
}

The above CSS rule will change the color of every third list item to #89f89f. But how is it doing that and what is this crazy algebra formula for?

The formula 3n+1 can also be expressed as (3xn)+1. In this case, you’d have the first selection for every third item. If you’re like me, and think this all sounds way more complicated than it should be and numbers give you nightmares, then don’t worry. Thankfully there’s a trick to figuring out how nth-child works without resorting to too much math.

Here’s the trick: Think of the 3n portion in the above equation as the pattern or how many you want. Think of the +1 portion in the above equation as the first number to start the pattern.

So if we had 3n+1, nth-child will start the pattern at the first item according to the +1 portion, then for every third item according to the 3n portion, it will affect those.

Here’s another example that we can walk through:

ul li:nth-child(4n+2) {  
  color: #6699ff;
}

In the above example, the second list item will have the color of #6699ff according to the +2 portion of the formula. Then every fourth list item will be of the color #6699ff because of the 4n portion of the formula.

Some very awesome people have created a tool called the :nth Tester to illustrate how the formula works. You can access this tool here: :nth Tester

Pseudo classes can throw a few people for a loop, but it is important to understand how they work because they come in handy if you were to dabble in jQuery, work with more complex HTML5 and CSS3 concepts or even while working with other web programming languages.



Coding a Button with CSS Only

When I found out how to create CSS buttons, it was like someone handed me a big, free piece of candy. Finally, no more time spent in Illustrator drawing buttons, variations for buttons and making sure all of them lined up and worked when someone moused over them! So here’s a beginner’s guide to creating some fancy CSS buttons!

First thing’s first, we have to create a link in HTML with a CSS class so we can style our fancy new button. Here’s what my code looks like:

 <a href="http://www.google.com" class="fancy_button">
Go to Google</a>

All right! So we got the first step in making a pretty button. But, if you preview your button right now, it’ll probably look something like this:

Go to Google

Yeaaaah, not the beautiful button we were expecting. That’s where the CSS class that we defined (fancy_button) comes into play. When we declared a class for the HTML link that we made, we’re telling the link to go search in our linked CSS Stylesheet for a class called .fancy_button and pull the styling options for our button from there.

Let’s add some styling to our link to make it more button-like. First I declare the link as a block element. By default, links are inline elements. We are making our fancy_button link a block element instead so we can manipulate different parameters such as background color. So, your CSS should look like this:

.fancy_button {
 display: block;
 }

In the above code, I’m declaring a CSS class named fancy_button so our link will know where its class styles are being referenced. Then I declared a display: block to make the link behave like a block element instead of an inline element. It still won’t look like much if you preview things at this stage. But let’s add a background color and see how that mixes things up:

.fancy_button {
 display: block;
 background: #62869b;
 }

Your button should now look like this:

The button has a background.

The button has a background.

Still doesn’t look like much of a button, right? Part of that is because there are no defined widths or heights for our button. So it assumes an automatic height, and an automatic width. The nature of a block element is to assume that it should take up the entire width of a page unless told otherwise. Let’s tell it otherwise now.

I’m going to give my button a set width so it doesn’t stretch all the way across my page, and I’ll give it a bit more height so there’s some padding between my link name and the edges of the button. Here’s my CSS code now:

.fancy_button {
 display: block;
 background: #62869b;
 width: 200px;
 height: 50px;
 }

And here’s what your button should look like now:

It's slowly coming together.

It’s slowly coming together.

Some problems with our button still, but let’s remedy the most immediate one which is the text of the button sitting in the upper left. We can resolve this issue by using some padding on the text to push it down into the middle and then center aligning it on the button:

.fancy_button {
 display: block;
 background: #62869b;
 width: 200px;
 height: 50px;
 text-align: center;
 padding: 25px 0 0 0;
 }

OK, I added a text-align that centers our text and then some padding to push our link down to the middle. I arrived at the padding amount of 25px at the top by dividing 50 in half. You can do this to other height values too. For example, if you had a button that was 20px high, you’d push your text down by 10px or 20px divided in half. Anyway, here’s what the button looks like now:

Now things are aligned properly.

Now things are aligned properly.

OK, let’s do something about that ugly font and link color. I’m going to go with a web standard to keep things easy, but I’ll definitely do something about that default blue. And while I’m at it, I’m going to get rid of the underline too:

.fancy_button {
 display: block;
 background: #62869b;
 width: 200px;
 height: 50px;
 text-align: center;
 padding: 25px 0 0 0;
 font: 1.2em/12px Verdana, Arial, Helvetica, sans-serif;
 color: #fff;
 text-decoration: none;
 }

I’ve added a font declaration that told my button to display the text as 12px Verdana (and some fallbacks), with a white color and to get rid of that unnecessary underline. Now here’s what things look like:

Text looks a bit better.

Text looks a bit better.

Before we go on to modify what the button will do when someone mouses over it, let’s fancy it up even more with some rounded corners to make it a little more interesting to look at:

.fancy_button {
 display: block;
 background: #62869b;
 width: 200px;
 height: 50px;
 text-align: center;
 padding: 30px 0 0 0;
 font: 1.2em/12px Verdana, Arial, Helvetica, sans-serif;
 color: #fff;
 text-decoration: none;
 -webkit-border-radius: 15px;
 -moz-border-radius: 15px;
 border-radius: 15px;
 }

I added a border-radius of 15px that will affect all four corners to my button, and since border-radius is one of those CSS3 things that are in the works, I’ve included a browser prefix for Webkit and Mozilla-based browsers. Also I’ve noticed the text on our button doesn’t look like it’s sitting quite in the middle. So I increased our padding from the top to 30px. Here’s what our button looks like:

looks more button like, already.

looks more button like, already.

I’m pretty happy with the button right now. So let’s go on and define how the button will react when a user mouses over it. For this, we’re going to need a pseudo class that does something when a user hovers (or mouses) over the button. Here’s my CSS:

.fancy_button {
 display: block;
 background: #62869b;
 width: 200px;
 height: 50px;
 text-align: center;
 padding: 30px 0 0 0;
 font: 1.2em/12px Verdana, Arial, Helvetica, sans-serif;
 color: #fff;
 text-decoration: none;
 -webkit-border-radius: 15px;
 -khtml-border-radius: 15px;
 -moz-border-radius: 15px;
 border-radius: 15px;
 }

 .fancy_button:hover {
 background: #3e5a6b;
 }

I used a pseudo class on my .fancy_button class to tell it to change its background color when someone hovers their mouse over the button. So now when you visit your button, someone hovering over it will change the button’s background color to something darker:

Hovering over the button makes it a darker color.

Hovering over the button makes it a darker color.

That was vastly easier to do than in the old days when you had to work with Javascript, or replace images in your CSS. At this point, you should have a pretty nicely working button. You can take it to the next level too and add shadows, gradients, and even transitions. But maybe that’s a tutorial for another day–or maybe you want to play around with that stuff on your own and see what masterpieces you’ll come up with.

Click here to download the source code for this tutorial.

Good luck on your button creation adventures!



Colors on the Web RGB vs. HEX vs. HSLa

For people just starting out in web design, finding out about all the different languages and their various nuances is difficult enough. Add in the fact that web languages change on a daily basis–even plain old HTML and CSS have hundreds of considerations to take into account, and all this stuff can be pretty overwhelming. One of the sources of confusion that I’m often asked about is the difference between RGB and HEX and why someone would want to use one or the other.

The Definitions

Before we get into when you use one or the other, we have to do some defining and for this, we’ll go back to a topic near and dear to my heart: Color Theory.

RGB or Red/Green/Blue is a color model. Similar to the more familiar RYB (Red/Yellow/Blue) model that most people are more familiar with if they’ve studied the color wheel. RGB is the color model used for websites. We use RGB instead of RYB because it is additive. An additive color model is one that works for illuminated media–which is a fancy way of saying, content that lights itself. Additive can also be thought of as colors that add up to emit light. When you’re using the RYB color model (or even the CMYK model) you should be working with subtractive color. This means that instead of lighting itself up, the content is displayed on a surface where light is bouncing off of it. This is because subtractive colors work by reflecting and absorbing light.

The Difference Between HEX and RGB

Back to the question at hand. This is an example of a HEX color code:

#33743b

This is the same HEX color code in RGB, where red is the first number, green is the second, blue is the third:

51, 116, 59

Having looked at those two, you’re probably wondering why on earth you would ever use RGB, considering the first example is a lot easier to remember and type out. The thing is, both versions have different uses, even if the end result is usually the same.

The one time you may wish to convert from HEX to RGB is when you need to access the opacity or alpha of an element. At which point, you would use RGBA. A typical RGBA value would look like this:

51, 116, 59, 0.65

In the above code, the first three values represent the Red, Green, Blue values and the last value (0.65) represents the alpha. At 0.65 alpha, we are seeing the above colors at 65% opacity.

Then There’s HSLa

HSLa stands for Hue Saturation Lightness alpha. HSLa somewhat resembles RGBA, except the first value stands for our Hue–or color. We declare our colors by looking at the color wheel. The wheel is–obviously–a circle where reds are around the 0 degrees to 360 degrees area, 120 degrees are our greens, and 240 degrees are our blues.

Saturation is expressed as a percentage. When we talk about the saturation of a color, we’re talking about how much grey it has. AT 100% the saturation is all the way up and we are viewing the color fully.

Lightness is also expressed as a percentage and describes how much black or white we have in a color. At 0% we have full black. At 100% we have full white.

Opacity works as a decimal like it does in RGBA, where 1 represents fully opaque, 0 represents fully transparent, and everything in between is partially opaque. For example, 0.45 means that an element is 45% opaque.

A typical HSLa value looks like this:

310, 30%, 20%, 0.65

Writing CSS for HEX, RGBA & HSLa

Here are some examples for how you would write a CSS rule for a paragraph tag using these three values.

First, let’s look at HEX:

p {
color: #25547f;
}

Here’s that same declaration, only in RGBA this time:

p {
color: rgba(37, 84, 127, 1);
}

And here’s the same declaration in HSLa:

p {
color: hsla(209, 55%, 32%, 1);
}

Which one to use?

HEX is very attractive because it’s short and simple to remember and type out. But HEX might not work for you in all situations, which is when you may wish to consider one of the other two methods. Both of which have their pluses and minuses.

RGBA is well-known and supported in older versions of Internet Explorer (9 and older). It has an additional field for alpha values which come in handy when you want to work with opacity.

HSLa is a newer, more intuitive way to work with colors. Unlike RGBA where we have to mesh some numbers around to get the color we want, we can grab the Hue then work with percentages to get the saturation and lightness levels that we need. HSLa also includes an alpha value for opacity.

The one thing to note regarding HSLa is that, like I said above, it isn’t supported by older versions of Internet Explorer. So if you were going to use HSLa, you’ll need to rely on some fallback codes and this could mean bringing in RGBA or HEX. A typical fallback code for an HSLa statement would look like this:

p { 
    color: #25547f;
    color: hsla(209, 55%, 32%, 1);
}

In the above code, the first declaration is a HEX value that’s meant to work as our fallback. The second declaration is for HSLa and will be used for browsers that can support it.

Some stuff to make things easy

Some designers/developers use programs that favor HEX over RGBA, and some even use programs that have built-in tools for HSLa, but what if you find yourself needing to convert one to the other? Here are some online tools to help you convert these values back and forth:

HEX to RGB: Converts HEX values into RGB.

RGB to HEX: Converts RGB to HEX.

HSL Color Picker: Can convert HEX or RGB into HSL.



CSS Tutorial: Simple Text Transitions with CSS3

With CSS3 comes some very fancy effects including text transitions that you can use to create a bit of animation and interest in your websites. Some CSS3 transitions work with pseudo classes to create an effect when a user hovers their mouse over a link or image. I will only be covering transition effects for links or text in this post.

What I mean when I talk about a text transition is something like this:

Hover Over Me!

See how the colors shift and change gradually? Good thing you can easily create a simple text transition like that with almost no code at all. Here’s the most basic text transition you can write in CSS:

a { 
   color: #888777; 
   -webkit-transition: color 1s ease-in;
   -moz-transition: color 1s ease-in;
   -o-transition: color 1s ease-in;
   -ms-transition: color 1s ease-in;
   transition: color 1s ease-in;
} 
a:hover {
   color: #668899;
}

The above chunk of shorthand code has a couple of things going on. First are the two link rules, one sets the styling for the general link, the other one sets the styling for the link when someone hovers over it. You’ll notice, rather obviously, that we’re applying the transition effect on the general link rule.

The Anatomy

When we declare a transition, we have to target the specific browsers that contain the functionality for it. In the above code, I’ve targeted three major browsers and I have a general “transition” declaration at the end. In this case, “-webkit-transition” refers to Webkit based browsers (Chrome, Safari). “-moz-transition” refers to Mozilla browsers (Firefox), -ms- is Internet Explorer, and “-o-transition” refers to Opera.

The four values in a transition declaration are the value to be transitioned, duration of the transition, type of transition, and transition delay.

For the value, we are declaring that the color should be affected here. For the duration, we are saying it should only last about a second. You can draw this number out longer, but a 5s transition for a hover over link can get pretty silly. Finally, we declare the type of transition, which in this case, is ease-in.

Breaking it Down

Let’s break down the above shorthand CSS declaration and view transition in its basic components. If we were to dissect the above chunk of code into its base elements, it would look like this:

a { 
   color: #888777;
   -webkit-transition-property: color;
   -webkit-transition-duration: 1s;
   -webkit-transition-timing-function: ease-in;
   -webkit-transition-delay: 0;

   -moz-transition-property: color;
   -moz-transition-duration: 1s;
   -moz-transition-timing-function: ease-in;
   -moz-transition-delay: 0;

   -ms-transition-property: color;
   -ms-transition-duration: 1s;
   -ms-transition-timing-function: ease-in;
   -ms-transition-delay: 0;

   -o-transition-property: color;
   -o-transition-duration: 1s;
   -o-transition-timing-function: ease-in;
   -o-transition-delay: 0;

   transition-property: color;
   transition-duration: 1s;
   transition-timing-function: ease-in;
   transition-delay: 0;
}

You can see how all the different declarations of transition work together now that they’re broken up from the shorthand declaration. However, I don’t really want to spend all day writing out my transitions in long chunks like that, so the short hand version is more efficient and works the same way.

Delays

Notice the addition of a “transition-delay” declaration that is set to 0 in the broken down declarations. You can use transition delay to tell an element to wait before beginning a transition. This allows you to stagger your effects for an interesting visual. To declare a delay in a shorthand declaration, simply add the delay value after the duration value. Like this:

a { 
   color: #888777; 
   -webkit-transition: color 1s 2s ease-in;
   -moz-transition: color 1s 2s ease-in;
   -ms-transition: color 1s 2s ease-in;
   -o-transition: color 1s 2s ease-in;
   transition: color 1s 2s ease-in;
}

The above chunk of code tells my link to wait two seconds before it starts to run the ease-in transition for my link. Not very practical for a link, but if you were working with multiple images or pop-ups, this could come in handy!

Types of Timing Transitions

Ease-in is only one of the timing transitions available. There’s a bunch more with W3 Documentation. Understanding and reading all that could take a full day. Here’s a quick run-down:

Ease: Begins an effect faster than it ends the effect (slower).
Ease-In-Out: Begins an effect slower than it ends the effect (faster).
Ease-In: Starts an effect slowly.
Ease-Out: Finishes an effect slowly.
Linear: Executes an effect at one speed.

Any one of these timing transitions can be substituted in for the above effect like this:

a { 
   color: #888777; 
   -webkit-transition: color 1s ease-in-out;
   -moz-transition: color 1s ease-in-out;
   -ms-transition: color 1s ease-in-out;
   -o-transition: color 1s ease-in-out;
   transition: color 1s ease-in-out;
} 
a:hover {
   color: #668899;
}

The above code will modify my link so that an ease-in-out timing transition will be applied to it instead of ease-in.

To add a level of complexity to your transitions, you can use the power of cubic beziers. Most of the time, if you’re just developing websites, you probably won’t need to mess with cubic beziers much. But, if you do and because I am barely competent with math (d’aww), I will let someone who is much better with numbers do the hard work to explain those. Check out The Art of Web for their excellent cubic bezier documentation.

There is also a super cool website where you can check out the various types of transitions and cubic beziers here: Cubic-Bezier

Transitions also work with images and can create some really cool effects. In fact, you can make entire slideshows with CSS transitions and images. But we’ll be covering that in another tutorial that’s coming soon!



How to Remove the Smiley Face on WordPress

So you’re working with WordPress, possibly editing a theme, maybe making your own, or applying a theme you just downloaded. Everything is working OK and looking fine until you get to the bottom of the page and notice this:

Smile!

Smile!

What the heck is that smiley face doing there? At this point, you might be going through all your theme’s options or looking through your CSS and PHP files trying to figure out what the heck is generating that thing and how you get rid of it.

Relax. The smiley is actually a part of WordPress’ stats function. Basically they load a small smiley image onto your page so WordPress can collect information about how many visitors you have, which of your posts your visitors are favoring and so on. In fact, I’ll let WordPress field this question:

In order to tell you all the cool stats about how many visitors you’ve had, which of your posts are most popular, and how people get to your site, we need a way to track things.

We’re able to collect this information by loading a small image to your page when someone looks at it.

And we chose a small smiley 🙂

Source:WordPress.com

OK, that’s just fine and dandy, but what if that smiley is causing a lot of problems for your theme and you just want to get rid of it? Easy!

First, go into your WordPress installation’s Dashboard. Then click on Appearance > Editor. Most themes out there will default to show you the stylesheet.css file in your Editor. If you don’t see stylesheet.css immediately, try to find it on the right hand side where it lists the PHP and CSS files that your WordPress site uses. Sometimes, you may have to navigate further into your file system to locate the right stylesheet.css. But, for the vast majority of us, simply navigating to Appearance > Editor will take us to where we need to go.

Next, scroll down in your stylesheet.css file until you get to the very bottom and add this line of CSS code:

img#wpstats{
 display: none;
 }

Now click on the “Update File” button below your editor textarea and check out your website and the smiley should be gone!

For those of you who aren’t familiar with CSS or WordPress and want to know, “what did we actually do with that piece of code? And is WordPress still counting my stats?”

Yes, WordPress is still counting your stats. What we did with that piece of code was we hid the WordPress Stats image, which in this case, is that little smiley. So browsers will know not to display it, but the underlying purpose of it (to count your stats) is still functioning.

If editing or adding code to CSS or your stylesheet.css file makes you too nervous, there’s another way you can hide the smiley face. If your WordPress site is using Jetpack, you can disable the smiley by going to your Dashboard and clicking Jetpack on the menu on the left. Then click on the “Configure” button under “WordPress.com Stats”. Like this:

Click on the Configure button.

Click on the Configure button.

On the page that opens up, find the checkbox for the option that says, “Hide the stats smiley face image.” Check the checkbox, then click on the “Save Configuration” button at the bottom.

 

Check the box, then save it!

Check the box, then save!

Once you do that, check your website and the smiley face should be gone, but your stats will still work. And there you have it!