Stretch a relative div to the height of its static parent - html

I have a div (the red box) that is in a horizontal flex box (black outline). Its height is calculated according to the heights of the other things in the flex box, in this case the tall blue boxes; this is all good.
The red div has a child -- the green box in the picture. Which is positioned relative to the red box. I'd like the green box to exactly normally cover the red box. The width is no problem, since the red box has a fixed width. But how can I say that the height of the green box should equal the height of its parent, the red box?
The reason for the green box on top of the red box is that I want the green box to expand horizontally when hovered over, but I don't want this expansion to affect the layout of other elements. Like this:
There is a JSFiddle here
div.dropZone {
position: relative;
font-family: serif;
margin: 0px;
border-radius: 5px;
left: 0px;
top: 0px;
width: 2em;
height: inherit;
min-height: 3ex;
background-color: green;
}
div.dropZone:hover {
width: 4em;
left: -1em;
}
div.dzContainer {
position: static;
font-family: serif;
margin: 0px 0px 0px 0px;
-webkit-align-self: stretch;
align-self: stretch;
width: 2em;
height: auto;
min-height: 3ex;
background-color: red;
}
div.tall {
position: static;
font-family: serif;
margin: 0px 0px 0px 0px;
-webkit-align-self: stretch;
align-self: stretch;
background-color: blue;
width: 3em;
height: 10ex;
}
.H {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-flex-wrap: nowrap;
flex-wrap: nowrap;
-webkit-justify-content: flex-start;
justify-content: flex-start;
-webkit-align-items: flex-start;
align-items: flex-start;
border: 1px solid black;
}
<div class="H">
<!-- black flex-->
<div class="tall"> </div>
<!-- blue static-->
<div class="dzContainer">
<!-- red static-->
<div class="dropZone"> </div>
<!-- green relative-->
</div>
<div class="tall"> </div>
<!-- blue static-->
</div>

If you want the green box to fill the parent height add height: 100%; to the .dropZone class.
div.dropZone {
position: relative;
font-family: serif;
margin: 0px;
border-radius: 5px;
left: 0px;
top: 0px;
width: 2em;
height: 100%;
min-height: 3ex;
background-color: green;
}

You just have to make one change in your CSS. Add "height:100%" to your div.dropZone selector. I hope that helps.
You can run the code snippet below to try it out.
div.dropZone {
position: relative;
font-family: serif;
margin: 0px;
border-radius: 5px;
left: 0px;
top: 0px;
width: 2em;
height: inherit;
min-height: 3ex;
background-color: green;
height: 100%;
}
div.dropZone:hover {
width: 4em;
left: -1em;
}
div.dzContainer {
position: static;
font-family: serif;
margin: 0px 0px 0px 0px;
-webkit-align-self: stretch;
align-self: stretch;
width: 2em;
height: auto;
min-height: 3ex;
background-color: red;
}
div.tall {
position: static;
font-family: serif;
margin: 0px 0px 0px 0px;
-webkit-align-self: stretch;
align-self: stretch;
background-color: blue;
width: 3em;
height: 10ex;
}
.H {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: row;
flex-direction: row;
-webkit-flex-wrap: nowrap;
flex-wrap: nowrap;
-webkit-justify-content: flex-start;
justify-content: flex-start;
-webkit-align-items: flex-start;
align-items: flex-start;
border: 1px solid black;
}
<div class="H"> <!-- black flex-->
<div class="tall"> </div> <!-- blue static-->
<div class="dzContainer"> <!-- red static-->
<div class="dropZone"> </div> <!-- green relative-->
</div>
<div class="tall"> </div> <!-- blue static-->
</div>

Related

images and buttons are not horizontally aligned

