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

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

Related

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>

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

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>

Scroll in a deeply nested flex container - min-height not working

I have the following flex layout. I need overflow to occur in div.stretchy. I would like div.stretchy to reach the boundary of the page and then overflow it's content. Per this stackoverflow post, I have tried many combinations of min-height: 0 and overflow: hidden, but div.stretchy will not shrink.
body {
margin: 0;
}
.page-wrapper {
display: flex;
height: 100vh;
width: 100vw;
overflow: hidden;
}
.sidebar {
background: blue;
flex: 0 0 40px;
}
.main {
background: green;
display: flex;
flex-direction: column;
flex: 1;
min-width: 0;
}
.topbar {
display: flex;
flex: 0 0 40px;
background-color: red;
}
.content {
display: flex;
overflow: auto;
}
.row {
display: flex;
}
.column {
display: flex;
flex-direction: column;
}
.grow {
flex-grow: 1;
}
.card {
height: 300px;
border: solid 1px;
min-width: 600px;
}
.card .row {
justify-content: space-between;
}
.wrapper {
padding: 16px;
height: fit-content;
}
.stats {
padding: 8px;
background-color: pink;
}
.overflow-hidden {
overflow: hidden;
}
.body .column {
background-color: indigo;
}
.wide-content {
background-color: yellow;
height: 50px;
width: 800px;
}
.block {
flex: none;
width: 300px;
height: 200px;
&:nth-of-type(odd) {
background: darken(green, 10%);
}
}
<div class="page-wrapper">
<div class="sidebar">sidebar</div>
<div class="main">
<div class="topbar">topbar</div>
<div class="content">
<div class="wrapper">
<div class="card column grow">
<div class="stats row">
<span>12345</span>
<span>12345</span>
<span>12345</span>
</div>
<div class="body row grow">
<div class="column">
<span>Dynamic Width Content</span>
</div>
<div class="stretchy column grow overflow-hidden">
<div class="wide-content grow"></div>
</div>
<div class="column">
<span>Dynamic Width Content</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
This is a tough battle. The enemy is cunning, deceitful and ruthless. I say we launch a massive carpet bombing campaign, showering the whole area with min size overrides. That should clear out 80% of the problem. Then we send in the ground troops to finish the job :-)
* {
min-width: 0 !important;
min-height: 0 !important;
}
.page-wrapper {
display: flex;
height: 100vh;
/* width: 100vw; */
/* overflow: hidden; */
}
.sidebar {
background: cornflowerblue;
/* flex: 0 0 40px; */
flex: 0 0 100px; /* changed for demo purposes */
}
.main {
background: lightgreen;
display: flex;
flex-direction: column;
flex: 1;
}
.topbar {
display: flex;
flex: 0 0 40px;
background-color: orangered;
}
.content {
display: flex;
/* overflow: auto; */
flex: 1; /* added */
}
.row {
display: flex;
}
.column {
display: flex;
flex-direction: column;
}
.grow {
flex-grow: 1;
}
.card {
/* height: 300px; */
border: solid 1px;
min-width: 600px;
}
.stretchy {
overflow: auto;
}
.card .row {
justify-content: space-between;
}
.wrapper {
padding: 16px;
/* height: fit-content; */
display: flex; /* added */
}
.stats {
padding: 8px;
background-color: pink;
}
.overflow-hidden {
/* overflow: hidden; */
}
.body .column {
background-color: violet;
}
.wide-content {
background-color: yellow;
height: 50px;
width: 800px;
}
body {
margin: 0;
}
/*
.block {
flex: none;
width: 300px;
height: 200px;
&:nth-of-type(odd) {
background: darken(green, 10%);
}
}
*/
<div class="page-wrapper">
<div class="sidebar">sidebar</div>
<div class="main">
<div class="topbar">topbar</div>
<div class="content">
<div class="wrapper">
<div class="card column grow">
<div class="stats row">
<span>12345</span>
<span>12345</span>
<span>12345</span>
</div>
<div class="body row grow">
<div class="column">
<span>Dynamic Width Content</span>
</div>
<div class="stretchy column grow overflow-hidden">
<div class="wide-content grow">test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br></div>
</div>
<div class="column">
<span>Dynamic Width Content</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
jsFiddle demo

Flex direction column is ignored if one of the child has relative postion

