Why are my divs disappearing when I use flex-direction? - html

I am currently working on my webpage's responsiveness and have implemented flexbox for the positioning of my page's elements. The issue that I am experiencing is that when I use 'flex-direction: column' in my media query, the divs disappear upon the browser being resized. What am I doing wrong here?
#blue {
background-color: #57afb5;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#dark-green {
background-color: #29914c;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#green {
background-color: #91e3ad;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#orange {
background-color: #c98a32;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#top {
display: flex;
justify-content: space-around;
margin-top: 2%;
}
#bottom {
display: flex;
justify-content: space-around;
margin-top: 2%;
}
/* responsive web design*/
#media only screen and (max-width: 960px) {
#top {
display: flex;
flex-direction: column;
}
#bottom {
display: flex;
flex-direction: column;
}
/*end of responsive web design*/
<div class='main-content'>
<div id='Navbar_Link-Toggle' style='font-size: 20px'>
<i id='main' class='fas fa-bars'></i>
</div>
<div class='container'>
<div class='Navbar'>
<a class='links' href=''>FOOD</a>
<a class='links' href=''>FUN</a>
<img id='center-logo' src='img/SAMO.png'>
<a class='links' href=''>HISTORY</a>
<a class='links' href=''>LOCATION</a>
</div>
</div>
<div class='header'>
<img id='food' src='img/food.jpg'>
</div>
</div>
<div id='top'>
<div id='blue'></div>
<div id='dark-green'></div>
</div>
<div id='bottom'>
<div id='green'></div>
<div id='orange'></div>
</div>
</div>

The #top and #bottom divs have no height. You could give them a hight. I.E #top, #bottom { hight: 700px; }
Alternatively, use display: block; as illustrated in the example below.
Side note: When I have missing elements, I'll sometimes throw a border: solid red 2px; on them to better visualize what is happening. Hope that helps for next time!
#blue {
background-color: #57afb5;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#dark-green {
background-color: #29914c;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#green {
background-color: #91e3ad;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#orange {
background-color: #c98a32;
max-width: 470px;
height: 350px;
flex: 1;
justify-content: space-around;
}
#top {
display: flex;
justify-content: space-around;
margin-top: 2%;
}
#bottom {
display: flex;
justify-content: space-around;
margin-top: 2%;
}
/* responsive web design*/
#media only screen and (max-width: 960px) {
#top,
#bottom {
display: block;
}
}
/*end of responsive web design*/
<div class='main-content'>
<div id='Navbar_Link-Toggle' style='font-size: 20px'>
<i id='main' class='fas fa-bars'></i>
</div>
<div class='container'>
<div class='Navbar'>
<a class='links' href=''>FOOD</a>
<a class='links' href=''>FUN</a>
<img id='center-logo' src='img/SAMO.png'>
<a class='links' href=''>HISTORY</a>
<a class='links' href=''>LOCATION</a>
</div>
</div>
<div class='header'>
<img id='food' src='img/food.jpg'>
</div>
</div>
<div id='top'>
<div id='blue'></div>
<div id='dark-green'></div>
</div>
<div id='bottom'>
<div id='green'></div>
<div id='orange'></div>
</div>

Related

Problem with positioning 2 div horizontally

