- CSS styling text
- Looking to become a front-end web developer?
- Prerequisites
- Guides
- Assessments
- Found a content problem with this page?
- Using Wildcards Selectors in CSS for classes, IDs, names, etc.
- Containing wildcard CSS selector
- Wildcard Example
- «Starts with» wildcard CSS selector
- Wildcard Example
- «Ends with» wildcard CSS selector
- Wildcard Example
- «Containing» where property is separated by a space
- Wildcard Example
- Fundamental text and font styling
- What is involved in styling text in CSS?
- Fonts
- Color
- Font families
- Web safe fonts
- Default fonts
CSS styling text
With the basics of the CSS language covered, the next CSS topic for you to concentrate on is styling text — one of the most common things you’ll do with CSS. Here we look at text styling fundamentals including setting font, boldness, italics, line and letter spacing, drop shadows, and other text features. We round off the module by looking at applying custom fonts to your page, and styling lists and links.
Looking to become a front-end web developer?
We have put together a course that includes all the essential information you need to work towards your goal.
Prerequisites
Before starting this module, you should already have basic familiarity with HTML, as discussed in the Introduction to HTML module, and be comfortable with CSS fundamentals, as discussed in Introduction to CSS.
Note: If you are working on a computer/tablet/other device where you don’t have the ability to create your own files, you could try out (most of) the code examples in an online coding program such as JSBin or Glitch.
Guides
This module contains the following articles, which will teach you all of the essentials behind styling HTML text content.
In this article we go through all the basics of text/font styling in detail, including setting font weight, family and style, font shorthand, text alignment and other effects, and line and letter spacing.
Lists behave like any other text for the most part, but there are some CSS properties specific to lists that you need to know about, and some best practices to consider. This article explains all.
When styling links, it is important to understand how to make use of pseudo-classes to style link states effectively, and how to style links for use in common varied interface features such as navigation menus and tabs. We’ll look at all these topics in this article.
Here we will explore web fonts in detail — these allow you to download custom fonts along with your web page, to allow for more varied, custom text styling.
Assessments
The following assessment will test your understanding of the text styling techniques covered in the guides above.
In this assessment we’ll test your understanding of styling text by getting you to style the text for a community school’s homepage.
Found a content problem with this page?
This page was last modified on Jun 30, 2023 by MDN contributors.
Your blueprint for a better internet.
Using Wildcards Selectors in CSS for classes, IDs, names, etc.
CSS is a way for web developers to define the visual appearance of the web pages that they were creating. It was intended to allow web professionals to separate the content and structure of a website’s code from the visual design. Now, in CSS (Cascading Style Sheet), selectors are patterns used to select the element(s) you want to style or, in other words, pattern matching rules to determine which style applies to which element in the document.
To use a selector we need to take advantage of the attribute selector, for example [attribute=’property’]. The attribute selector can be used on any valid element attribute – id, class, name etc.
Now, let’s say that we need to select, using CSS, multiple classes that begins, ends or contains a specific text (usually for IDs, names or classes generated dinamically): the following examples will show what to do in this cases.
Containing wildcard CSS selector
[attribute*=»str»] Selector: The [attribute*=»str»] selector is used to select that elements whose attribute value contains the specified sub string str.
This example shows how to use a wildcard to select all div’s with a class that contains string. This could be at the start, the end or in the middle of the class.
/* Define styles of selected items, h1 and rest of the body */ [class*="str"] < /* WE USE * HERE */ background: rgba(118,140,181,1); color: white; >h1 < color:red; >bodyWildcard Example
The first div element.The second div element.The third div element.Paragraph Text
«Starts with» wildcard CSS selector
[attribute^=»str»] Selector: The [attribute^=»value»] selector is used to select those elements whose attribute value begins with a specified value str. This example shows how to use a wildcard to select all div with a class that starts with str.
This example shows how to use a wildcard to select all div’s with a class that starts with string.
[class^="str"] < /*WE USE ^ HERE */ background: rgba(118,140,181,1); color: white; >h1 < color:red; >bodyWildcard Example
The first div element.The second div element.The third div element.The fourth div element.Paragraph Text
«Ends with» wildcard CSS selector
[attribute$=»str»] Selector: The [attribute$=»value»] selector is used to select those elements whose attribute value ends with a specified value str. The following example selects all elements with a class attribute value that ends with str.
This example shows how to use a wildcard to select all div’s that end with string.
[class$="str"] < /* WE USE $ HERE */ background: green; color: white; >h1 < color:green; >bodyWildcard Example
The first div element.The second div element.The third div element.This is some text in a paragraph.
«Containing» where property is separated by a space
[attribute~=»str»] Selector: The [attribute~=»str»] selector is used to select that elements whose attribute value contains the specified sub string stras a standalone property, even considering space characters.
This example shows how to use a wildcard to select all div’s that contain string as a standalone property.
/* Define styles of selected items, h1 and rest of the body */ [class~="str"] < /* WE USE * HERE */ background: rgba(118,140,181,1); color: white; >h1 < color:red; >bodyWildcard Example
The first div element.The second div element.The third div element.Paragraph Text
Fundamental text and font styling
In this article we’ll start you on your journey towards mastering text styling with CSS. Here we’ll go through all the basic fundamentals of text/font styling in detail, including setting font weight, family and style, font shorthand, text alignment and other effects, and line and letter spacing.
Prerequisites: | Basic computer literacy, HTML basics (study Introduction to HTML), CSS basics (study Introduction to CSS). |
---|---|
Objective: | To learn the fundamental properties and techniques needed to style text on web pages. |
What is involved in styling text in CSS?
If you have worked with HTML or CSS already, e.g., by working through these tutorials in order, then you know that text inside an element is laid out inside the element’s content box. It starts at the top left of the content area (or the top right, in the case of RTL language content), and flows towards the end of the line. Once it reaches the end, it goes down to the next line and flows to the end again. This pattern repeats until all the content has been placed in the box. Text content effectively behaves like a series of inline elements, being laid out on lines adjacent to one another, and not creating line breaks until the end of the line is reached, or unless you force a line break manually using the element.
Note: If the above paragraph leaves you feeling confused, then no matter — go back and review our Box model article to brush up on the box model theory before carrying on.
The CSS properties used to style text generally fall into two categories, which we’ll look at separately in this article:
- Font styles: Properties that affect a text’s font, e.g., which font gets applied, its size, and whether it’s bold, italic, etc.
- Text layout styles: Properties that affect the spacing and other layout features of the text, allowing manipulation of, for example, the space between lines and letters, and how the text is aligned within the content box.
Note: Bear in mind that the text inside an element is all affected as one single entity. You can’t select and style subsections of text unless you wrap them in an appropriate element (such as a or ), or use a text-specific pseudo-element like ::first-letter (selects the first letter of an element’s text), ::first-line (selects the first line of an element’s text), or ::selection (selects the text currently highlighted by the cursor).
Fonts
Let’s move straight on to look at properties for styling fonts. In this example, we’ll apply some CSS properties to the following HTML sample:
h1>Tommy the cath1> p>Well I remember it as though it were a meal ago…p> p> Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator — Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did. p>
Color
The color property sets the color of the foreground content of the selected elements, which is usually the text, but can also include a couple of other things, such as an underline or overline placed on text using the text-decoration property.
color can accept any CSS color unit, for example:
This will cause the paragraphs to become red, rather than the standard browser default of black, like so:
h1>Tommy the cath1> p>Well I remember it as though it were a meal ago…p> p> Said Tommy the Cat as he reeled back to clear whatever foreign matter may have nestled its way into his mighty throat. Many a fat alley rat had met its demise while staring point blank down the cavernous barrel of this awesome prowling machine. Truly a wonder of nature this urban predator — Tommy the cat had many a story to tell. But it was a rare occasion such as this that he did. p>
Font families
To set a different font for your text, you use the font-family property — this allows you to specify a font (or list of fonts) for the browser to apply to the selected elements. The browser will only apply a font if it is available on the machine the website is being accessed on; if not, it will just use a browser default font. A simple example looks like so:
This would make all paragraphs on a page adopt the arial font, which is found on any computer.
Web safe fonts
Speaking of font availability, there are only a certain number of fonts that are generally available across all systems and can therefore be used without much worry. These are the so-called web safe fonts.
Most of the time, as web developers we want to have more specific control over the fonts used to display our text content. The problem is to find a way to know which font is available on the computer used to see our web pages. There is no way to know this in every case, but the web safe fonts are known to be available on nearly all instances of the most used operating systems (Windows, macOS, the most common Linux distributions, Android, and iOS).
The list of actual web safe fonts will change as operating systems evolve, but it’s reasonable to consider the following fonts web safe, at least for now (many of them have been popularized thanks to the Microsoft Core fonts for the Web initiative in the late 90s and early 2000s):
Name | Generic type | Notes |
---|---|---|
Arial | sans-serif | It’s often considered best practice to also add Helvetica as a preferred alternative to Arial as, although their font faces are almost identical, Helvetica is considered to have a nicer shape, even if Arial is more broadly available. |
Courier New | monospace | Some OSes have an alternative (possibly older) version of the Courier New font called Courier. It’s considered best practice to use both with Courier New as the preferred alternative. |
Georgia | serif | |
Times New Roman | serif | Some OSes have an alternative (possibly older) version of the Times New Roman font called Times. It’s considered best practice to use both with Times New Roman as the preferred alternative. |
Trebuchet MS | sans-serif | You should be careful with using this font — it isn’t widely available on mobile OSes. |
Verdana | sans-serif |
Note: Among various resources, the cssfontstack.com website maintains a list of web safe fonts available on Windows and macOS operating systems, which can help you make your decision about what you consider safe for your usage.
Note: There is a way to download a custom font along with a webpage, to allow you to customize your font usage in any way you want: web fonts. This is a little bit more complex, and we will discuss it in a separate article later on in the module.
Default fonts
CSS defines five generic names for fonts: serif , sans-serif , monospace , cursive , and fantasy . These are very generic and the exact font face used from these generic names can vary between each browser and each operating system that they are displayed on. It represents a worst case scenario where the browser will try its best to provide a font that looks appropriate. serif , sans-serif , and monospace are quite predictable and should provide something reasonable. On the other hand, cursive and fantasy are less predictable and we recommend using them very carefully, testing as you go.
The five names are defined as follows:
body font-family: sans-serif; >