I have met some problems while doing a image-viewer project. The problem is that my buttons and the image are not following justify-content property, which they don't distributed equally inside my div block, how could it be solved? Also the image is not centered as the title does despite I set the align item property. I dow know how to fix that. I've searched over the website for solutions but none of them seems working.
Could anyone help me, please? Thanks in advance.
Here are the html and css code:
<div class="image-viewer__container">
<div class="image-viewer__title">Image Viewer</div>
<div class="image-viewer__main">
<div class="image-viewer__button"><img src="./images/back.png" id="previous" /></div>
<div class="image-viewer__display" style="background-image: url(./images/loading.gif);background-repeat: no-repeat; background-position: center;">
<img src="https://scontent.ftpe7-2.fna.fbcdn.net/v/t1.0-0/p640x640/119893827_3212042898922322_5684339818610522875_o.jpg?_nc_cat=104&_nc_sid=730e14&_nc_ohc=fGG3wRqLaLEAX8MrIY-&_nc_ht=scontent.ftpe7-2.fna&tp=6&oh=36c5e163223a1e8abca79a2b3892c915&oe=5F976AFF" id="display">
<div class="image-viewer__display-source-wrapper">
<span><a href="https://scontent.ftpe7-2.fna.fbcdn.net/v/t1.0-0/p640x640/119893827_3212042898922322_5684339818610522875_o.jpg?_nc_cat=104&_nc_sid=730e14&_nc_ohc=fGG3wRqLaLEAX8MrIY-&_nc_ht=scontent.ftpe7-2.fna&tp=6&oh=36c5e163223a1e8abca79a2b3892c915&oe=5F976AFF" target="_blank">
https://scontent.ftpe7-2.fna.fbcdn.net/v/t1.0-0/p640x640/119893827_3212042898922322_5684339818610522875_o.jpg?_nc_cat=104&_nc_sid=730e14&_nc_ohc=fGG3wRqLaLEAX8MrIY-&_nc_ht=scontent.ftpe7-2.fna&tp=6&oh=36c5e163223a1e8abca79a2b3892c915&oe=5F976AFF</a>
</span>
</div>
</div>
<div class="image-viewer__button"><img src="./images/next.png" id="next" /></div>
</div>
</div>
.image-viewer__container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-evenly;
}
.image-viewer__title {
font-size: 5rem;
font-weight: 600;
color: #615dec;
margin: 0;
margin-top: 2rem;
}
.image-viewer__main {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
margin: auto;
}
.image-viewer__button {
display: inline;
background: none;
border: none;
border-radius: 50%;
}
.image-viewer__button img {
width: 80px;
height: 80px;
border: 1px solid transparent;
border-radius: 50%;
cursor: pointer;
}
.image-viewer__display {
position: relative;
padding: 15px;
margin: 3rem;
max-width: 80rem;
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-items: center;
font-size: 0.6rem;
}
.image-viewer__display-source-wrapper {
position: absolute;
font-size: 12px;
left: 50%;
margin-right: 50%;
transform: translate(-50%, -50%);
min-width: 100em;
text-align: center;
bottom: 0;
}
#display {
object-fit: contain;
width: 50rem;
height: 30rem;
margin-bottom: 1rem;
}
#source {
display: inline;
color: black;
}
This is because you've set a fixed width to your image. By setting the main image to 100% the image will fit and fill up the remaining space so the 3 elements are always distributed equally.
main image size = full width - both your arrows
current
#display {
object-fit: contain;
width: 50rem; /*fixed width*/
height: 30rem; /*fixed width*/
margin-bottom: 1rem;
}
amended
#display {
margin-bottom: 1rem;
width: 100%; /*was added*/
height: auto; /*was added*/
}
jsFiddle
Add css float:"right" in css button.

Edge Browser Issue : Width : fit-content is not honoured while in chrome its working fine

