Flex Layout not following flex percentages - html

I am trying out flex layout in my angular app.
I am trying to create a layout with base as :
I have tried writing a code.
my .html file
<div flexLayout = "row">
<div fxFlex="100%" class="first_bar">
Second Div
</div>
</div>
<div flexLayout = "row">
<div fxFlex="100%" class="second_bar">
Third Div
</div>
</div>
<div flexLayout = "row">
<div fxFlex="12%" class="third_bar_1">
Fourth Div 1
<!-- <h5>third div</h5> -->
</div>
<div fxFlex="86%" class="third_bar_2">
Fourth Div 2
</div>
</div>
.first_bar{
background-color: #cdf7fb;
height: 6%;
}
.second_bar{
background-color: #cdf7;
height: 12px;
}
.third_bar_1{
background-color: #6390c3;
}
.third_bar_2{
background-color: white;
}
But my output looks like
Flex Layout is not working properly and height of divs is also not being followed.
Can some one help me with this?

<div fxLayout="column" fxLayoutAlign="start space-between" fxLayoutGap="10px">
<div class="first_bar">
Second Div
</div>
<div fxLayout="row" fxLayoutAlign="space-bewteen start" fxLayoutGap="10px">
<div fxFlex="12" class="second_bar">
Side
</div>
<div fxFlex="88" fxLayout="column" fxLayoutAlign="space-bewteen" fxLayoutGap="10px">
<div [ngClass]="['third_bar_1']">
first
</div>
<div [ngClass]="['third_bar_2']">
second
<div>
</div>
</div>
</div>
CSS
.first_bar{
background-color: #cdf7fb;
height: 100px;
}
.second_bar{
background-color: #cdf7;
height: calc(100vh - 200px);
}
.third_bar_1{
background-color: #6390c3;
height: 100px;
}
.third_bar_2{
border:1px solid red;
height: calc(100vh - 315px);
}
Working stackblitz link here

Simple use flex and wrap all with div
.main {
display: flex;
flex-wrap: wrap;
}
.first_line {
width: 100%;
height: 50px;
}
.first_bar {
background-color: #cdf7fb;
height: 6%;
width: 100%;
height: 100%;
}
.all_below {
width: 100%;
display: flex;
flex-wrap: wrap;
}
.second_wrap {
height: 200px;
width: 20%;
}
.second_bar {
background-color: #6390c3;
height: 100%;
}
.third_wrap {
width: 80%;
}
.third_bar_1 {
background-color: #cdf7;
height: 100px;
}
.third_bar_2 {
background-color: lightblue;
height: 100px;
}
<div></div>
<div class="main">
<div flexLayout="row" class="first_line">
<div fxFlex="100%" class="first_bar">
Second Div
</div>
</div>
<div class="all_below">
<div flexLayout="row" class="second_wrap">
<div fxFlex="100%" class="second_bar">
Third Div
</div>
</div>
<div flexLayout="row" class="third_wrap">
<div fxFlex="12%" class="third_bar_1">
Fourth Div 1
<!-- <h5>third div</h5> -->
</div>
<div fxFlex="86%" class="third_bar_2">
Fourth Div 2
</div>
</div>
</div>
</div>

Related

How to put blocks in line in html

I have the following code, where I wanted to align logo(picture) and one block, which includes Username, his level and progress bar. And used flex for it, but it ate my progress bar, how can I do it so error bar's width would be 100%?
<div class="class" id="user">
<img src="user.svg">
<div>
<h2>username</h2>
<h2> Уровень </h2>
<div id="container">
<div id="progress"> 80% </div>
</div>
</div>
</div>
The CSS of the page:
#user {
display:flex;
}
#container {
background-color: white;
width: 100%;
}
#progress{
background-color: #fd6a72;
width: 100%;
}
give your elements some width.
#user {
display: flex;
align-items: center;
}
#container {
background-color: white;
width: 100%;
}
#progress {
background-color: #fd6a72;
width: calc(100vw - 25px - 8px);
height: 20px;
}
img{
height:25px;}
<div class="class" id="user">
<img src="https://via.placeholder.com/25">
<div>
<h2>username</h2>
<h2> Уровень </h2>
<div id="container">
<div id="progress"> 80% </div>
</div>
</div>
</div>

set div width to remaining space [duplicate]

