FlexBox: How can i keep boxes fixed one below the other - css

I'm trying to create a page using FlexBox so i can keep the main image always centered with the top logo and page. And add of "off-center" text, next to the left.
My problem now is that i cannot make each box (image+text) keep their place vertically, boxes float and they have different space between each other based on what i'm using to view (big screen, laptop).
Not an expert with FlexBox, can i do that?
And also, i want to add a bar after the third box, 100% width...but it's placing in the middle of the page, between the boxes.
Any help will be welcome and i'll keep learning :)
body {
margin: 0;
}
.container {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
height: 100vh;
}
.top {
border: 1px solid white;
width: 50%;
height: 10%;
align-self: flex-start;
text-align: center;
}
.inner-container {
border: 1px solid white;
width: 50%;
height: 60%;
display: flex;
}
.center {
position: absolute;
width: 50%;
text-align: left;
align-self: flex-end;
}
.off-center {
position: absolute;
vertical-align: bottom;
text-transform: uppercase;
padding-left: 10px;
min-width: 200px;
align-self: flex-end;
}
.center, .off-center {
position: absolute;
}
.center img {width: 100%;}
.off-center h2 {font-size:12px; font-family: 'Open Sans', sans-serif; font-weight: 400;text-transform: uppercase; color:black; display: block!important;margin: 0px;}
.off-center p {font-size: 20px; font-family:'aileronbold', sans-serif; display: block!important;margin: 0px;text-transform: none;}
.row.subscriptionBar {display:flex; background-color: black; display: block; padding:20px;}
.subscriptionBar h2 {color:white;}
.firstPart {display: }
#media screen and (min-width: 769px) {
.off-center {
margin-left: 50%;
}
#media screen and (max-width: 768px) {
.off-center {
margin-top: 105px;
}
}
<div class="container">
<div class="top"><img src="https://www.oilandgasreinvented.com/img/logo-placeholder.svg" width="80px"></div>
<div class="inner-container">
<div class="center">
<img src="http://hdimages.org/wp-content/uploads/2017/03/placeholder-image1.gif" alt=""></div>
<div class="off-center">
<h2 class="">ArtistName 1</h2>
<p>509476ZclHtqXy</p></div>
</div>
<div class="inner-container">
<div class="center">
<img src="http://hdimages.org/wp-content/uploads/2017/03/placeholder-image1.gif" alt=""></div>
<div class="off-center">
<h2 class="">ArtistName 2</h2>
<p>Title lala 1</p></div>
</div>
<div class="inner-container">
<div class="center">
<img src="http://hdimages.org/wp-content/uploads/2017/03/placeholder-image1.gif" alt=""></div>
<div class="off-center">
<h2 class="">ArtistName 3 444</h2>
<p>Title 093830</p></div>
</div>
</div>

.wrapper {
display: flex;
flex-flow: row wrap;
font-weight: bold;
text-align: center;
}
.wrapper > * {
padding: 10px;
flex: 1 100%;
}
header{
background: tomato;
}
section{
display: flex
}
.main {
background: deepskyblue;
}
.main img{
max-width: 100%;
}
.full-width {
background: lightgreen;
}
.main { order: 2; flex: 3 0px; }
.aside { flex: 1 auto; }
.aside-1 { order: 1; background: gold; width: 10%}
.aside-2 { order: 3; background: hotpink; position: relative; width: 10%}
.aside-2 > span {
position: absolute;
bottom: 0;
left: 0;
}
.vertical {
width: 15%; /* Modify this value for different screen size*/
}
/* .full-width { order: 4; } */
/*#media all and (min-width: 600px) {
.aside { flex: 1 auto; }
}
#media all and (min-width: 800px) {
.main { flex: 3 0px; }
.aside-1 { order: 1; }
.main { order: 2; }
.aside-2 { order: 3; }
.footer { order: 4; }
}*/
body {
padding: 2em;
}
<div class="wrapper">
<header><img src="https://www.oilandgasreinvented.com/img/logo-placeholder.svg" width="80px"></header>
<section>
<div class="main">
<img src="http://hdimages.org/wp-content/uploads/2017/03/placeholder-image1.gif" alt="">
</div>
<aside class="aside aside-1"></aside>
<aside class="aside aside-2"><span>Aside Right</span></aside>
</section>
<section>
<div class="main">
<img src="https://picsum.photos/200/300" alt="">
</div>
<aside class="aside aside-1 vertical"></aside>
<aside class="aside aside-2 vertical"><span>Aside Right</span></aside>
</section>
<section>
<div class="main">
<img src="http://hdimages.org/wp-content/uploads/2017/03/placeholder-image1.gif" alt="">
</div>
<aside class="aside aside-1"></aside>
<aside class="aside aside-2"><span>Aside Right</span></aside>
</section>
<section class="full-width">Full Width</section>
<section>
<div class="main">
<img src="http://hdimages.org/wp-content/uploads/2017/03/placeholder-image1.gif" alt="">
</div>
<aside class="aside aside-1"></aside>
<aside class="aside aside-2"><span>Aside Right</span></aside>
</section>
</div>
I'm just doing a quick sample because it's not clear on what you're looking for. If this is the direction you're hoping to move toward, then I may be able to assist further.

