HTML Styles

HTML styles define the visual presentation of web pages using CSS (Cascading Style Sheets). CSS allows you to control the appearance of text, layout, colors, spacing, and more.

There are three primary ways to apply CSS styles in HTML:

Inline CSS applies styles directly to a specific HTML element using the style attribute. This method is useful for quick styling but is not recommended for large-scale projects because it makes maintenance difficult.

Example:

<p style="color: blue; font-size: 18px; font-weight: bold;">This is an inline-styled paragraph.</p>
HTML

Explanation:

  • color: blue; โ†’ Sets the text color to blue.
  • font-size: 18px; โ†’ Increases text size to 18 pixels.
  • font-weight: bold; โ†’ Makes the text bold.

๐Ÿ”น  For quick styling of a single element.
๐Ÿ”น  Not recommended for large projects due to poor maintainability.

Internal CSS applies styles to multiple elements in a web page by placing CSS rules inside a <style> tag in the <head> section.

Example:

<!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: lightgray;
        }
        h1 {
            color: darkblue;
            text-align: center;
        }
        p {
            font-size: 20px;
            color: darkgreen;
        }
    </style>
</head>
<body>
    <h1>Welcome to Internal CSS</h1>
    <p>This is a paragraph styled using internal CSS.</p>
</body>
</html>
HTML

Explanation:

  • background-color: lightgray; โ†’ Changes the page background to light gray.
  • color: darkblue; โ†’ Changes the h1 heading text color to dark blue.
  • text-align: center; โ†’ Centers the heading text.
  • font-size: 20px; โ†’ Increases paragraph text size.
  • color: darkgreen; โ†’ Changes paragraph text color to dark green.

When to Use Internal CSS?

๐Ÿ”น  Useful for single-page projects or testing styles.
๐Ÿ”น  Not ideal for large projects because it does not separate design from content.

External CSS stores styles in a separate .css file and links it to the HTML document using the <link> tag. This is the best practice for styling because it keeps HTML clean and allows reusability across multiple pages.

Example:

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

CSS File (styles.css)

body {
    background-color: #f0f0f0;
    font-family: Arial, sans-serif;
}

h1 {
    color: #333;
    text-align: center;
    font-size: 28px;
}

p {
    font-size: 18px;
    color: #555;
    text-align: justify;
}
CSS

Explanation:

  • The HTML file does not contain styles, keeping it clean.
  • The CSS file is linked using <link rel="stylesheet" href="styles.css">.
  • All styles are applied consistently across multiple pages.

When to Use External CSS?

๐Ÿ”น  Best for large projects with multiple pages.
๐Ÿ”น  Improves maintainability and consistency.
๐Ÿ”น  Allows caching for better performance.

Here are some commonly used CSS properties:

PropertyDescriptionExample
colorSets the text colorcolor: red;
background-colorSets the background colorbackground-color: yellow;
font-sizeSets the text sizefont-size: 18px;
font-familyDefines the text font stylefont-family: Arial, sans-serif;
text-alignAligns texttext-align: center;
marginAdds space outside an elementmargin: 10px;
paddingAdds space inside an elementpadding: 15px;
borderAdds a border around an elementborder: 2px solid black;

Selectors define which HTML elements should be styled.

SelectorDescriptionExample
*Universal selector (selects all elements)* { margin: 0; padding: 0; }
pElement selector (selects all <p> elements)p { color: blue; }
.classNameClass selector (selects elements with a class).box { background: red; }
#idNameID selector (selects an element with an ID)#header { font-size: 24px; }

The box model consists of four parts:

  1. Content โ€“ The actual content inside the element.
  2. Padding โ€“ Space inside the border around the content.
  3. Border โ€“ A line surrounding the padding and content.
  4. Margin โ€“ Space outside the border.
.box {
    width: 200px;
    height: 100px;
    padding: 20px;
    border: 5px solid black;
    margin: 30px;
}
CSS

Flexbox makes it easy to align items in a row or column.

.container {
    display: flex;
    justify-content: space-between;
}
CSS

๐Ÿ”น  Inline CSS is quick but not recommended for large projects.

๐Ÿ”น  Internal CSS is useful for small projects but hard to manage.

๐Ÿ”น  External CSS is the best approach for clean and maintainable code.

๐Ÿ”น  Understanding CSS properties, selectors, and layout techniques helps in creating modern and responsive web designs.