8

I find myself having to use JS too often for what I would consider to be something that ought to be solvable in CSS alone.

Here is an example situation of what I am trying to do:

div.outer { height:{Y}px }
div.inner { padding-top:{Y}px }

I don't really want to specifically set the height of the outer div, but I want is for the padding-top property of the inner div to match the height of the outer div. Is there something about CSS I am missing? Or is JS the usual approach?

In jQuery I'd do something like this:

var y = $('div.outer').height();
$('div.inner').css('padding-top',y+'px');

Although rare, what about clients that disable JS?

Clarification: Please notice that this is not so much a "how do I do this in CSS" question. I am aware that currently, you cannot do this in CSS. Rather it is more of a question about what other sorts of solutions might accomplish the coordination that I am after. I can write CSS in PHP with vars if I wanted to just calculate values for initial delivery, but that's not what I'm after. It is more about coordinating two elements so that when one changes (due to the window being resized or a device being rotated, or another element being introduced via AJAX, etc.) another element's styling also changes to match it.

Octopus
  • 8,075
  • 5
  • 46
  • 66
  • 5
    You have no support for variables in CSS currently.. If you still want to go with that approach you have to use a dynamic stylesheet language like `less` or `sass` that parses and compiles to CSS. – Sushanth -- May 30 '14 at 18:51
  • 1
    I don't understant what are you trying to get, what is the visual result you want to achieve. – miguel-svq May 30 '14 at 19:18
  • would you mind sharing what you are trying to achieve? There may be another approach to it... – Hugo Silva Jun 10 '14 at 02:18
  • i disable js (or, rather, use noscript), and seeing websites that can't even _render their initial content_ without js generally makes me leave immediately – Eevee Jun 10 '14 at 04:11

4 Answers4

2

The answer is no it's not possible (at this moment). By pure coincidence I stumbled upon the fact that apparently, you can only set the padding-top property relative to the parent element's width!? In your demo case that'd be:

div.outer { width: 400px; }
div.inner { padding-top: 50%; } /* will be exactly 200px */
/* You can also set a relative width (%), or inherit it */

I've made a little test case that demonstrates this: http://jsbin.com/rukabaso/1/edit. Resize the output window to see the values. When set to 50%; the padding-top property is exactly 50% of the parent's width, because apparently:

Percentages on all padding/ margin properties refer to the parent's width

For reference: How is padding-top as a percentage related to the parent's width?

Community
  • 1
  • 1
webketje
  • 10,376
  • 3
  • 25
  • 54
0

Seems to be no support on variables in CSS. Try using a dynamic stylesheet,

http://www.example.com/example.css

#outer { height:{Y}px; }

#inner { padding-top:{Y}px; }

http://www.example.html

<div id="outer">

<div id="inner">
jmr333
  • 183
  • 1
  • 9
0

I don't think you can set padding based on the height of the parent. You could use a preprocessor like SASS and LESS which support variables.

Not sure on what you are trying to do but I guess this would have a similar result:

.inner {
    transform: translateY(100%);
}

Browser support is decent with the appropriate prefixes - http://caniuse.com/transforms2d

Hugo Silva
  • 6,748
  • 3
  • 25
  • 42
0

By assigning IDs or Classes to the HTML elements in question, it should be relatively easy to accomplish what I assume you're trying to do. Which is:

.outer {
    height: 100px;
}

.inner {
    padding-top: 100px;
}

I imagine that what you're wanting the CSS to do is automatically set the .inner padding-top to the height of the .outer without having to constantly adjust the CSS. However, as jmr333 stated, there is currently no support for variables in CSS.

In a situation like this though, I don't see why you would need to implement any JavaScript at all when you can just adjust the CSS to suit your needs.

  • 1
    `I don't really want to specifically set the height of the outer div`. I think that the OP is meaning that the height of the elements may be variable, so hardcoding values is what he is attempting to avoid. That's when you could use js to set the value dynamically. – filoxo Jun 10 '14 at 13:49
  • @filoxo is apparently the only poster so far, who has understood the question. – Octopus Jun 10 '14 at 19:17
  • @Octopus, that is because your explanation is very poor, and you don't provide feedback when requested. – Hugo Silva Jun 10 '14 at 23:00