You just have a few too many things going on with your combination of flexbox and absolute positioning. Below is a version of your snippet with much of the unnecessary css commented out (so you can see exactly what it is) plus a couple of new additions.
Most important changes other than removing some of the absolute positioning are removing flex-wrap from your .container and adding flex-direction: column as well as matching the widths of .center and .off-center so you can use position: absolute on .off-center (good use of it in that case) along with margin-left equivalent to the width to ensure that it is positioned just to the right of your image container.
body {
margin: 0;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
/*justify-content: center;
flex-wrap: wrap;
height: 100vh;*/
}
.top {
border: 1px solid white;
/*width: 50%;*/
height: 10%;
/*align-self: flex-start;*/
text-align: center;
}
.inner-container {
border: 1px solid white;
/*width: 50%;*/
height: 60%;
display: flex;
justify-content: space-around;
}
.center {
/*position: absolute;*/
width: 50%;
min-width: 200px;
text-align: left;
/*align-self: flex-end;*/
}
.off-center {
position: absolute;
/*vertical-align: bottom;*/
text-transform: uppercase;
padding-left: 10px;
width: 50%;
min-width: 200px;
align-self: flex-end;
margin-left: 50%;
}
/*.center, .off-center {
position: absolute;
}*/
.center img {
width: 100%;
}
.off-center h2 {
font-size: 12px;
font-family: 'Open Sans', sans-serif;
font-weight: 400;
text-transform: uppercase;
color: black;
display: block!important;
margin: 0px;
}
.off-center p {
font-size: 20px;
font-family: 'aileronbold', sans-serif;
display: block!important;
margin: 0px;
text-transform: none;
}
.row.subscriptionBar {
display: flex;
background-color: black;
display: block;
padding: 20px;
}
.subscriptionBar h2 {
color: white;
}
.firstPart {
display:
}
/*#media screen and (min-width: 769px) {
.off-center {
margin-left: 50%;
}
#media screen and (max-width: 768px) {
.off-center {
margin-top: 105px;
}
}*/
<div class="container">
<div class="top"><img src="https://www.oilandgasreinvented.com/img/logo-placeholder.svg" width="80px"></div>
<div class="inner-container">
<div class="center">
<img src="http://hdimages.org/wp-content/uploads/2017/03/placeholder-image1.gif" alt="">
</div>
<div class="off-center">
<h2 class="">ArtistName 1</h2>
<p>509476ZclHtqXy</p>
</div>
</div>
<div class="inner-container">
<div class="center">
<img src="http://hdimages.org/wp-content/uploads/2017/03/placeholder-image1.gif" alt=""></div>
<div class="off-center">
<h2 class="">ArtistName 2</h2>
<p>Title lala 1</p>
</div>
</div>
<div class="inner-container">
<div class="center">
<img src="http://hdimages.org/wp-content/uploads/2017/03/placeholder-image1.gif" alt=""></div>
<div class="off-center">
<h2 class="">ArtistName 3 444</h2>
<p>Title 093830</p>
</div>
</div>
</div>

Related

Why does the section slips under the other? CSS

