Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Basic font styling

From: Up and Running with HTML

Video: Basic font styling

Although there is simply no way to show everything that CSS is capable of in one chapter, I do want to give you an idea of some of the options that you have when styling elements. We're going to start by exploring some of the things that you can control around styling text. So here we are in Chapter_06 again, the 06_04 folder, I've opened up the type.htm. Just so you have an idea of kind of what's happening on this page, we have a heading that says HTML and CSS and then three paragraphs below that, so we have an h1 and three paragraphs.

Basic font styling

Although there is simply no way to show everything that CSS is capable of in one chapter, I do want to give you an idea of some of the options that you have when styling elements. We're going to start by exploring some of the things that you can control around styling text. So here we are in Chapter_06 again, the 06_04 folder, I've opened up the type.htm. Just so you have an idea of kind of what's happening on this page, we have a heading that says HTML and CSS and then three paragraphs below that, so we have an h1 and three paragraphs.

And we are just going to control some of the basic typography on the page. So I am going to go up to my style element, and I could see that there is already one rule in place and that's the body rule, which is affecting everything within the body. And I can see that I am setting the width of my page to 70%, and then I'm doing this little trick right here, which is margin. And margin is the space between elements. And by saying margin 0 we're saying set the margin to 0 to top and bottom. The first value when you use 2 here is top and bottom, and then right and left will be auto, and that basically means center of the page, if you will.

I want to focus on the heading 1 first. So I am going to go ahead and create a selector for the heading 1, and I am just going go ahead and create my opening and closing curly braces there. And now I am going to start creating some properties for these. Now, in this one we are focusing on typography or basic font styling. Now, the first thing that you can control, that most people want to control anyway, is the family. Which font am I using? So to do that we are going to do font- family, and you are going to see that a lot of the font properties have the word font, a dash, and then what you want to change. Now, for the font-family we're going to set a specific font, and in this case we are going to say Georgia.

Now, what's really interesting about this is that you can pass in a comma-separated list of fonts, so I could say Georgia comma and then in quotation marks, because it's more than one word, Times New Roman, and then another comma, and then Times, and then finally, serif. Now, what's going on here? Well, basically what I'm asking a browser is, do you have Georgia? And if the browser says no, I say well, okay, fine. Give me Times New Roman. It says, well, I still don't have that. I say okay, one more time. Give me Times, and it's like no, I don't even have that.

So that's when you're at your wits end and you say, fine, give me whatever default serif font you've got. These are fallback fonts, and it's basically saying if my first option is not available to me, give another one. And you can have just one font if you want; you can have multiple of these. It's always a really good idea, however, to at least give it that default family, give me serif or sans serif or cursive or monospace so that it knows sort of what style of font you want, in case it doesn't have any of these. Okay, now after a font family, I want to do font-size.

You could, if you wanted to, express this in pixels: 50 pixels, 40 pixels, 16 pixels, that sort of thing. I, on the other hand, am going to ems, and I am going to say 2ems. Okay, so what is an em? An em is a relative unit of measurement. We live in a very relative world these days. You don't know if somebody is going to be looking at your site on a browser or on a phone or tablets obviously, but I've even seen a stove that had a browser in it. What a relative unit of measurement does is it says, make this size relative to whatever your default font size is. So for a phone that might be 6 pixels; for a browser it might be 16 pixels. So by saying 2ems, you are basically saying give me twice whatever your default is.

The next thing I am going to do is I am going to say font-weight, and I want font-weight to be normal. Usually, you're looking at either bold or normal there. Since headings are almost always bold by default, if you don't want them bold, you have to explicitly set font-weight to normal. And finally, I am going to do font- style, and I am going to do that italic. So you can italicize by using font-style. So weight handles whether it's bold or not; style handles whether it's italic or not. If I save this and preview this back in my browser again, you can see that it changes the formatting.

The size stays about the same, because 2ems is very close to the default size of most heading 1. And it's no longer bold, the font- weight is normal, and the font is italicized. We are going to continue setting font properties for our paragraphs. So I am going to go ahead and create a selector for the paragraph, which would p, and use the opening and closing curly braces as well. So every single selector looks exactly the same except for the characters you use for the selector. We're going to do the same thing for paragraphs: we can do font-family.

I am just going to go head and copy that, paste that in there. Save myself just a little bit of time there. Next, I am going to do font size again and for paragraphs I am going to do 1em size, and that's basically saying whatever the default font size is on your user agent, give me that. And then I am going to set a property we haven't done yet, which is line-height. Line-height is line spacing. And then I am just going to do 1.6. Now that's not space between paragraphs; it's space between the lines within the paragraph. 1.6, that's using a multiple. You could use pixels.

You can even use ems there, but by using a multiple that's 1.6 times whatever the size of the font is. So about 160% of the size of the font is the amount of line-height I am getting. And then finally, you might want also control alignment of the text. We have a couple of options available to us in CSS: we can do center alignment; we can left-align it; we can right-align it; we can also justify. So I am going to do justify which is going to justify the type so that it's neither right- or left-aligned. It's going to be solid on both sides. So if I save this and again preview this again, notice that our paragraph text gets a little larger, because we told it to be 1 em.

We've got the line spacing going on and we have this justified text as well. It's not too efficient to have to keep doing the same thing over and over again, and if you have to define a font family for every single element that you are working with, that's not very efficient at all. So one of the things I want to pass along to you here before we move on is the concept of what's known as inheritance. So if I go up to the body and let's say I take this font family declaration and I just cut this out of the paragraph selector and then I paste it into body--maybe I want it to be something a little bit differently. Maybe I say Arial, Helvetica, and then we'll do sans serif.

