Is it permissible to utilize multiple ":root" selectors in a CSS file?

One of the features in TWBS 4 is the presence of color variables within the :root selector in bootstrap.css.

Is it possible for my child stylesheet to also include a :root selector, allowing me to define my own variables?

I assume that :root {} can be overridden and expanded upon like any other selector, although I haven't come across any discussions or examples regarding this.

Currently, I have placed my color variables within a div {} selector where they function properly. However, I aim to apply them to elements beyond just <div>, indicating the necessity for my custom :root section.

The following example illustrates my current setup:

my_stylesheet.css

div {
  --myColor1: rgba(16, 128, 0, 1.0);
  --myColor2: rgba(16, 128, 0, 0.6);
}

Nonetheless, I desire to structure it as follows:

:root {
  --myColor1: rgba(16, 128, 0, 1.0);
  --myColor2: rgba(16, 128, 0, 0.6);
}

Answer №1

When it comes to CSS, the :root selector holds a special place as it represents the <html> tag and carries higher specificity compared to the html selector. This selector is often utilized for declaring CSS variables but can also be treated similar to any other common selector -- combining multiple selectors in various valid ways.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can one get rid of a sudden strong beginning?

Recently, I delved into the world of CSS animation and encountered a frustrating issue. I've tried numerous methods and workarounds to achieve a smoothly looping animation without interruptions. Despite my efforts, I have not been able to replicate th ...

Press the button to update several span elements

Imagine I have multiple span elements like this: <span>A</span> <span>B</span> <span>C</span> <span>D</span> and a div element (which will be converted to a button later) named "change". <div id="chan ...

Insert HTML elements into nested CSS classes

My goal is to utilize jQuery for looping through classes and adding text to an HTML element. I am currently working with the following example HTML: <div class="question"> <div class="title"> <strong>Here's the question ...

Is it possible to restrict the application of jquery .css() to a particular media query only?

Whenever a user's browser width is below 1160px, a media query is set up to hide my website's right sidebar. They can bring it back by clicking on an arrow icon, causing it to overlap the content with absolute positioning. To achieve this functi ...

What is the best way to combine multiple classes when declaring them in CSS?

I'm attempting to combine the following code snippets (possibly misunderstood the term) - is there a way to have them both perform the same function without creating another class with identical attributes? .games span { display: inline-block; ...

Issue with customizing the appearance of the selected option in a dropdown menu

Is there a way to change the background color of each option in a select box when it is selected? Currently, when an option is clicked, it appears blue along with the selected text color. I have tried various selectors like :active, :focus, ::selection, [s ...

Seeking assistance to identify the Xpath or CSS selector for the specified element - can anyone

I've been struggling for hours to locate the selector for that success message at the bottom of the page in my Selenium C# test. Unfortunately, I haven't had any luck so far. Every time I run the test, it fails due to not being able to find the e ...

Text remains unaltered on input focus

When I click on the input field, I expect the label to transform but it doesn't. However, using ".user-input:focus, .user-input:valid" changes the input, indicating that it should work with the label as well. .user-input:focus+.user-label, .user-in ...

Currently experimenting with jQuery in conjunction with the reddit API to generate individual divs for every post

Issue with displaying post titles, URLs, and permalinks separately. Need them to display together for each post. See the code below: $(document).ready(function() { $.getJSON( "http://www.reddit.com/r/pics.json?jsonp=?", function foo(data) { ...

Issue with negative margin not functioning properly in Internet Explorer 7

My code snippet looks like this: <style> .earthlogo{float: right; margin: -270px 0px 0px 0px;} </style> <div class="banner"> <p>ENVISIONING A BETTER TOMORROW</p> <div class="earthlogo ...

The middle toggle line stubbornly refuses to properly disappear

I'm facing an issue with the toggle button on my practice site. When clicked, it undergoes a transition and displays a cross or X symbol. I've used CSS to create a cross shape by hiding the middle line. However, after the transition, the middle l ...

Dynamically modifying the styling of elements within an iframe

In my current project, I encountered a challenge with changing the background color to black and the body text color to white within an iframe. Additionally, all elements that are originally styled with black in an external stylesheet also need to be chang ...

CSS: The addition selection operator is not functioning as expected

The span's background color is not changing correctly when the radio button is changed. Why is this happening and how can it be fixed? div { margin: 0 0 0.75em 0; } .formgroup input[type="radio"] { display: none; } input[type="radio"], label { ...

The placement of the div only shifts in Firefox

Looking to place a small 25px - 25px image at the end of a line of text? Using a DIV (re1DOC) to position the element can make it easier. However, the issue arises when the element displays correctly in IE & Chrome, but not in Firefox. The text shifts slig ...

Creating a layout in jQuery Mobile with two HTML <input type="button"> elements positioned side by side and each taking up 50% of the screen

After trying numerous strategies, I am still struggling to place two buttons next to each other evenly: <input type="button" value="This week's Schedule" onclick= 'window.location.href = dic[current_sunday]' /> <input type="button ...

How can I design an SVG page similar to Coin360 and Market Maps?

I need to design a homepage similar to coin360.com, where I can display market maps and cryptocurrency rates. This page will be created using SVG elements for the answers section. Is there a pre-made template available for this design, or can someone gui ...

An unusual icon with three varying sizes in a single file

I have come across an interesting favicon file. Click here to download the favicon file Unfortunately, I am unable to upload this image file as it does not meet the required format standards. Upon viewing the file in Windows 7, a fascinating discovery w ...

Which language should I focus on mastering in order to create a script for my Epson receipt printer? (Check out the manual for reference)

Printer Model: TM-T88V User Manual: Receipt-marker Web template for the printer: (Provided by Epson) I'm completely new to computer languages and have been trying to understand how to print receipts. I've researched XML, HTML, CSS, and Jav ...

Hover-triggered text animation error

Hey there, I'm currently experimenting with creating an animation that involves moving text up and revealing some content when hovering over a card. It seems like everything works fine when I hover over the card, but as soon as my cursor reaches the t ...

Error: Bootstrap/CSS not functioning properly on IE and ED browsers

Currently, I am tackling a web application project, and much to my surprise, I discovered that the CSS/bootstrap is not functioning properly on Internet Explorer and Microsoft Edge. Despite attempting various solutions found on Stack Overflow, none seem to ...