I recently started to learn html and css. I have problem with positioning 2 div horizontally
My code:
HTML:
<div class = "cart__itemwraper">
<div class = "cart__item__image">
<img src="images/laptop.png" style = "width: 100%; height: 100%;"/>
</div>
<div class = "cart__item__desc">
<div class = "cart__item__name">
<span>name name</span>
</div>
<div class = "cart__item__price">
<span>price price</span>
</div>
</div>
<div class= "cart__item__counter">
<span> counter</span>
</div>
</div>
CSS:
.cart__itemwraper{
width: 100%;
display: flex;
}
.cart__item__image {
width: 20%;
background-color: red;
}
.cart__item__desc {
width: 40%;
display: flex;
background-color: #814ce4;
}
.cart__item__name {
background-color: greenyellow;
width: 100%;
}
.cart__item__price {
width: 100%;
background-color: #e44caa;
display: flex;
align-items: flex-end;
}
.cart__item__counter{
width: 40%;
background-color: yellow;
display: flex;
align-items: flex-end;
justify-content: flex-end;
}
I would like to move the price to the position marked with the arrow in my using flexbox screenshot. Can someone tell my how i can do it?
you can just add flex-direction: column; justify-content: space-between; to the cart__item__desc class.
.cart__itemwraper {
width: 100%;
display: flex;
}
.cart__item__image {
width: 20%;
background-color: red;
}
.cart__item__desc {
width: 40%;
display: flex;
background-color: #814ce4;
flex-direction: column;
justify-content: space-between;
}
.cart__item__name {
background-color: greenyellow;
width: 100%;
}
.cart__item__price {
width: 100%;
background-color: #e44caa;
display: flex;
align-items: flex-end;
}
.cart__item__counter {
width: 40%;
background-color: yellow;
display: flex;
align-items: flex-end;
justify-content: flex-end;
}
<div class = "cart__itemwraper">
<div class = "cart__item__image">
<img src="images/laptop.png" style = "width: 100%; height: 100%;"/>
</div>
<div class = "cart__item__desc">
<div class = "cart__item__name">
<span>name name</span>
</div>
<div class = "cart__item__price">
<span>price price</span>
</div>
</div>
<div class= "cart__item__counter">
<span> counter</span>
</div>
</div>
Hope this answers your question.
You can do this using order:1 and order:2 on the cart__item__name and cart__item__price classes, like this:
.cart__item__name {
background-color: greenyellow;
width: 100%;
order:2;
}
.cart__item__price {
width: 100%;
background-color: #e44caa;
display: flex;
align-items: flex-end;
order:1;
}
this will move the cart__item__price div to the first position, and the cart__item__name to the second.

How to make nested flex child scrollable

I have a list of messages (which is a flex child) in a container with unknown height and want to make them scrollable. But I cannot find a proper combination of flex-grow: 1, min-height: 0 and other flex tricks to make it working - message list is still bigger than its parent.
When I add overflow-y: auto to its parent - it works but this parent besides messages list includes some content which should not scroll.
Here's my example for this case: https://jsfiddle.net/ecbtrn58/2/
<div class="page">
<div class="messages-section">
<div class="header">Your messages</div>
<div class="content">
<img src="https://http.cat/100" width="70" height="50"/>
<div class="messages-list">
<div class="message">Hi.</div>
<div class="message">Hello.</div>
<div class="message">Good morning.</div>
<div class="message">Yo!</div>
</div>
</div>
</div>
</div>
.page {
background-color: #ddd;
width: 300px;
height: 300px;
.messages-section {
display: flex;
flex-direction: column;
width: 50%;
height: 100%;
background-color: #ccc;
.header {
background: #bbb;
padding: 5px;
}
.content {
display: flex;
flex-direction: column;
align-items: center;
padding: 5px;
.messages-list {
display: flex;
flex-direction: column;
overflow-y: auto;
/* What to add here to make it scrollable? */
.message {
height: 50px;
margin: 10px;
padding: 10px;
background: #1dc497;
}
}
}
}
}
How can I make messages list to scroll?
You have to set the height of .content to 100% and make it scrollable:
.page {
background-color: #ddd;
width: 300px;
height: 300px;
}
.page .messages-section {
display: flex;
flex-direction: column;
width: 50%;
height: 100%;
background-color: #ccc;
}
.page .messages-section .header {
background: #bbb;
padding: 5px;
}
.page .messages-section .content {
display: flex;
flex-direction: column;
align-items: center;
padding: 5px;
height: 100%;
overflow-x: scroll;
}
.page .messages-section .content .messages-list {
display: flex;
flex-direction: column;
overflow-y: auto;
/* What to add here to make it scrollable? */
}
.page .messages-section .content .messages-list .message {
height: 50px;
margin: 10px;
padding: 10px;
background: #1dc497;
}
<div class="page">
<div class="messages-section">
<div class="header">Your messages</div>
<div class="content">
<img src="https://http.cat/100" width="70" height="50" />
<div class="messages-list">
<div class="message">Hi.</div>
<div class="message">Hello.</div>
<div class="message">Good morning.</div>
<div class="message">Yo!</div>
</div>
</div>
</div>
</div>

