Main menu

Pages

CSS : Select multiple elements , Use different classes , Naming conventions

 Select multiple elements



To select multiple elements using CSS, you can use the comma-separated selector syntax. Here's an example:

h1, h2, h3 { color: blue; }

This CSS rule selects all h1, h2, and h3 elements on the page and applies the color property with a value of blue to them.

You can also use the universal selector (*) to select all elements on the page. For example:

* { margin: 0; padding: 0; }

This CSS rule sets the margin and padding properties to 0 for all elements on the page.

You can combine different types of selectors to target specific elements. For example:

ul.nav li a { color: red; }

This CSS rule selects all a elements that are descendants of li elements, which in turn are descendants of ul elements with a class of nav, and applies the color property with a value of red to them.

Use different classes

Using different classes allows you to apply different styles to multiple elements in your HTML document. You can define different classes in your CSS stylesheet and then apply those classes to HTML elements using the class attribute.

Here's an example of how to use different classes in CSS:

/* Define styles for class 'class1' */ .class1 { color: blue; font-size: 16px; } /* Define styles for class 'class2' */ .class2 { color: red; font-size: 20px; }

And here's an example of how to apply those classes to HTML elements:

<p class="class1">This paragraph will be blue with a font size of 16px.</p> <p class="class2">This paragraph will be red with a font size of 20px.</p>

In this example, the first paragraph will be styled using the class1 class, which will make the text blue with a font size of 16 pixels. The second paragraph will be styled using the class2 class, which will make the text red with a font size of 20 pixels.

Using different classes like this can help you keep your CSS code organized and make it easier to apply consistent styles across your HTML document.

Naming conventions


Naming conventions in CSS are a set of guidelines or standards that help developers write consistent and easy-to-understand code. Adhering to naming conventions can make it easier to maintain, update and collaborate on CSS code.

There are several naming conventions used in CSS, including:

  1. BEM (Block, Element, Modifier) - this convention uses a naming structure of .block__element--modifier to describe the purpose and function of each element.

  2. SMACSS (Scalable and Modular Architecture for CSS) - this convention uses a modular approach to CSS and emphasizes the use of classes over element selectors.

  3. OOCSS (Object-Oriented CSS) - this convention emphasizes the use of reusable, modular code that can be easily extended and modified.

  4. Atomic CSS - this convention uses small, atomic classes that can be combined to create complex styles.

No matter which convention you choose, it is important to stick to a consistent naming convention throughout your codebase. This can help make your code more maintainable and easier to understand for yourself and other developers.





Comments

table of contents title