Display content in between two div elements via CSS [duplicate] - html

This question already has answers here:
Fill the remaining height or width in a flex container
(2 answers)
Closed 2 years ago.
I'd like to display some "dots" in between a label and a price, like this:
from..........£2,000.49
total........£20,000.00
However, the dots must "adapt/reduce/increase", if the length of the price increases. (Like in the example above), as the prices are dynamic and not static/hardcoded.
I thought I would try this with flex. I have a working example below, where I have two columns, in two rows.
There is no width on the .price-big class, so the width of these divs increases/decreases, with the length of the numbers.
I am then adding the dots to the label class. However, this then pushes my divs onto separate lines/stacked, like in the example below.
.label {
content: ".............................................";
}
Any ideas on how to achieve this, would be helpful as I'm kinda getting stuck on this one.
Thank you,
Reema
.main {
display: flex;
flex-wrap: wrap;
align-items: baseline;
border: 1px solid green;
width: 200px;
}
.label {
font-size: 14px;
/* flex: 0 50%; */
flex-basis: 50%;
flex-shrink: 1;
flex-grow: 1;
border: 1px red solid;
/* width: 100%; */
text-align: left;
font-size: 14px;
}
.label:after {
content: ".............................................";
}
.price-big {
flex-basis: 0;
flex-shrink: 1;
flex-grow: 1;
border: 1px red solid;
text-align: right;
font-size: 20px;
}
<div class="main">
<div class="label">price</div>
<div class="price-big total">£2,000.49</div>
<div class="label">total</div>
<div class="price-big">£20,000.00</div>
</div>

You may combine float and flex to modify the formating context layout of the non floatting element and use a pseudo to fill that empty space inside it:
your CSS code modified :
.main {
/*display: flex;
flex-wrap: wrap;
align-items: baseline;*/
border: 1px solid green;
width: 200px;
overflow:hidden; /* because of the float label */
}
.label {
font-size: 14px;
/* flex: 0 50%;
flex-basis: 50%;
flex-shrink: 1;
flex-grow: 1; */
border: 1px red solid;
/* width: 100%;
text-align: left;*/
font-size: 14px;
margin-top:0.4em;
float:left;
clear:left;
}
.price-big {
border: 1px red solid;
font-size: 20px;
display:flex;
}
.price-big:before {
content:'';
border-bottom:dotted;
margin-bottom:0.2em;
flex-grow:1;
}
<div class="main">
<div class="label">price</div>
<div class="price-big total">£2,000.49</div>
<div class="label">total</div>
<div class="price-big">£20,000.00</div>
</div>

Omg, I literally figured out the answer one minute after posting this. I added overflow: overlay; to the label class:
.label {
font-size: 14px;
flex-basis: 50%;
flex-shrink: 1;
flex-grow: 1;
border: 1px red solid;
text-align: left;
font-size: 14px;
overflow: overlay; <--- added this
}
.main {
display: flex;
flex-wrap: wrap;
align-items: baseline;
border: 1px solid green;
width: 200px;
}
.label {
font-size: 14px;
/* flex: 0 50%; */
flex-basis: 50%;
flex-shrink: 1;
flex-grow: 1;
border: 1px red solid;
/* width: 100%; */
text-align: left;
font-size: 14px;
overflow: overlay;
}
.label:after {
content: ".............................................";
}
.price-big {
flex-basis: 0;
flex-shrink: 1;
flex-grow: 1;
border: 1px red solid;
text-align: right;
font-size: 20px;
}
<div class="main">
<div class="label">price</div>
<div class="price-big total">£2,000.49</div>
<div class="label">total</div>
<div class="price-big">£20,000.00</div>
</div>

Related

Setting width of an element to that of one which overflows its container when both are in a flexbox?

