I've been looking around the internet and through a number of different reference lists, but can't find the "universal" equivalent to -moz-box-orient.
The problem I am currently met with is in reversing my boxes without reversing the entire placement.
This is what I want to achieve, that is, keeping all of the boxes to the left hand side through orientation:
display: -moz-box;
-moz-box-orient: horizontal;
-moz-box-direction: reverse;
#parent {
border: 2px solid blue;
display: -moz-box;
-moz-box-orient: horizontal;
-moz-box-direction: reverse;
}
#parent > div {
border: 2px solid red;
padding: 20px;
margin: 10px;
}
#child1 { background: green; }
#child2 { background: yellow; }
#child3 { background: red; }
#child4 { background: orange; }
<section id="parent">
<div id="child1">Child #1</div>
<div id="child2">Child #2</div>
<div id="child3">Child #3</div>
<div id="child4">Child #4</div>
</section>
This is my attempt without -moz-box-orient:
display: flex;
flex-direction: row-reverse;
#parent {
border: 2px solid blue;
display: flex;
flex-direction: row-reverse;
}
#parent > div {
border: 2px solid red;
padding: 20px;
margin: 10px;
}
#child1 { background: green; }
#child2 { background: yellow; }
#child3 { background: red; }
#child4 { background: orange; }
<section id="parent">
<div id="child1">Child #1</div>
<div id="child2">Child #2</div>
<div id="child3">Child #3</div>
<div id="child4">Child #4</div>
</section>
To surmise my question once more:
Does anyone know the equivalent to -moz-box-orient: horizontal;?
box-orient is a property of the original CSS Flexible Box Layout Module draft, and has been replaced in newer drafts.
A more or less equivalent is flex-direction, which you already use.
If you want to align the elements to the left, you can try justify-content:
justify-content: flex-end;
#parent {
border: 2px solid blue;
display: flex;
flex-direction: row-reverse;
justify-content: flex-end;
}
#parent > div {
border: 2px solid red;
padding: 20px;
margin: 10px;
}
#child1 { background: green; }
#child2 { background: yellow; }
#child3 { background: red; }
#child4 { background: orange; }
<section id="parent">
<div id="child1">Child #1</div>
<div id="child2">Child #2</div>
<div id="child3">Child #3</div>
<div id="child4">Child #4</div>
</section>
However, note that with your old code, the reversed items are aligned to the right side too, but you don't see it because the container shrinks because of display: -moz-box.
If you want the same behavior, you can use display: inline-flex:
#parent {
border: 2px solid blue;
display: inline-flex;
flex-direction: row-reverse;
}
#parent > div {
border: 2px solid red;
padding: 20px;
margin: 10px;
}
#child1 { background: green; }
#child2 { background: yellow; }
#child3 { background: red; }
#child4 { background: orange; }
<section id="parent">
<div id="child1">Child #1</div>
<div id="child2">Child #2</div>
<div id="child3">Child #3</div>
<div id="child4">Child #4</div>
</section>
Related
I understand how to truncate the text which is wrapped inside a flex child however this particular case seems a bit complicated.
The desired behaviour is that the text between the two blue blocks should get truncated to keep them inside the red border.
.container {
display: flex;
align-items: center;
justify-content: space-between;
border: 1px solid black;
padding: 10px;
width: 300px;
}
.block {
display: inline-block;
width: 50px;
height: 50px;
}
.container .left {
min-width: 0;
white-space: nowrap;
padding: 10px;
border: 1px solid red;
}
.left .label {
display: inline-block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.left .block {
background-color: blue;
}
.container .right {
padding: 10px;
border: 1px solid blue;
}
.right .block {
background-color: red;
}
<div class="container">
<div class="left">
<div class="block">
</div>
<div class="label">
Is this the real life?
</div>
<div class="block">
</div>
</div>
<div class="right">
<div class="block">
</div>
</div>
</div>
Is this what you are looking for? Basically i added flexbox property for left and right div
.container {
display: flex;
align-items: center;
justify-content: space-between;
border: 1px solid black;
padding: 10px;
width: 300px;
}
.block {
display: inline-block;
width: 50px;
height: 50px;
}
.left {
display: flex;
justify-content: space-between;
align-items: flex-end;
min-width: 0;
white-space: nowrap;
padding: 10px;
border: 1px solid red;
}
.label {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.block {
background-color: blue;
}
.right {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
border: 1px solid blue;
}
.right .block {
background-color: red;
}
<div class="container">
<div class="left">
<div class="block">
</div>
<div class="label">
Is this the real life?
</div>
<div class="block">
</div>
</div>
<div class="right">
<div class="block">
</div>
</div>
</div>
I made this layout that seems to work, the only problem is that I would like that all che filled div are centered.
There are three possible "states":
the cyan element is to the left of all the others
the gold and pink elements are on two columns
all elements are on a single column.
What I want:
In the first case, everything is centered and it works, in the other two it doesn't: the elements are always aligned to the left.
To center I was thinking of using margin: 0 auto but it seems the divs are bigger than their content even if I used inline-flex (look at the grey area during state #3 in the running example). Why?
How can I solve?
.container {
outline: 1px solid black;
max-width: 490px;
margin: 0 auto;
}
.columns {
outline: 1px solid black;
display: inline-flex;
flex-wrap: wrap;
}
.map {
background-color: cyan;
width: 150px;
min-width: 150px;
height: 150px;
min-height: 150px;
margin-right: 20px;
}
.content {
outline: 1px solid black;
background-color: lightgray;
max-width: 320px;
}
.cards {
outline: 1px solid black;
display: inline-flex;
flex-wrap: wrap;
}
.card {
background-color: pink;
outline: 1px solid black;
width: 150px;
height: 70px;
display: inline-block;
}
.card.left {
margin-right: 20px;
}
.texts {
outline: 1px solid black;
display: inline-flex;
flex-wrap: wrap;
}
.text {
background-color: gold;
outline: 1px solid black;
width: 150px;
height: 200px;
display: inline-block;
}
.text.left {
margin-right: 20px;
}
<div class="container">
<div class="columns">
<div class="map"></div>
<div class="content">
<div class="cards">
<div class="card left">card #1</div>
<div class="card">card #2</div>
<div class="card left">card #3</div>
<div class="card">card #4</div>
</div>
<div class="texts">
<div class="text left">text #1</div>
<div class="text">text #2</div>
</div>
</div>
</div>
</div>
use media queries in the proper way and here you go, to play with it find this fiddle link, try to resize the result window.
.container {
max-width: 490px;
margin: 0 auto;
}
.columns {
display: inline-flex;
flex-wrap: wrap;
}
.map {
background-color: cyan;
width: 150px;
min-width: 150px;
height: 150px;
min-height: 150px;
margin-right: 20px;
}
.content {
background-color: lightgray;
max-width: 320px;
}
.cards {
display: inline-flex;
flex-wrap: wrap;
}
.card {
background-color: pink;
width: 150px;
height: 70px;
display: inline-block;
}
.card.left {
margin-right: 20px;
}
.texts {
display: inline-flex;
flex-wrap: wrap;
}
.text {
background-color: gold;
width: 150px;
height: 200px;
display: inline-block;
}
.text.left {
margin-right: 20px;
}
#media(max-width: 520px){
.container {
display: flex;
flex-direction: column;
align-items: center;
}
.columns {
display: inline-block;
}
}
#media(max-width: 352px){
.container {
display: flex;
flex-direction: column;
align-items: center;
}
.map {margin-right: 0;}
.content {
max-width: min-content;
}
.card.left {
margin-right: 0px;
}
.text.left {
margin-right: 0px;
}
}
<div class="container">
<div class="columns">
<div class="map"></div>
<div class="content">
<div class="cards">
<div class="card left">card #1</div>
<div class="card">card #2</div>
<div class="card left">card #3</div>
<div class="card">card #4</div>
</div>
<div class="texts">
<div class="text left">text #1</div>
<div class="text">text #2</div>
</div>
</div>
</div>
</div>
Hi I am trying to build this layout with flexbox.I provided my current code because i dont know how to move further.Even i posted image how iut should look like under the code.I tried everything but i cant achieve these result. Columns 2,3,5,6,7,8 must be same size. Im new to flex box and i really want to achieve this goal. Thanks for any help.
.tall {
height: 300px;
}
.content {
display: flex;
height: 100%;
}
.left {
display: flex;
flex-direction: column;
flex: 1;
}
.box {
padding-bottom: 50px;
}
.right3collumns {
display: flex;
flex-direction: column;
flex: 2;
}
.box2:nth-child(1) {
background-color: teal;
}
.box2:nth-child(2) {
background-color: red;
}
.box2:nth-child(3) {
background-color: blue;
}
.right {
flex: 2;
background: #22B14C;
}
.right2 {
display: flex;
flex-basis: 200px;
flex-direction: column;
background-color: red;
}
.right2small {
flex-basis: 100px;
background-color: turquoise;
}
.box:nth-child(1) {
background: #ED1C24;
}
.box:nth-child(2) {
background: #00A2E8;
}
.box:nth-child(3) {
background: #FFAEC9;
}
<div class="content">
<div class="right">
<img src="assets/group.png" alt="group">
</div>
<div class="left">
<div class="box">Small DIv</div>
<div class="box">Small DIv</div>
</div>
<div class="right2">bigger</div>
<div class="right2small">smaller</div>
<div class="right3collumns">
<div class="box2">Small DIv</div>
<div class="box2">Small DIv</div>
<div class="box2">Small DIv</div>
</div>
</div>
Here is one way of achieving the layout, I strongly advise, if you can, to use CSS Grid instead.
.grid {
display: flex;
flex: 1;
}
.grid--col {
flex-direction: column;
}
.grid__item {
display: flex;
justify-content: center;
align-items: center;
flex: 1;
border: 1px solid #ddd;
}
.grid__item--x2 {
flex: 2;
}
.grid--main {
background: #f5f5f5;
border: 1px dashed #999;
max-width: 960px;
margin: 50px auto;
}
<div class="grid grid--main">
<div class="grid__item">1</div>
<div class="grid__item grid__item--x2">
<div class="grid grid--col">
<div class="grid">
<div class="grid__item">2</div>
<div class="grid__item grid__item--x2">4</div>
<div class="grid__item">8</div>
</div>
<div class="grid">
<div class="grid__item">3</div>
<div class="grid__item">5</div>
<div class="grid__item">6</div>
<div class="grid__item">7</div>
</div>
</div>
</div>
</div>
You can modify the CSS/SCSS code to change the layout for different breakpoints using the CSS #media rules.
For example, you can have everything stacked, when the viewport is less than or equal to 960px.
#media only screen and (max-width: 960px) {
.grid {
flex-direction: column;
}
}
.grid {
display: flex;
flex: 1;
}
.grid--col {
flex-direction: column;
}
.grid__item {
display: flex;
justify-content: center;
align-items: center;
flex: 1;
border: 1px solid #ddd;
}
.grid__item--x2 {
flex: 2;
}
.grid--main {
background: #f5f5f5;
border: 1px dashed #999;
max-width: 960px;
margin: 50px auto;
}
#media only screen and (max-width: 960px) {
.grid {
flex-direction: column;
}
}
<div class="grid grid--main">
<div class="grid__item">1</div>
<div class="grid__item grid__item--x2">
<div class="grid grid--col">
<div class="grid">
<div class="grid__item">2</div>
<div class="grid__item grid__item--x2">4</div>
<div class="grid__item">8</div>
</div>
<div class="grid">
<div class="grid__item">3</div>
<div class="grid__item">5</div>
<div class="grid__item">6</div>
<div class="grid__item">7</div>
</div>
</div>
</div>
</div>
This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 3 years ago.
Following is my code in which I am trying to align the last div (class="four") to the right and I am using align-self: flex-end; but still its not going to the right. Let me know what I am doing wrong here.
.container {
display: flex;
flex-direction: row;
border: 2px solid blue;
}
.one {
background: red;
}
.two {
background: yellow;
}
.three {
background: pink;
}
.four {
background: teal;
display: inline-block;
align-self: flex-end;
}
<div class="container">
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>
<div class="four">Four</div>
</div>
margin-left:auto; will do the job.
One use of auto margins in the main axis is to separate flex items
into distinct "groups"...
.container {
display: flex;
flex-direction: row;
border: 2px solid blue;
}
.one {
background: red;
}
.two {
background: yellow;
}
.three {
background: pink;
}
.four {
background: teal;
display: inline-block;
margin-left:auto;
}
<div class="container">
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>
<div class="four">Four</div>
</div>
use margin-left: auto
.container {
display: flex;
flex-direction: row;
border: 2px solid blue;
}
.one {
background: red;
}
.two {
background: yellow;
}
.three {
background: pink;
}
.four {
background: teal;
display: inline-block;
margin-left: auto;
}
<div class="container">
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>
<div class="four">Four</div>
</div>
Align self property is used to adjust the flex items on the cross axis.
Please try this code.
.container {
display: flex;
flex-direction: row;
border: 2px solid blue;
}
.one {
background: red;
}
.two {
background: yellow;
}
.three {
background: pink;
}
.four {
background: teal;
margin-left: auto;
}
<div class="container">
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>
<div class="four">Four</div>
</div>
Another way to do.
.container {
display: flex;
flex-direction: row;
border: 2px solid blue;
position: relative;
}
.one {
background: red;
}
.two {
background: yellow;
}
.three {
background: pink;
}
.four {
background: teal;
right: 0;
position: absolute;
}
<div class="container">
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>
<div class="four">Four</div>
</div>
I have a flexbox 'table' where I'm basically trying to put an interesting thing on the top. The problem I've encountered is being unable to write it in HTML
My current result
The result I'm trying to get
I have tried to do it without inner divs and spans, by doing margin:auto but unfortunately it relocates borders from the left and right to the middle :( So the code for the current result is:
.flex-container {
width: auto;
height: 100vh;
display: flex;
flex-direction: column;
}
.flex-container .middle {
flex: 1;
display: flex;
}
.top {
padding-top: 30px;
border: 2px solid #05788D;
display:flex;
}
.leftSide {
padding-top: 30px;
display: flex;
flex-wrap: wrap;
flex-basis: 50%;
overflow: auto;
border: 2px solid #05788D;
}
.rightSide {
padding-top: 30px;
display: flex;
flex-wrap: wrap;
flex-basis: 50%;
overflow: auto;
border: 2px solid #05788D;
border-left-style: none;
}
.firstOption
{
border: 2px solid #05788D;
border-top-style: none;
border-bottom-style:none;
}
.anotherOption
{
border: 2px solid #05788D;
border-top-style: none;
border-bottom-style:none;
border-left-style:none;
}
<div class="flex-container">
<div class="top">
<div style="width:50%;">
<span class="firstOption">One option</span>
</div>
<div style="width:50%;">
<span class="anotherOption">Another option</span>
</div>
</div>
<div class="middle">
<div class="leftSide">
left
</div>
<div class="rightSide">
right
</div>
</div>
</div>
Simply use text-align to control text-alignment of your span and use padding inside your span and don't forget to make them inline-block:
.flex-container {
width: auto;
height: 100vh;
display: flex;
flex-direction: column;
}
.flex-container .middle {
flex: 1;
display: flex;
}
.top {
border: 2px solid #05788D;
display: flex;
}
.top div {
flex:1;
}
.leftSide {
padding-top: 30px;
display: flex;
flex-wrap: wrap;
flex-basis: 50%;
overflow: auto;
border: 2px solid #05788D;
}
.rightSide{
display: flex;
flex-wrap: wrap;
flex-basis: 50%;
overflow: auto;
border: 2px solid #05788D;
border-left-style: none;
}
.firstOption {
text-align:right;
}
.firstOption span,.anotherOption span{
border: 2px solid #05788D;
padding-top: 30px;
display:inline-block;
}
<div class="flex-container">
<div class="top">
<div class="firstOption"><span>One option</span></div>
<div class="anotherOption"><span>Another option</span></div>
</div>
<div class="middle">
<div class="leftSide">
left
</div>
<div class="rightSide">
right
</div>
</div>
</div>
You've added padding-top: 30px to class="top". Instead, the inner child (which are 50%) should have padding-top:30px;
While of course this can be done in a better way, above is the quickest solution to your problem.
You could use justify-content: flex-end on the left option to make it position at the end of the div. I applied the suggested changes to your code in this fiddle.
I can really recommend this guide!