How to use image inside a div without moving the other elements

I'm using flex-box and I'm trying to make my website responsive. Right now I need to put an image inside the yellow divs but when I do that all the structure changes cause of that. I trying playing around with the width and height but could make that work. Hope one of you could help me out
:root {
--main-color: #0077be;
}
body {
font-family: "Open Sans", sans-serif;
text-align: right;
}
ul,
li {
list-style: none;
padding: 0;
margin: 0;
}
div {
border: 1px solid gray;
}
.screen {
padding: 100px;
background-color: #ededed;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.wrapper {
width: 1200px;
height: 450px;
background-color: #fff;
display: flex;
flex-direction: column;
}
.section-title {
flex-grow: 1;
}
.news-conainer {
flex-grow: 11;
display: flex;
flex-direction: column;
padding: 17px;
}
.main-news {
flex-grow: 11;
display: flex;
flex-direction: row-reverse;
}
.foot-news {
flex-grow: 1;
}
.right {
flex-grow: 7;
display: flex;
flex-direction: row-reverse;
margin-left: 20px;
}
.left {
flex-grow: 10;
display: flex;
flex-direction: column;
}
.img {
max-height: 100%;
overflow: hidden;
}
.img img {
max-width: 100%;
min-height: 100%;
}
.info {
flex-grow: 1;
}
.left-news {
flex-grow: 1;
display: flex;
flex-direction: row-reverse;
}
.left-news:nth-child(2) {
margin: 10px 0;
}
.left-news-img {
flex-grow: 1.5;
background-color: yellow;
}
.left-news-info {
flex-grow: 3;
}
<div class="screen">
<div class="wrapper">
<div class="section-title">title</div>
<div class="news-conainer">
<div class="main-news">
<div class="right">
<div class="img">
<img src="./new1.JPG" alt="" />
</div>
<div class="info">info</div>
</div>
<div class="left">
<div class="left-news">
<div class="left-news-img">
<img src="./new1.JPG" alt="" />
</div>
<div class="left-news-info"></div>
</div>
<div class="left-news">
<div class="left-news-img">
<img src="./new1.JPG" alt="" />
</div>
<div class="left-news-info"></div>
</div>
<div class="left-news">
<div class="left-news-img">
<img src="./new1.JPG" alt="" />
</div>
<div class="left-news-info"></div>
</div>
</div>
</div>
<div class="foot-news"></div>
</div>
</div>
</div>
The result I need is at the yellow divs to include a < img > tag without destroying the other sizes and their positions
Thanks!
result by adding
.left-news-img {
max-width: 30%;
}
.left-news-img > img {
max-width: 100%;
}
you have to use max-width
.left-news-img {
max-width: 30%;
}
.left-news-img > img {
max-width: 100%;
}
:root {
--main-color: #0077be;
}
body {
font-family: "Open Sans", sans-serif;
text-align: right;
}
ul,
li {
list-style: none;
padding: 0;
margin: 0;
}
div {
border: 1px solid gray;
}
.screen {
padding: 100px;
background-color: #ededed;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.wrapper {
width: 1200px;
height: 450px;
background-color: #fff;
display: flex;
flex-direction: column;
}
.section-title {
flex-grow: 1;
}
.news-conainer {
flex-grow: 11;
display: flex;
flex-direction: column;
padding: 17px;
}
.main-news {
flex-grow: 11;
display: flex;
flex-direction: row-reverse;
}
.foot-news {
flex-grow: 1;
}
.right {
flex-grow: 7;
display: flex;
flex-direction: row-reverse;
margin-left: 20px;
}
.left {
flex-grow: 10;
display: flex;
flex-direction: column;
}
.img {
max-height: 100%;
overflow: hidden;
}
.img img {
max-width: 100%;
}
.info {
flex-grow: 1;
}
.left-news {
flex-grow: 1;
display: flex;
flex-direction: row-reverse;
}
.left-news:nth-child(2) {
margin: 10px 0;
}
.left-news-img {
flex-grow: 1.5;
background-color: yellow;
}
.left-news-info {
flex-grow: 3;
}
.left-news-img {
max-width: 30%;
}
.left-news-img > img {
max-width: 100%;
}
.right > .img{
max-width: 30%;
}
<div class="screen">
<div class="wrapper">
<div class="section-title">title</div>
<div class="news-conainer">
<div class="main-news">
<div class="right">
<div class="img">
<img src="https://via.placeholder.com/250x130" alt="" />
</div>
<div class="info">info</div>
</div>
<div class="left">
<div class="left-news">
<div class="left-news-img">
<img src="https://via.placeholder.com/250x130" alt="" />
</div>
<div class="left-news-info"></div>
</div>
<div class="left-news">
<div class="left-news-img">
<img src="https://via.placeholder.com/250x130" alt="" />
</div>
<div class="left-news-info"></div>
</div>
<div class="left-news">
<div class="left-news-img">
<img src="./new1.JPG" alt="" />
</div>
<div class="left-news-info"></div>
</div>
</div>
</div>
<div class="foot-news"></div>
</div>
</div>
</div>
Update
for your right section you have to provide some max-width like
.right > .img{
max-width: 30%;
}
and get rid of height: 100% from .img img selector, check above demo as I have update the code there

