Why cannot resize height: 100%; a flex item? [duplicate] - html

This question already has answers here:
Chrome / Safari not filling 100% height of flex parent
(5 answers)
Why is percentage height not working on my div? [duplicate]
(2 answers)
Closed 5 years ago.
I would like to know why clm1 with height: 100%; does not resize the div when display flex is being used.
How to fix this problem, I need the div resize as clm2 content?
Why does not work my version?
I need the orange div to matchs the size of the blue div.
#container {
background-color: red;
width: 500px;
height: 400px;
}
#flex {
background-color: yellow;
display: flex;
flex-direction: row;
}
#clm1 {
background-color: orange;
width: 20%;
height: 100%;
}
#clm2 {
background-color: blue;
width: 80%;
}
<div id="container">
<div id="flex">
<div id="clm1">
1
</div>
<div id="clm2">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum
</div>
</div>
</div>

Because the parent height is not set. Add height:100% (or any height) to parent and it will work.
#container {
background-color: red;
width: 500px;
height: 400px;
}
#flex {
background-color: yellow;
display: flex;
flex-direction: row;
height: 100%;
}
#clm1 {
background-color: orange;
width: 20%;
height: 100%;
}
#clm2 {
background-color: blue;
width: 80%;
}
<div id="container">
<div id="flex">
<div id="clm1">
1
</div>
<div id="clm2">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum
</div>
</div>
</div>
Or simply remove the height:100% from child as with the default behavior of flex it will be stretched to parent height. This is due to the property align-items set to stretch by default.
#container {
background-color: red;
width: 500px;
height: 400px;
}
#flex {
background-color: yellow;
display: flex;
flex-direction: row;
}
#clm1 {
background-color: orange;
width: 20%;
}
#clm2 {
background-color: blue;
width: 80%;
}
<div id="container">
<div id="flex">
<div id="clm1">
1
</div>
<div id="clm2">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum
</div>
</div>
</div>

'cause your #clm2 is 100% height of... the #flex, which is auto height, not the #container that has a defined 400px.
So set the #flex to height 100% (or 400px) and it will work
#container {
background-color: red;
width: 500px;
height: 400px;
}
#flex {
background-color: yellow;
display: flex;
flex-direction: row;
height:100%;
}
#clm1 {
background-color: orange;
width: 20%;
height: 100%;
}
#clm2 {
background-color: blue;
width: 80%;
}
<div id="container">
<div id="flex">
<div id="clm1">
1
</div>
<div id="clm2">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum
</div>
</div>
</div>

You have clm1 and clm2 in the div flex and you set c1lm1 to height 100% clm2 to height 80%. Since height property fills in the parent, the total height would be 180% which is not possible. Try making the clm1 and clm2 height percentages add up to 100%

Related

How to enable CSS scroll both X and Y

I've searched few hours, but cannot got an answer, So I need some help
There is a div element, provides scroll-x
<div id='parent'>
<div id='el1'></div>
<div id='el2'></div>
<div id='el3'></div>
</div>
When my mouse cursor locates on that div area, scrolling y does not work
Had tried =>
#id overflow: auto
#id overflow-x: auto, overflow-y: auto
#id overflow-x: scroll, overflow-y: scroll
How can I solve it?
Here, this can give you some idea:
#el1, #el2, #el3 {
width: 200px;
height: 200px;
background: lightblue;
display:inline-block;
}
.parent {
width:100px;
height:100px;
border: 2px dashed grey;
overflow: auto;
}
<div class="parent">
<div id='el1'>a</div>
<div id='el2'>b</div>
<div id='el3'>c</div>
</div>
I this snippet working the way you want?
The key is on setting the height and width of your parent and children elements.
A horizontal scroll (X-axis) can be achieved if the total width of the children's elements is bigger than the width of the parent element plus using overflow: auto to the parent element.
A vertical scroll (Y-axis) can be achieved if the total height of the children's elements is bigger than the height of the parent element plus using overflow: auto to the parent element.
Here is the working code: https://codesandbox.io/s/kind-gates-djhnx?file=/src/styles.css
html file:
<div id="parent">
<div id="child-1">
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book. It has survived not only five
centuries, but also the leap into electronic typesetting, remaining
essentially unchanged. It was popularised in the 1960s with the release
of Letraset sheets containing Lorem Ipsum passages, and more recently
with desktop publishing software like Aldus PageMaker including versions
of Lorem Ipsum.
</div>
<div id="child-2">
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book. It has survived not only five
centuries, but also the leap into electronic typesetting, remaining
essentially unchanged. It was popularised in the 1960s with the release
of Letraset sheets containing Lorem Ipsum passages, and more recently
with desktop publishing software like Aldus PageMaker including versions
of Lorem Ipsum.
</div>
<div id="child-3">
Lorem Ipsum is simply dummy text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy text ever
since the 1500s, when an unknown printer took a galley of type and
scrambled it to make a type specimen book. It has survived not only five
centuries, but also the leap into electronic typesetting, remaining
essentially unchanged. It was popularised in the 1960s with the release
of Letraset sheets containing Lorem Ipsum passages, and more recently
with desktop publishing software like Aldus PageMaker including versions
of Lorem Ipsum.
</div>
</div>
css file:
#parent {
overflow: auto;
height: 300px;
width: 300px;
background-color: gainsboro;
}
#child-1 {
overflow: auto;
width: 500px;
background-color: lightblue;
}
#child-2 {
overflow: auto;
width: 500px;
background-color: lightgreen;
}
#child-3 {
overflow: auto;
width: 500px;
background-color: lightcoral;
}
Here is a working example on overflow, you have to set height and width property in #parent plus you have to set overflow property to auto
if the width of any children is greater than parent's width, then it will automatically scroll on x direction
if the total height of all children is greater than parent's height, then it will automatically scroll on y direction
#parent{
background:#333;
width:300px;
height:300px;
overflow:auto;
}
#el1{
background:#999;
height:200px;
width:200px;
}
#el2{
background:green;
height:400px;
width:400px;
}
#el3{
background:yellow;
height:400px;
width:400px;
}
<div id='parent'>
<div id='el1'></div>
<div id='el2'></div>
<div id='el3'></div>
</div>