This question already has answers here:
How to make a div fill a remaining horizontal space?
(26 answers)
Closed 5 months ago.
I'm working on a page layout and have divs as columns, A,B,C,D,E. The problem is that column C needs to have its width set to/eat up the remaining horizontal space of the parent, in this case, the body.
width of A and E is percent based.
width of B and D is pixel based.
html,
body {
height: 100%;
margin: 0px;
padding: 0px;
}
.col {
display: inline-block;
position: relative;
height: 100%;
padding: 0 10px;
}
.a {
background-color: #2a85ff;
width: 20%;
}
.b {
background-color: #83bf6e;
width: 100px;
}
.c {
background-color: #ff6a55;
}
.c span {
font-weight: bolder;
}
.d {
background-color: #8e59ff;
width: 200px;
}
.e {
background-color: #b5e4ca;
width: 20%;
}
<div class="col a">
<h1>A</h1>
w:%percent
<br/> h:100%
</div>
<div class="col b">
<h1>B</h1>
w:pixel
<br/> h:100%
</div>
<div class="col c">
<h1>C</h1>
<span>w:remaining</span>
<br/> h:100%
</div>
<div class="col d">
<h1>D</h1>
w:pixel
<br/> h:100%
</div>
<div class="col e">
<h1>E</h1>
w:%percent
<br/> h:100%
</div>
jsfiddle: https://jsfiddle.net/2bu7zv59/
thank you!
Using flex this is very easy to accomplish with the flex-grow property.
In the snippet below I wrapper the columns in a container div and then applied display: flex to the container. You can also simply apply display: flex to the body if you prefer that.
Then I applied flex-grow: 1 to column c.
html,
body {
height: 100%;
margin: 0px;
padding: 0px;
}
.container {
display: flex;
height: 100%;
}
.col {
display: inline-block;
position: relative;
height: 100%;
padding: 0 10px;
}
.a {
background-color: #2a85ff;
width: 20%;
}
.b {
background-color: #83bf6e;
width: 100px;
}
.c {
background-color: #ff6a55;
flex-grow: 1
}
.c span {
font-weight: bolder;
}
.d {
background-color: #8e59ff;
width: 200px;
}
.e {
background-color: #b5e4ca;
width: 20%;
}
<div class="container">
<div class="col a">
<h1>A</h1>
w:%percent
<br/> h:100%
</div>
<div class="col b">
<h1>B</h1>
w:pixel
<br/> h:100%
</div>
<div class="col c">
<h1>C</h1>
<span>w:remaining</span>
<br/> h:100%
</div>
<div class="col d">
<h1>D</h1>
w:pixel
<br/> h:100%
</div>
<div class="col e">
<h1>E</h1>
w:%percent
<br/> h:100%
</div>
</div>
the easiest thing to do is to make the body of type display:flex. You can then use the flex-grow rule to expand the desired column to use up the rest of the width as follows:
html,
body {
height: 100%;
margin: 0px;
padding: 0px;
width: 100%;
display: flex;
}
.col {
/* display: inline-block; */
position: relative;
height: 100%;
padding: 0 10px;
}
.a {
background-color: #2a85ff;
width: 20%;
}
.b {
background-color: #83bf6e;
width: 100px;
}
.c {
background-color: #ff6a55;
flex-grow: 2;
}
.c span {
font-weight: bolder;
}
.d {
background-color: #8e59ff;
width: 200px;
}
.e {
background-color: #b5e4ca;
width: 20%;
}
<div class="col a">
<h1>A</h1>
w:%percent
<br /> h:100%
</div>
<div class="col b">
<h1>B</h1>
w:pixel
<br /> h:100%
</div>
<div class="col c">
<h1>C</h1>
<span>w:remaining</span>
<br /> h:100%
</div>
<div class="col d">
<h1>D</h1>
w:pixel
<br /> h:100%
</div>
<div class="col e">
<h1>E</h1>
w:%percent
<br /> h:100%
</div>

how to center the first item in a flexbox slider/carousel

I want to build a simple scroll slider with flexbox with three items. I want the first item to be centered in the page (under the headline) but the following items only should have a less margin.
HTML:
<div class="page-width">
<h1>Headline</h1>
<div class="container">
<div class="flex-item">
<div class="flex-wrapper">
Center me
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
Hello World
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
Hello World
</div>
</div>
</div>
</div>
CSS:
.page-width {
max-width: 500px;
border: 5px solid green;
}
h1 {
text-align: center;
}
.container {
display: flex;
justify-content: flex-start;
overflow: scroll;
}
.flex-item {
margin-left: 20px;
}
.flex-wrapper {
background: blue;
width: 250px;
height: 250px;
}
How can I achieve to center the first item, while the second and third only remain with a margin of 20px? Also, it should be responsive, for example when the page width is smaller, the first item should still be centered.
I tried to use
.flex-item {
flex: 0 0 100%
}
and center the wrapper inside, so the box would be in the center, but then the second and third item are outside of the screen.
Codepen: https://codepen.io/ascena/pen/wvqZgzg
.page-width {
max-width: 500px;
border: 5px solid green;
}
h1 {
text-align: center;
}
.container {
display: flex;
justify-content: flex-start;
overflow: scroll;
padding-left:20%;
}
.flex-item {
margin-left: 20px;
}
.flex-wrapper {
background: blue;
width: 250px;
height: 250px;
}
<div class="page-width">
<h1>Headline</h1>
<div class="container">
<div class="flex-item">
<div class="flex-wrapper">
Center me
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
Hello World
</div>
</div>
<div class="flex-item">
<div class="flex-wrapper">
Hello World
</div>
</div>
</div>
</div>

Content Div overlaps Menu Div - HTML & CSS

