Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
In this hands-on course, James Williamson demonstrates the concepts that form the foundation of Cascading Style Sheets (CSS), including styling text, adding margins and padding, and controlling how images display. The course also explores the tools needed to work with CSS, the differences between embedded and external styles, how to use selectors to target elements, and what to do when styles conflict.
To control the spacing between letters and words in CSS, we use the letter spacing and word spacing properties respectively. Now those of you familiar with print design will recognize letter spacing as tracking, which essentially means adding an equal amount of space between letter pairs. Now true kerning through CSS still eludes us at the moment, but as we'll discuss at the end of this lesson, it shouldn't be much longer until those capabilities are added as well. So for this example, we're going to be using spacing.htm which you can find in the 04_11 folder.
And just real briefly I'm going to look at structure of this to tell you a little bit about what we're going to be doing here. We've got a very simple structure to the page, but we have couple of headings that I want you to pay close attention to. We have one h2 with a class of letter, another h2 with a class of word, and we're going to use these to practice letter spacing and word spacing. We also have a class with render applied to it, h2 class of speed applied to it, and a paragraph with a class of render applied to it. And that's going to allow us take a little closer look at another property that's really not directly tied to word spacing or letter spacing, but it can help achieve optimal spacing in your text. Okay.
So let's jump in by taking a look at letter spacing. So we're going to target again this h2 right here with the class of letter applied to it. So I'm going to go up to where I'm supposed to add my styles. I'm going to type-in .letter and we're just going to go ahead and add letter-spacing. And we can use any value we want, we'll start off with something really kind of wacky, maybe 25 pixels, that's a lot. I'm going to go ahead and save that and I'm going to go ahead and preview that in a Browser. And you can see that has added a lot of letter spacing or a lot of tracking if you will to that particular heading.
Now I can come in and modify that a little bit. A lot of times in something like letter spacing, a little goes a long way. So I'm going to reduce that down to 5 pixels and save it and then test that again. And now you can see that as opposed to the other headlines, it's spaced out a little bit, looks nice, it might even be just a little bit too much tracking, but you can modify this anyway that you want. You can even use negative values, so I could come in here and say like -1, but I really wanted to tighten my letter spacing up. And you can see it really, really tightens those guys up.
And you can even go a little bit further with this to create kind of a special effect where the letters are running in together. And actually these days using CSS transitions, I could transition from that property and actually animate those letters coming out. So that's pretty cool. I'm going to take that back to a positive 5 pixel value and now let's go down to our next h2 which has a class of word applied to that and let's use that class selector word to practice using word spacing. So again, word-spacing, so very easy to remember those guys.
Here again, we'll just start off with something like 15 pixels maybe, we'll save that and test it and we can see that now word spacing again is getting this sort of extra spacing between the words. You can use pixels, you can use ems, you can use percentages, you can use whatever you'd like. So if I wanted to do 1em there, I could save that and it'll extend it just a little bit more. You can also use point ems, so if I said 0.2ems, for example, and save that, it's just going to increase the word spacing a little bit but not a lot. So those values are pretty cut and dried and pretty basic in terms of what you can do.
You can extend the spacing between the letters, you can extend the spacing between the words. But unlike true kerning, you're not to going to go in there and sort of manipulate letter pairs. Now there is a property that we have available to us right now that can help optimize the spacing between our letter pairs. And it's actually not even a CSS property. One of the properties in the SVG specification, and that would be the text rendering property, has been adopted by almost all the browsers as a way to control the rendering of the text within the Browser. Now SVG is a vector graphics specification, and this was really designed to control the rendering of vector text within the Browser space.
But that property has become so widespread you can pretty much use it anywhere. All right, so we're going to go ahead and practice with this property a little bit. I can see that I have an h2 with a class of render, I have an h2 with a class of speed, and I have a paragraph with a class of render. These two classes are going to basically deal with a couple of the values of this text render property. Let's write the selector for it and I'll show you want I mean. So I'm going to go up just below my last class selector which is word and the first thing I'm going to do to these heading 2s is I want to make them a little bit larger because the bigger the text is, the easier it is to see kind of what the Browser is doing here.
So I'm going to type-in h2.render, h2. speed, and then again, we're just going to make this text a little bit larger. So font-size 60 pixels. And then we're going to take the margins down to 0 so that they butt right up against each other, we can really compare them well. So if I save this and again preview this in the Browser, you can see, those two, now we can really sort of compare exactly what's going on with both of those headings. Okay. So on the next line below that I'm going to use the .render, so just any element with render applied to it.
And that is where I'm going to use this text rendering property. So if you look for it in CSS Specification, you won't find it. It's in the SVG Specification, so text-rendering. And there are several values that we can use for this. We can use keywords Auto, Optimize Speed, Optimize Legibility, or Geometric Precision. It's the Optimize Speed and Optimize Legibility that we're really concerned with. Optimize Speed basically says, okay, go ahead and render those characters with speed being foremost in your mind. So don't worry about the quality of the characters, don't worry about the spacing of them.
Just go ahead and get them rendered as quickly as you can. Optimize Legibility on the other hand tells the Browser or the user agent, hey, really take some time, take a look at this text, figure out what the optimize rendering for this is in terms of character spacing, in terms of the rendering of the characters themselves, take your time on it. So for render here, we're going to go ahead and optimizeLegibility, which is bad news for me because I'm terrible at this, but there is our text-rendering: optimizeLegibility. All right, now we're going to follow that with the class of speed and here we're going to change the text- rendering to optimizeSpeed; Once again, we're using CamelCase naming here where we have uppercase second word, lowercase first word.
I'm going to go ahead and save this, and then I want to preview this back in my Browser again. Now if you look at this and say, well, it doesn't really look like a lot has changed, James, you would be 100% correct. And that's because different browsers are implementing it slightly different ways. In Firefox, Optimize Legibility has pretty much become the default, especially for text, any text over 20 pixels. So this is one of those things that the Browser is just trying to do to help you out without requiring a lot of extra effort from you. Now other browsers like WebKit for example, they are still at the stage where you can turn it on manually, but it's not a default setting.
So if I go back into my code and preview this using say Chrome, which is a WebKit browser, here we can see there's a huge difference between it. You'll notice that for Optimize Legibility, take a look at the positioning of the T and the e next to each other. So those two letter pairs look a lot better than optimize for speed. So while this text might render a little quicker, this text is certainly rendering in a little bit higher quality. So it's one of those things that if you choose to turn it on for specific headlines where you think spacing really matters, it's going to you help in certain browsers, but in other browsers such as Firefox, it's sort of the default way of doing things.
Going back to the beginning of the lesson, word spacing and letter spacing, those two properties, they are not terribly exciting, but they do give you some capabilities that are very handy in specific situations. Remember, you can use negative values to tighten things up, positive values to sort of move things apart. Now I would caution you just as a designer against overusing them and be really careful about using large values for those properties, again a little goes a long way with those. Now if you want to learn more about some of the advanced typographic capabilities that are currently in development with CSS, I've given you a couple of links here on this page.
One is to the Text Level 3 specification which we've kind of already looked at. The other one is to the Fonts Level 3 specification. I'm going to open that up really quickly. And if you look in this specification, there's some really cool stuff in here that's currently under development. For example, these Font features properties, we have some Kerning, so font kerning is being developed, Ligatures, Numerical formatting, Alternates and swashes. So a lot of the stuff that we sort of take for granted in print design that we can do certainly with OpenType fonts, that's starting to come to the Web as well specifically with the adoption of Web fonts, which is bringing OpenType fonts to it.
So we're getting a lot of those capabilities added and as browsers begin to implement them, you're going to want to keep track of those so that you know when you can start taking advantage of those particular features. So overall, just make sure you keep track of what's going on so that you can use those properties when they become available in browsers, and for the time being, just remember you do have properties like letter spacing and word spacing that can extend your current typographic capabilities.
There are currently no FAQs about CSS: Core Concepts.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.