Grid Element expand animations using CSS - css

I have the following
setTimeout(function(){
var sections = document.querySelectorAll('section'), main = document.querySelector('.grid-container'),
//section = sections[Math.floor(Math.random() * 3)];
section = sections[1];
section.classList.add('expand');
section.parentElement.classList.add('expand');
main.classList.add('expanding');
}, 2000);
*{
box-sizing : border-box;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.grid-container {
width:100%;
height: 100vh;
display:flex;
border: 1px solid;
flex-wrap: wrap;
flex-direction: column;
}
.grid-row, .grid-container {
overflow:hidden;
}
.grid-column, .grid-row {
display: flex;
transition: width .2s, height .2s, margin .2s, transform .2s;
}
.grid-column {
width: inherit;
height: inherit;
align-items: center;
justify-items: center;
}
.grid-column:nth-child(1) {
background-color:green;
}
.grid-column:nth-child(2) {
background-color:orange;
}
.grid-row:nth-child(2) .grid-column:nth-child(1) {
background-color:violet;
}
.grid-row:nth-child(2) .grid-column:nth-child(2) {
background-color:brown;
}
#media screen and (orientation: portrait) {
.grid-row {
width: 100%;
height: 50%;
flex-direction: column;
}
.grid-column {
width: 100%;
height: 50%;
}
.grid-row.expand .grid-column {
transform: translateY(-50%);
}
}
#media screen and (orientation: landscape) {
.grid-row {
width: 50%;
height: 100%;
}
.grid-column {
width: 50%;
height: 100%;
}
}
#media screen and (min-height: 500px) {
.grid-row {
height: 50%;
width: 100%;
}
}
.expanding .expand {
width: 100% !important;
height: 100% !important;
}
<main class="grid-container">
<div class="grid-row" data-index="1">
<section class="grid-column" data-index="1">
<article>
<p>
1
</p>
</article>
</section>
<section class="grid-column" data-index="2">
<article>
<p>
2
</p>
</article>
</section>
</div>
<div class="grid-row" data-index="2">
<section class="grid-column" data-index="1">
<article>
<p>
3
</p>
</article>
</section>
<section class="grid-column" data-index="2">
<article>
<p>
4
</p>
</article>
</section>
</div>
</main>
that I'm trying to develop where the "cells" in the grid that when expands to full area of the view will animate to as if its "pushing" the adjacent elements out of the view.
I'm trying to use a combination of width, height and transforms to give the appropriate animation but the transforms seem to give some unexpected results...
Is there a way to accomplish this WITHOUT position:absolute or with as little javascript as possible??

