Basic CSS
Typography in CSS
Typography is a fundamental part of web design. Through CSS, we can control how text is displayed on our web pages, improving both its appearance and readability. In this chapter, we will learn how to use CSS properties to customize typography.
font-family
Property
The font-family
property allows us to change the font used in an element. You can specify a list of fonts, where the browser will use the first one available on the user's system.
css
It's a good practice to provide more than one font, as not all systems have the same fonts installed. For instance, you can include a generic font like serif or sans-serif at the end of the list as a fallback.
Here are examples of the serif font family
font-size
Property
The font-size
property sets the size of the text. You can use different units to define the size:
- Pixels (
px
): Absolute units that do not depend on the screen size. - Em (
em
): Relative units based on the font size of the parent element. - Rem (
rem
): Relative units based on the root font size (usually the font size of thehtml
). - Percentages (
%
): Units relative to the font size of the parent element.
css
This image shows a text comparison with different font sizes using px, em, and rem
font-weight
Property
The font-weight
property controls the thickness of the text. It can take values like normal
, bold
, or numeric values between 100 and 900 for finer control.
css
font-style
Property
The font-style
property allows you to define whether the text should be normal or slanted. The most common values are normal
, italic
, and oblique
.
css
This image shows an example of text in normal, italic, and oblique styles
line-height
Property
The line-height
property defines the vertical space between lines of text. A higher value increases the line spacing, which can improve readability in long texts.
css
text-align
Property
The text-align
property controls the alignment of text within a container. Common values include:
left
: Aligns text to the left.right
: Aligns text to the right.center
: Centers the text.justify
: Justifies the text, making the lines have the same width.
css
text-transform
Property
The text-transform
property allows you to change the capitalization of the text. The most common values are:
uppercase
: Converts text to uppercase.lowercase
: Converts text to lowercase.capitalize
: Converts the first letter of each word to uppercase.
css
text-decoration
Property
The text-decoration
property is used to add or remove text decorations, such as underlines or strikethroughs. The most common values are:
underline
: Underlines the text.line-through
: Strikesthrough the text.none
: Removes any decoration.
css
letter-spacing
and word-spacing
Properties
The letter-spacing
property controls the space between letters in a text, while word-spacing
controls the space between words.
css
This image shows word-spacing and letter-spacing
Web Fonts and Google Fonts
To ensure that your page has access to a wider variety of fonts, you can use web fonts, such as those offered by Google Fonts. These fonts are loaded from the internet, so they do not depend on fonts installed on the user's system.
How to Use Google Fonts
-
Visit Google Fonts and select the font you want to use.
-
Copy the provided link and insert it into your HTML document within the
<head>
tag.html"This code links the Roboto font from Google Fonts in our HTML document." -
Then, apply the font in your CSS:
css"With this rule, we are applying the Roboto font to the body of the page."
Examples of Google Fonts are shown
Conclusion
In this chapter, we have learned to use CSS properties to control typography and improve text readability on a web page. Now we know how to change fonts, sizes, styles, and text alignments. In the next chapter, we will delve into how to create page layouts using CSS positioning techniques.
- Introduction to CSS
- CSS Selectors
- The Box Model in CSS
- Colors and Backgrounds in CSS
- Typography in CSS
- Design Techniques with CSS
- Responsive Design Fundamentals in CSS
- Responsive Navigation in CSS
- Responsive Forms in CSS
- Combinando CSS con HTML para un diseño completo
- CSS Debugging and Optimization
- Working with CSS Libraries and Frameworks
- Customizing CSS Frameworks
- Structuring Large CSS Projects
- Best Practices for CSS Performance
- Keeping CSS Code Clean and Well-Documented
- Testing and Debugging CSS
- Ensuring Accessibility with CSS
- Use animations and transitions in an accessible manner
- CSS Optimization for Large Websites
- Keeping Clean and Scalable CSS Code