Basic CSS in HTML
CSS (Cascading Style Sheets) is used to format the appearance of HTML elements.
Ways to Add CSS
1. Inline Style
<p style="color: red; font-size: 16px;">Red text</p>
2. Internal CSS
<head>
<style>
p {
color: blue;
font-size: 18px;
}
</style>
</head>
3. External CSS
<head>
<link rel="stylesheet" href="style.css">
</head>
Basic Selectors
Element Selector
p {
color: blue;
}
Class Selector
.text {
color: red;
}
ID Selector
#header {
background: #333;
}
Basic Properties
Color and Background
p {
color: #333;
background-color: #f0f0f0;
}
Text
p {
font-size: 16px;
font-family: Arial, sans-serif;
text-align: center;
line-height: 1.5;
}
Box Model
div {
margin: 10px;
padding: 20px;
border: 1px solid #ccc;
width: 300px;
}
Case Study: Styling a Campus Website
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Campus Website</title>
<style>
:root {
--primary: #1e40af;
--secondary: #f59e0b;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
color: #333;
}
header {
background: var(--primary);
color: white;
padding: 1rem;
text-align: center;
}
nav ul {
display: flex;
justify-content: center;
gap: 2rem;
list-style: none;
}
nav a {
color: white;
text-decoration: none;
}
main {
padding: 2rem;
max-width: 1200px;
margin: 0 auto;
}
.card {
background: #f9fafb;
border: 1px solid #e5e7eb;
border-radius: 8px;
padding: 1.5rem;
margin: 1rem 0;
}
footer {
background: #333;
color: white;
text-align: center;
padding: 1rem;
}
</style>
</head>
<body>
<header>
<h1>Technology University</h1>
<nav>
<ul>
<li><a href="beranda.html">Home</a></li>
<li><a href="academic.html">Academic</a></li>
<li><a href="kontak.html">Kontak</a></li>
</ul>
</nav>
</header>
<main>
<div class="card">
<h2>Welcome</h2>
<p>Welcome to the university portal.</p>
</div>
</main>
<footer>
<p>© 2024 Technology University</p>
</footer>
</body>
</html>
Best Practices
- Use external CSS: For maintainability
- CSS variables: For consistency
- Mobile-first: Design for mobile first
- Specificity: Understand the priority order
- Reset CSS: Use a reset for consistency
Summary
CSS controls the appearance of web pages. Use external CSS for large projects and CSS variables for consistency.