Few external CSS examples, where the styles are stored in a separate .css
file and linked to the HTML document.
Example 1: Basic Styling with External CSS
HTML File (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>External CSS Example 1</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Welcome to External CSS</h1>
<p>This page is styled using an external CSS file.</p>
</body>
</html>
CSS File (styles.css)
body {
background-color: lightyellow;
font-family: Arial, sans-serif;
text-align: center;
margin: 0;
padding: 0;
}
h1 {
color: darkblue;
}
p {
color: #555;
font-size: 16px;
}
Output:
- The background color is light yellow.
- The header (
h1
) has dark blue text. - The paragraph (
p
) has grayish text, and the page is centered.
Example 2: Navigation Menu with External CSS
HTML File (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>External CSS Example 2</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</body>
</html>
CSS File (styles.css)
body {
font-family: 'Verdana', sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
nav {
background-color: #333;
}
nav ul {
list-style-type: none;
padding: 0;
margin: 0;
text-align: center;
}
nav ul li {
display: inline-block;
}
nav ul li a {
color: white;
padding: 15px 30px;
text-decoration: none;
display: inline-block;
}
nav ul li a:hover {
background-color: #575757;
}
Output:
- A horizontal navigation menu with a dark background.
- Each link in the menu is white and becomes darker when hovered over.
Example 3: Card Layout with External CSS
HTML File (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>External CSS Example 3</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="card">
<h2>Card Title</h2>
<p>This is a description for the card. It can contain some interesting content!</p>
<button>Read More</button>
</div>
</body>
</html>
CSS File (styles.css)
body {
font-family: 'Arial', sans-serif;
background-color: #eaeaea;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.card {
background-color: white;
width: 300px;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
text-align: center;
}
.card h2 {
color: #333;
}
.card p {
color: #666;
}
.card button {
background-color: #4CAF50;
color: white;
padding: 10px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
.card button:hover {
background-color: #45a049;
}
Output:
- A centered card with a shadow, rounded corners, and a “Read More” button.
- The button changes color when hovered.
Example 4: Form Styling with External CSS
HTML File (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>External CSS Example 4</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<form class="contact-form">
<h2>Contact Us</h2>
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea>
<button type="submit">Submit</button>
</form>
</body>
</html>
CSS File (styles.css)
body {
font-family: 'Helvetica', sans-serif;
background-color: #f9f9f9;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.contact-form {
background-color: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
width: 400px;
}
.contact-form h2 {
text-align: center;
color: #333;
}
.contact-form label {
display: block;
margin: 10px 0 5px;
}
.contact-form input, .contact-form textarea {
width: 100%;
padding: 10px;
border-radius: 5px;
border: 1px solid #ddd;
margin-bottom: 15px;
}
.contact-form button {
background-color: #4CAF50;
color: white;
padding: 12px;
border: none;
border-radius: 5px;
width: 100%;
cursor: pointer;
}
.contact-form button:hover {
background-color: #45a049;
}
Output:
- A neatly styled contact form with inputs and a submit button.
- The form fields and button are clean and responsive.
Example 5: Footer Styling with External CSS
HTML File (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>External CSS Example 5</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<footer>
<p>© 2025 My Website | All rights reserved</p>
</footer>
</body>
</html>
CSS File (styles.css)
footer {
background-color: #333;
color: white;
text-align: center;
padding: 20px;
position: fixed;
width: 100%;
bottom: 0;
}
footer p {
margin: 0;
font-size: 14px;
}
Output:
- A fixed footer at the bottom of the page with white text on a dark background.
Summary of External CSS:
In these examples, the styles are separated from the HTML file and placed in a .css
file. This separation promotes better organization and makes it easier to maintain and reuse styles across multiple HTML pages. By linking the CSS file to the HTML using the <link>
tag, the page is styled externally.