I have the code below.
On the .container I set
display:flex;
flex-direction: column;
to make my previous and next arrow to go before and after the container items. It doesn't work the flex direction remains row.
.container {
margin: 0;
position: relative;
overflow: hidden;
border: 1px solid green;
display: flex;
flex-direction: column;
}
.items {
left: 0;
top: 0;
display: flex;
flex-direction: row;
padding: 0.5rem 0;
position: relative;
margin: 2px;
}
.item {
align-items: center;
display: flex;
height: 60px;
justify-content: center;
margin: 2px;
}
.item>* {
cursor: pointer;
display: flex;
max-height: 100%;
}
.item img {
display: block;
max-height: 100%;
}
.previous::before,
.next::before {
color: #9e9e9e;
font: 400 1.5rem/1 iconfont !important;
speak: none;
text-transform: none !important;
}
<div class="container">
<div class="previous">S</div>
<div class="items">
<div class="item">
<img src="https://via.placeholder.com/50">
</div>
<div class="item">
<img src="https://via.placeholder.com/50">
</div>
<div class="item">
<img src="https://via.placeholder.com/50">
</div>
<div class="item">
<img src="https://via.placeholder.com/50">
</div>
</div>
<div class="next">S</div>
</div>
Actually you need to remove flex-direction: column from .container, because you are trying to create a row by putting the arrows inline with the items
.container {
margin: 0;
position: relative;
overflow: hidden;
border: 1px solid green;
display: flex;
align-items: center
}
.items {
left: 0;
top: 0;
display: flex;
padding: 0.5rem 0;
position: relative;
margin: 2px;
flex: 1;
background: lightblue;
justify-content: center
}
.item {
align-items: center;
display: flex;
height: 60px;
justify-content: center;
margin: 2px;
}
.item>* {
cursor: pointer;
display: flex;
max-height: 100%;
}
.item img {
display: block;
max-height: 100%;
}
.previous::before,
.next::before {
color: #9e9e9e;
font: 400 1.5rem/1 iconfont !important;
speak: none;
text-transform: none !important;
}
<div class="container">
<div class="previous">S</div>
<div class="items">
<div class="item">
<img src="https://via.placeholder.com/50">
</div>
<div class="item">
<img src="https://via.placeholder.com/50">
</div>
<div class="item">
<img src="https://via.placeholder.com/50">
</div>
<div class="item">
<img src="https://via.placeholder.com/50">
</div>
</div>
<div class="next">S</div>
</div>

Accomplishing this grid with css

Can I accomplish this grid layout with flexbox? To have the first element take up 2 rows height and then continue after it?
Check image.
You can achive it by dividing this layout in 2 columns while the 2nd column will have a nested flexbox layout as well.
HTML Structure:
<div class="container">
<div class="col box1">1</div>
<div class="col col2">
<div class="box2">2</div>
<div class="box3">3</div>
<div class="box4">4</div>
<div class="box5">5</div>
</div>
</div>
Necessary Styles:
.container {
min-height: 100vh;
display: flex;
}
.col {
flex-grow: 1;
color: #fff;
}
.col2 {
flex-wrap: wrap;
display: flex;
}
.col2 > div {
flex-basis: 50%;
flex-grow: 1;
}
.box1 {
display: flex;
}
* {box-sizing: border-box;}
body {
margin: 0;
}
.container {
min-height: 100vh;
display: flex;
}
.col {
flex-grow: 1;
color: #fff;
}
.col2 {
flex-wrap: wrap;
display: flex;
}
.col2 > div {
flex-basis: 50%;
padding: 10px;
flex-grow: 1;
}
.box1 {
background: brown;
padding: 10px;
display: flex;
}
.box2 {
background: pink;
}
.box3 {
background: black;
}
.box4 {
background: yellow;
}
.box5 {
background: royalblue;
}
<div class="container">
<div class="col box1">1</div>
<div class="col col2">
<div class="box2">2</div>
<div class="box3">3</div>
<div class="box4">4</div>
<div class="box5">5</div>
</div>
</div>
You can use this HTML structure but you need to set fixed height on parent div. Then you just use flex-direction: column and flex-wrap: wrap.
* {
box-sizing: border-box;
}
.content {
height: 100vh;
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
div div:first-child {
flex: 0 0 100%;
width: 50%;
background: #880015;
}
div div:not(:first-child) {
width: 25%;
flex: 0 0 50%;
border: 1px solid black;
}
<div class="content">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
</div>