Here's an example seemingly doing what you want, coded from scratch, without any JavaScript. I used flexbox instead of grid, but it can be achieved with grid as well. I used :hover as a trigger:
body {
padding: 15px;
background-color: #999;
margin: 0;
}
.grid>*:hover,
.grid>*>*:hover {
flex-grow: 3;
}
.grid>*>*:hover {
box-shadow: 0 4px 5px -2px rgba(0, 0, 0, .2), 0 7px 10px 1px rgba(0, 0, 0, .14), 0 2px 16px 1px rgba(0, 0, 0, .12);
z-index: 1;
opacity: 1;
font-size: 5rem;
}
.grid:hover>*:not(:hover) {
flex-grow: 0;
max-height: 0;
overflow: hidden;
}
.grid>*,
.grid>*>* {
transition: all .6s cubic-bezier(0.5, 0, 0.3, 1);
}
.grid {
flex-direction: column;
min-height: calc(100vh - 30px);
box-sizing: border-box;
}
.grid,
.grid>* {
display: flex;
max-height: 100%;
}
.grid>* {
flex-direction: row;
margin: 0;
}
.grid>*,
.grid>*>* {
flex-grow: 1;
}
.grid>*>* {
margin: 0;
z-index: 0;
position: relative;
background-color: #fff;
outline: 1px solid #ddd;
opacity: .95;
display: flex;
align-items: center;
justify-content: center;
line-height: 0;
font-size: 3rem;
overflow: hidden;
}
<div class="grid">
<div>
<div>1</div>
<div>2</div>
</div>
<div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
<div>
<div>7</div>
<div>8</div>
<div>9</div>
</div>
</div>
You can play around with flex-grow, change animation or the ratio between hovered and non-hovered elements but, for that, you need to define the requirements better than "seem to give some unexpected results":
You can obviously limit it to 2 + 2 elements. I just wanted to point out it's flexible in terms of structure. Here's an example, expanding to full on both vertical and horizontal:
body {
padding: 15px;
background-color: #999;
margin: 0;
}
.grid>*:hover,
.grid>*>*:hover {
flex-grow: 3;
}
.grid>*>*:hover {
box-shadow: 0 4px 5px -2px rgba(0, 0, 0, .2), 0 7px 10px 1px rgba(0, 0, 0, .14), 0 2px 16px 1px rgba(0, 0, 0, .12);
z-index: 1;
opacity: 1;
font-size: 5rem;
}
.grid:hover>*:not(:hover) {
flex-grow: 0;
max-height: 0;
overflow: hidden;
}
.grid>*:hover>*:not(:hover) {
flex-grow: 0;
flex-basis: 0;
overflow: hidden;
}
.grid>*,
.grid>*>* {
transition: all .6s cubic-bezier(0.5, 0, 0.3, 1);
}
.grid {
flex-direction: column;
min-height: calc(100vh - 30px);
box-sizing: border-box;
}
.grid,
.grid>* {
display: flex;
max-height: 100%;
}
.grid>* {
flex-direction: row;
margin: 0;
}
.grid>*,
.grid>*>* {
flex-grow: 1;
}
.grid>*>* {
margin: 0;
z-index: 0;
position: relative;
background-color: #fff;
outline: 1px solid #ddd;
opacity: .95;
display: flex;
align-items: center;
justify-content: center;
line-height: 0;
font-size: 3rem;
overflow: hidden;
max-width: 100%;
flex-basis: 100%;
flex-grow: 0;
}
<div class="grid">
<div>
<div>1</div>
<div>2</div>
</div>
<div>
<div>3</div>
<div>4</div>
</div>
</div>
Generally, I'd avoid going full width/full height, as it makes selecting another item way more difficult.

Related

After adding the pop-up menu, the rest of the content disappear and the overflow-x:hidden does not work

