I have 5 divs, every second div should have a different colour than the others.
<div class="element element1">Element1</div>
<div class="element element2">Element2</div>
<div class="element element3">Element3</div>
<div class="element element4">Element4</div>
<div class="element element5">Element5</div>
In my CSS I have
.element {
background-color: grey;
}
.element:nth-child(odd) {
background-color: pink;
}
Now dynamically the order of those elements will change, which I want to do with flexbox. Meaning my CSS looks like this then:
.element {
background-color: grey;
display:flex;
}
.element5 {
order: 1;
}
.element2 {
order: 2;
}
As flexbox is not changing the DOM, the nth-child(odd) will still style every second DOM Element, which is not the order the user will see. But that's what I want. Every second element the users sees should have a different colour, even if the element changes the order with flexbox. Has anyone an idea how this could work? I can only use CSS or HTML for this, no JavaScript or PHP.
Assuming the layout is always going to be as you put it above.
First You need to set display flex in a container, not in the elements.
Second, if you set Element 5|2 to order 1|2 respectively they will always be at the end as default order is 0(zero).
so you end with | assume bold is odd new colour
ORIGINAL ORDER(DOM): 1 2 3 4 5
FLEX ORDER: 1 3 4 5 2
.flex-container {
/*SET DISPLAY FLEX ON CONTAINER*/
-ms-box-orient: horizontal;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -moz-flex;
display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
}
/*SET THE NEW ORDER*/
.element:nth-of-type(2){ order: 2;}
.element:nth-of-type(5) { order: 1; }
/*PRESENTATION PROPOUSES ONLY*/
.element,
.normal{
background: grey;
padding: 5px;
width: 100px;
height: 100px;
margin: 5px;
line-height: 100px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}
/* TARGET NEW ODD ELEMENTS*/
.element:nth-of-type(1),
.element:nth-of-type(2),
.element:nth-of-type(4){
background: pink;
}
/* TARGET ORIGINAL ORDER*/
.normal:nth-child(odd){
background: pink;
}
<h1>FLEX ORDER</h1>
<div class="flex-container">
<div class="element element1">1</div>
<div class="element element2">2</div>
<div class="element element3">3</div>
<div class="element element4">4</div>
<div class="element element5">5</div>
</div>
<h1>ORIGINAL ORDER</h1>
<div class="flex-container">
<div class="normal">1</div>
<div class="normal">2</div>
<div class="normal">3</div>
<div class="normal">4</div>
<div class="normal">5</div>
</div>
If you were to use the Flex & Order only in certain device.Let's say you want default order(12345) in mobile and flex + order(13452) from tablet up you can have #media queries leaving the default(ORIGINAL ORDER) to change colours with nth-child(odd) and then for other devices inside media queries add the above code(FLEX ORDER).
Thanks.
My solution:
div {
display: flex;
flex-direction: column;
}
div > div {
order: 1;
background-color: #868d95;
}
div > div:nth-child(odd) {
background-color: #a7acb2;
}
div > div.active {
order: 0;
background-color: #20c36d;
}
div > div.active ~ div {
background-color: #a7acb2;
}
div > div.active ~ div:nth-child(odd) {
background-color: #868d95;
}
<div>
<div>1</div>
<div>2</div>
<div class="active">3</div>
<div>4</div>
<div>5</div>
<div>7</div>
<div>8</div>
</div>
scss:
div {
display: flex;
flex-direction: column;
& > div {
order: 1;
background-color: #868d95;
&:nth-child(odd) {
background-color: #a7acb2;
}
&.active {
order: 0;
background-color: #20c36d;
& ~ div {
background-color: #a7acb2;
&:nth-child(odd) {
background-color: #868d95;
}
}
}
}
}
Related
I'm trying to create this structure using only flexbox.
This is how my html looks like.
<div class="container">
<div class="first_container"></div>
<div class="second_container"></div>
<div class="third_container"></div>
</div>
I know it's really basic problem if i could just change divs order or add additional wrapper for the first line. The problem is it have to stay as it is.
Basic dimensions:
1. first_container - should takes 100% minus third_container width
2. second_container - shoudl takes 100%
I am asking for some tips, because my hands are slowly falling.
You can use flexbox order property
* {
box-sizing: border-box;
}
.container {
display: flex;
flex-wrap: wrap;
}
.container div {
padding: 10px;
border: 5px solid #fff;
background: gray;
text-align: center;
}
.first_container {
width: 70%;
order: 1;
}
.second_container {
width: 100%;
order: 3;
}
.third_container {
width: 30%;
order: 2;
}
<div class="container">
<div class="first_container">1</div>
<div class="second_container">2</div>
<div class="third_container">3</div>
</div>
First I used the flex: property to make two adjacent flexitems grow and shrink when the browser window was resized. That worked.
Then I put a TEXTAEA element into the second dynamic flexitem. That worked fine.
Added a single-line TEXT input element next to the textarea and observed that the bottom line of the text input element aligned with the bottom line of the textarea, but I want the top lines of the two elements to align. How can I do that without scripting using CSS only?
.parent {
display: flex;
background-color: yellow;
}
.verttop {
align-items: flex-start;
}
.dimen {
height: 50px;
width: 50px;
}
.placeholder {
background-color: khaki;
width: 300px;
}
.bs1 {
background-color: green;
width: 20px;
}
.bs2 {
flex: 1 1 auto;
background-color: cyan;
}
.bs3 {
flex: 1 1 auto;
background-color: red;
}
.bs4 {
background-color: gray;
width: 100px;
}
<div class="parent">
<div class="placeholder dimen"></div>
<div class="bs1 dimen"></div>
<div class="bs2 dimen"></div>
<div class="bs3">
<textarea rows="10" cols="18">This is a fairly lengthy and annoyingly meaningless sentence.
</textarea>
<input class="verttop" type="text" value="Hello">
</div>
<div class="bs4 dimen"></div>
</div>
You can do this by making .bs3 flex and aligning its items.
.bs3 {
flex: 1 1 auto;
background-color: red;
display: flex;
align-items: flex-start;
}
I am trying to use flex box for mobile view but it does not seem to work for a specific box(the title flexbox works fine but the labels flex doesnt show). Also, I have divs inside the labels flexbox as child elements
#media screen and (max-width:450px) {
.title {
display: flex;
}
.click {
display: none;
}
.name {
display: flex;
font-size: 50px;
}
.labels {
display: flex;
width: 100%;
background-color: aliceblue;
}
}
<div class="labels">
<div class="port social">Social</div>
<div class="port bio">Bio</div>
<div class="port web">Website</div>
<div class="fancybox fancyboxy-iframe port resume" href="image/Weldons%20resume%20final.pdf" data-fancybox="gallery">Resume</div>
</div>
https://codepen.io/ew39020/pen/MmEmyZ
I found two problems:
In Codepen CSS, row 19: you have diplay instead of display
In your snippet here: Media queries seem not to work here, because result is too wide.
And if you want elements to have equal width, just add flex: 1 to them.
.title {
display: flex;
}
.click {
display: none;
}
.name {
display: flex;
font-size: 50px;
}
.labels {
display: flex;
width: 100%;
background-color: aliceblue;
}
.labels > * {
flex: 1;
}
<div class="labels">
<div class="port social">Social</div>
<div class="port bio">Bio</div>
<div class="port web">Website</div>
<div class="fancybox fancyboxy-iframe port resume" href="image/Weldons%20resume%20final.pdf" data-fancybox="gallery">Resume</div>
</div>
In your CodePen code, you misspelled the display property under labels:
.labels{
diplay:flex; /* missing the `s` */
width:100%;
background:blue;
}
When I corrected this, the flexbox works. I also added a media query condition to test this under a smaller screen, and it worked.
This question already has answers here:
Is it possible for flex items to align tightly to the items above them?
(5 answers)
Make a div span two rows in a grid
(2 answers)
Closed 5 years ago.
I am trying to have one div on the left and two on the right. The bottomright should always be below the topRight div. The topRight is the only div with a variable height.
I am currently trying to achieve this using flexbox als you can see in my code below.
I would like to have some directions.
.wrapper {
display: flex;
height: 100px;
}
.left {
background-color: green
}
.topRight {
background-color: yellow
}
.bottomright {
background-color: red
}
<div class="wrapper">
<div class="left">Left</div>
<div class="topRight">TopRight</div>
<div class="bottomright">Bottom</div>
</div
With a fixed height on the container, as you have in your code, you can use flex-direction: column and flex-wrap: wrap. The fixed height serves as a break point, telling flex items where to wrap.
.wrapper {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 100px;
}
.left {
flex: 0 0 100%; /* consumes full height of first column; forces siblings to wrap */
background-color: lightgreen
}
/* variable height div */
.topRight {
background-color: yellow
}
.bottomright {
flex: 1; /* consumes remaining space in column */
background-color: red
}
<div class="wrapper">
<div class="left">Left</div>
<div class="topRight">TopRight<br>variable height</div>
<div class="bottomright">Bottom</div>
</div>
On html put a div with a class called right wrapping both topRight and bottomRight and use this css on css:
.wrapper {
display: flex;
height: 100px;
}
.right {
display: flex-flow;
}
.left {
background-color: green
}
.topRight {
background-color: yellow;
height: 50px;
}
.bottomright {
background-color: red;
height: 50px;
}
I hope that helps you :)
For infos
display:grid is made for this .... very soon available for most browsers and yet for a few
A tutorial among others : https://css-tricks.com/snippets/css/complete-guide-grid/
.wrapper {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
/* any height s */
background-color: green;
}
.leftspan {
grid-row: span 2;/* if 2 rows avalaible */
}
.topRight {
background-color: yellow;
grid-column: 2 /-1
}
.bottomright {
background-color: red;
grid-column: 2 /-1
}
.bottomfull {
background-color: red;
grid-column: 1 /-1
}
<div class="wrapper">
<div class="leftspan">Left spanning 2 rows</div>
<div class="topRight">Top <br/>Right</div>
<div class="bottomright">Bottom <br/>Right</div>
</div>
<p> or did you mean ?
<div class="wrapper">
<div class="left">Left</div>
<div class="topRight">Top Right</div>
<div class="bottomfull">Bottom <br/>Right</div>
</div>
render if your browsers understand grid:
I have a parent div (.tags) that contains links and a title.
The parent div is set to display: flex; with flex-wrap: wrap;.
I would like the link elements to break onto a new line, clearing the title when the wrap effect takes place.
I have tried using flex-grow: 1 on the title, but this makes it push the links to the right of the screen at all times, which is not what I am after.
I have attached the code I have so far, but here is a link to the Codepen
What I am trying to achieve:
Default - the width of the screen is large enough so nothing wraps and everything is on one line >
Wrapped - the width of the screen is smaller, the wrap has occurred - the title now has 100% width and the links clear it >
Note that the number of links could vary.
.container {
background: lightgray;
width: 100%;
}
.tags {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: flex-start;
}
.tags span {
margin: 1rem;
}
.tags .tag {
margin: 0.2rem;
padding: 0.2rem;
background: dodgerblue;
color: white;
}
<div class="container">
<div class="tags">
<span>Tagged in:</span>
<a class="tag" href="#">capabilities</a>
<a class="tag" href="#">sales</a>
</div>
</div>
Yes, with a change in the structure.
Wrap the tags in their own div with flex:1. Then it will expand automatically and the tags will drop to the second line when the wrap occurs.
.container {
background: lightgray;
width: 100%;
}
.tags {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: flex-start;
}
.tags span {
margin: 0 1rem;
}
.tags .tag-wrap {
display: flex;
flex: 1;
}
.tags .tag {
margin: 0.2rem;
padding: 0.2rem;
background: dodgerblue;
color: white;
}
<div class="container">
<div class="tags">
<span>Tagged in:</span>
<div class="tag-wrap">
<a class="tag" href="#">capabilities</a>
<a class="tag" href="#">sales</a>
</div>
</div>
</div>
Codepen Demo
flex grow of 1, flex shrink of 0, flex basis auto:
span {
flex:1 0 auto;
}