2

I have a bunch of classes, here are 3 as example:

.col-1-3{
    width:calc(100%/(3/1));
}
.col-2-3{
    width:calc(100%/(3/2));
}
.col-1{
    width:100%;
}

(all of these are inline-block and position relative if that info might be useful....)

Now, if an element with any of those classes applied, also have another class applied, lets call it 'batman', I need the element to grow 30px in width.

Without touching each and everyone of my .col-* classes and in there add the 30px, is there any! other way to add to an elements width? see example pseudo code:

.batman{
    add-to-width:30px;
}

I was thinking perhaps with :before and/or :after. Adding a pseudo element and somehow move it 15px to the left/right and the main element would follow/grow...but it didnt work....

requirement: strictly css, no javascript please.

Any idea?

thanks in advance!! :)

4
  • Can you use something other than width, like padding or margin? Commented Sep 26, 2016 at 20:05
  • padding, margin, or use calc(100% + 30px) Commented Sep 26, 2016 at 20:08
  • and all of this said, anything other than determining if and how it could be done, you should probably consider using SASS to create a grid layout and anything that breaks the mold, should have very modular css Commented Sep 26, 2016 at 20:09
  • Yeah I guess sass is the only way, thats what i was afraid of. calc() on the col* classes is exactly what im trying not to do. tried margin and padding, did not solve my problem. thanks though guys! Commented Sep 26, 2016 at 21:07

1 Answer 1

6

I believe You have 2 options - first is something like a margin since margins will stack, the other is using calc()

.batman {
    margin: 0 15px;
}

or

.batman {
    width: calc(100% + 30px);
}
Sign up to request clarification or add additional context in comments.

7 Comments

calc is what im trying not to do. Tried margin but it didnt solve my problem, will try again. thank you Adjit
@FrankUnderwood Why are you trying to avoid calc?
Because then I have to do it on every single col-* class, as they got different widths. If i instead could tack on the 30px, regardless of the current width, by just creating one new class, it would save a lot of (almost) repeating myself.
This question is related, to the answer of a question i asked yesterday: stackoverflow.com/questions/39685497/gutter-between-divs
@FrankUnderwood well you could just create a rule that selects any class that starts with col- if you are using CSS3 (which you are if you are already utilizing calc) stackoverflow.com/questions/3338680/…
|

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.