'How to use flex to allocate elements in a row to maintain two elements [duplicate]
I want my elements to be in the middle of the screen and arranged vertically when on the mobile phone, but when switching to the tablet, the screen I hope to present is two elements in a row, presented from left to right!
Attached is the schematic diagram, but I'm stuck and don't know how to implement such a layout.
ul {
display: flex;
flex-direction: column;
align-items: center;
}
@media (min-width: 768px) {
ul {
flex-direction: row;
}
}
<ul>
<li>title</li>
<li>title</li>
<li>title</li>
<li>title</li>
<li>title</li>
<li>title</li>
<li>title</li>
</ul>
Solution 1:[1]
You can try something like this. Essentially just make two different ul's then nest it in a wrapper. Then you can tell the browser to switch to flex-column for phones. Resize the browser to see the effect.
ul {
display: flex;
flex-direction: column;
align-items: center;
margin: 0;
}
.wrapper {
display: flex;
justify-content: center;
}
/* change to column for phones */
@media only screen and (max-width: 600px) {
.wrapper {
flex-direction: column;
}
}
<div class="wrapper">
<ul>
<li>title</li>
<li>title</li>
<li>title</li>
<li>title</li>
<li>title</li>
<li>title</li>
<li>title</li>
</ul>
<ul>
<li>title</li>
<li>title</li>
<li>title</li>
<li>title</li>
</ul>
</div>
Solution 2:[2]
Is this what you are looking for?
ul {
display: flex;
flex-direction: column;
align-items: center;
}
@media (min-width: 768px) {
ul {
flex-direction: row;
flex-wrap:wrap
}
li {
min-width:50%
}
<ul>
<li>title</li>
<li>title</li>
<li>title</li>
<li>title</li>
<li>title</li>
<li>title</li>
<li>title</li>
</ul>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kameron |
| Solution 2 | Erfan |