the section #portfolio slips under the section #team-container when resizing the page to a smaller width
the website should show up like this: https://content.codecademy.com/PRO/independent-practice-projects/flexbox-business-site/example-site/index.html#team
Despite that what would you do to make the code better?
HTML:
<html><head>
<title>VC Enterprise Portfolio</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rel="shortcut icon" href="/tiefes-lernen.png" type="image/x-icon">
<link href="https://fonts.googleapis.com/css?family=Lexend+Mega" rel="stylesheet" type="text/css">
</head>
<body>
<div class="header">
<h1>The VC Experience</h1>
<div class="navbar">
<p>Mission Statement</p>
<p>Portfolio</p>
<p>Investment Team</p>
</div>
</div>
<div class="main">
<div class="mission">
<div class="content">
<h2>The VC Experience</h2>
<h4>Our fund is committed to the furthering entrepreneurs in the mobility sector.</h4>
</div>
</div>
<div id="portfolio">
<div class="content">
<h2>Our Mobility Portfolio</h2>
<h4>Impacting diverse sectors of mobility to better the human experience and the world we inhabit</h4>
<div class="companies">
<div class="company">
<img src="img URL">
<span>AeroArgonauts</span>
</div>
<div class="company">
<img src="img URL">
<span>AutoBueno</span>
</div>
<div class="company">
<img src="img URL">
<span>LaneChatter</span>
</div>
<div class="company">
<img src="img URL">
<span>TigerTreads</span>
</div>
<div class="company">
<img src="img URL">
<span>ValetToday</span>
</div>
</div>
</div>
</div>
<div id="team-container">
<h2>Our Team</h2>
<div class="team">
<div class="teammate">
<img src="img URL">
<h3>Lisa Fischer</h3>
<p>C.E.O. - Founder and Principal Investor</p>
</div>
<div class="teammate">
<img src="img URL">
<h3>Alex Lasker</h3>
<p>Partner - Research & Development</p>
</div>
<div class="teammate">
<img src="img URL">
<h3>Omar Carlsen</h3>
<p>Partner - Partnerships & Marketing</p>
</div>
</div>
</div>
<div class="contact">
<h2>The VC Experience</h2>
<h5>introduction#vcexperience.investors</h5>
<h5>555-867-5309</h5>
</div>
<div class="copyright">
<h5>copyright VC Enterprises 2020</h5>
</div>
</div>
</body></html>
CSS:
/*https://content.codecademy.com/PRO/independent-practice-projects/flexbox-business-site/img/skyscrapers.jpg*/
html {
display: block;
}
body {
font-family: "Lexend Mega", sans-serif;
}
h1 {
display: block;
margin-top: .75em;
font-size: 2.75rem;
}
.navbar {
display: flex;
justify-content: space-around;
font-size: 1.375rem;
margin: 0 10%;
}
.navbar p{
padding: 0 15px 10px 30px;
display: inline-block;
}
a {
color: black;
}
header {
display: flex;
flex-direction: column;
align-items: center;
}
body {
text-align: center;
}
.navbar {
justify-content: space-around;
margin: 0 10px;
display: flex;
}
.mission {
display: flex;
background-image: img URL;
width: auto;
padding-top: 10px;
}
.content {
display: block;
width: 100%;
height: 300px;
position: relative;
top: 80px;
margin-bottom: 0;
}
h2, h4 {
background-color: white;
position: relative;
font-size: 33px;
margin: 0;
padding: 10px;
}
h4 {
font-size: 22px;
}
#portfolio {
min-height: 500px;
margin: auto;
padding-top: 35px;
}
.company {
display: flex;
align-items: center;
justify-content: space-between;
}
.company img {
width: 200px;
height: auto;
padding: 10px;
margin-left: auto;
margin-right: auto;
}
span {
min-width: 240px;
padding: 5px;
text-align: center;
font-size: 22px;
font-weight: bold;
}
.companies {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
}
#team-container {
position: relative;
display: block;
}
#team-container h2 {
margin: 0;
padding: 5px;
display: block;
}
.team {
padding-top: 75px;
justify-content: center;
display: flex;
flex-wrap: wrap;
background-image: img URL;
}
.team img {
padding: 10px;
height : 300px;
width: 200px;
}
.teammate {
padding: 5px;
color: white;
flex-basis: 280px;
}
Tried to make some paddings to top or bottom but it looks whacky. I also tried to inspect the original site but can't find the reason why.

Define scroll div height by parent. Any tips?

