Have multiple classes css

Multiple Class / ID and Class Selectors

They look nearly identical, but the top one has no space between #header and .callout while the bottom one does. This small difference makes a huge difference in what it does. To some of you, that top selector may seem like a mistake, but it’s actually a quite useful selector. Let’s see the difference, what that top selector means, and exploring more of that style selector. Here is the “plain English” of #header .callout :

Select all elements with the class name callout that are decendents of the element with an ID of header.

Side-by side comparison of two selectors with the divs they target highlighted. On the left, is an example of a selector that will affect elements with an ID of header and a class of callout. On the right, is an an example that will affect elements with a class of callout that are children of elements with an ID of header.

Maybe this graphic will make that more clear:

Combinations of Classes and IDs

The big point here is that you can target elements that have combinations of class es and ID s by stringing those selectors together without spaces.

Target an element that has all of multiple class es. Shown below with two class es, but not limited to two.

We aren’t limited to only two here, we can combine as many class es and ID s into a single selector as we want.

Although bear in mind that’s getting a little ridiculous. Learn more about how to select IDs, classes, and multiple classes at DigitalOcean.

So how useful is all this really? Especially with ID s, they are supposed to be unique anyway, so why would you need to combine it with a class ? I admit the use cases for the ID versions are slimmer, but there are certainly uses. One of those is overriding styles easily.

Or perhaps prefacing the selector with something even more specific. More useful is multiple class es and using them in the “object oriented” CSS style that is all the rage lately. Let’s say you had a bunch of div s on a page, and you used multiple various descriptive class names on them:

They all share the class “box”, which perhaps sets a width or a background texture, something that all of them have in common. Then some of them have color names as class es, this would be for controlling the colors used inside the box. Perhaps green means the box has a greenish background and light green text. A few of them have a class name of “border”, presumably these would have a border on them while the rest would not. So let’s set something up:

.box < width: 100px; float: left; margin: 0 10px 10px 0; >.red < color: red; background: pink; >.blue < color: blue; background: light-blue; >.green < color: green; background: light-green; >.border

Cool, we have a good toolbox going here, where we can create new boxes and we have a variety of options, we can pick a color and if it has a border or not just by applying some fairly semantic class es. Having this class name “toolbox” also allows us to target unique combinations of these class es. For example, maybe that black border isn’t working on the red boxes, let’s fix that:

Читайте также:  Java trees and graphs

Three different colored boxes. A red box, a blue box, and a green box. This image depicts how the multiple class selectors can be used in a way that helps the developer understand what the code is doing.

Based on this demo page.

Also important to note here is that the specificity values of selectors like this will carry the same weight as if they were separate. This is what gives these the overriding power like the example above. Learn more about specificity in CSS at DigitalOcean.

All good current browsers support this as well as IE back to version 7. IE 6 is rather weird. It selects based on the last selector in the list. So “ .red.border ” will select based on just “ .border “, which kinda ruins things. But if you are supporting IE 6, you are used to this kind of tomfoolery anyway and can just fix it with conditional styles.

Источник

CSS Multiple Class Selectors

Example of CSS Multiple Class Selectors

Using multiple class selectors for applying styles to elements is a powerful approach. There are practical use cases when using a single selector is not enough, or adds lesser value than it adds. In this article, we will observe how multiple selectors enhance our style code in a real-life example.

Note that this article is about using multiple class selectors, not combinators. An example of multiple selectors is .class1.class2 — which targets an element having both, class1 and class2, classes. On the other hand, .class1 > .class2 is a combinator that targets all the direct children (not all descendants) of .class1 with the .class2 applied.

Let’s consider that you are tasked to build a file-sharing UI. It offers the following basic features

Both the files and recipients are represented as a grid of icons. Once a file/member is selected, its icon should be styled with a specific design to represent the selection. Both the icon types have different appearances and are required to have different selection styles.

We can use one class .icon to style the default appearance and another class .selected . But the problem is, the icons have different default and selected state styles. Multiple class selectors solve this issue smoothly. We can separate the styles as .file-icon.selected and .recipient-icon.selected rules.

The following codepen demonstrates such a UI and styles. Multiple class selector styles have been placed at the top of styles for emphasis. The general details of the implementation of this UI are out of the scope of this article. Interested readers can go through the codepen to get to know how it has been built.

 

Select Files

Select Recipients

