Is it possible to amalgamate CSS declarations together?

You have the ability to combine CSS selectors by using a comma, like in this example:

.one, .two {
  color: #F00;
}
<div class="one">One</div>
<div class="two">Two</div>

Using this method produces the same outcome as specifying each selector individually:

.one {
  color: #F00;
}
.two {
  color: #F00;
}
<div class="one">One</div>
<div class="two">Two</div>

Merging selectors as shown above is very helpful, because it allows you to change multiple elements by only altering one value. This is particularly advantageous for adjusting color schemes.

However, can CSS declarations be combined too?

For instance, if you want to vertically center text in an element where line-height should always match height:

.test {
  border: 1px solid #000;
  padding-left: 10px;
  height: 100px;
  line-height: 100px;
}
<div class="test">Test</div>

The anticipated combined declaration of height, line-height: 100px; does not apply both declarations, resulting in an invalid property value.

In SASS, you could link line-height to height by simply using:

$height = 100px;

.test {
  border: 1px solid #000;
  padding-left: 10px;
  height: $height;
  line-height: $height;
}

Is there a way to specify that one property should use the same value as another property in raw CSS?

Answer №1

Of course, you have the capability to do so:

:root {
--height: 100px;
}
.test {
  border: 1px solid #000;
  padding-left: 10px;
  height: var(--height);
  line-height: var(--height);
}
<div class="test">Test</div>

However, it is worth noting that not all browsers fully support CSS variables - http://caniuse.com/#feat=css-variables

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

Using Phonegap alongside ons-scroller and ons-button

Recently, I have been using Phonegap with the Onsen UI system on iOS devices. I encountered an issue where buttons included within an ons-scroller were not clickable when running on an iPad or iPhone. Here is the code snippet that caused the problem: < ...

Internet Explorer (on mobile devices) does not display submenus when touched

Hello, I have a query about displaying a sublist on touch. I have created a sublist that is supposed to show up when you hover over it. You can view the demo here: @import url(//fonts.googleapis.com/css?family=Titillium+Web:400,200,300,600,700); body { ...

displaying a video within an iframe without occupying the entire screen

After successfully implementing a fullscreen video background using the "video" tag with a video hosted on my own server, I decided to switch it out for a Vimeo video. However, after making the necessary code and CSS changes, the video now appears like thi ...

A step-by-step guide on utilizing jQuery to cyclically change the background color of elements upon clicking, and resetting the colors upon clicking another element

So I've been working on a solution to change the background color for accessibility purposes on a client's website. To achieve this, I have a set of div elements with different background colors and I'm using jQuery to update the body backgr ...

Issue with text-nowrap not functioning as intended within Bootstrap 4.5 table cells

Is there a way to eliminate the space between two columns in my layout? I want to get rid of the highlighted space below. I attempted using text-nowrap, but it didn't achieve the desired result. <link href="https://stackpath.bootstrapcdn.co ...

Finding solutions for activating items and setting data sources in Bootstrap carousel thumbnails for Wordpress

I recently attempted to incorporate a bootstrap-powered carousel with thumbnails into my product slider. I managed to get it working for the main image (large image), but ran into issues when trying to resolve problems with the thumbnail section. Here is ...

`Balance in structure`

Having trouble with CSS. I would like to make this form symmetrical. Buttons should be in one column, textfields in another, and the question mark should also have its own column. Apologies if the ticket structure is not perfect, this is only my second on ...

Enhance your CSS tables by including margin columns and adding a bottom border to each row

After working on an HTML table element, I decided to include some space between the columns. To achieve this, I used the following css properties: table { border-collapse: separate; border-spacing: 36px 0px; } However, I encountered an issue when attem ...

How to change the background color of a slider using jQuery

As a newcomer to web programming, I am currently working on incorporating a slider into my website using jQuery. My goal is to change the background color of the slider to red when the value is less than 5 and green when it exceeds 5. Can this be achieved ...

"Create a flexible CSS grid with a dynamically changing number of rows and customizble

I am currently working on a project that involves creating a dynamic grid layout with two resizable columns, namely #red-container and #green-container. The goal is to make each column resizable horizontally while also allowing the main container #containe ...

Tips for confining a float within a div with fluctuating space

I have a scenario where I have multiple divs ('group') containing text, and there is a floated div ('toggle') in the bottom corner. The current code works well if the text length in 'group' is consistent, but the position of t ...

Adjusting the cell size to align with the height of another cell in the same row

Within a table row, I have two cells that contain divs and the height varies due to changing content. Is there a way for one cell to take up 100% height to match the other cell? You can see an example here. The goal is for the second cell and its div to ...

Apple's iPhone does not adhere to media query specifications

My responsive website was functioning perfectly on desktop, Android, and Windows Phone devices. However, when I asked my friends to check it on their iPhones running iOS 10, they informed me that the media queries were being ignored on iPhone models 5, 5s, ...

Latest Chrome Update Resolves Fixed Positioning Bug

I am currently facing an issue with setting the second background image in the "Great people providing great service" section to a fixed position. You can view the website here: While the fixed position works in Safari and Firefox, it used to work in Chro ...

Unleashing the Power of Reactstrap: Making Uncontrolled Collapse Panels Open by Default

Can the uncontrolled collapse be displayed as open by default? Below is the standard code for uncontrolled collapse, which is initially shown as a collapsed tab. <Button color="primary" id="toggler"> Toggle </Button> < ...

Is the navigation bar offset causing an issue?

After struggling with my navigation menu in Google Chrome, I finally found a solution. However, the nav bar is now offset on one page but not on another. For the aligned main site view, visit , and for the left-offset forum view, visit . This is the HTML ...

Use Jquery to locate and modify any occurrences of the div text through find and replace

Here is a screenshot I captured. https://i.stack.imgur.com/VUGEg.png This content resides inside my div. My goal is to replace "" with "-" and "" with "-" wherever they occur within the div, and then send this modified content via ajax using jQuery since ...

Troubleshooting CSS3 Media Queries in Notepad++

Below is the media query I've included directly in my webpage's style sheet: @media screen and (max-width: 480px) { background-color: red; } By setting the background color to red when the screen media type has a maximum width of 480px. Howe ...

Struggling to properly position my dropdown menu without causing the div content to shift downward

<div id="header"> <a href="#" id="logo"></a> </div> <div id="nav_cont"> <ul id="nav"> <li id="main_btn"><a class="mainlink" href="#">Parent 01</a></li> <li id="communities_btn">< ...

Applying a CSS border to a div that perfectly encloses a span, adjusting to the

My goal is to create a nested structure where a div contains a span element like this: ---------- |Sentence| ---------- ----------------- |It's a looooong| |sentence | ----------------- While it works well for short sentences, long ones end u ...