Size of the first level header (in grey background) is growing in size as per the size of the largest flex item in case of edge browser. While in case of chrome it is honouring fit-content width and is being rendered accordingly.
HTML:
<section id = 'tileContainer' class = 'tileContainer'>
<div id = 'leftSideContent' class = 'leftSideContent'>
<div id = 'firstLevelHeader' class = 'firstLevelHeader'>
First Level Header
</div>
<div id = 'secondLevelHeader' class = 'secondLevelHeader'>
Second level header has a got a very long text inside of it
</div>
</div>
<div id = 'rightSideContent' class = 'rightSideContent'>
X
</div>
</section>
CSS:
.tileContainer{
margin: 25px;
height: 120px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
border: 1px solid black;
border-radius: 5px;
box-shadow: solid 0.2px #d2d3d3;
.leftSideContent {
display: flex;
flex-direction: column;
justify-content: center;
min-width: 0;
.firstLevelHeader{
background-color: #8a8a8a;
padding-left: 10px;
padding-right: 10px;
border-radius: 10px;
height: 20px;
align-content: center;
width: fit-content;
}
.secondLevelHeader {
height: 20px;
}
}
.rightSideContent{
justify-content: center;
width: 50px;
}
}
Behavior in edge:
Behavior in Chrome:
From your description, it seems that you're using Edge Legacy. width: fit-content is not supported by Edge Legacy but supported by Edge Chromium, you could check it on caniuse.
One alternative is to use display: table, which has the same effect as width: fit-content:
.tileContainer {
margin: 25px;
height: 120px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
border: 1px solid black;
border-radius: 5px;
box-shadow: solid 0.2px #d2d3d3;
}
.tileContainer .leftSideContent {
display: flex;
flex-direction: column;
justify-content: center;
min-width: 0;
}
.tileContainer .leftSideContent .firstLevelHeader {
background-color: #8a8a8a;
padding-left: 10px;
padding-right: 10px;
border-radius: 10px;
height: 20px;
align-content: center;
width: fit-content;
display: table; /*add this line*/
}
.tileContainer .leftSideContent .secondLevelHeader {
height: 20px;
}
.tileContainer .rightSideContent {
justify-content: center;
width: 50px;
}
<section id='tileContainer' class='tileContainer'>
<div id='leftSideContent' class='leftSideContent'>
<div id='firstLevelHeader' class='firstLevelHeader'>
First Level Header
</div>
<div id='secondLevelHeader' class='secondLevelHeader'>
Second level header has a got a very long text inside of it
</div>
</div>
<div id='rightSideContent' class='rightSideContent'>
X
</div>
</section>
width: fit-content is not supported on Edge. you can find out more here
you can use display: table as an alternative. works the same way.

set max-height flex parent, stretch nested child but inside of it overflow-y auto

