Center parent div with float children - html

Parent hasnt got a defined width because there are unknown number of children inside.
Why do children fall into new line and how to prevent that? Children need to be on the same line.
.wrap {
position: absolute;
left: 50%;
top: 50px;
height:40px;
transform: translateX(-50%);
}
.box {
width:40px;
height:40px;
float:left;
background:red;
margin-right:1px;
}
<div class="wrap">
<div class="box">0</div>
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
<div class="box">9</div>
<div class="box">10</div>
</div>

You should consider using flex-box.
And to answer your question: floated DIVs are not supposed and will not expand their parent's width, as they are 'floating' above the content flow.
Absolute positioned block elements will have an initial width of zero, if not specified otherwise. Thus, they're behaving differently than usual.
The floating elements indeed behave as usual: as the parent element provides not enough width (zero!) to display them all in one line, of course they break to the next line.
.wrap {
display: flex;
justify-content: center;
flex-wrap: nowrap;
margin-top: 50px;
}
.box {
width:40px;
height:40px;
float:left;
background:red;
margin-right:1px;
}
<div class="wrap">
<div class="box">0</div>
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
<div class="box">9</div>
<div class="box">10</div>
</div>

Since you are using left: 50%, the maximum width of .wrap is the 50% remaining of body.
That's why the wrap is happening.
To avoid it, add more space to it with margin-right: -50%. This will give it an aditional 50% (that is the space lost with the left: 50% style)
.wrap {
position: absolute;
left: 50%;
margin-right: -50%;
top: 50px;
height:40px;
transform: translateX(-50%);
}
.box {
width:40px;
height:40px;
float:left;
background:red;
margin-right:1px;
}
<div class="wrap">
<div class="box">0</div>
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
<div class="box">9</div>
<div class="box">10</div>
</div>

What happens here is that when you use translate the element is visually re-positioned, though from a document flow perspective it is still positioned at 50% left.
So as you can see in this sample, they wrap at the same time though one think that the first shouldn't as there is space left, but there isn't, as technically the first has the same position as the second, hence it wrap at the same time.
.wrap {
position: absolute;
left: 50%;
top: 150px;
height:40px;
}
.wrap.translate {
top: 50px;
transform: translateX(-50%);
}
.box {
width:40px;
height:40px;
float:left;
background:red;
margin-right:1px;
}
<div class="wrap translate">
<div class="box">0</div>
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
<div class="box">9</div>
<div class="box">10</div>
</div>
<div class="wrap">
<div class="box">0</div>
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
<div class="box">9</div>
<div class="box">10</div>
</div>
The simplest and best way to solve this is to make the wrap an inline-block and set text-align: center on its parent, in this case the body
body {
text-align: center;
}
.wrap {
display: inline-block;
height:40px;
}
.box {
width:40px;
height:40px;
float:left;
background:red;
margin-right:1px;
}
<div class="wrap">
<div class="box">0</div>
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
<div class="box">9</div>
<div class="box">10</div>
</div>
Yet another is to make the box's inline-block and set text-align: center on the wrap
Note, to defeat the white space between inline block elements, I also change the markup so each box's end and start tag are on the same line. Here is a few more ways to solve that: how-to-remove-the-space-between-inline-block-elements
.wrap {
height:40px;
text-align: center;
}
.box {
display: inline-block;
width:40px;
height:40px;
background:red;
margin-right:1px;
}
<div class="wrap">
<div class="box">0
</div><div class="box">1
</div><div class="box">2
</div><div class="box">3
</div><div class="box">4
</div><div class="box">5
</div><div class="box">6
</div><div class="box">7
</div><div class="box">8
</div><div class="box">9
</div><div class="box">10
</div>
</div>
If you don't need to support older browsers you can also use Flexbox
.wrap {
display: flex;
justify-content: center;
height:40px;
}
.box {
width:40px;
height:40px;
background:red;
margin-right:1px;
}
<div class="wrap">
<div class="box">0</div>
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
<div class="box">9</div>
<div class="box">10</div>
</div>