I have content navigation div overlapping menu navigation div. Please let me know what am i missing here. Please find fiddle link below:
https://jsfiddle.net/y4c2xs5j/1/
HTML:
<div class="top-nav">
<div class="menu-nav">
<div class="row">
<div class="col-md-12">
<span>Test</span>
</div>
</div>
</div>
<div class="content-nav">
<div class="row">
<div class="col-md-12">
<div>
<p>
Card content
</p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<div>
<p>
Card content
</p>
</div>
</div>
<div class="col-md-4">
<div>
<p>
Card content
</p>
</div>
</div>
</div>
</div>
</div>
CSS:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: red;
height: 100vh;
}
.top-nav {
width: 100vw;
}
.menu-nav {
width:60px;
background: green;
height: 100vh;
float: left;
}
.content-nav {
width: calc(100vw - 60px);
background: yellow;
height: 100vh;
}
As per my understanding, you want to cover only 60px width with menu-nav, and rest want to cover with content-nav, According to below code:
.menu-nav {
width:60px;
background: green;
height: 100vh;
float: left;
}
.content-nav {
width: calc(100vw - 60px);
background: yellow;
height: 100vh;
}
If I am getting correct then you just neeed to add one more property with content-nav, overflow:hidden;
.menu-nav {
width:60px;
background: green;
height: 100vh;
float: left;
}
.content-nav {
width: calc(100vw - 60px);
background: yellow;
height: 100vh;
overflow:hidden;
}
By adding overflow hidden, you will get complete width rest 60px with content-nav, That is issue cause by float:left, when we are use float property, then the issue is generated, for the same we have to use overflow:hidden
Try this code. Is this what you needed ?
<div class="top-nav">
<div class="menu-nav">
<div class="row">
<div class="col-md-12">
<span>Test</span>
</div>
</div>
</div>
<div class="content-nav">
<div class="row">
<div class="col-md-12">
<div>
<p>
Card content
</p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<div>
<p>
Card content
</p>
</div>
</div>
<div class="col-md-4">
<div>
<p>
Card content
</p>
</div>
</div>
</div>
</div>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: red;
height: 100vh;
}
.top-nav {
width: 100vw;
display: flex;
flex-direction: column;
}
.menu-nav {
width: 100vw;
background: green;
height: 20vh;
float: left;
}
.content-nav {
width: calc(100vw - 100px);
background: yellow;
height: 100vh;
}
You just need to add one property in ".content-nav" and also add clearifx class in the parent of both tag (.menu-nav, .content-nav)
<div class="top-nav clearfix">
.menu-nav {
width:60px;
background: green;
height: 100vh;
float: left;
}
.content-nav {
width: calc(100vw - 60px);
background: yellow;
height: 100vh;
float: left;
}
Whenever you use rows and columns, please check if you have at least one container that contains them. The gap you see on the right is caused by the negative margins from the rows.
The easy fix is to have .container-fluid on or inside menu and content nav.
On menu and content nav
<div class="top-nav">
<div class="menu-nav container-fluid">
...
</div>
<div class="content-nav container-fluid">
...
</div>
</div>
demo: https://jsfiddle.net/davidliang2008/x9d3bvLp/8/
Inside menu and content nav
<div class="top-nav">
<div class="menu-nav">
<div class="container-fluid">
...
</div>
</div>
<div class="content-nav">
<div class="container-fluid">
...
</div>
</div>
</div>
demo: https://jsfiddle.net/davidliang2008/x9d3bvLp/7/
You don't need to calculate the width for content-nav as fluid container will set its width to 100%:
.content-nav {
/*width: calc(100vw - 60px);*/
background: yellow;
height: 100vh;
}

Parent div is flex, children divs are columns with flex:1. How to push the content of the right column to the right side of the column?

I'm trying to push the "HELLO" in the second column to the right side of the column without changing the position of the "HELLO" in the first column. What is the easiest way of accomplishing this?
.row {
display: flex;
}
.column {
flex: 1;
}
.slideshow-photos {
width: 100%;
height: 100%;
object-fit: cover;
}
.mySlides {
width: 100px;
height: 100px;
}
<div class='row'>
<div class='column'>
<p>HELLO</p>
</div>
<div class='column'>
<div class="photos-container">
<div class="mySlides">
<img class='slideshow-photos' src="https://www.animalfriends.co.uk/app/uploads/2014/08/06110347/Kitten-small.jpg">
</div>
</div<
</div>
</div>
If you want your last column to collapse, you can reset the flex-grow value to 0 with the last-of-type pseudo-class.
.row {
display: flex;
}
.column {
flex: 1;
}
.column:last-of-type {
flex: 0;
}
.slideshow-photos {
width: 100%;
height: 100%;
object-fit: cover;
}
.mySlides {
width: 100px;
height: 100px;
}
<div class='row'>
<div class='column'>
<p>HELLO</p>
</div>
<div class='column'>
<div class="photos-container">
<div class="mySlides">
<img class='slideshow-photos' src="https://www.animalfriends.co.uk/app/uploads/2014/08/06110347/Kitten-small.jpg">
</div>
</div<
</div>
</div>
try this code it should work
.row {
display: flex;
justify-content: space-between;
}
.column {
/* not need flex 1 here*/
}
<div class='row'>
<div class='column'>
<p>HELLO</p>
</div>
<div class='column'>
<p>HELLO</p>
</div>
</div>