Thank you for reading the post. I'm having a weird issue with Flexbox. I have a div container with some divs that need to be displayed 3 items in a row, 2 items for screen sizes less than 992 and 1 item for less than 568.
To achieve this I used display: flex and flex-wrap: wrap to the container and flex: 1 0 26% to the inner divs. It seems to be working fine but the problem is with the inner contents of the divs. They are all left-aligned.
I then used display: flex, flex-direction: column and align-items: center to the inner container but then all the inner contents get centered but not left-aligned.
Here's the issue:
Here's what I've been trying to achieve:
Here's the snippet:
#topic-list {
display: flex;
flex-wrap: wrap;
}
#topic-list div.list-wrapper {
flex: 1 0 26%;
margin: 10px;
border: 1px solid #ccc;
padding: 10px;
display: flex;
flex-direction: column;
align-items: center;
}
<div id="topic-list">
<div class="list-wrapper">
<div class="el">A</div>
<div>
Apple
</div>
</div>
<div class="list-wrapper">
<div class="el">B</div>
<div>
Ball
</div>
</div>
<div class="list-wrapper">
<div class="el">F</div>
<div>
Fan
</div>
<div>
Fanta
</div>
<div>
Follow
</div>
</div>
<div class="list-wrapper">
<div class="el">I</div>
<div>
Inspire
</div>
</div>
<div class="list-wrapper">
<div class="el">L</div>
<div>
Love
</div>
</div>
<div class="list-wrapper">
<div class="el">M</div>
<div>
Mad
</div>
<div>
Money
</div>
<div>
Mother
</div>
</div>
<div class="list-wrapper">
<div class="el">S</div>
<div>
Sad
</div>
<div>
Son
</div>
<div>
Sick
</div>
</div>
<div class="list-wrapper">
<div class="el">T</div>
<div>
Tea
</div>
<div>
Total
</div>
</div>
<div class="list-wrapper">
<div class="el">W</div>
<div>
Wrap
</div>
</div>
</div>
You can have one more div inside the list-wrapper and make its text align left.
#topic-list {
display: flex;
flex-wrap: wrap;
}
#topic-list div.list-wrapper {
flex: 1 0 26%;
margin: 10px;
border: 1px solid #ccc;
padding: 10px;
display: flex;
flex-direction: column;
align-items: center;
}
.test {
text-align: left;
}
<div id="topic-list">
<div class="list-wrapper">
<div class='test'>
<div class="el">A</div>
<div>
Apple
</div>
</div>
</div>
<div class="list-wrapper">
<div class='test'>
<div class="el">B</div>
<div>
Ball
</div>
</div>
</div>
<div class="list-wrapper">
<div class='test'>
<div class="el">F</div>
<div>
Fan
</div>
<div>
Fanta
</div>
<div>
Follow
</div>
</div>
</div>
<div class="list-wrapper">
<div class='test'>
<div class="el">I</div>
<div>
Inspire
</div>
</div>
</div>
<div class="list-wrapper">
<div class='test'>
<div class="el">L</div>
<div>
Love
</div>
</div>
</div>
<div class="list-wrapper">
<div class='test'>
<div class="el">M</div>
<div>
Mad
</div>
<div>
Money
</div>
<div>
Mother
</div>
</div>
</div>
<div class="list-wrapper">
<div class='test'>
<div class="el">S</div>
<div>
Sad
</div>
<div>
Son
</div>
<div>
Sick
</div>
</div>
</div>
<div class="list-wrapper">
<div class='test'>
<div class="el">T</div>
<div>
Tea
</div>
<div>
Total
</div>
</div>
</div>
<div class="list-wrapper">
<div class='test'>
<div class="el">W</div>
<div>
Wrap
</div>
</div>
</div>
</div>
Related
I am trying to lay down all these square divs horizontally but when I set display:inline-block on the parent div, everything shrinks down and collapses into a pellet. If I use flex, I feel like I need to tweak flex property to my need. Is there a simple way to fix it?(I want them to always occupy two lines evenly with the gap in between in response to the changing viewport width)
.square {
position: relative;
margin: 0;
max-width: 60px;
border: 1px dotted black;
border-radius: 4px;
}
.square:after {
content: "";
display: block;
padding-bottom: 100%;
}
.square .content {
position: absolute;
width: 100%;
height: 100%;
}
<div class="number-of-round">
<div class="square">
<div class="content flex flex-center">1</div>
</div>
<div class="square">
<div class="content flex flex-center">2</div>
</div>
<div class="square">
<div class="content flex flex-center">3</div>
</div>
<div class="square">
<div class="content flex flex-center">4</div>
</div>
<div class="square">
<div class="content flex flex-center">5</div>
</div>
<div class="square">
<div class="content flex flex-center">6</div>
</div>
<div class="square">
<div class="content flex flex-center">7</div>
</div>
<div class="square">
<div class="content flex flex-center">8</div>
</div>
<div class="square">
<div class="content flex flex-center">9</div>
</div>
<div class="square">
<div class="content flex flex-center">10</div>
</div>
<div class="square">
<div class="content flex flex-center">11</div>
</div>
<div class="square">
<div class="content flex flex-center">12</div>
</div>
</div>
You need to set a width not max-width when using inline-block. Unlike block elements, inline-block doesn't take the full parent width so max-width will have no effect
.square {
display: inline-block;
position: relative;
margin: 0;
width: 60px;
border: 1px dotted black;
border-radius: 4px;
}
.square:after {
content: "";
display: block;
padding-bottom: 100%;
}
.square .content {
position: absolute;
width: 100%;
height: 100%;
}
<div class="number-of-round">
<div class="square">
<div class="content flex flex-center">1</div>
</div>
<div class="square">
<div class="content flex flex-center">2</div>
</div>
<div class="square">
<div class="content flex flex-center">3</div>
</div>
<div class="square">
<div class="content flex flex-center">4</div>
</div>
<div class="square">
<div class="content flex flex-center">5</div>
</div>
<div class="square">
<div class="content flex flex-center">6</div>
</div>
<div class="square">
<div class="content flex flex-center">7</div>
</div>
<div class="square">
<div class="content flex flex-center">8</div>
</div>
<div class="square">
<div class="content flex flex-center">9</div>
</div>
<div class="square">
<div class="content flex flex-center">10</div>
</div>
<div class="square">
<div class="content flex flex-center">11</div>
</div>
<div class="square">
<div class="content flex flex-center">12</div>
</div>
</div>
.square {
width:10vw;
max-width:60px;aspect-ratio:1;
margin-bottom:3vw;
border: 1px dotted black;
border-radius: 4px;
}
.number-of-round{
display:flex;
justify-content: space-between;}
<div class="number-of-round">
<div class="square">
<div class="content flex flex-center">1</div>
</div>
<div class="square">
<div class="content flex flex-center">2</div>
</div>
<div class="square">
<div class="content flex flex-center">3</div>
</div>
<div class="square">
<div class="content flex flex-center">4</div>
</div>
<div class="square">
<div class="content flex flex-center">5</div>
</div>
<div class="square">
<div class="content flex flex-center">6</div>
</div>
</div>
<div class="number-of-round">
<div class="square">
<div class="content flex flex-center">7</div>
</div>
<div class="square">
<div class="content flex flex-center">8</div>
</div>
<div class="square">
<div class="content flex flex-center">9</div>
</div>
<div class="square">
<div class="content flex flex-center">10</div>
</div>
<div class="square">
<div class="content flex flex-center">11</div>
</div>
<div class="square">
<div class="content flex flex-center">12</div>
</div>
</div>
I'm trying to make it such that I have two half-width wrappers side by side. Currently the wrappers do take half the space but don't appear side to side. The display:flex seems to be taking the whole width and leaving the unused space on the side as margin.
.wrapper {
display: flex;
flex-direction: column;
width: 40%;
border: 1px solid black;
}
<div class="wrapper">
<div class="title">Test Title</div>
<div class="info">
<div class="column">
<b>1</b>
<span>One</span>
</div>
<div class="column">
<b>2</b>
<span>Two</span>
</div>
</div>
</div>
<div class="wrapper">
<div class="title">Test Title</div>
<div class="info">
<div class="column">
<b>1</b>
<span>One</span>
</div>
<div class="column">
<b>2</b>
<span>Two</span>
</div>
</div>
</div>
I also tried adding another div outside wrapper with width 50% but it didn't help. Any ideas?
Instead of display: flex, use display: inline-flex.
The first is a block-level element which, by default, takes the full width of the parent.
The second is an inline-level element, which can co-exist with other elements on the same line.
.wrapper {
display: inline-flex;
flex-direction: column;
width: 40%;
border: 1px solid black;
}
<div class="wrapper">
<div class="title">Test Title</div>
<div class="info">
<div class="column">
<b>1</b>
<span>One</span>
</div>
<div class="column">
<b>2</b>
<span>Two</span>
</div>
</div>
</div>
<div class="wrapper">
<div class="title">Test Title</div>
<div class="info">
<div class="column">
<b>1</b>
<span>One</span>
</div>
<div class="column">
<b>2</b>
<span>Two</span>
</div>
</div>
</div>
Alternatively, set the parent element to display: flex which, by default, forces the children to exist in the same row.
Add this to your code: body { display: flex }.
.wrapper {
display: flex;
flex-direction: column;
width: 40%;
border: 1px solid black;
}
body {
display: flex;
}
<div class="wrapper">
<div class="title">Test Title</div>
<div class="info">
<div class="column">
<b>1</b>
<span>One</span>
</div>
<div class="column">
<b>2</b>
<span>Two</span>
</div>
</div>
</div>
<div class="wrapper">
<div class="title">Test Title</div>
<div class="info">
<div class="column">
<b>1</b>
<span>One</span>
</div>
<div class="column">
<b>2</b>
<span>Two</span>
</div>
</div>
</div>
I believe display: flex is similar to display: block if it's a top level element. The difference being; the children of the flex container will be able to utilize the flex behavior. What you need to do is something like this:
body {
margin: 0;
}
.parent {
display: flex;
height: 100vh;
background: #eee;
}
.child {
display: flex;
flex-direction: column;
flex: 1;
border: 1px dashed #ccc;
}
<div class='parent'>
<div class='child column'>child 1</div>
<div class='child column'>child 2</div>
</div>
Wrap the wrappers with div and then display: flex.
.main-wrapper {
display: flex;
}
.wrapper {
display: flex;
flex-direction: column;
width: 50%;
border: 1px solid black;
}
<div class="main-wrapper">
<div class="wrapper">
<div class="title">Test Title</div>
<div class="info">
<div class="column">
<b>1</b>
<span>One</span>
</div>
<div class="column">
<b>2</b>
<span>Two</span>
</div>
</div>
</div>
<div class="wrapper">
<div class="title">Test Title</div>
<div class="info">
<div class="column">
<b>1</b>
<span>One</span>
</div>
<div class="column">
<b>2</b>
<span>Two</span>
</div>
</div>
</div>
</div>
is there a way to achieve, that rows in columns side by side are aligned to baseline?
In the example, only "A" is aligned correctly. What I want is to make align baseline also "B" and "C" with the same html structure.
https://jsfiddle.net/hxzuar7f/
I know that something like this will work
<div class="row">
<div class="column">A</div>
<div class="column">A</div>
<div class="column">A</div>
</div>
<div class="row">
<div class="column">B</div>
<div class="column">B</div>
<div class="column">B</div>
</div>
But I want to work this
<div class="container">
<div class="element">
<div class="row">
<h1>A</h1>
</div>
<div class="row">
<div>B</div>
</div>
<div class="row">
<div>C</div>
</div>
</div>
<div class="element">
<div class="row">
<div>A</div>
</div>
<div class="row">
<div>B</div>
</div>
</div>
</div>
(green lines are correct, red lines are wrong)
this is what I want to achieve with CSS:
this is what I got now:
Solution 1 : column → row
That's because your element are in column instead of inline. The align-items: baseline attribut works when elements are horizontaly align. So the best way to acheive what's on your image is with flex-direction: row like this:
h1, h3, div {
display: flex;
align-items: baseline;
flex-direction: row;
box-sizing: border-box;
}
.element {
display: flex;
padding: 20px;
width: 200px;
justify-content: space-between;
}
.container {
width: 100%;
display: flex;
flex-direction: column;
}
.row {
display: flex;
}
<div class="container">
<div class="element">
<div class="row">
<h1>A</h1>
</div>
<div class="row">
<div>B</div>
</div>
<div class="row">
<div>C</div>
</div>
</div>
<div class="element">
<div class="row">
<div>A</div>
</div>
<div class="row">
<div>B</div>
</div>
</div>
<div class="element">
<div class="row">
<div>A</div>
</div>
<div class="row">
<h3>B</h3>
</div>
<div class="row">
<div>C</div>
</div>
</div>
</div>
Solution 2 : add a margin-bottom
If you really want to keep element in column, you need to add a margin-bottom to each row element. But remove some padding of h1and h2. Like this:
h1, h3, div {
display: flex;
align-items: baseline;
flex-direction: row;
box-sizing: border-box;
margin:0;
}
.element {
display: flex;
padding: 20px;
width: 200px;
flex-direction: column;
}
.container {
flex-direction: row;
}
.row {
margin-bottom: 40px;
display: flex;
}
<div class="container">
<div class="element">
<div class="row">
<h1>A</h1>
</div>
<div class="row">
<div>B</div>
</div>
<div class="row">
<div>C</div>
</div>
</div>
<div class="element">
<div class="row">
<div>A</div>
</div>
<div class="row">
<div>B</div>
</div>
</div>
<div class="element">
<div class="row">
<div>A</div>
</div>
<div class="row">
<h3>B</h3>
</div>
<div class="row">
<div>C</div>
</div>
</div>
</div>
I am looking for ways to add in a 3x3 grid of circle icons to my website. Each icon needs to contain a caption text (including sub-caption text) and be spaced evenly apart. The center icons need to be in the center of the webpage.
I unfortunately have been stuck for the last couple of hours, and I have no idea on how to achieve this. I would appreciate any help.
Here is a mostly flexbox grid with square cells and centered content that will evenly space the circle/text.
* {
box-sizing: border-box; margin: 0; padding: 0;
}
.grid {
display: flex;
flex-wrap: wrap;
max-width: 960px;
width: 90%;
margin: auto;
background: #eee;
}
.cell {
flex-basis: 33.3%;
display: flex;
justify-content: center;
align-items: center;
border: 1px solid #fff;
}
.cell:before {
padding-bottom: 100%;
display: block;
content: '';
}
.circle {
background: #ccc;
border-radius: 50%;
width: 100px;
height: 100px;
margin: 0 auto 1em;
}
.inner {
text-align: center;
}
<div class="grid">
<div class="cell">
<div class="inner">
<div class="circle"></div>
<div class="caption">
<h2>text</h2>
<h3>sub</h3>
</div>
</div>
</div>
<div class="cell">
<div class="inner">
<div class="circle"></div>
<div class="caption">
<h2>text</h2>
<h3>sub</h3>
</div>
</div>
</div>
<div class="cell">
<div class="inner">
<div class="circle"></div>
<div class="caption">
<h2>text</h2>
<h3>sub</h3>
</div>
</div>
</div>
<div class="cell">
<div class="inner">
<div class="circle"></div>
<div class="caption">
<h2>text</h2>
<h3>sub</h3>
</div>
</div>
</div>
<div class="cell">
<div class="inner">
<div class="circle"></div>
<div class="caption">
<h2>text</h2>
<h3>sub</h3>
</div>
</div>
</div>
<div class="cell">
<div class="inner">
<div class="circle"></div>
<div class="caption">
<h2>text</h2>
<h3>sub</h3>
</div>
</div>
</div>
<div class="cell">
<div class="inner">
<div class="circle"></div>
<div class="caption">
<h2>text</h2>
<h3>sub</h3>
</div>
</div>
</div>
<div class="cell">
<div class="inner">
<div class="circle"></div>
<div class="caption">
<h2>text</h2>
<h3>sub</h3>
</div>
</div>
</div>
<div class="cell">
<div class="inner">
<div class="circle"></div>
<div class="caption">
<h2>text</h2>
<h3>sub</h3>
</div>
</div>
</div>
</div>
I want some solution to adjust the width of the container according children width when children are on multiple lines. When children are on just single line then display:inline-block for the container element make what I need. But when they are on multiple lines, the container just behave the same like display:block.
Only two solutions what I know are:
1) Set container max-width as some multiplying of children width. But it is not a universal solution (I mean display on devices with lower resolution).
2) Use jQuery to compute the container width. but I'd rather like to use some pure HTML/CSS solution.
HTML:
<div id="wrapper1">
<div id="wrapper2">
<div class="el">
</div>
<div class="el">
</div>
<div class="el">
</div>
<div class="el">
</div>
<div class="el">
</div>
<div class="el">
</div>
<div class="el">
</div>
<div class="el">
</div>
<div class="el">
</div>
</div>
</div>
<div id="wrapper2">
<div class="el">
</div>
<div class="el">
</div>
<div class="el">
</div>
<div class="el">
</div>
<div class="el">
</div>
<div class="el">
</div>
<div class="el">
</div>
<div class="el">
</div>
<div class="el">
</div>
</div>
</div>
CSS:
#wrapper1{
max-width:1200px;
margin:auto;
}
#wrapper2{
background: #FF0;
display:inline-block;
}
.el{
float:left;
margin:10px;
width:200px;
height:200px;
background:#00F;
}
Please test this:
#wrapper1{
display: -webkit-flex;
-webkit-flex-wrap: wrap; /* Safari 6.1+ */
display: flex;
flex-wrap: wrap;
justify-content: center;
-webkit-justify-content: center;
}
#wrapper2{
max-width:880px;
display: -webkit-flex;
-webkit-flex-wrap: wrap; /* Safari 6.1+ */
display: flex;
flex-wrap: wrap;
justify-content: space-between;
-webkit-justify-content: space-betweeen;
}
Here is a sample:
Sample
Click on start page and see the page after i think that is what you want.
#wrapper1{
width:1200px;
}
#wrapper2{
display:inline-block;
}
.el{
width:200px;
height:200px;
background:#00F;
}
.ul{
padding: 20px;
float:left;
width:200px;
height:200px;
background: #FF0;
}
<div id="wrapper1">
<div id="wrapper2">
<div class="ul">
<div class="el"></div>
</div>
<div class="ul">
<div class="el"></div>
</div>
<div class="ul">
<div class="el"></div>
</div>
<div class="ul">
<div class="el"></div>
</div>
<div class="ul">
<div class="el"></div>
</div>
<div class="ul">
<div class="el"></div>
</div>
<div class="ul">
<div class="el"></div>
</div>
<div class="ul">
<div class="el"></div>
</div>
</div>
</div>