After adding the pop-up menu, the content inside "main" disappear and the overflow-x:hidden of the body does not work. Does anyone know why?
const slide = () => {
const burger = document.querySelector(".burger");
const menu = document.querySelector(".menu");
const links = document.querySelectorAll(".menu div");
//Toggle Menu
burger.addEventListener("click", () => {
menu.classList.toggle("menupop");
})
}
slide();
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
font-size: calc(16px + 0.25vw);
overflow: scroll;
font-family: 'Antic Slab', serif;
}
body {
min-height: 100vh;
overflow-x: hidden;
}
main {
display: grid;
width: 100vw;
grid-template-columns: 50% 50%;
grid-template-rows: 50% 50% 50% 50%;
}
header {
grid-column: 1/3;
grid-row: 1/2;
background-color: pink;
}
#rose {
grid-column: 1/2;
grid-row: 2/3;
background-color: hotpink;
}
#rose-img {
grid-column: 2/3;
grid-row: 2/3;
background-color: rgb(134, 184, 204);
}
#tree {
grid-column: 1/3;
grid-row: 3/4;
background-color: lawngreen;
}
#about-us {
grid-column: 1/2;
grid-row: 4/5;
background-color: lightcoral;
}
#contact-us {
grid-column: 2/3;
grid-row: 4/5;
background-color: orange;
}
.burger {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-content: space-around;
width: 50px;
height: 50px;
cursor: pointer;
display: none;
}
.line1,
.line2,
.line3 {
flex: 1 1 1;
width: 80%;
height: 5%;
background: black;
border-radius: 5px;
box-shadow: 1px 1px grey;
transition: all .5s ease-in-out;
}
.a {
transform: translateY(2px);
}
.b {
transform: translateY(-2px);
}
nav {
display: flex;
justify-content: flex-end;
align-items: center;
height: 50px;
background-color: rgb(245, 249, 250, 0.5);
padding: 5px 10px;
position: sticky;
top: 0;
}
ul {
display: flex;
list-style: none;
justify-content: space-around;
width: 40%
}
li a {
text-decoration: none;
color: grey;
}
.logo {
font-family: 'Italianno', cursive;
margin-right: auto;
font-size: 30px;
font-weight: bold;
}
.menu {
display: flex;
flex-direction: column;
justify-content: flex-start;
background-color: rgba(91, 126, 172, 0.5);
height: 100vh;
width: 50vw;
position: absolute;
top: 50px;
right: 0;
transform: translateX(100%);
transition: transform 0.5s ease-in;
}
.menupop {
transform: translateX(0%);
}
.menupop div {
animation-name: menuFade;
animation-duration: 1s;
animation-delay: 2s;
animation-timing-function: ease;
animation-fill-mode: forwards;
}
.menu div {
width: 100%;
height: 20%;
background-color: rgb(245, 249, 250, 0.6);
margin-bottom: 1px;
display: flex;
justify-content: center;
align-items: center;
transform: translateX(100%);
}
.menu div a {
text-decoration: none;
}
#keyframes menuFade {
from {
transform: translateX(100%);
}
to {
transform: translateX(0px);
}
}
#media only screen and (max-width: 600px) {
ul {
width: 60%;
}
}
#media only screen and (max-width: 430px) {
.burger {
display: flex;
}
ul {
display: none;
}
}
<body>
<nav>
<div class="logo">Logo</div>
<ul>
<li>Flower</li>
<li>Tree</li>
<li>About us</li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
<div class="menu">
<div class="first">Flower</div>
<div class="second">Tree</div>
<div class="third">About us</div>
</div>
<main>
<header id="jumbotron">
</header>
<section id="rose">
</section>
<section id="rose-img">
</section>
<section id="tree">
<div class="privacy">
</div>
<div class="hedge">
</div>
</section>
<section id="about-us">
</section>
<section id="contact-us">
</section>
</main>
</body>
In your CSS, remove overflow: scroll;.
This should prevent your side menu from staying outside of your viewport width, because with overflow: scroll;, you're allowing the browser to have your side menu out of view, making it only reachable via scrolling.
I'd also like to add that your side menu transition is too long.
If you think about it from a user experience, a user might think that your site is slow.
I've shortened the transition to 200ms and removed the animation-duration and animation-delay. It'll seem more responsive to users now.
EDIT: Also, is anyone else finding it weird how when you use "Run Snippet" on Stackoverflow, use Full Page view, then try to shrink it down to mobile width, the media queries aren't working properly? It works fine when I run the code in VSCode and launch a Live Server, but for some reason, Stackoverflow's Run Snippet is being buggy...
EDIT 2: I've added content to each of your div tags and section tags. So the reason why you couldn't see any content under the main tag was because the height of all of your containers (div, section, main, and header tags) is, by default, auto. If height is auto, then the container will shrink or grow based on the content inside of the container. In your case, you had NO content in ANY of your containers, hence why all of your containers weren't visible, they were essentially at 0px height.
Now that I've added content, you can see your containers.
const slide = () => {
const burger = document.querySelector(".burger");
const menu = document.querySelector(".menu");
const links = document.querySelectorAll(".menu div");
//Toggle Menu
burger.addEventListener("click", () => {
menu.classList.toggle("menupop");
})
}
slide();
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
font-size: calc(16px + 0.25vw);
font-family: 'Antic Slab', serif;
}
body {
min-height: 100vh;
overflow-x: hidden;
}
main {
display: grid;
width: 100vw;
grid-template-columns: 50% 50%;
grid-template-rows: 50% 50% 50% 50%;
}
header {
grid-column: 1/3;
grid-row: 1/2;
background-color: pink;
}
#rose {
grid-column: 1/2;
grid-row: 2/3;
background-color: hotpink;
}
#rose-img {
grid-column: 2/3;
grid-row: 2/3;
background-color: rgb(134, 184, 204);
}
#tree {
grid-column: 1/3;
grid-row: 3/4;
background-color: lawngreen;
}
#about-us {
grid-column: 1/2;
grid-row: 4/5;
background-color: lightcoral;
}
#contact-us {
grid-column: 2/3;
grid-row: 4/5;
background-color: orange;
}
.burger {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-content: space-around;
width: 50px;
height: 50px;
cursor: pointer;
display: none;
}
.line1,
.line2,
.line3 {
flex: 1 1 1;
width: 80%;
height: 5%;
background: black;
border-radius: 5px;
box-shadow: 1px 1px grey;
transition: all .5s ease-in-out;
}
.a {
transform: translateY(2px);
}
.b {
transform: translateY(-2px);
}
nav {
display: flex;
justify-content: flex-end;
align-items: center;
height: 50px;
background-color: rgb(245, 249, 250, 0.5);
padding: 5px 10px;
position: sticky;
top: 0;
}
ul {
display: flex;
list-style: none;
justify-content: space-around;
width: 40%
}
li a {
text-decoration: none;
color: grey;
}
.logo {
font-family: 'Italianno', cursive;
margin-right: auto;
font-size: 30px;
font-weight: bold;
}
.menu {
display: flex;
flex-direction: column;
justify-content: flex-start;
background-color: rgba(91, 126, 172, 0.5);
height: 100vh;
width: 50vw;
position: absolute;
top: 50px;
right: 0;
transform: translateX(100%);
transition: transform 200ms ease-in;
}
.menupop {
transform: translateX(0%);
}
.menupop div {
animation-name: menuFade;
animation-timing-function: ease;
animation-fill-mode: forwards;
}
.menu div {
width: 100%;
height: 20%;
background-color: rgb(245, 249, 250, 0.6);
margin-bottom: 1px;
display: flex;
justify-content: center;
align-items: center;
transform: translateX(100%);
}
.menu div a {
text-decoration: none;
}
#keyframes menuFade {
from {
transform: translateX(100%);
}
to {
transform: translateX(0px);
}
}
#media only screen and (max-width: 600px) {
ul {
width: 60%;
}
}
#media only screen and (max-width: 430px) {
.burger {
display: flex;
}
ul {
display: none;
}
}
<body>
<nav>
<div class="logo">Logo</div>
<ul>
<li>Flower</li>
<li>Tree</li>
<li>About us</li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
<div class="menu">
<div class="first">Flower</div>
<div class="second">Tree</div>
<div class="third">About us</div>
</div>
<main>
<header id="jumbotron">
This is a header with ID "jumbotron".
</header>
<section id="rose">
This is a section with ID "rose".
</section>
<section id="rose-img">
this is a section with ID "rose-img".
</section>
<section id="tree">
<div class="privacy">
This is a div with class "privacy".
</div>
<div class="hedge">
This is a div with class "hedge".
</div>
</section>
<section id="about-us">
This is a section with ID "about-us".
</section>
<section id="contact-us">
This is a section with ID "contact-us".
</section>
</main>
</body>