Use center class :
.wrap {
text-align: center;
margin: 50px auto 0 auto;
height:40px;
}
.center {
display: inline-block;
}
.box {
width: 40px;
height:40px;
float:left;
background:red;
margin-right:1px;
}
#media all and (max-width: 474px) {
.box {
width: 25px;
}
}
<div class="wrap">
<div class="center">
<div class="box">0</div>
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
<div class="box">9</div>
<div class="box">10</div>
</div>
</div>

Related

Different flex containers side by side

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>

How can I reduce the horizontal space between col-md-4 divs?

HTML
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="content">1x1</div>
</div>
<div class="col-md-4">
<div class="content">1x2</div>
</div>
<div class="col-md-4">
<div class="content">1x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="content">2x1</div>
</div>
<div class="col-md-4">
<div class="content">2x2</div>
</div>
<div class="col-md-4">
<div class="content">2x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="content">3x1</div>
</div>
<div class="col-md-4">
<div class="content">3x2</div>
</div>
<div class="col-md-4">
<div class="content">3x3</div>
</div>
</div>
</div>
CSS
.container {
text-align: center;
display: flex;
flex-direction: column;
}
.content {
background-color: #1A1919;
color: white;
height: 400px;
width: 300px;
}
.row{
padding: 5px;
}
I have managed to make vertical spaces between the columns by adding padding to the rows. But now the horizontal spaces between the contents are ways too much. How can I configure the spacing between them?
That large horizontal space is because of the fixed width of the content class if you remove that, you'll see it grow.
You can set the width of the content in % or add a margin to the content class.
.container {
text-align: center;
}
.content {
background-color: #1A1919;
color: white;
height: 400px;
}
.row{
padding:15px;
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<div class="row">
<div class="col-md-4 col4">
<div class="content">1x1</div>
</div>
<div class="col-md-4 col4">
<div class="content">1x2</div>
</div>
<div class="col-md-4 col4">
<div class="content">1x3</div>
</div>
</div>
<div class="row ">
<div class="col-md-4 col4">
<div class="content">2x1</div>
</div>
<div class="col-md-4 col4">
<div class="content">2x2</div>
</div>
<div class="col-md-4 col4">
<div class="content">2x3</div>
</div>
</div>
<div class="row col4 col4">
<div class="col-md-4 col4">
<div class="content">3x1</div>
</div>
<div class="col-md-4 col4">
<div class="content">3x2</div>
</div>
<div class="col-md-4">
<div class="content">3x3</div>
</div>
</div>
</div>
CSS Grid to achieve that layout you desire:
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
grid-gap: 5px;
position:absolute;
left:50%;
transform:translateX(-50%);
width:80%;
}
.item {
background-color: gray;
text-align: center;
font-size: 30px;
min-height:100px;
max-width: 350px;
}
<div class="grid-container">
<div class="item">1x1</div>
<div class="item">1x2</div>
<div class="item">1x3</div>
<div class="item">2x1</div>
<div class="item">2x2</div>
<div class="item">2x3</div>
<div class="item">3x1</div>
<div class="item">3x2</div>
<div class="item">3x3</div>
</div>
Using flexbox:
.flex-container {
display: flex;
/*Generates a flexbox layout with default flex direction as row */
width: 100%;
/* Not really required */
align-items: center;
/*Aligns contents vertically */
justify-content: center;
/*Aligns contents horizontally */
text-align: center;
/*Aligns further text in the center */
}
.item {
background-color: gray;
text-align: center;
font-size: 30px;
min-height: 400px;
width: 300px;
margin: 5px;
}
<div class="flex-container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
<div class="flex-container">
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
<div class="flex-container">
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
</div>
Use float:left; with class content as below:
.container {
text-align: center;
display: flex;
flex-direction: column;
}
.content {
float: left;
background-color: #1A1919;
color: white;
height: 400px;
width: 300px;
}
.row{
padding: 5px;
}
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="content">1x1</div>
</div>
<div class="col-md-4">
<div class="content">1x2</div>
</div>
<div class="col-md-4">
<div class="content">1x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="content">2x1</div>
</div>
<div class="col-md-4">
<div class="content">2x2</div>
</div>
<div class="col-md-4">
<div class="content">2x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="content">3x1</div>
</div>
<div class="col-md-4">
<div class="content">3x2</div>
</div>
<div class="col-md-4">
<div class="content">3x3</div>
</div>
</div>
</div>
If I'm reading your request correctly, you are just asking how to make it look like the boxes are in the center of the page, and they are evenly spaced. see if this is what you're looking for:
.container {
text-align: center;
display: flex;
flex-direction: column;
}
.content {
background-color: #1A1919;
color: white;
height: 400px;
width: 300px;
}
.col-md-4 {
margin: 5px -10px 0px -4px;;
}
play with the numbers until you get the desired location.
Though I would strongly suggest that you add your own class in addition to col-md-4 to the boxes, which will prevent this new setting to col-md-4 from affecting any future use of this bootstrap class.
in other words . . .
CSS:
.container {
text-align: center;
display: flex;
flex-direction: column;
}
.content {
background-color: #1A1919;
color: white;
height: 400px;
width: 300px;
}
.box-move {
margin: 5px -10px 0px -4px;;
}
and HTML:
<div class="container">
<div class="row">
<div class="col-md-4 box-move">
<div class="content">1x1</div>
</div>
<div class="col-md-4 box-move">
<div class="content">1x2</div>
</div>
<div class="col-md-4 box-move">
<div class="content">1x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4 box-move">
<div class="content">2x1</div>
</div>
<div class="col-md-4 box-move">
<div class="content">2x2</div>
</div>
<div class="col-md-4 box-move">
<div class="content">2x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4 box-move">
<div class="content">3x1</div>
</div>
<div class="col-md-4 box-move">
<div class="content">3x2</div>
</div>
<div class="col-md-4 box-move">
<div class="content">3x3</div>
</div>
</div>
</div>
You can remove a lot of the stuff there and simplify it to make it responsive width a set margin:
.contents {
background-color: black;
color: white;
height: 500px;
margin: 20px 0px;
}
<div class="container-fluid text-center">
<div class="row">
<div class="col-md-4">
<div class="contents">1x1</div>
</div>
<div class="col-md-4">
<div class="contents">1x2</div>
</div>
<div class="col-md-4">
<div class="contents">1x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="contents">2x1</div>
</div>
<div class="col-md-4">
<div class="contents">2x2</div>
</div>
<div class="col-md-4">
<div class="contents">2x3</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="contents">3x1</div>
</div>
<div class="col-md-4">
<div class="contents">3x2</div>
</div>
<div class="col-md-4">
<div class="contents">3x3</div>
</div>
</div>
</div>
</div>

