Navigation

Linking an External Style Sheet CSS Syntax Basic CSS Properties Classes and IDs Descendant Selectors CSS for Tables Example of a Webpage with CSS Refering to Insert

CSS Basics

Cascading Style Sheets (CSS) allow you to style and format HTML elements. Here's a detailed guide with dark-boxs:


1. Linking an External Style Sheet

To apply styles from an external CSS file, use the <link> tag inside the <head> element:

<head> <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> </head>

2. CSS Syntax

The basic structure of CSS involves selecting an element and defining its style properties:

h1 { color: red; font-size: 12px; }

3. Basic CSS Properties


4. Classes and IDs

Classes and IDs help target specific elements for styling:

/* CSS */ p { color: blue; } #about_me { color: red; } .main { color: green; } /* HTML */ <p id="about_me">About Me</p> <p>I enjoy coding.</p> <p class="main">Good morning.</p> <p class="main">Good afternoon.</p> ---------------------------------------

About Me

I enjoy coding.

Good morning.

Good afternoon.


5. Descendant Selectors

Style elements based on their nesting structure:

/* CSS */ div p { color: yellow; } p { color: orange; } /* HTML */ <div> <p>Hello</p> </div> <p>Goodbye</p> ---------------------------------------

Hello

Goodbye

In this dark-box, div p targets paragraphs inside a <div>, while p applies to all other paragraphs.


6. CSS for Tables

Using CSS, you can make tables visually appealing by customizing borders, colors, padding, and more.

Table with HTML and CSS

/* CSS */ table { width: 100%; border-collapse: collapse; margin-bottom: 20px; } th, td { border: 2px solid #ddd; padding: 8px; text-align: left; } th { background-color: magenta; font-weight: bold; } tr { background-color: grey; } tr:hover { background-color: red; } /* HTML */ <table> <tr> <th>Subject</th> <th>Grade</th> </tr> <tr> <td>Math</td> <td>A</td> </tr> <tr> <td>Science</td> <td>B</td> </tr> </table> ---------------------------------------
Subject Grade
Math A
Science B

Explanation:

  1. table {}: Applies styles to all table elements.
  2. th, td {}: Applies styles to both table headers and data cells.
  3. th {}: Adds additional styles to table headers.
  4. tr {}: Styles applied to table rows.
  5. tr:hover {}: Adds a hover effect to rows.

7. Example of a Webpage with CSS

Below is an dark-box of a complete HTML file styled with CSS:

/* HTML */ <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> <title>Styled Page</title> </head> <body> <h1>Welcome to CSS</h1> <p class="main">This paragraph is styled using a class.</p> <p id="unique">This paragraph is styled using an ID.</p> </body> </html> /* CSS */ h1 { color: purple; text-align: center; } .main { color: green; font-size: 16px; } #unique { color: red; font-weight: bold; } ---------------------------------------

Welcome to CSS

This paragraph is styled using a class.

This paragraph is styled using an ID.


8. Refering to Insert

This insert is provided during your exams and do use it only for reference

CSS Reference