Adapt the height of one div to another div CSS

0

I am trying to make the div color negro suit the height of div color amarillo . The div color amarillo has the property top: 320px; in this example, but it will be dynamic, its property will change constantly ...

<html>
<head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="home.css" rel="stylesheet">
</head>
<body> 
  <div class="container">
    <div class="row">
        <div class="col-md-6 col-sm-6 col-xs-6" style="width: 50%; height: 600px; background-color: blue;">

        </div>
        <div class="col-md-6 col-sm-6 col-xs-6" style="width: 50%; height: 600px; background-color: black;">
            <div style="position: relative; top: 320px; background-color: yellow; height: 600px;">
                <h2 style="font-size: 100px; color: black;">CUADRO AMARILLO</h2>
            </div>
        </div>
    </div>
</div>
</body>
</html>
    
asked by zerokira 25.01.2018 в 17:20
source

3 answers

1

Use a css class to define the height, and apply the same class to both divs:

.div_height {
    height: 600px;
}

And in the divs:

                            

    </div>
    <div class="col-md-6 col-sm-6 col-xs-6 div_height" style="width: 50%; background-color: black;">
        <div class="div_height" style="position: relative; top: 320px; background-color: yellow;">
            <h2 style="font-size: 100px; color: black;">CUADRO AMARILLO</h2>
        </div>
    </div>
</div>

That way, by modifying the height in the class, you will update both.

Based on your request, for more advanced handling, there are 2 other ways:

Form 1

Form 2 with Javascript

I hope you serve

    
answered by 25.01.2018 в 17:37
1

Yes I have understood correctly, do you want the div color black and blue, always have the height of the yellow div and you only have to use css?

Have you tried with flex?

To the main container, you should only apply:

.container{
  display: flex;
  align-items: stretch;
}

This forces the children, in this case black and blue to take the same height as the father.

And if you give a height to yellow , then this is the only one that determines the height of black and blue **.

Now, if you are not going to declare or assign any height to the yellow element, it will continue to work as long as you keep it contained, that is, if it is only going to grow with a text that you put inside, this same size will have both the black element and the yellow element.

Other things you can try is to use variable css (custom properties), in which you can do something like this:

.contenedor-principal{
  -altura-yellow: 100px;
}

.negro{
  height: var(--atura-yellow);
}

.yellow{
  height: var(--altura-yellow);
}

Or if you are going to assign the size of yellow dynamically by means of js, you can use the variable, like this:

<div class="contenedor" style="-altura-yellow: 100px">
  <div class="blue" style="height: var(--altura-yellow)">
  <div class="black" style="height: var(--altura-yellow)">
    div class="yellow" style="height: var(--altura-yellow)">
  </div>
</div>

Or combine the two, something like this:

*{
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

.container{
  display: flex;
  align-items: stretch;
  --h: 120px;
}

.col{  
  width: 50%;
}

.blue{
  background-color: blue;
}

.black{
  background-color: black;
}

.yellow{
  transform: translateY(50px);
  background-color: yellow;
  height: var(--h);
}
<div class="container">
  <div class="col blue">
  </div>
  <div class="col black">
    <div class="yellow">
    </div>
  </div>
</div>

See how it also grows without needing to assign height, only yellow content:

*{
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

.container{
  display: flex;
  align-items: stretch;
}

.col{  
  width: 50%;
}

.blue{
  background-color: blue;
}

.black{
  background-color: black;
}

.yellow{
  transform: translateY(50px);
  background-color: yellow;
  font-size: 20vmin;
}
<div class="container">
  <div class="col blue">
  </div>
  <div class="col black">
    <div class="yellow" contenteditable>
      Aqui va a ir X contenido, sí quieres escribe algo, este elemento es editable para poder hacer un mejor ejemplo
    </div>
  </div>
</div>

Just in case, I put your code here below, so you can see how I propose it:

<html>
<head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="home.css" rel="stylesheet">
</head>
<body> 
  <div class="container" style="--y: 600px">
    <div class="row">
        <div class="col-md-6 col-sm-6 col-xs-6" style="width: 50%; height: var(--y); background-color: blue;">

        </div>
        <div class="col-md-6 col-sm-6 col-xs-6" style="width: 50%; height: var(--y); background-color: black;">
            <div style="position: relative; top: 50px; background-color: yellow; height: var(--y);">
                <h2 style="font-size: 100px; color: black;">CUADRO AMARILLO</h2>
            </div>
        </div>
    </div>
</div>
</body>
</html>

If it is not this, you could better explain what you want to achieve because we still do not understand.

    
answered by 27.01.2018 в 20:13
1

The elements always adapt to the size of their content except when you give a fixed size in your case when you tell the div negro that it is 600px size because of that size it will be and it will not change. Now what I think you want is to be at least that size and if it needs to change to adapt to the content of div amarillo , for this we use the property min-height . However, you are using the top property in objects with position: relative that cause a certain amount of height (px, em, rem, etc) to move from the edge of the parent element but without that the parent element finds out about this so it will continue to "think" that the element is in a certain position and auto-adjusts to this position, I advise you to change the property top by margin-top otherwise you will not be able to achieve it what do you want

<html>
<head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="home.css" rel="stylesheet">
</head>
<body> 
  <div class="container">
    <div class="row">
        <div class="col-md-6 col-sm-6 col-xs-6" style="width: 50%; height: 600px; background-color: blue;">

        </div>
        <div class="col-md-6 col-sm-6 col-xs-6" style="width: 50%; min-height: 600px; background-color: black;">
            <div style="position: relative; margin-top: 320px; background-color: yellow; height: 600px;">
                <h2 style="font-size: 100px; color: black;">CUADRO AMARILLO</h2>
            </div>
        </div>
    </div>
</div>
</body>
</html>
    
answered by 10.09.2018 в 17:01