Adjust the width of the container according children width when children are on multiline

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>

Responsive 3*3 square div grid without scroll bars

I want to have a responsive 9x9 div grid with no scrollbar. The div grid should resize according to the visible browser window. I've combined "How to maintain the aspect ratio of a div using only CSS" with "Grid of responsive squares":
html
<div class="stretchy-wrapper">
<div class="inner">
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
98%
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
<div class="square">
<div class="content">
<div class="table">
<div class="table-cell numbers">
3.9/5
</div>
</div>
</div>
</div>
</div>
</div>
css
#import url(http://fonts.googleapis.com/css?family=Lato:400,900); /* <-- Just for the demo, Yes I like pretty fonts... */
.square {
float:left;
position: relative;
width: 30%;
padding-bottom : 30%; /* = width for a 1:1 aspect ratio */
margin:1.66%;
background-color:#1E1E1E;
overflow:hidden;
}
.content {
position:absolute;
height:90%; /* = 100% - 2*5% padding */
width:90%; /* = 100% - 2*5% padding */
padding: 5%;
}
.table{
display:table;
width:100%;
height:100%;
}
body {
font-size:20px;
font-family: 'Lato',verdana, sans-serif;
color: #fff;
text-align:center;
background:#ECECEC;
}
.numbers{
font-weight:900;
font-size:100px;
}
div.stretchy-wrapper {
width: 100%;
padding-bottom: 56.25%; /* 16:9 */
position: relative;
background: lightgrey;
}
div.stretchy-wrapper > .inner {
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
color: white;
font-size: 24px;
text-align: center;
}
However the divs still get expanded to maximum width which causes scroll bars: Fiddle.
What is the css trick that the divs are only expanded to the maximum visible width or height? So I don't get scroll bars?
Solved it with Flexbox and vmin
css
.container {
margin: 0 auto;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
align-content: space-around;
height: 93vmin;
width: 93vmin;
background: lightgrey;
}
.cell {
height: 30vmin;
width: 30vmin;
background-color:#1E1E1E;
}
html
<div class="container">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
Full Screen Fiddle
Fiddle
Recommended read about flexbox

