Select class start with css

CSS selectors

In CSS, selectors are used to target the HTML elements on our web pages that we want to style. There are a wide variety of CSS selectors available, allowing for fine-grained precision when selecting elements to style. In this article and its sub-articles we’ll run through the different types in great detail, seeing how they work.

Prerequisites: Basic computer literacy, basic software installed, basic knowledge of working with files, HTML basics (study Introduction to HTML), and an idea of how CSS works (study CSS first steps.)
Objective: To learn how CSS selectors work in detail.

What is a selector?

A CSS selector is the first part of a CSS Rule. It is a pattern of elements and other terms that tell the browser which HTML elements should be selected to have the CSS property values inside the rule applied to them. The element or elements which are selected by the selector are referred to as the subject of the selector.

Some code with the h1 highlighted.

In other articles you may have met some different selectors, and learned that there are selectors that target the document in different ways — for example by selecting an element such as h1 , or a class such as .special .

Читайте также:  Parsing char to int in java

In CSS, selectors are defined in the CSS Selectors specification; like any other part of CSS they need to have support in browsers for them to work. The majority of selectors that you will come across are defined in the Level 3 Selectors specification and Level 4 Selectors specification, which are both mature specifications, therefore you will find excellent browser support for these selectors.

Selector lists

If you have more than one thing which uses the same CSS then the individual selectors can be combined into a selector list so that the rule is applied to all of the individual selectors. For example, if I have the same CSS for an h1 and also a class of .special , I could write this as two separate rules.

h1  color: blue; > .special  color: blue; > 

I could also combine these into a selector list, by adding a comma between them.

White space is valid before or after the comma. You may also find the selectors more readable if each is on a new line.

In the live example below try combining the two selectors which have identical declarations. The visual display should be the same after combining them.

When you group selectors in this way, if any selector is syntactically invalid, the whole rule will be ignored.

In the following example, the invalid class selector rule will be ignored, whereas the h1 would still be styled.

h1  color: blue; > ..special  color: blue; > 

When combined however, neither the h1 nor the class will be styled as the entire rule is deemed invalid.

Types of selectors

There are a few different groupings of selectors, and knowing which type of selector you might need will help you to find the right tool for the job. In this article’s subarticles we will look at the different groups of selectors in more detail.

Type, class, and ID selectors

This group includes selectors that target an HTML element such as an .

It also includes selectors which target a class:

Источник

wildcard * in CSS for classes

I have these divs that I’m styling with .tocolor , but I also need the unique identifier 1,2,3,4 etc. so I’m adding that it as another class tocolor-1 .

 
tocolor 1
tocolor 2
tocolor 3
tocolor 4
.tocolor

Is there a way to have just 1 class tocolor-* . I tried using a wildcard * as in this css, but it didn’t work.

Here’s the official CSS3 site about selectors: w3.org/TR/css3-selectors And a compability list: findmebyip.com/litmus

4 Answers 4

What you need is called attribute selector. An example, using your html structure, is the following:

div[class^="tocolor-"], div[class*=" tocolor-"]

In the place of div you can add any element or remove it altogether, and in the place of class you can add any attribute of the specified element.

[class^=»tocolor-«] — starts with «tocolor-«.
[class*=» tocolor-«] — contains the substring «tocolor-» occurring directly after a space character.

More information on CSS attribute selectors, you can find here and here. And from MDN Docs MDN Docs

Cool info. Only caveat is if performance is an issue, most CSS linters will reject attribute selectors that resemble regex (e.g. using ‘*’) b/c of slow performance. With the exception of using a preprocessor (e.g. Sass), is there any other possible way of doing this?

Is there a way to check if the class attribute contains multiple substrings? Maybe something like div[class*=»foo»][class=»bar»] ?

That space in our «contains» example is pretty killer. From what I can see, it shouldn’t be there, correct?

@Thomas That is required to ensure class=»foo tocolor-red» matches, but not class=»foo fromtocolor-red-blue»

Thanks for this. I’ve been using this technique for a few years, but never realized that class^= only applies to the beginning of the HTML class entry. I thought it applied to all class names defined for the HTML tag.

This will select all ids that start with ‘term-‘ .

As for the reason for not doing this, I see where it would be preferable to select this way; as for style, I wouldn’t do it myself, but it’s possible.

This is a superior solution I voted up because using style classes for identification is poor semantics, poor HTML design, and adds extra code. Your solution combines ID with style so is more compact. I still do not understand why so many new developers are still terrified of using the «id» attribute, when it is designed for unique identification of tags, allows fast access by scripts, is supported in every browser ever made the past 20+ years, and has high CSS specificity. Silly kids

div[class|=’tocolor’] will match for values of the «class» attribute that begin with «tocolor-«, including «tocolor-1», «tocolor-2», etc.

Beware that this won’t match

[att|=val]

Represents an element with the att attribute, its value either being exactly «val» or beginning with «val» immediately followed by «-» (U+002D)

Источник

Match all elements having class name starting with a specific string [duplicate]

Is it possible to use a «wildcard» for elements having a class name starting with a specific string in CSS3? Example:

3 Answers 3

The following should do the trick:

div[class^='myclass'], div[class*=' myclass']

Edit: Added wildcard ( * ) as suggested by David

Only if the class beginning with myclass is also the first class-name contained within the class attribute. You might want to try div[class*=myclass] < /* . stuff. */>.

Schweet! I always forget about the attribute thing (probably because it looks so ugly.) What I -still- don’t understand is this: According to the W3C page: w3schools.com/cssref/css_selectors.asp the syntax is: div[class*=»test»] Yet, it -seems- to work like this, with no ‘element’ specified: [class*=»test»] . which I don’t get. IOW, -why- does this work? (I’m not complaining, I like it -better-!) I just don’t want to start using it and find out that it’s a bug that later gets ‘fixed’. Any ideas? THANKS! —JC

@jchwebdev—it is not a bug. The attribute selector is just like any other selector (it can be used as a «stand alone»). It is no different that .myClass potentially being used with div.myClass instead. The attribute selector can stand alone (matching any elements that meet that criteria) or be narrowed to specific element. So don’t worry about it being odd to have [class*=»myClass»] all by itself.

Источник

css class starts with & CSS class selector

css class starts with use a wildcard to select all div with a class that starts with str. Match an element that has the specified class.

css class starts with

A class selector in CSS starts with a dot (.). A CSS class is an attribute used to define a group of HTML elements in order to apply unique styling and formatting to those elements with CSS.

Match all elements having class name starting with a specific string

CSS [attribute^=value] Selector

Example

div[class^=’loverank’], div[class*=’ loverank’]

Wildcard Selectors (*, ^ and $) in CSS for classes

    /* Define styles of selected items, h1 and rest of the body */ [class*="str"] < /* WE USE * HERE */ background: red; color: white; >h1 < color:red; >body 

www.pakainfo.com

CSS: Class name selector- name starts with

You can use the jQuery filter() function to match all elements having class name starting with a specific string.

Here’s an example code snippet that demonstrates how to use filter() to select elements with class names that start with a specific string:

In this example, we use the attribute-starts-with selector ([attribute^=”value”]) to select all

elements with class names that start with the string “foo”. We then apply a style to those elements using the css() function.

The resulting output will be the first two

I hope you get an idea about css class starts with.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Also Read This 👉 full width responsive slider html5 — How To Create Responsive Full Width Carousel / Slider with Bootstrap 3, HTML5 and jQuery?

Источник

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