Skip to main content

CSS Basics in HTML β€” Styling Your Web Pages

HTML + CSS: Structure Meets Style​

HTML provides the structure (the skeleton), and CSS adds the visual style (the paint, furniture, and decor).


Three Ways to Add CSS​

There are three ways to integrate CSS with HTML.

1. Inline CSS​

Apply styles directly to an element using the style attribute.

<p style="color: blue; font-size: 18px;">
This paragraph is blue and 18 pixels tall.
</p>
ProsCons
Quick for testingHard to maintain
Overrides other stylesMixes content with presentation
Useful for dynamic stylesNot reusable

2. Internal CSS​

Place a <style> block inside the <head> of your HTML document.

<!DOCTYPE html>
<html>
<head>
<style>
p {
color: blue;
font-size: 18px;
}
.highlight {
background-color: yellow;
}
#main-title {
font-size: 32px;
font-weight: bold;
}
</style>
</head>
<body>
<h1 id="main-title">Welcome</h1>
<p>This is a blue paragraph.</p>
<p class="highlight">This one has a yellow background.</p>
</body>
</html>
ProsCons
Centralized styles for one pageDoesn't scale across multiple pages
No extra HTTP requestsIncreases page size

3. External CSS​

Link to a separate .css file using the <link> tag.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Styled from an external file</h1>
</body>
</html>

styles.css:

body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
}
h1 {
color: #333;
text-align: center;
}
ProsCons
Reusable across entire siteExtra HTTP request (but cached)
Clean separation of concernsRequires file management
Cached by browser for faster loadsβ€”

CSS Integration Methods β€” Which to Choose?​


Class and ID Attributes​

These HTML attributes connect elements to CSS rules.

<!-- ID β€” unique, targeted with # -->
<div id="header">Site Header</div>

<!-- Class β€” reusable, targeted with . -->
<p class="warning">This is a warning message.</p>
<p class="warning">This is another warning.</p>
/* ID selector */
#header {
background-color: navy;
color: white;
}

/* Class selector */
.warning {
color: red;
font-weight: bold;
border: 1px solid orange;
}

CSS Selectors Introduction​

SelectorExampleTargets
Elementp { }All <p> elements
Class.card { }All elements with class="card"
ID#logo { }The element with id="logo"
Descendantdiv p { }<p> inside a <div>
Grouph1, h2, h3 { }All headings

Basic CSS Properties​

/* Colors */
color: red; /* text color */
background-color: #f0f0f0; /* background color */

/* Sizing and spacing */
font-size: 16px; /* text size */
margin: 10px; /* space outside element */
padding: 15px; /* space inside element */

/* Layout */
width: 100%; /* element width */
border: 1px solid black; /* border shorthand */

The Box Model β€” A Quick Introduction​

Every HTML element is a rectangular box with four layers:

LayerRole
ContentThe actual text or image
PaddingSpace between content and border
BorderThe edge around the element
MarginSpace outside the border, between elements
.box {
width: 200px;
padding: 20px; /* inside space */
border: 2px solid black;
margin: 15px; /* outside space */
}

Try It Yourself​

<!DOCTYPE html>
<html>
<head>
<style>
.card {
background-color: #fff;
border: 1px solid #ddd;
border-radius: 8px;
padding: 20px;
margin: 20px auto;
max-width: 400px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.card h2 {
color: #333;
margin-top: 0;
}
.btn {
background-color: #007bff;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}
.btn:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<div class="card">
<h2>Hello, CSS!</h2>
<p>This card is styled entirely with CSS.</p>
<button class="btn">Click Me</button>
</div>
</body>
</html>


Key Takeaways​

ConceptSummary
Inline CSSQuick, element‑specific, not reusable
Internal CSS<style> block, good for single pages
External CSSBest practice, cached, site‑wide reuse
ClassesReusable (. in CSS)
IDsUnique (# in CSS)
Box ModelContent β†’ Padding β†’ Border β†’ Margin
SelectorsElement, class, ID, descendant, group