Apply Css to multiple class [duplicate] - css

This question already has answers here:
Using regular expression in css?
(6 answers)
Closed 3 years ago.
In my HTML template I have some CSS generated classes like :
.term-1-2
.term-2-2
.term-10-0-1
Ho can I create a CSS class to include all letters after "term" like :
.term-*
How is it possible in css ?
Thank you

[class*="term-"] { /* your rules here */ }
This is called attribute selector.
It reads
"Apply the following rules to any element with a class attribute that has term- in it."
Please note that this would also match e.g. <div class="regular-term-a">. If that is a problem, go with #Roko C. Buljan's answer.
https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

Use this rather complicated selector:
[class^="term-"], [class*=" term-"] {
}
to make sure you're targeting both
^= Starts with "term-" i.e: class="term-1-2 foo bar baz"
*= Contains (notice the leading space!) " term-" i.e: class="foo bar term-1-2 baz"
MDN: Attribute selectors

Related

How to style/reference a html tag like <h1 anyname>Text</h1> without any class or id specified within it? [duplicate]

This question already has answers here:
Select elements by attribute in CSS
(6 answers)
Closed 2 years ago.
How can I reference a html element like <h1 anyname>Text</h1> in CSS, if there is no class or id specified within it? Is there an option?
.h1 {...} is of course valid for all, but I only want to style "h1 anyname".
Thanks in advance!
You can use an attribute selector — square brackets with the attribute name.
h1[anyname] {
/* ... */
}
You can use attribute selector https://www.w3schools.com/css/css_attribute_selectors.asp
h1[anyname] { /*** your style ***/ }

CSS selector when an item has no class [duplicate]

This question already has an answer here:
Is there a CSS selector for element without any class?
(1 answer)
Closed 4 years ago.
I am trying to target all instances of an h tag but only when it has no class.
For example:
<h1 class="class1">First</h1>
<h1 class="class2">Second</h1>
<h1>Third</h1>
In this instance I only want to target Third but not the others - I found an answer (Is there a CSS selector for element without any class?) which referred to [class=""] selector but that only seems to work on:
<h1 class="">Third</h1>
It is not practical to go through and list all the instances of classes and then reference them using :not so is there another way?
I accept that this is being made worse through the use of !important etc in the current CSS on some of the h1 classes but I have inherited this and I don't have the ability to start from scratch.
if you use h1: not ([class]) this tag search all.
If there are many !important you should use it in the new css and this new css place it at the end of all .css
Example:
<h1 class="class1">First</h1>
<h1>Second</h1>
<h1>Third</h1>
h1:not([class]) {
color: red;
}

CSS Selectors, using more than one for the same classes [duplicate]

This question already has answers here:
CSS "and" and "or"
(9 answers)
Closed 4 years ago.
I'm not 100% sure if I'm saying this right, but basically, right now I am using a global CSS sheet, and in Drupal it generates a CSS sheet for each of my sub-sites based off of this one.
So we use [class*=""] to determine which subsite needs which code from this one CSS document.
If I have multiple subsites, that use one chunk of code is there a way to place an or statement?
For example...
[class*="site-1"] || [class*="site-2"] {
.class {
my css stuff
}}
Not sure if the above works or not.
[class*="site-3"] {
.class2 {
more css stuff
}
}
This will work on any element that has one of the classes:
.site1, .site2 {
...
}
So you can use it for style that is shared between the sites.

How to use attribut selector for corresponding class? [duplicate]

This question already has answers here:
Is there a CSS selector by class prefix?
(4 answers)
Closed 9 years ago.
<div class="myclass class1"></div>
Simple class selector is to be like this:
.myclass.class1{/*....*/}
But how to select it with attribute selector?
I've tried like this:
.myclass[class^=class]{/*....*/}
But not working!
If it was <div class="myclass"><div class="class1"></div></div>:
Then this would work: .myclass [class^=class]{/*....*/}
Amazingly this would work: .myclass[class*=class]{/*.....*/} but why not with ^?
Yeah I know I can just use [class^=class]{/*....*/} but I need to apply styles only for .myclass.class....
working demo with *
not working demo with ^
Try this:
.myclass[class^="myclass class"]{
color: blue;
}
[attr^=value]
Represents an element with an attribute name of attr and whose value is prefixed by "value"
DEMO

CSS selector for "foo that contains bar"? [duplicate]

This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 3 years ago.
Is there a way to make a CSS Selector that matches the following?
All OBJECT elements
which have a PARAM element inside of them
The selector
OBJECT PARAM
doesn't work, as it matches the PARAM, not the OBJECT. I'd like to apply { display:none } to the objects; it's useless to apply that to the PARAMs.
(I'm aware I could pull this off with jQuery - $("object param").closest("object") - and VanillaJS - document.querySelector("object param").closest("object") - but I'm trying to create CSS rules on a page.)
To select all OBJECT containing PARAM, in CSS:
OBJECT:has(PARAM)
To select all OBJECT having a direct child PARAM, in CSS:
OBJECT:has(> PARAM)
No, what you are looking for would be called a parent selector. CSS has none; they have been proposed multiple times but I know of no existing or forthcoming standard including them. You are correct that you would need to use something like jQuery or use additional class annotations to achieve the effect you want.
Here are some similar questions with similar results:
Is there a CSS parent selector?
CSS Parent/Ancestor Selector
Complex CSS selector for parent of active child
Only thing that comes even close is the :contains pseudo class in CSS3, but that only selects textual content, not tags or elements, so you're out of luck.
A simpler way to select a parent with specific children in jQuery can be written as (with :has()):
$('#parent:has(#child)');
Is there any way you could programatically apply a class to the object?
<object class="hasparams">
then do
object.hasparams

Resources