4

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! :)

The Student
  • 27,520
  • 68
  • 161
  • 264

4 Answers4

6

Not in pure CSS, no.

CSS precompilers like LESS offer it, though.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • LESS looks incredible! It adds all the features CSS as a language misses so dearly! +1 for the reference :-) – Bojangles Dec 10 '10 at 18:12
  • +1 thanks! The doubt now is: [can I use it with xul](http://stackoverflow.com/questions/4411973/can-i-use-less-with-xul)? – The Student Dec 10 '10 at 18:35
  • @Tom I'm not sure I understand? LESS is a *pre*-processor, you will have to run a small program on the style sheets before you can use them. The end result is normal CSS that should work everywhere – Pekka Dec 10 '10 at 19:03
  • Ah, ok.. so, with this 2 lines of code from my example, saving a file as test.less and running it with command line `less test.less` it just opens, as if was vim editor opening the file.. I'm reading about how to it.. – The Student Dec 10 '10 at 19:48
  • the [docs](http://lesscss.org/docs) talk about `lessc`, but I found just `less` with ubuntu's apt-get... – The Student Dec 10 '10 at 19:50
  • "`less`" is another thing, a text pager from UNIX systems. Probably the reason why the CSS compiler was specifically named `lessc` was to avoid the name clash. – Luis Machuca May 04 '12 at 04:21
3

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

Community
  • 1
  • 1
David Thomas
  • 249,100
  • 51
  • 377
  • 410
1

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

krusty.ar
  • 4,015
  • 1
  • 25
  • 28
0

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.

Goran Jovic
  • 9,418
  • 3
  • 43
  • 75