1. Inline CSS (Inside HTML Elements)

This method applies styles directly within the HTML tags using the ‘style‘ attribute.

				
					<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Inline CSS Example</title>
</head>
<body>
    <h1 style="color: blue; text-align: center;">Hello, World!</h1>
    <p style="font-size: 18px; color: gray;">This paragraph is styled using inline CSS.</p>
</body>
</html>

				
			

2. Internal CSS (Inside the 'style' Tag)

This method places CSS styles inside the <style> tag within the <head> section of the HTML document.

				
					<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Internal CSS Example</title>
    <style>
        body {
            background-color: #f0f8ff;
            text-align: center;
            font-family: Arial, sans-serif;
        }
        h1 {
            color: darkred;
        }
        p {
            font-size: 18px;
            color: black;
        }
    </style>
</head>
<body>
    <h1>Welcome to My Website</h1>
    <p>This paragraph is styled using internal CSS.</p>
</body>
</html>

				
			

3. External CSS (Using a Separate .css File)

This method links an external .css file to the HTML document.

HTML (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</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>External CSS Example</h1>
    <p>This paragraph is styled using an external CSS file.</p>
</body>
</html>

				
			

CSS (style.css):

				
					body {
    background-color: lightgray;
    text-align: center;
    font-family: Arial, sans-serif;
}

h1 {
    color: navy;
}

p {
    font-size: 18px;
    color: darkslategray;
}

				
			

4. CSS Classes and IDs

Classes and IDs allow you to apply styles selectively.

				
					<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Classes and IDs</title>
    <style>
        .highlight {
            color: blue;
            font-weight: bold;
        }
        #special {
            color: red;
            font-size: 20px;
        }
    </style>
</head>
<body>
    <h1 class="highlight">This is a highlighted heading</h1>
    <p class="highlight">This paragraph shares the same class.</p>
    <p id="special">This paragraph has a unique ID and different styling.</p>
</body>
</html>

				
			

5. CSS Hover Effect

This example changes the color of a button when the mouse hovers over it.

				
					<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Hover Effect</title>
    <style>
        .btn {
            background-color: #008cba;
            color: white;
            padding: 10px 20px;
            border: none;
            cursor: pointer;
            font-size: 16px;
        }
        .btn:hover {
            background-color: #005f73;
        }
    </style>
</head>
<body>
    <button class="btn">Hover Over Me</button>
</body>
</html>

				
			
Scroll to Top