12

The global problem:

I want to set the width of an element depending on the parent's height, I know that you can use padding-top to set the height depending on the parent's width, maybe someone knows a trick for my case.

A possible solution(trick) to The global problem would be setting height: 100% to the element and then rotate(90deg) that would simulate that it has the width equal to the parent's height but that don't fit my case.

The specific problem ( Maybe it's possible to do some workaround):

Simplified problem:

I want a dynamic square element that has width and height = x where x = parent's height.

enter image description here


Full problem:

I want something like this

enter image description here

where x = d / sqrt(2) (Pythagorean theorem)

so as you can see "d" is the parent's height, I try with

.blue{
    background: #1AA9C8;
    width: 200px;
    height: 100px;
    position: relative;
    margin: 25px auto;
}

.blue:before{
    content: '';
    position: absolute;
    right: calc(100% - 36px);
    top: 15px;
    background: firebrick;    
    -webkit-transform: rotate(45deg);
    transform: rotate(45deg);
    height: calc(100% / 1.41);/*this worked because height depends on the parent's height (div.blue)*/
    width: 70.9px; /* calc(100% / 1.41) here is my problem  because width depends on the parent's width and I don't know how to make it depends on the parent's height
}
<div class="blue"></div>

Note that I set a fixed width because I don't know how to make it depends on the height of div.blue

Here a jsfiddle example to do some workaround.

I would be grateful if someone could help me.

ONLY CSS

Yandy_Viera
  • 4,320
  • 4
  • 21
  • 42
  • I've come up with two practical implementations that allow you to configure height and width independently from one another. One option requires one wrapper `div` and the other requires two. See http://stackoverflow.com/questions/30972182/setting-the-width-depending-on-the-height-of-its-parent/31568851#31568851 for the code and JSFiddle demos. – John Slegers Jul 22 '15 at 17:34

4 Answers4

6

Addressing your specific, yet not full, problem:

I want a dynamic square element that has width and height = x where x = parent's height.

The dynamic square can be an image, and the parent a div.

<div class="parent-container">
    <img class="image" src="{{imageUrl}}" />
</div>

Now for this setup you give the parent a desired height and tell the element (image) to take it all. Like this:

.parent-container {
    height: 400px;   
}

.image {
    height: 100%;
}

This way, the width is not of your concern.


Edit

Modifying the CSS to this:

.parent-container {
    height: 400px;
    overflow: hidden;
}

.image {
    height: 70.92%;
    position: relative;
    transform: translate(-50%, 0)rotate(-45deg);
    top: 14.4%;
}

Should address the "Full Problem".

Please, mind that the height and top values are rough calculations and should be re-carried out.

The fiddle to boot: https://jsfiddle.net/0pok1bf0/

wiktus239
  • 1,404
  • 2
  • 13
  • 29
  • This is a great workaround, possibly setting a container with a transparent img and setting the `height: calc(100% / 1.41)` to img I will achieve what I want, I will try to adapt it to my real case and I let you know – Yandy_Viera Jul 22 '15 at 16:45
  • The value is my 70.92%, rounded up just a little bit. That should do. I don't know your real case tho, so let me know. – wiktus239 Jul 23 '15 at 07:20
  • This is not the ideal solution but at least in my case worked, thank you very much. – Yandy_Viera Aug 17 '15 at 13:17
  • @Yandy_Viera I agree. For one it changes the structure or the choice of elements. But it can find a usecase. I'm glad it worked. – wiktus239 Aug 17 '15 at 13:59
1

Just for kicks and giggles, here is an interesting attempt that makes use of vertical percentages (padding), as explained here: http://www.impressivewebs.com/vertical-percentages-css/

.d1 {
 margin-left: 50px;
 width: 50%;
 background-color: #CCCCFF;
 border: solid black 1px;
}

.d2 {
 width: 25%;
 background-color: #FF6666;
 border: solid black 1px;
 padding-top: 25%;
 margin: 5%;
 margin-left: -13%;
 transform: rotateZ(45deg);
}
<div class="d1">
 <div class="d2"></div>
</div>

View full page mode and resize... everything scales nicely, almost :P

wwwmarty
  • 858
  • 4
  • 10
  • A percentage value on top/bottom padding or margins is relative to the width of the containing block, and what I want is the width relative to the height of the container. – Yandy_Viera Jul 22 '15 at 16:48
  • Note if I set a fixed height to d1 so d2 don't fit its container(d1). – Yandy_Viera Jul 22 '15 at 16:55
  • Yes in this example the height of the container is controlled by the height of the child div, which is controlled by the WIDTH of the container. Confusing, useless, and beautiful :) – wwwmarty Jul 22 '15 at 17:07
0

You may try this with the pseudo elements. However, you will still need to set the border-width if not height/width.

Demo: http://jsfiddle.net/lotusgodkk/GCu2D/752/

CSS:

.box {
    position:relative;
    background-color:#7092BE;
    width:500px;/*sample*/
    height:150px;/*sample*/
    margin:0 auto;
}
.box:before {
    position: absolute;
    content:"";
    right: 100%;
    border-style: solid;
    border-color: transparent #880015 transparent transparent;
    top: 0;
    bottom: 0;
    border-width:75px;
}
.box:after {
    position: absolute;
    content:"";
    left: 0;
    border-style: solid;
    border-color: transparent transparent transparent #880015;
    top: 0;
    bottom: 0;
    border-width:75px;
}

HTML:

<div class="box"></div>
K K
  • 17,794
  • 4
  • 30
  • 39
  • I'm using pseudo elements but I think it is harder to achieve what I want with the border of the element than with the width and height, in fact div.red is a pseudo element but anyway thanks for your time and your advice. – Yandy_Viera Jun 22 '15 at 05:13
0

I've come up with two practical implementations for your problem. One option requires one additional wrapper. The other requires two.

Both options allow you to configure height AND width, albeit in a different way.

For your specific use case, option 1 is the best approach.

Option 1

This option requires only one additional wrapper div. You define the values for the height of your rectangle and the value of width - height (defined as a padding). The width and height of the square are adjusted automatically.


The Fiddle :

http://jsfiddle.net/w9wgb72e/9/

The code :

.bluecontainer {
    width: 100px; /* set height of rectangle */
    margin: 8px auto;
    padding-right: 100px; /* this value equals height - width of the rectangle */
    background: #1AA9C8;
}

.ratiocontainer {
    width: 100%;
    margin-left: -49.6%;
    position: relative;
    padding-bottom: 100%;
}

.ratiocontainer:before {
    content: '';
    background: firebrick;
    padding: 35.4%;
    margin: 14.5%;
    float: left;
    -webkit-transform: rotate(45deg);
    transform: rotate(45deg);
}
<div class="bluecontainer">
    <div class="ratiocontainer">
    </div>
</div>

Option 2

This option requires two additional wrapper divs. You define the values for the height and width of your square and the width of your rectangle. The height of the rectangle is automatically adjusted to allow the rectangle to wrap around the square.

The Fiddle :

http://jsfiddle.net/w9wgb72e/10/

The code :

.bluecontainer {
    width: 200px; /* set width of rectangle */
    margin: 8px auto;
    background: #1AA9C8;
}

.ratiocontainer {
    width: 100px; /* set width & height of red square */
}

.stretchy-square {
    width: 100%;
    padding-bottom: 100%;
    position: relative;
    margin-left: -50%;
}

.stretchy-square:before {
    content: '';
    background: firebrick;
    margin: 15%;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    -webkit-transform: rotate(45deg);
    transform: rotate(45deg);
}
<div class="bluecontainer">
    <div class="ratiocontainer">
        <div class="stretchy-square"></div>
    </div>
</div>
John Slegers
  • 45,213
  • 22
  • 199
  • 169
  • the problem is that `.bluecontainer` has a fixed height, try setting a fixed height and you will see that stretchy-square don't fit its container. – Yandy_Viera Jul 22 '15 at 17:37
  • @YandYViera : Try playing around with option 1. Set the `width` to match whatever fixed HEIGHT you have in mind. Then, adjust the `padding-right` to match the value of WIDTH - HEIGHT. Everything else should adapt automatically. – John Slegers Jul 22 '15 at 17:59