The principle of separating CSS from HTML is based on the concept of separation of concerns. HTML is used to structure the content of a web page, while CSS is used to style that content. This separation makes the codebase more organized and easier to manage. When it comes to fonts, CSS provides a way to define font - families, sizes, weights, and styles without cluttering the HTML code.
px
), relative values like em or rem, or percentages.normal
, bold
, or numerical values like 400
(normal) and 700
(bold).normal
, italic
, or oblique
.To separate CSS from HTML, we need to link the CSS file to the HTML file. This is typically done using the <link>
tag in the HTML’s <head>
section.
First, create a new file with a .css
extension, for example, styles.css
. In this file, you can define all your CSS rules, including those related to fonts.
/* styles.css */
body {
font-family: 'Open Sans', Arial, sans - serif;
font-size: 16px;
font-weight: 400;
font-style: normal;
}
In the HTML file, use the <link>
tag to connect the CSS file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial - scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>My Web Page</title>
</head>
<body>
<h1>Welcome to my page</h1>
<p>This is a sample paragraph with the defined font.</p>
</body>
</html>
You can use custom fonts by either hosting them locally or using web - based font services like Google Fonts.
<link>
tag provided by Google Fonts and add it to the <head>
section of your HTML file.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial - scale=1.0">
<link rel="stylesheet" href="styles.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<title>My Web Page</title>
</head>
<body>
<h1>Welcome to my page</h1>
<p>This is a sample paragraph with a Google Font.</p>
</body>
</html>
/* styles.css */
body {
font-family: 'Roboto', sans - serif;
font-size: 18px;
}
If you don’t want to rely on third - party services, you can host fonts locally. First, download the font files (usually in formats like .ttf
, .woff
, or .woff2
). Then, use the @font - face
rule in your CSS file:
@font - face {
font-family: 'MyCustomFont';
src: url('fonts/MyCustomFont.woff2') format('woff2'),
url('fonts/MyCustomFont.woff') format('woff');
font-weight: normal;
font-style: normal;
}
body {
font-family: 'MyCustomFont', sans - serif;
font-size: 16px;
}
To make the font sizes adapt to different screen sizes, you can use relative units like em
or rem
. For example:
html {
font-size: 16px;
}
body {
font-size: 1em;
}
h1 {
font-size: 2em;
}
Group related font rules together in your CSS file. For example, you can have a section dedicated to global font settings and another for heading font styles.
/* Global font settings */
body {
font-family: Arial, sans - serif;
font-size: 16px;
font-weight: 400;
}
/* Heading font styles */
h1, h2, h3 {
font-family: 'Open Sans', sans - serif;
font-weight: 700;
}
h1 {
font-size: 2.5em;
}
h2 {
font-size: 2em;
}
h3 {
font-size: 1.5em;
}
When specifying font - family
, use a font stack. A font stack is a list of fonts in order of preference. If the first font is not available on the user’s device, the browser will try the next one in the list.
body {
font-family: 'Open Sans', 'Helvetica Neue', Arial, sans - serif;
}
.woff2
which have better compression and performance compared to older formats like .ttf
.Separating CSS files from HTML with proper font handling is a powerful technique in web development. It provides a clean and organized way to manage the visual presentation of text on a web page. By understanding the fundamental concepts, usage methods, common practices, and best practices, developers can create more maintainable and visually appealing websites. Whether you are using built - in fonts or custom ones, the key is to keep your code organized, efficient, and accessible.