Skip to content

Practical Examples of Using CSS Selectors to Style Web Pages

Posted on:April 14, 2023 at 11:00 AM

When it comes to styling web pages, CSS selectors are an essential tool for targeting specific elements and applying styles to them. Here are some practical examples of how selectors can be used to create stylish and functional web pages.

  1. Navigation Menus

    Navigation menus are a crucial element of any website. By using CSS selectors, we can style the menu to make it stand out and easy to use.

    To create a simple horizontal navigation menu, we can use an unordered list and apply styles to the list items

    HTML:

    <nav>
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </nav>

    CSS:

    nav ul {
      list-style: none;
      display: flex;
    }
    
    nav li {
      margin-right: 20px;
    }
    
    nav a {
      text-decoration: none;
      color: #333;
    }
    
    nav a:hover {
      color: #666;
    }

    In this example, we use the ul and li selectors to target the list and list items respectively, and apply styles to them. We also use the a selector to style the links within the list items, and the :hover pseudo-class to add a hover effect.

  2. Buttons

    Buttons are another important element of web design, and can be styled in many different ways. Here’s an example of how we can use CSS selectors to create a simple button with a hover effect

    HTML:

    <button class="btn">Click Me</button>

    CSS:

    .btn {
      display: inline-block;
      padding: 10px 20px;
      border-radius: 5px;
      background-color: #333;
      color: #fff;
      text-decoration: none;
    }
    
    .btn:hover {
      background-color: #666;
    }

    In this example, we use the .btn class selector to target the button element, and apply styles to it. We also use the :hover pseudo-class to add a hover effect when the user hovers over the button.

  3. Forms

    Forms are a common feature of many websites, and can be styled in various ways to make them more attractive and user-friendly. Here’s an example of how we can use CSS selectors to style a basic form

    HTML:

    <form>
      <div>
        <label for="name">Name:</label>
        <input type="text" id="name" name="name">
      </div>
    
      <div>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email">
      </div>
    
      <div>
        <label for="message">Message:</label>
        <textarea id="message" name="message"></textarea>
      </div>
    
      <button type="submit">Send</button>
    </form>

    CSS:

    form div {
      margin-bottom: 10px;
    }
    
    form label {
      display: block;
      margin-bottom: 5px;
      font-weight: bold;
    }
    
    form input,
    form textarea {
      padding: 5px;
      border-radius: 5px;
      border: 1px solid #ccc;
      font-size: 16px;
    }
    
    form input:focus,
    form textarea:focus {
      border-color: #333;
    }
    
    form button {
      display: inline-block;
      padding: 10px 20px;
      border-radius: 5px;
      background-color: #333;
      color: #fff;
    }

Conclusion

CSS selectors are a powerful tool for web developers to style their web pages and create a visually appealing design. Understanding the different types of selectors and how they can be combined can allow developers to create more specific styles and target specific elements on their pages. By using practical examples and code snippets, developers can learn how to use selectors to create navigation menus, style buttons, and customize the appearance of their web pages. With a solid understanding of CSS selectors, developers can create professional-looking web pages that are both functional and visually appealing.

What’s next?

In the coming articles, we will be covering how to apply CSS to HTML elements. Don’t forget to follow and share this with your friends that are just starting their web development journey