Img height on auto height div

https://jsfiddle.net/ry9gyb8n/
Hi guys , I have been trying to solve this problem since a couple of weeks.
I have an auto height container , the left div in the container is auto height as it can have various different content inside it.
The right div will always have an image but I dont know the height of the image.
How do I make it so the image doesnt exceed the height of the content on the left?
.container {
float: left;
width: 100%;
height: auto;
border: 2px solid black;
}
.leftContainer {
float: left;
width: 48%;
border: 2px solid black;
}
.rightContainer {
width: 50%;
float: left;
}
img {
max-width: 100%;
}
<div class="container">
<div class="leftContainer">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div class="rightContainer">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/London_Bridge_Illuminated.jpg/1200px-London_Bridge_Illuminated.jpg">
</div>
</div>
I would go with flex to create the layout and make the image out of the flow using absolute position so it won't give its container a height and thus the height will be equal to the left one. Then simply adjust the properties of the image to make it fit the container as you need:
.container {
display:flex;
border: 2px solid black;
}
.leftContainer {
flex:1;
border: 2px solid black;
}
.rightContainer {
flex:1;
position:relative;
}
img {
position:absolute;
top:0;
max-height:100%; /* Or height:100%*/
max-width:100%;
/*to center the image if needed*/
left:50%;
transform:translateX(-50%);
/**/
}
<div class="container">
<div class="leftContainer">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div class="rightContainer">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/London_Bridge_Illuminated.jpg/1200px-London_Bridge_Illuminated.jpg">
</div>
</div>
Another solution is to use the same layout as above and make the image as background. You will have the same situation because there is no content and thus the height will be the same as the left column. Then adjust the image position/size using background properties:
.container {
display: flex;
border: 2px solid black;
}
.leftContainer {
flex: 1;
border: 2px solid black;
}
.rightContainer {
flex: 1;
position: relative;
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/London_Bridge_Illuminated.jpg/1200px-London_Bridge_Illuminated.jpg");
background-size: contain; /* or cover or 100% 100% or auto*/
background-position: top center;
background-repeat:no-repeat;
}
<div class="container">
<div class="leftContainer">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div class="rightContainer">
</div>
</div>

Full height image as the text