Is there a CSS way of getting the blue line (class .cover) in this snippet to have a width equal that of the .scrl_can, which exceeds the width of its container .prnt?
The width of .scrl_can changes based on user input. A width of 100% sets the width of .cover to the width of .prnt. I tried align-items: stretch; in .prnt and it is the same as width of 100%.
I realize there are other ways of getting the blue line in that position and of the desired width, but the real UI piece has some relatively positioned elements that "drop" below the container when selected; and setting overflow-x: hidden, cuts them off; so I was trying this method which almost works.
Thank you.
.flex_can {
display: flex;
flex-flow: row nowrap;
width: 600px;
}
.space {
flex: none;
width: 150px;
height: 50px;
margin-left: 10px;
background-color: rgb(200,200,200);
}
.prnt {
flex: 1 1;
border: 1px solid black;
padding: 5px;
display: flex;
flex-flow: column nowrap;
overflow-x: scroll;
}
.scrl_can {
width: 500px;
height: 50px;
border: 0.1px solid green;
white-space: nowrap;
}
.cover {
flex: none;
width: 100%;
height: 2px;
background-color: blue;
margin-bottom: 20px;
}
<div class="flex_can">
<div class="prnt">
<div class="scrl_can">Some words here to span the width of prnt to make it a scroll can.</div>
<div class="cover"></div>
</div>
<div class="space"></div>
</div>
I would use the ::after pseudo element.
.flex_can {
display: flex;
flex-flow: row nowrap;
width: 600px;
}
.space {
flex: none;
width: 150px;
height: 50px;
margin-left: 10px;
background-color: rgb(200,200,200);
}
.prnt {
flex: 1 1;
border: 1px solid black;
padding: 5px;
display: flex;
flex-flow: column nowrap;
overflow-x: scroll;
}
.scrl_can {
position: relative;
width: 500px;
height: 50px;
border: 0.1px solid green;
white-space: nowrap;
}
.scrl_can::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 2px;
background-color: blue;
margin-bottom: 20px;
}
<div class="flex_can">
<div class="prnt">
<div class="scrl_can">Some words here to span the width of prnt to make it a scroll can.</div>
<!-- <div class="cover"></div> DON'T NEED THIS ANYMORE -->
</div>
<div class="space"></div>
</div>

Align items 2/3rd through div?

I have the following code in react:
.alignHorizontally {
display: flex;
}
.firstTitle {
align-items: center;
display: flex;
margin-top: 16px;
word-spacing: 2px;
line-height: 1;
}
.secondTitle {
margin-left: 80px;
display: flex;
margin-top: 16px;
word-spacing: 2px;
line-height: 1;
}
/* added by editor for demonstration purpose */
.alignHorizontally > * {
box-sizing: border-box;
}
.firstTitle {
border: 2px dashed red;
}
.secondTitle {
border: 2px dashed blue;
}
<div class="alignHorizontally">
<div class="firstTitle">Title
</div>
<div class="secondTitle">
Second title
</div>
</div>
I want the first div(firstTitle) to be on the far left hand side and the second div (secondTitle) to be about 2/3rds of the way through the screen. I know I can force this by adding padding-left: 100px but it feels ugly. Is there a nice way of doing this?
You can also use justify-content: space-between in your alignHorizontally class or try any of the other justify-content parameters that most closely match the layout you want.
.alignHorizontally {
display: flex;
justify-content: space-between;
}
.firstTitle {
align-items: center;
display: flex;
margin-top: 16px;
word-spacing: 2px;
line-height: 1;
}
.secondTitle {
margin-left: 80px;
display: flex;
margin-top: 16px;
word-spacing: 2px;
line-height: 1;
}
/* added by editor for demonstration purpose */
.alignHorizontally > * {
box-sizing: border-box;
}
.firstTitle {
border: 2px dashed red;
}
.secondTitle {
border: 2px dashed blue;
}
<div class="alignHorizontally">
<div class="firstTitle">Title
</div>
<div class="secondTitle">
Second title
</div>
</div>
It's not in English but it was the best tutorial I've seen so far about aligning items with CSS grid.
Alura's example justify-content CSS grid
https://www.alura.com.br/artigos/css-guia-do-flexbox
Add .secondTitle { width: 33%; } to occupy 1/3 of the space which means it will occupy 1/3 of the space.
with margin-left: auto you can push it then to the right to occupy that 1/3 at the right space.
Alternativly you could give the first div a width of 66% directly.
/* original CSS */
.alignHorizontally {
display: flex;
}
.firstTitle {
align-items: center;
display: flex;
margin-top: 16px;
word-spacing: 2px;
line-height: 1;
}
.secondTitle {
margin-left: 80px;
display: flex;
margin-top: 16px;
word-spacing: 2px;
line-height: 1;
}
/* CSS Chanegs !!! */
.secondTitle {
width: 33%;
margin-left: auto;
}
/* added by editor for demonstration purpose */
.alignHorizontally > * {
box-sizing: border-box;
}
.firstTitle {
border: 2px dashed red;
}
.secondTitle {
border: 2px dashed blue;
}
<div class="alignHorizontally">
<div class="firstTitle">Title
</div>
<div class="secondTitle">
Second title
</div>
</div>
.grid-container {
display: grid;
grid-template-columns: auto auto auto; // setting three columns in our grid layout
}
.grid-item2 {
grid-column-start: 3; // setting second div to start and end in 3d column
grid-column-end: 3;
}
<div class="grid-container">
<div class="grid-item1">Title #1</div>
<div class="grid-item2">Title #2</div>
</div>

