Student
Student

Reputation: 28375

Is there a way to use CSS attribute as value?

I mean, is there a way to do something like:

width: 92%;
left: (100-width)/2;

so I can change the width without care with the "left"?

obs. I don't want to know about other ways of doing this centering, like with magins=auto, I just want to know if I can use an css attribute as a value to other attribute. Thanks! :)

Upvotes: 4

Views: 204

Answers (4)

krusty.ar
krusty.ar

Reputation: 4060

There's no way unless you are generating the css itself dynamically, with this for instance

Also some browsers have some support for css variables, but you can't do math on them as AFAIK

Upvotes: 1

David Thomas
David Thomas

Reputation: 253466

Currently no, although, css calculations seem to allow for the possibility of this, albeit at a later date.

It might be worth reading this pre-existing question: Value calculation for CSS

Upvotes: 3

Pekka
Pekka

Reputation: 449753

Not in pure CSS, no.

CSS precompilers like LESS offer it, though.

Upvotes: 6

Goran Jovic
Goran Jovic

Reputation: 9508

As far as I know: No

EDIT:

You could do it from JavaScript using DOM like:

object.style.left = (100 - object.style.width) / 2

But, you would have to call this code, every now and then, and I don't think that's what you intended.

Upvotes: 0

Related Questions