So in resume, I have a #modal { max-height:90% } and I need to place a #footer element below and let the top remaing space to a scrollable div, but is not possible to have a scroll div without setting the height (or max-height, as far as I know).
The way I achieved this is calculating the height of the scrollable div, like so: https://codepen.io/murillotsoza/pen/xxpJPVJ (change the code view to the left/right side)
But I still think that there is a prettier solution without height calculations. Any tips?
#scroll {
max-height: calc(90vh - 18px - 50px);
overflow-y: auto;
overflow-x: hidden;
}
.item {
font-size: 18px; text-align: center;
line-height: 50px; width: 300px;
border-bottom: 1px solid #ccc;
}
#footer {
font-size: 18px; text-align: center;
line-height: 50px; width: 100%;
background-color: #333; color: white;
}
#title{
height: 18px;
background-color: black;
}
#content {
background-color: white;
}
#modal {
max-height: 90%;
overflow: hidden;
}
#background {
position: fixed;
top: 0; left: 0;
width: 100vw;
height: 100vh;
background-color: #eee;
display: flex;
justify-content: center;
align-items: center;
}
body { margin : 0; }
<div id='background'>
<div id="modal">
<div id="title"></div>
<div id="content">
<div id="scroll">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
<div id="footer">FOOTER</div>
</div>
</div>
</div>
Here is a modern CSS grid solution:
You can change around your grid-template fraction units to give more room to your header, scroll area and footer respectively. Currently the 80% height is divided into 4, with 1fr given to your header, 2fr given to your scroll area, and 1fr given to your footer.
* {
box-sizing: border-box;
}
html,
body {
height: 100vh;
}
body {
display: grid;
place-content: center;
margin: 0;
background-color: #eee;
}
.modal {
display: grid;
grid-template: 1fr 2fr 1fr / auto;
width: 300px;
height: 80%;
overflow: hidden;
}
header {
background-color: black;
}
.scroll {
overflow-y: auto;
}
.item {
font-size: 1.1rem;
text-align: center;
line-height: 3rem;
border-bottom: 1px solid #ccc;
}
footer {
font-size: 1.1rem;
text-align: center;
background-color: #333;
color: white;
}
<article class="modal">
<header></header>
<div class="scroll">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
<footer>FOOTER</footer>
</article>
Codepen: https://codepen.io/zachjensz/pen/MWrBVbv

flexbox item 100% of remaining width

I want to achieve the following scenario with flexbox
the green element is a text element and the width is flexible. The blue elements should have 100% of the remaining width beside the green element.
My current solution looks like this:
<div class="container">
<span class="title">title</span>
<div class="fullwidth"></div>
<div class="fullwidth"></div>
<div class="fullwidth"></div>
</div>
and the css:
.container {
display: flex;
align-items: center;
flex-wrap: wrap;
background-color: #EAEBEF;
.title {
padding: 20px;
background-color: #48CFAE;
}
.fullwidth {
background-color: #87BDFF;
flex: 0 0 100%;
height: 60px;
margin: 10px 0;
}
}
But it looks currently like this
here is a codepen example
Without changing the HTML this can be managed with CSS-Grid as an alternative to flexbox.
.container {
display: grid;
grid-template-columns: max-content 1fr;
grid-gap: 1em;
background-color: #EAEBEF;
margin-bottom: 1em;
}
.title {
padding: 10px;
grid-row: 2;
background-color: #48CFAE;
}
.fullwidth {
grid-column: 2;
background-color: #87BDFF;
height: 40px;
}
<div class="container">
<div class="title">title</div>
<div class="fullwidth"></div>
<div class="fullwidth"></div>
<div class="fullwidth"></div>
</div>
<div class="container">
<div class="title">Longer title</div>
<div class="fullwidth"></div>
<div class="fullwidth"></div>
<div class="fullwidth"></div>
</div>
Try this HTML and CSS markup. Basically You need to keep the left side in one div and the right side elements in another div.
.container {
display: flex;
align-items: center;
flex-wrap: nowrap;
background-color: #eaebef;
}
.container .title {
padding: 20px;
background-color: #48cfae;
}
.container .div1 {
width: 20%;
}
.container .div2 {
width: 80%;
}
.container .fullwidth {
background-color: #87bdff;
height: 60px;
margin: 10px 0;
}
<div class="container">
<div class="div1">
<span class="title">title</span>
</div>
<div class="div2">
<div class="fullwidth"></div>
<div class="fullwidth"></div>
<div class="fullwidth"></div>
</div>
</div>
See code below:
You have to warp fullwidth in div and set width to this div
also set width and margin to title
.container {
display: flex;
align-items: center;
flex-wrap: wrap;
background-color: #EAEBEF;
}
.title {
width: 20%;
padding: 20px;
background-color: #48CFAE;
margin-left: 15px;
}
.fullwidth {
background-color: #87BDFF;
flex: 0 0 100%;
height: 60px;
margin: 10px 0;
}
.a{
margin: 50px;
width: 50%;
}
<div class="container">
<span class="title">title</span>
<div class="a">
<div class="fullwidth"></div>
<div class="fullwidth"></div>
<div class="fullwidth"></div>
</div>
</div>
If you want to achive this without change your html but only your less, try this:
.container {
display: flex;
flex-direction: row;
align-items: center;
flex-wrap: wrap;
background-color: #EAEBEF;
justify-content: flex-end;
.title {
padding: 20px;
background-color: #48CFAE;
margin-right: 20px;
flex-grow: 1
}
.fullwidth {
background-color: #87BDFF;
height: 60px;
margin: 10px 0;
width: 80%;
}
}
your codepen edited