flexbox and container widths

so I have a strange issue,
I have this html:
<div class="container">
<div class="row">
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
</div>
and then I have this CSS
html,
body {
padding: 0;
margin: 0;
}
.container {
height: 100vh;
display: flex;
}
.row {
display: flex;
padding: 50px 20px 20px;
}
.flex-column {
background-color: pink;
width: auto;
margin-right: 100px;
width: 280px;
display: flex;
flex-direction: column;
flex-wrap: wrap;
justify-content: flex-start;
padding: 20px 0 0 20px;
}
.box {
background-color: blue;
width: 120px;
height: 120px;
margin: 0 20px 20px 0;
}
If I use it like that, the content all tries to fit in the rows width, which is not what I want. I would like the row to expand (and present a scrollbar) depending on how much content is in it.
Now, by accident I noticed that if I add the flex-column class to the row, it fixes it (sort of). You can see with the background colours that it isn't doing everything correctly, but the side effect is that it actually works the way I want it to.
My question is how can I get it to wrap the row around the content but still have the scroll bar?
Here is the codepen to show what I currently have.
http://codepen.io/r3plica/pen/waPoQo
if you remove the flex-column class from the row, you will see the issue.
Do you have to use flexbox? You can do it without it. I have posted a flex and a non-flex example. If you want to use flex, you have to make sure you that you have a block container element around your flex element with overflow:scroll. Also your main flex element inside your container needs to be set to float so that it will properly expand horizontally to include your inline-block child elements.
Non-Flex Example:
html,
body {
padding: 0;
margin: 0;
}
.row {
background-color: red !important;
display: block;
padding: 50px 20px 20px;
overflow:scroll;
white-space:nowrap;
}
.flex-column {
background-color: pink;
display: inline-block;
margin-right: 100px;
padding: 20px 0 0 20px;
width: 280px;
vertical-align:top;
}
.box {
background-color: blue;
display: block;
float: left;
height: 120px;
margin: 0 20px 20px 0;
width: 120px;
}
<div class="container">
<div class="row">
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
</div>
Flex Example:
html,
body {
padding: 0;
margin: 0;
}
.container{
display:block;
overflow:auto;
}
.row {
background-color: red !important;
display: flex;
padding: 50px 20px 20px;
overflow:scroll;
white-space:nowrap;
float:left;
}
.flex-column {
background-color: pink;
display: inline-block;
margin-right: 100px;
padding: 20px 0 0 20px;
width: 280px;
vertical-align:top;
}
.box {
background-color: blue;
display: block;
float: left;
height: 120px;
margin: 0 20px 20px 0;
width: 120px;
}
<div class="container">
<div class="row">
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
<div class="box">7</div>
<div class="box">8</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
<div class="flex-column">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
<div class="box">6</div>
</div>
</div>
</div>