Flexbox - Search result popup underneath searchbox

I want to learn how to build a textbox that is an auto-expanding sort of search box like you see on Google:
Desired Result
Before
After
High-Level Design
In this design below, I demonstrate that I want it to visually seem as if the textbox itself is expanding. The way I see it, there is an input box for the text, but the height of the underlying searchbox-container is auto expanding on the y-axis.
Attempt
nav {
font-family: "Inter", sans-serif;
display: flex;
border-bottom: 1px solid #b1aeae;
background-color: #f7f7f7;
height: 60px;
width: 100%;
}
.nav-container {
max-width: 925px;
width: 80%;
height: auto;
margin: 0 auto;
}
.search-container {
width: 50%;
display: flex;
align-items: center;
flex-direction: column;
position: relative;
}
.search-box {
border: 1px solid #b7b5b5;
height: 30px;
width: 100%;
background-color: white;
border-radius: 50px;
margin-top: 20px;
display: flex;
align-items: center;
}
.search-icon {
padding-left: 15px;
}
.search-box input[type="text"] {
border: none;
margin-left: 20px;
font-family: "Inter";
width: 70%;
}
.search-box input[type="text"]:focus {
outline-width: 0;
}
.search-results {
display: flex;
flex-direction: column;
width: 98%;
height: auto;
border-radius: 0px;
border-color: #b7b5b5;
border-style: solid;
background-color: white;
border-width: 0px 1px 1px 1px;
border-radius: 0px 0px 20px 20px;
-webkit-box-shadow: 10px 13px 0px 0px rgba(0, 0, 0, 0.07);
-moz-box-shadow: 10px 13px 0px 0px rgba(0, 0, 0, 0.07);
box-shadow: 10px 13px 0px 0px rgba(0, 0, 0, 0.07);
}
.search-result:last-child {
border-bottom: none;
}
.search-result:hover:last-child {
border-bottom: none;
border-radius: 0px 0px 20px 20px;
}
.search-result {
width: 100%;
border-bottom: 1px solid #b7b5b5;
padding-left: 20px;
padding-top: 10px;
padding-bottom: 10px;
}
.search-result:hover {
background-color: #f7f7f7;
}
.links-container {
width: 55%;
height: 100%;
}
<nav>
<div class="nav-container">
<div class="search-container">
<div class="search-box">
<ion-icon name="search-outline" class="search-icon"></ion-icon>
<input type="text" placeholder="Search articles & videos here" />
</div>
<div class="search-results">
<div class="search-result">Result 1</div>
<div class="search-result">Result 2</div>
<div class="search-result">Result 3</div>
</div>
</div>
<div class="links-container"></div>
</div>
</nav>
Current Outcome:
Clearly this is not looking good. I'm new to Flexbox so having some difficulty structuring this to achieve the previously mentioned goal.
This had a lot of stuff that needed to change, so I pretty much rewrote it. Hopefully, I've given you some ideas to build on.
Some points:
display: flex should only be set on a flex container, not on the cell elements.
Use flex-basis if you need to set the width of a flex cell (don't use width).
Don't use margins. Use flex-gap to space out your cells.
Don't set the height unless you have to. Let the flex display try to set it for you first. In this example, we have to set the height of the link elements to keep them from being the same height as the search elements. (Short of using a grid, there's no way around that that I can see.)
Prefer padding to size your elements. (Very much prefer it over height.)
If you want something with two dimensions, don't use flex at all. Use grid. A vertical flex within a horizontal flex is beginning to push the envelope for what flex is intended for (a one-dimensional arrangement of a set of related elements); you could just as well set this up as a grid with blank areas beneath the menu.
As for dynamically resizing the container div, you don't have to do anything special. If you programmatically add elements to the container, flex will take care of resizing it.
Here's some code:
* {
box-sizing: border-box;
}
nav {
min-width: 800px;
padding: 20px;
background-color: #f7f7f7;
width: 100%;
justify-content: center;
display: flex;
column-gap: 40px;
}
a {
text-decoration: none;
padding: 20px;
}
.search-container {
border: 2px solid darkgrey;
border-radius: 20px;
flex-basis: 40%;
display: flex;
flex-direction: column;
justify-content: center;
column-gap: 20px;
z-index: 1;
}
.search-container a, .search-container p {
border-bottom: 1px solid #b7b5b5;
background-color: white;
text-align: center;
}
.search-container p {
margin: 0;
padding: 30px 20px;
border-top-left-radius: 20px;
border-top-right-radius: 20px;
}
.search-container a:last-child {
border: none;
border-bottom-left-radius: 20px;
border-bottom-right-radius: 20px;
}
.links-container {
display: flex;
padding: 10px 0;
flex-basis: 40%;
z-index: 1;
}
.links-container a {
height: 60px;
padding: 20px 40px;
background-color: blue;
color: white;
}
.links-container a:hover {
background-color: green;
}
.header-overlay {
width: 100%;
min-width: 800px;
height: 120px;
background-color: lightgrey;
position: absolute;
}
<body>
<header>
<div class="header-overlay"></div>
<nav>
<div class="search-container">
<p>Search Results</p>
Result 1
Result 2
Result 3
Result 4
Result 5
</div>
<div class="links-container">
One
Two
Three
</div>
</nav>
</header>
</body>
As you can see, this code (especially the HTML) is simpler than yours. Basically, a flex is a container (with display: flex set) and a single set of nested elements. Any of those elements can be a container for another flex. That's what we have here: the nav is a flex with two elements, and each of those elements (search and links) is a flex container as well. A few observations:
Using box-sizing: border-box everywhere will make your life a lot easier. You probably have had the experience of setting up two divs, setting their width to 50%, and being mystified that they won't fit on one line. It's because by default, padding and borders get added onto the outside of the div at the specified width, so its width becomes more than 50%. What this setting does is put padding and borders inside the width instead of outside it.
Notice how to set border-radius for only some of the corners using border-top-left-radius, etc.
Your design appears to want to have your search results drop below the header. This is a bit difficult to do with any setting for the search results themselves. The easier way to do it is to simply "fake" it overlaying a div at the top. You'll see that I've set div.header-overlay to position: absolute. That positions it at the top of the screen. Then, setting the z-index to 1 for both the search and links elements brings them above the header overlay.
When you run the code here, the links take up more than 40% of the horizontal space; that's because the padding I used make it do that. I set the min-width to 800px so it wouldn't look too squashed, but that causes horizontal scrolling here, which isn't the best for an actual page. So, you'll want to play with flex-grow and flex-shrink, as well as media queries and different layouts for different screens, to make the layout more responsive.
That should give you some missing pieces for building flex displays. You can tinker with the markup and settings and learn more.
Hope this works for you!
nav {
display: flex;
border-bottom: 1px solid #b1aeae;
background-color: #f7f7f7;
height: 60px;
width: 100%;
}
.nav-container {
display: flex;
max-width: 925px;
width: 80%;
height: auto;
margin: 0 auto;
}
.search-container {
width: 50%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
position: relative;
}
.search-box {
border: 1px solid #b7b5b5;
position: absolute;
height: 30px;
width: 100%;
background-color: white;
border-radius: 50px;
padding-left: 20px;
display: flex;
flex-direction: column;
}
.search-results {
width: 90%;
display: flex;
height: 200px;
border-radius: 0px;
border-color: #b7b5b5;
border-style: solid;
background-color: white;
border-width: 0px 1px 1px 1px;
}
.search-result {
width: 100%;
border-bottom: 1px solid blue;
height: 20px;
}
.links-container {
width: 55%;
height: 100%;
}
nav {
display: flex;
border-bottom: 1px solid #b1aeae;
background-color: #f7f7f7;
height: 60px;
width: 100%;
}
.nav-container {
display: flex;
max-width: 925px;
width: 80%;
height: auto;
margin: 0 auto;
}
.search-container {
width: 50%;
display: flex;
justify-content: center;
align-items: top;
flex-direction: row;
position: relative;
}
.search-box {
border: 1px solid #b7b5b5;
position: absolute;
height: 30px;
width: 100%;
background-color: white;
border-radius: 50px;
padding-left: 20px;
display: flex;
flex-direction: column;
}
.search-results {
width: 90%;
display: flex;
flex-direction:column;
align-items: center;
height: 200px;
border-radius: 0px;
border-color: #b7b5b5;
border-style: solid;
background-color: white;
border-width: 0px 1px 1px 1px;
}
.search-result {
position:relative;
text-align:center;
top: 40px;
width: 100%;
border-bottom: 1px solid blue;
height: 20px;
z-index: 9999;
}
.links-container {
width: 55%;
height: 100%;
}
<nav>
<div class="nav-container">
<div class="search-container">
<div class="search-box"></div>
<div class="search-results">
<div class="search-result">Result 1</div>
<div class="search-result">Result 2</div>
<div class="search-result">Result 3</div>
<div class="search-result">Result 4</div>
<div class="search-result">Result 5</div>
</div>
</div>
<div class="links-container"></div>
</div>
</nav>
Add this to align center of div
.search-container {
align-items: top;
flex-direction: row;
}
instead of
.search-container {
align-items: center;
flex-direction: column;
}
And add
.search-results {align-items: center;} to align center
then add to search-result,
.search-result {
position:relative;
text-align:center;
top: 40px;
z-index: 9999;
}
For search result add js
function myFunction() {
var input, filter, ul, li, a, i, txtValue;
input = document.getElementById("search-box");
filter = input.value.toUpperCase();
ul = document.getElementById("search-results");
li = ul.getElementsByTagName("div");
for (i = 0; i < li.length; i++) {
a = li[i];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
Working demo
function searching(input) {
input.classList.add("active");
var input, filter, ul, li, a, i, txtValue;
filter = input.value.toUpperCase();
ul = document.getElementById("search-results");
li = ul.getElementsByTagName("div");
for (i = 0; i < li.length; i++) {
a = li[i];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
function fun(obj){
obj.classList.add("active");
}
nav {
display: flex;
border-bottom: 1px solid #b1aeae;
background-color: #f7f7f7;
height: 60px;
width: 100%;
}
.nav-container {
display: flex;
max-width: 925px;
width: 80%;
height: auto;
margin: 0 auto;
}
.search-container {
width: 50%;
display: flex;
justify-content: center;
align-items: top;
flex-direction: row;
position: relative;
}
.search-box {
border: 1px solid #b7b5b5;
position: absolute;
height: 30px;
width: 100%;
background-color: white;
border-radius: 50px;
padding:0 30px;
display: flex;
top:18px;
flex-direction: column;
}
.search-box.active{
border:none;
border-bottom:1px solid #b7b5b5;
border-radius:0px;
background:transparent;
}
.search-box.active ~ .search-results{
visibility:visible;
}
.search-results {
width: 100%;
display: flex;
flex-direction:column;
align-items: center;
height: 180px;
background:#fff;
border-radius: 30px;
border: 1px solid #b7b5b5;
visibility:hidden;
margin-top:15px;
}
.search-result {
position:relative;
text-align:center;
top: 40px;
width: 100%;
border-bottom: 1px solid #ccc;
height: 20px;
z-index: 9999;
}
.links-container {
width: 55%;
height: 100%;
}
<nav>
<div class="nav-container">
<div class="search-container">
<input class="search-box" id="search-box" type="search" onkeyup="searching(this)" onfocus="fun(this)" placeholder="Please search fruits..">
<div class="search-results" id="search-results">
<div class="search-result">Apple</div>
<div class="search-result">Mango</div>
<div class="search-result">Orange</div>
<div class="search-result">Grape</div>
<div class="search-result">Watermelon</div>
</div>
</div>
<div class="links-container"></div>
</div>
</nav>

Stretch columns in two columns layout with shared header using flexbox

I'm using flexbox to create a two-columns layout with a header row.
* {
box-sizing: border-box;
position: relative;
}
.container {
border: 2px solid gray;
display: flex;
flex-wrap: wrap;
height: 300px;
}
.header {
flex-basis: 100%;
border: 2px solid magenta;
height: 50px;
line-height: 50px;
text-align: center;
}
.column1 {
flex-basis: 150px;
/* height: calc(100% - 50px); */
border: 2px solid green;
}
.column2 {
/* height: calc(100% - 70px); */
flex: 1;
border: 2px solid orange;
}
<div class='container'>
<div class='header'>it's a header</div>
<div class='column1'>column 1</div>
<div class='column2'>column 2</div>
</div>
Feel free to see the full example here.
As you can see in the example there is a gap between columns and header. My aim is to stretch columns vertically to fill whole empty space in the container.
I can achieve it by setting height property like calc(100% - <header-height>). Is it the correct way?
I just tried to use "flex" style and set align-items: stretch to the container and align-self: stretch to columns but without success. Did I probably miss something trying to implement it this way?
I think specifying flex-direction as column is appropriate in this case.
The second row is itself a flex element with the flex-direction: row. You can fill the rest of the remaining space using flex: 1, which is equivalent to flex-grow: 1.
* {
box-sizing: border-box;
}
.container {
border: 2px solid gray;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 300px;
}
.header {
border: 2px solid magenta;
height: 50px;
line-height: 50px;
text-align: center;
}
.subcontainer {
display: flex;
flex-direction: row;
flex: 1;
}
.column1 {
flex-basis: 150px;
border: 2px solid green;
}
.column2 {
flex: 1;
border: 2px solid orange;
}
<div class='container'>
<div class='header'>it's a header</div>
<div class="subcontainer">
<div class='column1'>column 1</div>
<div class='column2'>column 2</div>
</div>
</div>
Do it like shown below
* {
box-sizing: border-box;
}
body,
html {
height: 100%;
}
.container {
border: 2px solid gray;
display: flex;
flex-direction: column;
height: 100%;
}
.header {
width: 100%;
border: 2px solid magenta;
height: 50px;
line-height: 50px;
text-align: center;
}
.body-container {
display: flex;
width: 100%;
flex: 1;
}
.column1 {
width: 50%;
border: 2px solid green;
}
.column2 {
width: 50%;
border: 2px solid orange;
}
<div class='container'>
<div class='header'>it's a header</div>
<div class="body-container">
<div class='column1'>column 1</div>
<div class='column2'>column 2</div>
</div>
</div>

Safari flex-grow behavior different from Chrome/FF/Edge (css flexbox)

I'm getting different behavior between Safari and Chrome/FF/Edge using flex-grow. I'm trying to get a vertical center, but safari is giving more of a fixed to bottom effect.
I'm using flex-grow with a decimal, but Safari seems to interpret it as a whole value.
HTML
<div class="fc">
<div>Align Top</div>
<div>Align Center</div>
<div>Align Bottom</div>
<div class="spacer">Bottom Spacer</div>
</div>
CSS
.fc {
height: 100%;
width: 100%;
background-color: darkBlue;
color: gold;
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
}
.fc div {
outline: 2px dashed gold;
padding: 15px;
width: 200px;
text-align: center;
}
.fc div:first-child {
outline: 1px dashed salmon;
padding: 15px;
flex-grow: .5;
opacity: .5;
}
Here's the pen: https://codepen.io/dmgig/pen/NvMKJW
Problem behavior on Safari 10 (10.12)
Desired behavior on other browsers
If you make the body a flex container, set the fc to flex-grow: 1 (and remove height: 100%) it will render as you want
Updated codepen
Stack snippet
html, body {
width: 100%;
height: 100%;
font-family: sans-serif;
font-size: 20px;
}
body {
display: flex;
flex-direction: column;
}
.fc {
flex-grow: 1;
width: 100%;
background-color: darkBlue;
color: gold;
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
}
.fc div {
outline: 2px dashed gold;
padding: 15px;
width: 200px;
text-align: center;
}
.fc div:first-child {
outline: 1px dashed salmon;
padding: 15px;
flex-grow: .5;
opacity: .5;
}
.fc div.spacer {
outline: 1px dashed salmon;
padding: 0;
height: 60px;
width: 200px;
text-align: center;
opacity: .5;
padding: 15px;
}
.footer {
position: fixed;
bottom: 0;
height: 75px;
width: 100%;
background-color: salmon;
color: darkBlue;
opacity: .5;
font-weight: bold;
}
<div class="fc">
<div>Align Top</div>
<div>Align Center</div>
<div>Align Bottom</div>
<div class="spacer">Bottom Spacer</div>
</div>
<div class="footer">
footer
</div>
You can also remove the position: fixed on the footer and make it all more responsive
Updated codepen 2
I found a bug report here: https://github.com/philipwalton/flexbugs/issues/182
It suggests just using a percentage height on the element and removing flex-grow altogether, which does indeed work well for the purpose.
.fc div:first-child {
outline: 1px dashed salmon;
padding: 15px;
height: 25%;
opacity: .5;
}