So now if I save this, what I'm saying is, for the body element, which is this little guy right here, I am saying everything inside there should be Arial. That means that I've just globally told my entire file, I want your font to be Arial. Now what's really interesting about CSS is these interior elements can than overwrite that. Instead of trying to define the font for every single element, what I am going to do is define it once on body and then anytime I need it to change, that's the only time I have to declare it. So now if I save this and once again go back into my browser and preview it, you can see kind of how that works.

The heading is Georgia, but the body is Arial, even though it's being set up here on the body. I just want to set a couple of more properties here and than we'll move on. I am going to go back down to the paragraph, and I am going to change the paragraph's width to 60%. width is a property you can use on just about any element. That's going to control, as you would imagine, how wide it is. Now this 60%, I could have used pixels, I could have used ems, I could use any unit of measurement I want, but when you use percentages, what that's doing is it's saying, hey, go up to the body and give me 60% of whatever the body is.

And in this case, if I save that and preview it, you'll see that it shortens that considerably. So even though the body is taking up this much room, by saying 60%, I am just saying not quite all the way to the edge of the page. This creates a more readable column. And then the last thing I am going to do is control the spacing between these lines. For the headings and paragraphs, all the browsers have default margins that they put between them, and that controls how much space is between them. Well, if you want to take control of that about yourself, you can use margins.

For the heading 1, I am going to do a margin, and I am going to set that margin's value to 1 em 0 and .4 ems. Margins you can do shorthand. Notice here I am passing two values; here I am passing three. When you pass three values, this is the top, this is left and right, and this is bottom. So I am saying give me 1 em's worth of space above the heading; don't give me any margin left and right, but give me 0.4 ems on the bottom. Now if I save this and preview it, I want you to watch the spacing right here.

You can see it added a little bit of spacing above it and took away a little bit of spacing below it. I want to do the same thing more or less for the paragraph. All I am going to do for paragraph is this. So I am going to type in margin 0. Now what that would do for me is it would take away all of the spacing in the paragraph. You can see, they're just budding right up against each other. Now what's interesting is you can have multiple declarations in the same rule. So right after margin, I could come down here on the next one and say margin-bottom and explicitly set a bottom margin to say 1 em.

Now, if I save that and test it, I can see that that bottom margin is restored, and now I'm only spacing paragraphs from the bottom of the paragraph and not the top of it. If you feel a little overwhelmed, don't. Now, remember, this isn't a CSS course. The main purpose of these exercises is to show you a little bit of what CSS is capable of and to exposure to the syntax so that you'll have a head start when you begin learning CSS in earnest.

Show transcript

This video is part of

Image for Up and Running with HTML
Up and Running with HTML

49 video lessons · 26073 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 2m 12s
    1. Welcome
      55s
    2. Using the exercise files
      1m 17s
  2. 29m 30s
    1. Learning HTML
      2m 47s
    2. Choosing a code editor
      5m 2s
    3. Exploring basic HTML syntax
      8m 18s
    4. Do I need to learn HTML5?
      5m 6s
    5. Exploring HTML references
      8m 17s
  3. 35m 40s
    1. Exploring an HTML document
      5m 19s
    2. Working with doctype declarations
      4m 3s
    3. Examining the document head
      8m 20s
    4. Looking at the document body
      3m 21s
    5. Adding document structure
      8m 52s
    6. Lab: Coding a basic page
      3m 9s
    7. Solution: Coding a basic page
      2m 36s
  4. 1h 23m
    1. How does HTML format text?
      5m 51s
    2. Adding headings
      7m 24s
    3. Formatting paragraphs
      4m 54s
    4. Controlling line breaks
      3m 50s
    5. Creating lists
      10m 37s
    6. Emphasizing text
      6m 42s
    7. Displaying special characters
      5m 8s
    8. Controlling whitespace
      4m 35s
    9. Inserting images
      9m 20s
    10. Lab: Controlling page content
      13m 57s
    11. Solution: Controlling page content
      10m 55s
  5. 31m 54s
    1. Linking to pages within your site
      6m 45s
    2. Linking to external pages
      3m 2s
    3. Linking to downloadable resources
      2m 25s
    4. Linking to page regions
      8m 0s
    5. Lab: Creating Links
      5m 57s
    6. Solution: Creating Links
      5m 45s
  6. 40m 27s
    1. Examining basic table structure
      5m 10s
    2. Adding content to tables
      6m 20s
    3. Setting table attributes
      7m 42s
    4. Adding table captions
      4m 3s
    5. Defining table headers
      2m 13s
    6. Making table data accessible
      5m 46s
    7. Lab: Building tables
      4m 13s
    8. Solution: Building tables
      5m 0s
  7. 43m 23s
    1. Understanding the relationship between HTML and CSS
      4m 58s
    2. Creating inline styles
      4m 53s
    3. Exploring the style element
      5m 13s
    4. Basic font styling
      9m 24s
    5. Changing color
      4m 55s
    6. Taking styles further
      5m 24s
    7. Lab: Controlling basic styles
      5m 10s
    8. Solution: Controlling basic styles
      3m 26s
  8. 5m 44s
    1. Next steps
      2m 56s
    2. Additional resources
      2m 48s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Up and Running with HTML.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.