/* Multiple Selectors Demonstration */ span.file-icon:not(.selected):hover, .file-icon:not(.selected):hover .page-turn < border-color: lightgreen; >.file-icon:not(.selected):hover .line < background-color: lightgreen; >span.file-icon.selected < background-color: lightgreen; >.avatar:not(.selected):hover < border-color: lightgreen; >.avatar.selected .head, .avatar.selected .body < background-color: lightgreen; >/* UI styling */ * < font-family: sans-serif; margin: 0; padding: 0; >body < background-color: rgba(72, 171, 224, 0.15); display: flex; justify-content: center; align-items: center; height: 100vh; >.file-sharing-card < display: flex; justify-content: space-around; align-items: center; text-align: center; background-color: white; width: 80%; height: 80%; padding: 10px; border-radius: 15px; box-shadow: 0px 0px 15px 9px lightgray; >span.file-icon < position: relative; box-sizing: border-box; display: inline-block; height: 20vh; width: 16vh; margin: 5px; border: 2px solid black; border-radius: 5px; border-top-right-radius: 20px; cursor: pointer; >.file-icon .page-turn < display: inline-block; height: 35%; width: 30%; position: absolute; right: 0; border-left: 2px solid black; border-bottom: 2px solid black; >.content-lines < display: flex; flex-direction: column; justify-content: space-between; position: absolute; top: 50%; left: 25%; width: 50%; height: 30%; >.content-lines .line < height: 1px; background: black; >.avatar < display: inline-block; position: relative; display: inline-block; height: 20vh; width: 20vh; margin: 5px; border: 2px solid black; border-radius: 50%; cursor: pointer; >.avatar .head < display: inline-block; position: absolute; display: inline-block; height: 30%; width: 30%; border: 2px solid transparent; border-radius: 20px; top: 13%; left: 50%; transform: translateX(-50%); background: #48abe0; >.avatar .body

Читайте также:  How to set locale java

Multiple CSS class selectors add further value when the states of different elements have partially shared styles. Instead of repeating the style code, we can share the overlapped ones and override the unique ones. Let’s demonstrate this using an example.

Let’s assume we are building a set of messages with highlighted text content that looks like the following:

This text has an important Random message

This text has an important Success message.

This text has an important Warning text.

This text has an important Error message.

Our requirements are as follows:

  • The highlighted text should be bold and large
  • Normal text will be highlighted as blue. Success, Warning, Error states will have Green, Yellow/Gold, Red colors respectively

We quickly note that our normal important texts have a large font-size and blue color.

Moving on, we notice that the success, warning, and error texts share the same font size although different colors. Instead of creating a separate class as important-success and repeating the styles besides the unique color, we can share overlapped styles and override unique ones using multiple class selectors.

.important.success < color: green; >.important.warning < color: gold; >.important.error

This is a basic example with very few properties. But in real-world codebases, this value addition can save large amounts of code from duplication.

Conclusion

CSS offers a very powerful targeting approach using multiple class selectors. This approach allows easy segregation of styles for different elements while reusing the names of classes. This is particularly handy if the desired name represents a generic state (such as .selected in the above example) but the style requirements are different. It adds even more value when we need to share some styles across multiple elements.

UnusedCSS helps website owners remove their unused CSS every day. Sign Up to see how much you could remove.

You Might Also Be Interested In

Источник

How to add multiple classes to HTML elements

Last Updated Jun 18, 2022

Any HTML element can have as many different classes as needed to style the element using CSS effectively.

To assign multiple classes to a single HTML element, you need to specify each class name inside the class attribute separated with a blank space.

For example, the following

element is assigned one class called heading :

 The following example below shows how to assign two classes: heading and font-large to the same paragraph:
 A class name that has more than one word is commonly joined with a dash ( - ) this is also known as the kebab-case.

There is no limit to how many class names you can add to a single HTML element. But keep in mind that you need to keep the class names intuitive and descriptive.

I’d recommend you put 10 class names at maximum in a single HTML element to keep your project maintainable.

HTML multiple classes for styling purpose

HTML classes are used for styling the elements rendered on the browser.

By creating multiple classes that serve different styling purposes, you can reuse the classes in many different elements without having to repeat the styling.

For example, you can have a class called font-small to adjust the font-size property to smaller than regular font size, while the font-large class will make the font-size larger than regular.

Suppose you have a style sheet with the following CSS rules:

You can add the CSS classes above to your HTML elements.

Here’s an example of adding the classes to a element:

  • What property will this class modify? Use it as the first part of your class name.
  • What is the value of the property being modified by this class? Use it as the second part of your class name

For example, if you’re changing the background-color property to grey , then the class name .bg-color-grey would be a good choice.

If the value of the property is not exact, then you can use a word that would describe the output relative to a regular element.

For example, font-small describes that the font size for the assigned element will be smaller than a regular element, so you can use that instead of font-size-12 although the latter is more precise.

Sometimes you can also abbreviate small as sm and create an xs style for extra-small :

Writing good HTML class names takes practice and time because classes can be used by both CSS and JavaScript to manipulate the presentation and the interactivity of HTML pages.

You’re going to get better as you write more code, and the rule of two above will help you start on the right mindset 👍

Now you’ve learned how to assign multiple classes to a single HTML element. Nice work!

Learn JavaScript for Beginners 🔥

Get the JS Basics Handbook, understand how JavaScript works and be a confident software developer.

A practical and fun way to learn JavaScript and build an application using Node.js.

About

Hello! This website is dedicated to help you learn tech and data science skills with its step-by-step, beginner-friendly tutorials.
Learn statistics, JavaScript and other programming languages using clear examples written for people.

Type the keyword below and hit enter

Tags

Click to see all tutorials tagged with:

Источник

Оцените статью