Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS percent height when parent has min/max height

Tags:

html

dom

css

I know that when you use a percent height on an element, the percentage is the percent of it's parent. Let's say that you want a child to be 40% of it's parent. The parent has a max and a min height assigned, but it does not have an explicit height assigned. For example:

<div id="container">
  <div id="one">
    <div id="two"></div>
  </div>
</div>

css

#container{
  height: 500px;
  background: yellow;
}
#one{
  background: red;
  min-height:100px;
  max-height: 50%;
  padding: 10px;
}
#two{
  background: blue;
  height: 40%;
}

Div two will not appear. When you change the css of his parent (div one) from this max-height:50% to this: height:50% div two will appear because it knows what the height of his parent is because it is explicitly defined. My question is there a way to make div two appear while using (min/max)-height and not height

Here is a fiddle

like image 513
Kyle Weller Avatar asked Oct 04 '13 21:10

Kyle Weller


People also ask

Does percentage work for height in CSS?

The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to auto .

Can I use height and Min height together?

The min-height property in CSS is used to set the minimum height of a specified element. The min-height property always overrides both height and max-height . Authors may use any of the length values as long as they are a positive value.

How do you set height to 100% of a parent?

container div has two parent elements: the <body> and the <html> element. And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.

What is min height 100vh in CSS?

– What is Min Height 100vh in CSS? Min height 100vh means the element should occupy the web browser viewport height. This is always 100 percent of the web browser's viewport height. If there is more content, the element will stretch more than the viewport's height, which is a code example that will clear things up.


1 Answers

Check this out, you are missing one tiny piece. Suppose you want to set the height of #one compared to #container and #two compared to #one (that's what I think you are going for). We need a height statement of #one to get a height from #two. The trick here is to set a max-height, a min-height, AND a height to the element, thereby giving it a height that is constrained by the min and the max.

Try this css:

 <style>
 #container{
   height: 74vh;
   background: yellow;
 }
 #one{
   background: red;
   min-height:100px;
   max-height: 50%;
   height: 100%;
   padding: 10px;
 }
 #two{
   background: blue;
   height: 40%;
 }
 </style>

The height is never achieved because it is constrained by the max-height, but without declaring the height it is height: auto, which is undeclared. I set the height of #container as 74vh to make the whole thing responsive according to the size of the viewport.

like image 112
TMKAB121 Avatar answered Oct 17 '22 13:10

TMKAB121