justify-content: center not working on flex:100% in mobile

I'm trying to build a 3 by 3 flexbox grid of button links that are responsive. The grid looks great on desktop and tablet, however, in the mobile version the items are left aligned and are unable to be horizontally centered. My goal is to get one column for the mobile with all the content centered. I switched to flex: 100%; for the mobile version and tried using justify-content: center, but it doesn't work and I'm not sure why.
#love_button,
#additude_button,
#isnpr_button,
#functional_medicine_button,
#immh_button {
width: 178px;
height: 111px;
display: block;
background-repeat: no-repeat;
}
.flex-container {
height: 100%;
padding: 0;
margin: 0;
list-style: none;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
border: 4px solid orange;
}
.flex-item {
width: 178px;
height: 111px;
flex: 33.3% 33.3% 33.3%;
margin-bottom: 65px;
background: royalblue;
}
Mobile Media Query #media only screen and (max-device-width: 640px) {
/* Fixes bottom gap between resource link and footer mobile */
.push {
margin-bottom: 0;
}
.flex-container {
justify-content: center;
}
.flex-item {
flex: 100%;
}
#hide {
display: none;
}
}
<main>
<section id="resources">
<h1 class="resources_header">RESOURCES</h1>
<div class="flex-container">
</div>
<div class="flex-container push">
</div>
</section>
</main>
Instead try changing display:block and margin:10px auto in the media query.
Please check my snippet, and let me know if you have any questions.
#love_button,
#additude_button,
#isnpr_button,
#functional_medicine_button,
#immh_button {
width: 178px;
height: 111px;
display: block;
background-repeat: no-repeat;
}
.flex-container {
height: 100%;
padding: 0;
margin: 0;
list-style: none;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
justify-content: space-evenly;
}
.flex-item {
width: 178px;
height: 111px;
flex: 33.3% 33.3% 33.3%;
margin-bottom: 65px;
border: 1px solid red;
background: green;
}
#media only screen and (max-width: 640px) {
/* Fixes bottom gap between resource link and footer mobile */
.push {
margin-bottom: 0;
}
.flex-container {
display: block;
margin: auto;
}
.flex-item {
margin: 10px auto;
}
#hide {
display: none;
}
}
<main>
<section id="resources">
<h1 class="resources_header">RESOURCES</h1>
<div class="flex-container">
</div>
<div class="flex-container push">
</div>
</section>
</main>
Use align-items: center or align-content: space-evenly or both.
Without align-items or align-content
.flex-container {
width: 150px;
height: 150px;
border: 1px solid black;
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
align-content: space-evenly;
}
.flex-children {
width: 40px;
height: 40px;
background-color: red;
}
<div class="flex-container">
<div class="flex-children"></div>
<div class="flex-children"></div>
<div class="flex-children"></div>
<div class="flex-children"></div>
<div class="flex-children"></div>
<div class="flex-children"></div>
<div class="flex-children"></div>
<div class="flex-children"></div>
<div class="flex-children"></div>
</div>
With align-items or align-content
.flex-container {
width: 150px;
height: 150px;
border: 1px solid black;
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
align-items: center;
}
.flex-children {
width: 40px;
height: 40px;
background-color: red;
}
<div class="flex-container">
<div class="flex-children"></div>
<div class="flex-children"></div>
<div class="flex-children"></div>
<div class="flex-children"></div>
<div class="flex-children"></div>
<div class="flex-children"></div>
<div class="flex-children"></div>
<div class="flex-children"></div>
<div class="flex-children"></div>
</div>
CSS-tricks: align-items
CSS-tricks: align-content

