Monday 5 August 2024

1.3. CSS [Cascading Style Sheet]

 

Introduction to CSS

CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of a document written in HTML. CSS allows you to style web pages, controlling layout, colors, fonts, and more, to create visually appealing and user-friendly websites.

How CSS Works

CSS works by selecting HTML elements and applying styles to them. It uses a set of rules that consist of selectors and declarations. The declarations contain properties and values that determine how the elements should be styled.

Basic CSS Syntax

A CSS rule-set consists of a selector and a declaration block:

css code
selector { property: value; property: value; }
  • Selector: Identifies the HTML element(s) to style.
  • Property: The aspect of the element to style (e.g., color, font-size).
  • Value: The value for the property (e.g., red, 16px).

Types of CSS

  1. Inline CSS: Styles are applied directly to HTML elements using the style attribute. This method is not recommended for large projects due to poor maintainability.

    html code
    <h1 style="color: blue; font-size: 24px;">Hello World</h1>
  2. Internal CSS: Styles are defined within a <style> element inside the <head> section of an HTML document.

    html code
    <head> <style> h1 { color: blue; font-size: 24px; } </style> </head>
  3. External CSS: Styles are defined in an external file with a .css extension, linked to the HTML document using a <link> element. This method is preferred for separating content from design.

    html code
    <head> <link rel="stylesheet" href="styles.css"> </head>

Sample Code: Styling a Web Page

Let's look at a simple HTML page styled using external CSS.

HTML File (index.html)

html code
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Styled Web Page</title> <link rel="stylesheet" href="styles.css"> </head> <body> <header> <h1>Welcome to My Website</h1> <nav> <ul> <li><a href="#home">Home</a></li> <li><a href="#about">About</a></li> <li><a href="#contact">Contact</a></li> </ul> </nav> </header> <main> <section> <h2>About Us</h2> <p>We are a company that values excellence and innovation.</p> </section> </main> <footer> <p>&copy; 2024 My Website</p> </footer> </body> </html>

CSS File (styles.css)

css code
/* Reset default margin and padding */ body, h1, h2, p, ul { margin: 0; padding: 0; } /* Set base font styles */ body { font-family: Arial, sans-serif; line-height: 1.6; background-color: #f4f4f9; color: #333; } /* Style the header */ header { background-color: #333; color: #fff; padding: 10px 0; text-align: center; } /* Style the navigation menu */ nav ul { list-style: none; display: flex; justify-content: center; } nav ul li { margin: 0 15px; } nav ul li a { color: #fff; text-decoration: none; font-weight: bold; } nav ul li a:hover { color: #ff6347; } /* Style the main content */ main { padding: 20px; } section h2 { color: #333; margin-bottom: 10px; } section p { font-size: 18px; margin-bottom: 20px; } /* Style the footer */ footer { background-color: #333; color: #fff; text-align: center; padding: 10px 0; position: fixed; bottom: 0; width: 100%; }

Explanation of CSS Styles

  • Reset Default Styles: The default margin and padding are reset to ensure consistent styling across different browsers.

  • Base Font Styles: The base font family and line height are set for the entire page, along with the background and text colors.

  • Header and Navigation: The header and navigation are styled with a dark background color and white text. The navigation links have a hover effect to improve user interaction.

  • Main Content: The main section is padded for spacing, with headings and paragraphs styled for readability.

  • Footer: The footer is styled similarly to the header, with a fixed position at the bottom of the page to ensure it's always visible.

Conclusion

CSS is a powerful tool for enhancing the appearance of web pages. By using CSS, you can separate content from presentation, making it easier to maintain and update your web designs. This introduction provides a foundation for understanding how to use CSS to create attractive, responsive websites. As you become more familiar with CSS, you can explore advanced features like Flexbox, Grid, and animations to create even more dynamic and engaging designs.s.

No comments:

Post a Comment

2.9. Bonus - Student friendly Learning Hub Web Site

Project:  Designing a creative and student-friendly homepage for a learner website involves balancing aesthetics, ease of navigation, and en...