I have some issues with the overflow of a nested flex child. I've set a max-height for the flex parent and let the child stretch themselves, to fit to their content. So far so good, but inside of the childs I've got some nested divs, ul and li. I only want to see the scrollbar inside of the ul, unfortunately it doesn't work. My content will get cut, due to the overflow: hidden. Height: 100% doesn't work for me either.
.modal-column {
display: flex;
flex-direction: column;
flex: 1;
padding: 0px 20px;
max-height: 1300px;
background:grey;
}
.modal-section {
width: 320px;
background: #fff;
padding: 25px;
border-radius: 6px;
margin-bottom: 20px;
box-shadow: 0 0.25rem 0.185rem 0 rgba(0, 0, 0, .04);
cursor: default;
flex-grow: 1;
flex-shrink: 1;
flex-basis: auto;
overflow: hidden;
}
.section-status .modal-container {
padding-bottom: 0px;
}
.modal-container {
margin-bottom: 3px;
padding: 15px 0px;
overflow: hidden;
}
.address-list-container {
padding-top: 18px;
overflow: hidden;
}
.address-list-header {
background-color: #f5f5f5;
padding: .5px 0px;
width: 100%;
}
.address-list-tr {
color: #7b7b7b;
font-size: 12.2px;
-webkit-box-pack: justify;
justify-content: space-between;
align-items: stretch;
display: flex;
padding: .5 0px
}
.address-list-th {
padding: 0px 5px;
display: flex;
flex-direction: column;
flex: 1;
}
.address-list-ul {
margin-top: 18px;
overflow-y: auto;
}
.address-list-li {
-webkit-box-pack: justify;
justify-content: space-between;
align-items: stretch;
display: flex;
padding: 7.5px 0px;
border-bottom: 1px dotted #d6d6d6;
}
.address-list-li:first-child {
padding-top: 0px;
}
.address-list-li:last-child {
border-bottom: none;
}
.address-column {
display: flex;
flex-direction: column;
flex: 1;
font-size: 13.5px;
padding: 0px 5px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
<div class="modal-column">
<section class="modal-section section-status"></section>
<section class="modal-section network-section">
<div class="modal-container">
<h2 class="modal-title">Network</h2>
<div class="address-list-container">
<table class="address-list-header">
<tr class="address-list-tr">
<th class="address-list-th">Name</th>
<th class="address-list-th">Address</th>
</tr>
</table>
<ul class="address-list-ul">
<div class="address-list-li">
<div class="address-column adress-key" title="1">1</div>
<div class="address-column adress-value" title="123">123</div>
</div>
......
</ul>
</div>
</div>
</section>
<section class="modal-section app-section">
<div class="modal-container">
<h2 class="modal-title">1</h2>
<ul class="bs-list-ul">
<li class="bs-list-li">
.......
</li>
</ul>
</div>
</section>
</div>
Try this in your css.
. address-list-ul{
overflow-y:scroll; /*Try this instead*/
}
Hope this could help.

fix elements to the bottom of a flex container

I am using flexbox to create part of a page and I have 2 buttons that will open modals in one of the flexbox items. I would like to fix the buttons to the bottom of the item like a footer if possible.
I have created a CodePen to demo the issue. I have tried several solutions but I can't seem to get the buttons aligned at the bottom. I am relatively new to HTML and CSS so it's very possible that I've missed something trivial.
#container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
align-content: center;
margin-top: 20px;
}
.one {
background-color: black;
color: white;
font-size: 30px;
padding: 10px;
width: 450px;
flex-grow: 1;
height: 600px;
}
.two {
background-color: grey;
color: white;
font-size: 30px;
padding: 10px;
flex-grow: 1.2;
width: 524px;
height: 600px;
}
button {
background-color: green;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
<div id="container">
<div class="one">
<p> I would like to align the buttons to the bottom of this item </p>
<button id="Btn1">Open Modal 1</button>
<button id="Btn2">Open Modal 2</button>
</div>
<div class="two">
<p> No buttons for this item </p>
</div>
You can make .one display: flex; flex-direction: column;, wrap the buttons in an element, and use justify-content: space-between on .one to push the buttons to the bottom of the column.
#container{
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
align-content: center;
margin-top: 20px;
}
.one{
background-color: black;
color: white;
font-size: 30px;
padding: 10px;
width: 450px;
flex-grow: 1;
height: 600px;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.two{
background-color: grey;
color: white;
font-size: 30px;
padding: 10px;
flex-grow: 1.2;
width: 524px;
height: 600px;
}
button {
background-color: green;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
<div id="container">
<div class="one">
<p> I would like to align the buttons to the bottom of this item </p>
<div class="buttons">
<button id="Btn1">Open Modal 1</button>
<button id="Btn2">Open Modal 2</button>
</div>
</div>
<div class="two">
<p> No buttons for this item </p>
</div>
Since flex properties only apply to the children of a flex container, your buttons are not flex items.
The buttons are descendants of the flex container (#container), but not children, so they are beyond the scope of flex layout.
One method to resolve the issue would be to make the parent of the buttons a flex container. Then flex properties can be applied to the buttons.
#container {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
align-content: center;
margin-top: 20px;
}
.one {
background-color: black;
color: white;
font-size: 30px;
padding: 10px;
width: 450px;
flex-grow: 1;
height: 600px;
display: flex; /* new */
flex-wrap: wrap; /* new */
}
p {
flex: 0 0 100%; /* new */
}
button {
margin: auto 10px 0; /* new */
background-color: green;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}
.two {
background-color: grey;
color: white;
font-size: 30px;
padding: 10px;
flex-grow: 1.2;
width: 524px;
height: 600px;
}
<div id="container">
<div class="one">
<p> I would like to align the buttons to the bottom of this item </p>
<button id="Btn1">Open Modal 1</button>
<button id="Btn2">Open Modal 2</button>
</div>
<div class="two">
<p> No buttons for this item </p>
</div>
Learn more about the flex formatting context here:
Proper use of flex properties when nesting flex containers
Learn more about flex auto margins here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
One approach would be to put your buttons in their own <div> like so:
<div id="container">
<div class="one">
<p> I would like to align the buttons to the bottom of this item </p>
<div class="btn-container">
<button id="Btn1">Open Modal 1</button>
<button id="Btn2">Open Modal 2</button>
</div>
</div>
<div class="two">
<p> No buttons for this item </p>
</div>
and change your css to:
#container{
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
align-content: center;
margin-top: 20px;
}
.one {
display: flex;
flex-direction: column;
flex-grow: 1;
justify-content: space-between;
height: 100vh;
background-color: black;
color: white;
font-size: 30px;
padding: 10px;
width: 450px;
height: 600px;
}
.two {
background-color: grey;
color: white;
font-size: 30px;
padding: 10px;
flex-grow: 1.2;
width: 524px;
height: 600px;
}
.btn-container {
justify-content: flex-end;
display: flex;
}
button {
margin: 10px;
background-color: green;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}

Centered responsive text

I have a jsfiddle here - http://jsfiddle.net/kw83kLmo/
It's justs a block of text that I need to center in the window.
The text needs a set width because I don't want it to be the full width of the container.
When the window gets smaller the text needs to stay in the center but get narrower.
In the example here it stays centered and responds until it get's to 600px then just stays that width.
I know I have set that width but I did that to center it
<div class="container-fluid ">
<div class="hero">
<div class="hero_heading text-center">
<h1>This is a heading This is a heading This is a heading </h1>
</div>
</div>
</div>
Update your h1 style like below.
.hero_heading h1 {
border: 1px solid red;
color: white;
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
max-width: 600px;
}
DEMO
Edit on your code
.hero_heading h1{
border: 1px solid red;
color: white;
//top: 0; left: 0; bottom: 0; right: 0;
width: 600px;/*added*/
max-width:80%;/*to leave spaces around your text on responsive*/
margin:auto;/*changed*/
}
You no need to position your element for making it unless if you need
NOTE: Remove your position:relative; from .hero
DEMO
Edit your code like this :
.hero_heading h1{
border: 1px solid red;
color: white;
margin: 0 auto;
max-width: 600px;
position: absolute;
bottom: 0;
}
Demo
JSfiddle Demo
I took the positioning off the h1 and put it on the wrapping div.
CSS
.hero{
background-image: url(http://placehold.it/800x300);
background-size: cover;
position: relative;
height: 400px;
}
.hero_heading {
border: 1px solid red;
color: white;
position: absolute;
left: 50%;
bottom: 0;
-webkit-transform:translateX(-50%);
transform:translateX(-50%);
max-width: 600px;
}
I'd go with:
.hero_heading{
border: 1px solid red;
color: white;
position: absolute;
width:50%;
top: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
CSS flex can do the magic for you in a compatible way across most popular browsers including IE. Take a look at this JSFiddle
.hero{
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-direction: normal;
-moz-box-direction: normal;
-webkit-box-orient: horizontal;
-moz-box-orient: horizontal;
-webkit-flex-direction: row;
-ms-flex-direction: row;
flex-direction: row;
-webkit-flex-wrap: nowrap;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
-webkit-box-pack: center;
-moz-box-pack: center;
-webkit-justify-content: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-align-content: stretch;
-ms-flex-line-pack: stretch;
align-content: stretch;
-webkit-box-align: center;
-moz-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
background-image: url(http://placehold.it/800x300);
background-size: cover;
height: 400px;
}
.hero_heading h1{
border: 1px solid red;
color: white;
max-width: 600px;
}