CSS make card the same height running React and Bootstrap

I am using the Bootstrap 4 in React. Currently, the card for Item 1 is shorter than Item 2, but I want both the cards to have the same height.
I am expected to have more cards added to the bottom in the future.
My CSS file:
.CountryDetail {
width: 70%;
display: flex;
flex-wrap: wrap;
justify-content: center;
margin-left: 15%;
margin-right: 15%;
}
.cards {
background: #fcfcfc;
margin: 20px 40px;
transition: 0.4s all;
width: 800px;
padding: 20px;
margin-left: auto;
margin-right: auto;
}
.icon-size {
font-size: 50px;
}
.shadow-1 {
position: relative;
left: 0;
right: 0;
top: 0;
bottom: 0;
z-index: 0;
box-sizing: border-box;
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.13);
}
.shadow-1:hover {
z-index: 1;
box-shadow: 0 8px 50px rgba(0, 0, 0, 0.2);
transform: scale(1.05);
transition: all 0.5s ease;
}
.vertical-center {
justify-content: center;
}
.show-hover {
opacity: 0;
transform: translateX(-20px);
transition: 1s all;
}
.cards:hover .show-hover {
opacity: 1;
transform: translateX(0px);
color: blue;
}
.vertical-align {
display: flex;
align-items: center;
}
My JavaScript file:
<div className="CountryDetail">
<div className="cards shadow-1 container">
<div className="row vertical-align">
<div className="col-2 text-center">
<FontAwesomeIcon className="icon-hover icon-size" icon={faPlane} color="#A9A9A9" />
</div>
<div className="col-8">
<h3>Item 1</h3>
</div>
<div className="col-2 text-center">
<FontAwesomeIcon className="show-hover icon-size" icon={faArrowRight} color="#A9A9A9" />
</div>
</div>
</div>
<div className="cards shadow-1 container">
<div className="row vertical-align">
<div className="col-2 text-center">
<FontAwesomeIcon className="icon-hover icon-size" icon={faHeart} color="#A9A9A9" />
</div>
<div className="col-8">
<h3>Item 2</h3>
<span>Under Construction...</span>
<h2>Testing</h2>
</div>
<div className="col-2 text-center">
<FontAwesomeIcon className="show-hover icon-size" icon={faArrowRight} color="#A9A9A9" />
</div>
</div>
</div>
</div>
I have tried to add line-height or height in CSS for cards class, but it would mess up the height of the contents inside the card.
If I modify my "cards" class like this by adding min-height:
.cards {
min-height: 200px;
background: #fcfcfc;
margin: 20px 40px;
transition: 0.4s all;
width: 800px;
padding: 20px;
margin-left: auto;
margin-right: auto;
}
I would get this:
Based on your need of the same height and centered content, update your CSS to:
.cards {
display: flex;
flex-direction: column;
justify-content: center;
min-height: 200px;
background: #fcfcfc;
margin: 20px 40px;
transition: 0.4s all;
width: 800px;
padding: 20px;
margin-left: auto;
margin-right: auto;
}
If you set the card to be display flex, since you have only one direct child (row), you can center that vertically.