i'm trying to make the image take the full height of the div as the text and keep it proportional with width as the overflow is hidden and image is centered,I used display flex and i don't want to use background-image in css because i have multiple images and articles . can anyone help?
div {
border: 1px dotted red;
display: block;
}
.article {
display: -webkit-flex;
display: flex;
flex-direction: row;
-webkit-flex-direction: row;
width: 100%;
overflow: auto;
padding: 10px;
}
.article-content {
height: 100%;
width: 48.6666666%;
text-align: left;
}
.image {
margin-left: 3.5%;
width: 48%;
overflow: hidden;
}
.img-control {
display: flex;
justify-content: center;
-webkit-justify-content: center;
align-items: center;
-webkit-align-items: center;
}
.img-control img {
height: 100%;
}
<div class="article">
<div class="article-content">
<h3>What is Lorem Ipsum?</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div class="image">
<div class="img-control">
<img src="http://via.placeholder.com/490x338" alt="" />
</div>
</div>
</div>
To get what you want I would first recommend using just 1 flex container instead of 2. This way both "content containers" (the text and the image) can be flex items in the same context and you can stretch them to match heights.
The updated HTML would then look like this:
<div class="article">
<div class="article-content">
<h3>What is Lorem Ipsum?</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div class="img-control">
<img src="http://via.placeholder.com/490x338" alt="" />
</div>
</div>
And the CSS would be:
.article {
display: -webkit-flex;
display: flex;
flex-direction: row;
-webkit-flex-direction: row;
width: 100%;
overflow: hidden;
padding: 10px;
}
.article-content {
height: 100%;
width: 48.6666666%;
text-align: left;
}
.img-control {
margin-left: 3.5%;
width: 48%;
overflow: hidden;
align-self: stretch;
}
On the .img-control flex item I added align-self: stretch; to stretch it to match the other flex item (the text in .article-content) and overflow: hidden; to hide the parts of the image that can overflow.
Then I just added a style to the image to make it center and take up 100% height of its stretched container:
.img-control img {
height: 100%;
position: relative;
left: 50%;
transform: translateX(-50%);
}
You can see a demo here by resizing the frame window.
But there is a problem with this approach. When the text content is shorter than the image they don't match height and the image might not fill the width leaving gaps of white space. You can see this by looking at the above demo fiddle and stretching the frame window very wide.
To fix this I would recommend using a background-image along with background-size: cover; and background-position: center center;. Then you just simpley make a div and set the image source as an inline style.
Like this:
<div class="img-control">
<div class="image" style="background-image: url(//via.placeholder.com/490x338);"></div>
</div>
CSS:
.img-control .image {
height: 100%;
width: 100%;
background-size: cover;
background-position: center center;
}
Now see this improved demo fiddle and stretch the frame window to any size.

Creating a vertically scrollable div from unknown height

I have a popup div which has a height of 100%.
.popup{
height:100%;
}
I have gone ahead and created a 60% margin top
.popup{
margin-top:60%;
}
and so only 40% of space is available at the bottom end of the popup.
In the 40% bottom space,i want to have a scrollable div container of maybe height of 500px. This does not work and only the 40% space is available and no scrolling bars appear.
How can i have 500px on the 40% space available?.
Check this snippet:
body,
html,
.outer {
width: 100%;
height: 100%;
}
.wrapper-top, .wrapper-bottom {
position: relative;
width: 400px;
top: 0;
bottom: 0;
border: 1px solid #555;
}
.wrapper-top {
height: 60%;
}
.wrapper-bottom {
height: 40%;
}
.inner {
position: absolute;
width: 100%;
top: 0px;
bottom: 0px;
overflow-y: auto;
}
<div class="outer">
<div class="wrapper-top">
<div class="inner">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
<div class="wrapper-bottom">
<div class="inner">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
</div>
.main{
width:100%;
height:350px;
border:1px solid;
}
.top{
width:100%;
height:60%;
border:1px solid red;
}
.bottom{
display:inline-block;
width:100%;
height:38%;
border:1px solid #ff6600;
padding:5px;
overflow:auto;
}
.content{
width:100%;
height:500px;
border:1px solid blue;
}
<div class="main">
<div class="top"></div>
<div class="bottom">
<div class="content"></div>
</div>
</div>
Please check this fiddle: https://jsfiddle.net/vadimb/wx2v7pj6/
.popup {
margin-top: 60%;
background-color: red;
height: 250px;
overflow: hidden;
}
.inner {
overflow-y: scroll;
height: 250px;
}
And html
<div class="popup">
<div class="inner">
abcd<br/>....
</div>
</div>

Make height of the children same inside flex-box

I have a div with display: flex and two children div say .left and .right.
The content size of the .left and .right are different. I want to set same height for the children
here is a minimal working code
.container{
display: flex;
align-items: center
}
.left, .right{
width: 50%;
}
.right{
background: blue;
height: 100%;
}
.left{
background: green;
height: 100%;
}
<div class="container">
<div class="left">
<span>this dis also should have same width as right div</span>
</div>
<div class="right">
orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
Here is the Fiddle for the question
Note: Prefer a solution without applying position: absolute as it will destroy my actual website
remove the height:100%; it will fix the issue .. flexbox apply its height in children itself.