Explain CSS with one complete example.-GRADE 11-NEB COMPUTER

 



What is CSS?

CSS (Cascading Style Sheets) is a language used to describe the presentation (look and feel) of a web page written in HTML. CSS allows you to control the layout, colors, fonts, and overall appearance of a website. It separates content from design, making it easier to maintain the look of a site.

Structure of CSS

CSS consists of selectors and rules. The selector targets the HTML element(s) you want to style, and the rule defines what the style should be.

A basic CSS rule looks like this:

selector { 

                 property: value;

             }


  • selector: The HTML element you want to style (e.g., h1, p, div).
  • property: The aspect of the element you want to change (e.g., color, font-size).
  • value: The desired value for that property (e.g., red, 16px).


  • Example: Building a Simple Web Page with CSS

    Below is an example of an HTML document styled with CSS.

    1. HTML Structure (index.html)

    html
    <!DOCTYPE html>
    <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My CSS Example</title> <!-- Link to the external CSS file --> <link rel="stylesheet" href="styles.css"> </head> <body> <header> <h1>Welcome to My Website</h1> </header> <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> <section id="home"> <h2>Home Section</h2> <p>This is the home section of the webpage.</p> </section> <section id="about"> <h2>About Section</h2> <p>Here, you will learn more about this website and its purpose.</p> </section> <footer> <p>&copy; 2024 My Website</p> </footer> </body> </html>

    2. CSS Styling (styles.css)

    css
    /* Global styles */
    body { font-family: Arial, sans-serif; background-color: #f0f0f0; margin: 0; padding: 0; } /* Header styling */ header { background-color: #4CAF50; color: white; text-align: center; padding: 20px; } /* Navigation bar styling */ nav { background-color: #333; padding: 10px; } nav ul { list-style-type: none; margin: 0; padding: 0; } nav ul li { display: inline; margin-right: 20px; } nav ul li a { color: white; text-decoration: none; font-weight: bold; } /* Section styling */ section { margin: 20px; padding: 20px; background-color: white; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); } section h2 { color: #4CAF50; } /* Footer styling */ footer { background-color: #333; color: white; text-align: center; padding: 10px; position: absolute; bottom: 0; width: 100%; }
  • Post a Comment

    0 Comments