1

Would it be possible to create a css class/id that would add a value(say, padding for example) to another class's same attrib value?

Let me try to put it down more clearly.

.someClass{
padding: 20px;
}

.thickenMe{
padding: 5px;
}

And when i apply these classes as follows,

<div class='someClass thickenMe'>
<!--planning to beef up this div-->
</div>

This div's net padding should become 25px.

Would it be possible using css only? Its just a thought!

2
  • .thickenMe would win in that case (5px of padding) Commented Aug 11, 2012 at 16:09
  • Indeed. Was wondering if there are any method to sum up those values. Commented Aug 11, 2012 at 16:12

3 Answers 3

2

This is not possible using pure CSS.

You could, however, write a CSS clause for each element that can be "thickened" like so:

.someClass{
padding: 20px;
}

.someClass.thickenMe{
padding: 25px;
}

Another alternative would be to use margin as well as padding, like so:

.someClass{
padding: 20px;
}

.thickenMe{
margin: 5px;
}

That might not be possible though, depending on your other CSS.

The easiest solution, although not pure CSS, would be to use JavaScript. Here is an example using JQuery:

var prevPad = $('.thickenMe').css('padding').replace("px", "");
prevPad =  parseInt(prevPad);
$('.thickenMe').css('padding', prevPad + 5 + "px");​
Sign up to request clarification or add additional context in comments.

Comments

0

No. You need to use client-side scripting (i.e. JavaScript) if you want to change attributes dynamically. Or, if you just want to define styles by compiling from some dynamic sources, try LESS or SASS.

Comments

0

Unfortunately, no. You could get this sort of functionality by using a CSS-processor like LESS, which gives you variables, or you could handle it through client-side scripting (easiest with jQuery), but native CSS simply doesn't work that way. Classes override each other when they specify the same attribute; they don't supplement each other.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.