Center flex divs horizontally [duplicate]

This question already has answers here:
Special div shrinking
(2 answers)
Closed 5 years ago.
I have three divs that have different flex-basis. In order to make page responsive I want to put them centered one under one if width of the screen is less then 1000px.
Here's my example on Codepen:
.wrapper {
display: flex;
margin: 0 auto;
max-width: 1310px;
}
.left {
flex-basis: 555px;
}
.left__nav {
display: inline-block;
padding: 5px;
text-decoration: none;
}
.wrapper .right {
flex-basis: 462px;
display: flex;
justify-content: flex-end; /* align right (at end) */
}
.wrapper .button {
flex-basis: 193px;
text-align: center;
}
/* style for this demo */
.wrapper > div {
box-sizing: border-box;
border: 1px solid black;
border-collapse: collapse;
}
.mark-center {
text-align: center;
font-size: 30px;
color: red;
}
.container-small {
text-align: center;
background-color: lightgray;
max-width: 1100px;
margin: auto;
}
<div class="wrapper">
<div class="left">
<div class="links">
<a class="left__nav" href="#">Left Nav </a>
</div>
</div>
<div class="right">
Right
</div>
<div class="button">
Button
</div>
</div>
<div class="mark-center">
↑
</div>
<div class="container-small">
Center
</div>
Help me to put them that way if width < 1000px next way:
Seems to me you don't want flex-basis, you want width since these elements don't grow or shrink.
If so, you can use a media query and change the flex-direction to column after switching the flex-basis properties to width.
#media (max-width:1100px) {
.wrapper {
flex-direction:column;
align-items: center;
}
}
.wrapper {
display: flex;
margin: 0 auto;
max-width: 1310px;
}
.left {
width: 555px;
}
.left__nav {
display: inline-block;
padding: 5px;
text-decoration: none;
}
.wrapper .right {
width: 462px;
/* display: flex; */
/* justify-content: flex-end; */
/* align right (at end) */
}
.wrapper .button {
width: 193px;
text-align: center;
}
/* style for this demo */
.wrapper>div {
box-sizing: border-box;
border: 1px solid black;
border-collapse: collapse;
}
.mark-center {
text-align: center;
font-size: 30px;
color: red;
}
.container-small {
text-align: center;
background-color: lightgray;
max-width: 1100px;
margin: auto;
}
#media (max-width:1100px) {
.wrapper {
flex-direction: column;
align-items: center;
text-align: center;
}
}
<div class="wrapper">
<div class="left">
<div class="links">
<a class="left__nav" href="#">Left Nav </a>
</div>
</div>
<div class="right">
Right
</div>
<div class="button">
Button
</div>
</div>
<div class="mark-center">
↑
</div>
<div class="container-small">
Center
</div>

How can I align a big div next to three stacked divs?

Three small divs, stacked upon each other, with a big three-small-divs-high div on their right side. How do I do this? Does Bootstrap have anything prepared for it?
You don't need to use any framework, you can do this with Flexbox.
.content {
display: flex;
}
.left {
display: flex;
flex-direction: column;
flex: 1;
}
.box {
padding-bottom: 50px;
}
.right {
flex: 3;
background: #22B14C;
}
.box:nth-child(1) {background: #ED1C24;}
.box:nth-child(2) {background: #00A2E8;}
.box:nth-child(3) {background: #FFAEC9;}
<div class="content">
<div class="left">
<div class="box">Small DIv</div>
<div class="box">Small DIv</div>
<div class="box">Small DIv</div>
</div>
<div class="right">Big div</div>
</div>
This should do it:
#left, #right {
float: left;
font-weight: bold;
font-family: Calibri, sans-serif;
font-size: 20px;
}
#left .small {
display: block;
width: 200px;
height: 55px;
padding: 12px;
box-sizing: border-box;
}
#right {
display: block;
width: 400px;
height: 165px;
padding: 20px;
box-sizing: border-box;
font-size: 60px;
}
<div id="left">
<div class="small" style="color: #22B14C; background-color: #ED1C24">Small div</div>
<div class="small" style="color: #FFAEC9; background-color: #00A2E8">Small div</div>
<div class="small" style="color: #ED1C24; background-color: #FFAEC9">Small div</div>
</div>
<div id="right" style="color: #0099DB; background-color: #22B14C">Big div</div>
If you want that it has a width of 100%, use
#right {
/* ... */
width: calc(100% - 200px);
}

Resources