why does my column go down?

http://codepen.io/anon/pen/OMLLwB
#news {
width: 85%;
margin: 0 auto;
}
#news ul {
list-style: none;
margin: 0;
padding: 0;
}
#worldMap img {
width: 100%;
}
.newspiece {
margin-bottom: 2.5%;
padding: 20px;
background-color: #90C3D4;
height: 130px;
}
.newspiece h3 {
border-bottom: 3px solid black;
margin-bottom: 10px;
}
#media(min-width: 600px) {
.newspiece {
width: 25%;
margin-left: 5%;
display: inline-block;
vertical-align: top;
overflow: hidden;
}
.newspiece:first-child {
margin-left:0;
}
}
Am i missing something here? the width the total container (#news) is 85%, the width of each item is 25%, and two of them have a 5% left margin, total sums to 85%, then why do i resize it, the rightmost column goes down?
i have changed your html/css. this is a cleaner solution and is suported among all browsers
html:
<div class="flex">
<div class="box">
<h3>Title</h3>
<p>Content</p>
</div>
<div class="box">
<h3>Title</h3>
<img src="http://www.placecage.com/400/300" alt="">
</div>
<div class="box">
<h3>Title</h3>
<p>Content</p>
</div>
</div>
css:
.flex {
display: flex;
justify-content: space-around;
flex-wrap: wrap;
}
.box {
width: 300px;
margin: 10px;
padding: 20px;
background: #90C3D4;
}
.box h3 {
padding-bottom: 5px;
border-bottom: 3px solid black;
}
.box img {
max-width: 100%;
}
* { box-sizing: border-box; }
The padding adds to the total width of the element if box-sizing: border-box is not used.

How to expand background from flexbox grid text & images with equal height

I have done a flexbox grid, it combines text cells with image cells. All the cells have the same height in each resolution, also text cells. Text cells have the text centered vertically, and this is a must. Now I need to assign each text cell a different background-color, but here I have the problem.
With align-items: center the text is centered vertically, but the background color is only applied to the text. Without align-items: center the background expands to all the cell, but the content is not centered vertically.
Here is the codepen
Any suggestions in how to achieve the two features at same time?
vertically centered text
different background-color for each text cell
Thanks!
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
margin: 0;
background: #333;
padding: 30px;
font-family: Helvetica;
}
a {
color: white;
text-decoration: none;
}
h2 {
font-size: 1.2em;
}
.wrap-items {
background-color: #ccc;
padding: 0;
margin: 0 auto;
display: -ms-flexbox;
-ms-flex-wrap: wrap;
-ms-flex-direction: row;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
display: -webkit-box;
display: flex;
justify-content: center;
align-items: center;
align-content: center;
}
.wrap-items .item {
-webkit-box-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 1 0 33.333%;
width: 33.33333%;
margin: 0;
padding: 0;
color: white;
font-size: 0;
border: none;
background-color: steelblue;
}
.wrap-items .item.span-2 {
-webkit-box-flex: 2 0 auto;
-ms-flex: 2 0 auto;
flex: 2 0 auto;
width: 66.6666%;
height: auto;
}
.wrap-items .item img {
width: 100%;
height: auto;
}
.wrap-items .item > .text {
padding: 10px;
font-size: 20px;
height: 100%;
}
.item.un .text{
background-color: orange;
}
.item.dos {
background-color: brown;
}
.item.tres {
background-color: violet;
}
#media screen and (max-width: 760px) {
.wrap-items .item {
margin: 0;
flex: 50%
}
}
#media screen and (max-width: 400px) {
.wrap-items .item {
margin: 0;
flex: 100%;
}
}
Use transforms instead of flexbox to vertically center the text.
1) Remove align-items: center from the .wrap-items class
2) Adjust the .wrap-items .item > .text class like this:
.wrap-items .item > .text {
padding: 10px;
font-size: 20px;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
Updated Codepen
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
margin: 0;
background: #333;
padding: 30px;
font-family: Helvetica;
}
a {
color: white;
text-decoration: none;
}
h2 {
font-size: 1.2em;
}
.wrap-items {
background-color: #ccc;
padding: 0;
margin: 0 auto;
display: -ms-flexbox;
-ms-flex-wrap: wrap;
-ms-flex-direction: row;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
display: -webkit-box;
display: flex;
justify-content: center;
//align-items: center;
//align-content: center;
}
.wrap-items .item {
-webkit-box-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 1 0 33.333%;
width: 33.33333%;
margin: 0;
padding: 0;
color: white;
font-size: 0;
border: none;
background-color: steelblue;
position: relative;
}
.wrap-items .item.span-2 {
-webkit-box-flex: 2 0 auto;
-ms-flex: 2 0 auto;
flex: 2 0 auto;
width: 66.6666%;
height: auto;
}
.wrap-items .item img {
width: 100%;
height: auto;
}
.wrap-items .item > .text {
padding: 10px;
font-size: 20px;
position: absolute;
top: 50%;
transform: translate(0, -50%);
}
.item.un {
background-color: orange;
}
.item.dos {
background-color: brown;
}
.item.tres {
background-color: purple;
}
#media screen and (max-width: 760px) {
.wrap-items .item {
margin: 0;
flex: 50%
}
}
#media screen and (max-width: 400px) {
.wrap-items .item {
margin: 0;
flex: 100%;
}
}
<div class="wrap-items">
<!-- row 1 -->
<div class="item span-2">
<div class="text">
<h2>THE TITLE</h2>
<p>Just be water my friend</p>
<p>Small people can do very big things.</p>
</div>
</div>
<div class="item">
<img src="https://placekitten.com/g/800/600" alt>
</div>
<!-- row 2 -->
<div class="item un">
<div class="text ">
one or more lines of text inside the box
</div>
</div>
<div class="item">
<img src="https://placekitten.com/g/800/600" alt>
</div>
<div class="item dos">
<div class="text">
text in the middle
</div>
</div>
<!-- row 3 -->
<div class="item">
<img src="https://placekitten.com/g/800/600" alt="">
</div>
<div class="item tres">
<div class="text">
three lines of text centered vertically in the middle of the box
</div>
</div>
<div class="item">
<img src="https://placekitten.com/g/800/600" alt>
</div>
</div>