How to align a div at the bottom of another which use flex box?

I need to place the orange button align at the bottom of the blue one.
With my current flex code I cannot get the result wanted. Any ideas how to fix it? Thanks
.content {
width: 350px;
height: 150px;
background-color: yellow;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.row1 {
background-color: red;
}
.row2 {
background-color: blue;
height: 100px
}
.icon {
width: 50px;
height: 50px;
background-color: orange;
align-items: center;
justify-content: center;
}
<div class="content">
<div class="row1">
</div>
<div class="row2">
<div class="icon">
</div>
</div>
</div>
You also need to set display: flex on row2 and then you can use align-self: flex-end on orange element.
.content {
width: 350px;
height: 150px;
background-color: yellow;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.row1 {
background-color: red;
}
.row2 {
background-color: blue;
display: flex;
height: 100px
}
.icon {
width: 50px;
height: 50px;
background-color: orange;
align-self: flex-end;
}
<div class="content">
<div class="row1"></div>
<div class="row2">
<div class="icon"></div>
</div>
</div>
Here we add this css in "row 2",
display: flex;
flex-direction: column;
its means if any object put at bottom then just parent block set above css and element we need to set as bottom add css "margin-top:auto;"
.content {
width: 350px;
height: 150px;
background-color: yellow;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.row1 {
background-color: red;
}
.row2 {
background-color: blue;
height: 100px;
display: flex;
flex-direction: column;
}
.icon {
width: 50px;
height: 50px;
background-color: orange;
align-items: center;
justify-content: center;
margin-top: auto;
}
<div class="content">
<div class="row1">
</div>
<div class="row2">
<div class="icon">
</div>
</div>
</div>
There is lot of stylings that can be removed from your code if it is not required, please find it here, I have changed .content and .row2 stylings
.content {
width: 350px;
height: 150px;
background-color: yellow;
display: flex;
}
.row1 {
background-color: red;
}
.row2 {
background-color: blue;
height: 100px;
flex: 1;
align-self: flex-end;
display: flex;
align-items: flex-end;
}
.icon {
width: 50px;
height: 50px;
background-color: orange;
align-items: center;
justify-content: center;
}
<div class="content">
<div class="row1">
</div>
<div class="row2">
<div class="icon">
</div>
</div>
</div>