Flexbox child not reading height/width of parent

I was experimenting with flexbox and created this layout. I'm trying to get the middle white box that says "hey" to be 90% width/height of the parent but percentages don't seem to affect it. (I have it set as 100px/100px currently which works)
It's strange since the parent has a defined width/height on inspection.
Can anyone tell me how to implement that? (also, general critique on how I used flexbox appreciated as well)
http://jsfiddle.net/yv3Lw5gy/1/
relevant class:
.super-thing{
height: 100px;
width: 100px;
background-color: white;
margin:auto;
box-shadow: 2px 1px 1px #000;
}
The .body parent of .super-thing is display: flex so its children don't inherit its height or width if they don't have flex properties.
###The power of flex compels you!
Set flex: 1 on .super-thing so it grows and shrinks with a 1% margin to create a gap. There is no need for a width or height.
.super-thing {
background-color: white;
margin: 1%;
box-shadow: 2px 1px 1px #000;
flex: 1;
}
###Complete Example
In this example, the box-sizing: border-box and the removal of the margin to be replaced with padding on <body>, get the entire container properly sized in the viewport so there is no scrollbar.
* {
background-color: rgba(255, 0, 0, .2);
box-sizing: border-box;
}
* * {
background-color: rgba(0, 255, 0, .2);
}
* * * {
background-color: rgba(0, 0, 255, .2);
}
* * * * {
background-color: rgba(255, 0, 255, .2);
}
* * * * * {
background-color: rgba(0, 255, 255, .2);
}
* * * * * * {
background-color: rgba(255, 255, 0, .2);
}
html,
body,
.container {
height: 100%;
}
body {
padding: 10px;
margin: 0;
}
.container {
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: stretch;
/* align-content: center; */
}
.header {
flex: 1 0 30px;
display: flex;
}
.header-left {
flex: 11 0 auto;
}
.header-right {
flex: 1 0 auto;
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;
}
.header-right .small-item {
width: 100%;
outline: 1px solid #fff;
}
.main {
background-color: #fff;
flex: 10 0 30px;
display: flex;
}
.left-column {
flex: 3;
flex-direction: column;
display: flex;
justify-content: center;
}
.left-column .story {
flex: 2;
outline: 1px solid white;
/* margin:auto; */
}
.right-column {
flex: 12;
background-color: #f0f;
display: flex;
flex-direction: column;
}
.right-column-body {
flex: 10;
display: flex;
flex-direction: column;
}
.right-column-body .header {
flex: 1;
display: flex;
justify-content: center;
}
.thing {
width: 20%;
margin: 5px
}
.super-thing {
background-color: white;
margin: 1%;
box-shadow: 2px 1px 1px #000;
flex: 1;
}
.right-column-body .body {
background-color: #ccc;
flex: 5;
display: flex;
flex-direction: column;
justify-content: center;
}
.right-column-footer {
flex: 1;
background-color: white;
}
.footer {
flex: 1 0 30px;
}
<div class="container">
<div class="header">
<div class="header-left">Left</div>
<div class="header-right">
<div class="small-item">A</div>
<div class="small-item">B</div>
<div class="small-item">C</div>
<div class="small-item">D</div>
</div>
</div>
<div class="main">
<div class="left-column">
<span class="story">A</span>
<span class="story">A</span>
<span class="story">A</span>
<span class="story">A</span>
</div>
<div class="right-column">
<div class="right-column-body">
<div class="header">
<div class="thing">A</div>
<div class="thing">B</div>
<div class="thing">C</div>
</div>
<div class="body">
<div class="super-thing">Hey</div>
</div>
<div class="right-column-footer">Footer</div>
</div>
</div>
</div>
<div class="footer">Footer</div>
</div>

Resources