How can I control the middle space between 2 flex items in a row? My goal is to minimize the middle space between the items.
In other words, I want to right and center align the columns, while keeping the text centered.
I would also like to be able to control the middle gutter. Also, maybe be able to say that the middle gutter should be some fixed pixel quantity like 20px.
This doesn't work because there is too much space in the middle
.row {
display: flex;
}
.column {
border: 1px solid #ccc;
width: 50%;
}
h2 {
text-align: center;
}
<div class="row">
<div class="column first">
<h2>Centered</h2>
<h2>Text</h2>
</div>
<div class="column second">
<h2>Centered</h2>
<h2>Text</h2>
</div>
</div>
This don't work because the text in not centered
.row {
display: flex;
}
.column {
border: 1px solid #ccc;
width: 50%;
}
h2 {
text-align: center;
}
.first h2 {
text-align: right;
}
.second h2 {
text-align: left;
}
<div class="row">
<div class="column first">
<h2>Centered</h2>
<h2>Text</h2>
</div>
<div class="column second">
<h2>Centered</h2>
<h2>Text</h2>
</div>
</div>
If you want your elements to have a particular proportion in relation to each other or to the whole (i.e: 50%), flexbox is not even the best solution (although it can be crow-bared to fulfill the requirement).
A simple display: inline-block; width: 50%; box-sizing: border-box on the children will do.
Flexbox was designed to allow fluid distribution of positive or negative space.
As in, after the browser determines how much space the children need and how much they have available, by comparing the two it deals with two cases:
positive space: when parent > sum of children, redistribute the space to each child, proportionally with their respective flex-grow values (until the space is filled) or, if no children have positive flex-grow values, distribute the space in between the children
negative space: if parent < sum of children, shrink each child proportionally with their flex-shrink values.
You have a case of positive space, where the children do not have flex-grow, so the space can be redistributed in between them. You have three options:
justify-content: space-between
justify-content: space-around
justify-content: space-evenly
Notice how the spaces are equally distributed in each case. That's why it's called flexbox, that's what it was designed for and, if you want, that's its superpower.
When you set width: 50% you kind of take all of that away:
.row {
display: flex;
width: 100%;
}
.row>* {
border: 1px solid #ccc;
text-align: center;
}
.one {
justify-content: space-between;
}
.two {
justify-content: space-around;
}
.three {
justify-content: space-evenly;
}
.grow>* {
flex-grow: 1;
margin: 1rem;
}
<code>justify-content: space-between</code>
<div class="row one">
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
</div>
<code>justify-content: space-between (unequal)</code>
<div class="row one">
<div>
<h2>A bigger element here</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
</div>
<hr>
<code>justify-content: space-around;</code>
<div class="row two">
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
</div>
<code>justify-content: space-around; (unequal)</code>
<div class="row two">
<div>
<h2>A bigger element here</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
</div>
<hr>
<code>justify-content: space-evenly;</code>
<div class="row three">
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
</div>
<code>justify-content: space-evenly; (unequal)</code>
<div class="row three">
<div>
<h2>A bigger element here</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
</div>
<hr>
<code>> flex-grow: 1;</code>
<div class="row grow">
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
</div>
<hr>
<code>> flex-grow: 1; (unequal elements)</code>
<div class="row grow">
<div>
<h2>A bigger element here</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
</div>
A more detailed explanation on how flexbox works can be found here.
The official spec is here.
Considering your question, there's a big chance justify-content: space-evenly does what you want, but I can't be sure, as your question is not extremely clear.
Important note: You can always use margin and padding on your elements, which will push them around accordingly. Also note margin: auto on flexbox children will steal the positive space from flexbox and will redistribute it equally between all the margin:autos present in parent space (and will make it look like flexbox doesn't work properly; it actually does, but there's nothing left to redistribute).
If you want to have a particular distance in between your elements and the entire composition should be centered in the available space, you could center a single item in the available space and inside that item you could place the items, so that the entire thing is centered regardless of the fact the items are unequal.
Example:
.row {
display: flex;
justify-content: center;
}
.row > * {
margin: 0 auto;
display: flex;
}
.row>*>* {
border: 1px solid #ccc;
text-align: center;
margin: 1rem;
}
<div class="row">
<div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
</div>
</div>
<hr>
<div class="row">
<div>
<div>
<h2>Much more text here</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
</div>
</div>
<hr>
<div class="row">
<div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Much more text here</h2>
<p>Text</p>
</div>
</div>
</div>
But, again, that's not something you need flexbox for.
The exact same can be achieved using a simple parent > child structure, where parent has display: block; text-align: center; and children have display: inline-block;
.row {
text-align: center;
}
.row>* {
border: 1px solid #ccc;
text-align: center;
margin: 1rem;
display: inline-block;
}
<div class="row">
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
</div>
<hr>
<div class="row">
<div>
<h2>Much more text here</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
</div>
<hr>
<div class="row">
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Centered</h2>
<p>Text</p>
</div>
<div>
<h2>Much more text here</h2>
<p>Text</p>
</div>
</div>
Notice the absence of the extra wrapper in markup.
Why not simply use a CSS grid for the task? It offers the grid-column-gap property which does exactly what you need:
.row {
display: grid;
grid-template-columns: 1fr 1fr;
grid-column-gap: 10px;
}
.column {
border: 1px solid #ccc;
}
h2 {
text-align: center;
}
<div class="row">
<div class="column">
<h2>Centered<br />Text</h2>
</div>
<div class="column">
<h2>Centered<br />Text</h2>
</div>
</div>
Try doing this
.column:not(:last-child){
margin-left: 20px;
}
Related
I have an Angular application that has a particular page with a number of rows of data that are generated by *ngFor, these rows look like a table in the sense that each row has similar elements that display a name, description, and have some action buttons. However the data is not stored in a table. The data can also not be stored in a table due to each row being an expansion panel that houses a lot of information within itself and reworking these into a table would probably take too much time.
I need to create a header for this "table", but obviously I can't just do it in the normal way by using table headers. Instead I need to create a separate element that sits above the "table" and has a number of child elements that are positioned in such a way that it gives the impression that it's a header for the series of rows below it.
I've tried doing this in several ways; flexbox, grid, etc but every time I have the same problem with my header. The problem is that when I have the headers aligned properly over the columns if the width of the browser is changed the headers then become unaligned.
I have created a stackblitz to showcase this, here: https://stackblitz.com/edit/angular-ivy-bzazjx?file=src/app/app.component.css
In this example "header 1" should be above "Info" and "header 2" the right-most button. If you adjust the window and make it very big or very small the headers will move around and become misaligned. For example as a very small width "heading 1" will move to be above the left most button. In a similar sense if the browser width is increased the heading will move to the right instead of staying fixed
Is there a way to fix these headers above the elements I want them above such that if the window is changed they will stay fixed above the correct elements and will not displace themselves? I'm all out of ideas as nothing seems to work.
HTML
<div class="test-holder">
<div class="card test-container1">
<h4 class="heading1">Heading 1</h4>
<h4 class="heading2">Heading 2</h4>
</div>
<div class="card d-flex test-container2">
<div class="div1">
<button>Button</button>
</div>
<div class="div1">
<h2>Info</h2>
</div>
<div class="div1">
<h4>More Info</h4>
</div>
<div class="div1">
<p>This is some content</p>
</div>
<div class="div1">
<button>Another button</button>
</div>
</div>
<div class="card d-flex test-container2">
<div class="div1">
<button>Button</button>
</div>
<div class="div1">
<h2>Info</h2>
</div>
<div class="div1">
<h4>More Info</h4>
</div>
<div class="div1">
<p>This is some content</p>
</div>
<div class="div1">
<button>Another button</button>
</div>
</div>
<div class="card d-flex test-container2">
<div class="div1">
<button>Button</button>
</div>
<div class="div1">
<h2>Info</h2>
</div>
<div class="div1">
<h4>More Info</h4>
</div>
<div class="div1">
<p>This is some content</p>
</div>
<div class="div1">
<button>Another button</button>
</div>
</div>
<div class="card d-flex test-container2">
<div class="div1">
<button>Button</button>
</div>
<div class="div1">
<h2>Info</h2>
</div>
<div class="div1">
<h4>More Info</h4>
</div>
<div class="div1">
<p>This is some content</p>
</div>
<div class="div1">
<button>Another button</button>
</div>
</div>
<div class="card d-flex test-container2">
<div class="div1">
<button>Button</button>
</div>
<div class="div1">
<h2>Info</h2>
</div>
<div class="div1">
<h4>More Info</h4>
</div>
<div class="div1">
<p>This is some content</p>
</div>
<div class="div1">
<button>Another button</button>
</div>
</div>
</div>
CSS
.test-holder {
width: 100%;
}
.test-container1 {
width: 100%;
display: flex;
flex-direction: row;
}
.test-container2 {
width: 100%;
flex-direction: row;
justify-content: space-between;
}
.heading1 {
width: 28%;
text-align: right;
}
.heading2 {
width: 68%;
text-align: right;
}
.card {
background-color: #f2f2f2;
}
.d-flex {
display: flex;
}
.div1 {
display: inline-block;
display: flex;
align-items: center;
}
I'm currently debugging a website to make it work on IE 11. Now there is one page, were there are two div tables inside a flexbox container. Under the container, there is another flexbox 'navLine', which should be positioned directly under the container. This works fine in every browser except IE 11. There the container and the navLine seem to overlap. Oddly enough, the navLine sticks to a button within the container. The button is within a cell of the div table, without any additional CSS properties.
Any help?
Here's the simplified code:
#NavLine
{
margin-bottom: 8px;
display: -webkit-flex;
display: -ms-flex;
display: flex;
justify-content: space-between;
align-items: center;
}
.SelectionBox
{
background: #f7f7f7;
border: 1px solid #d9d9d9;
padding: 16px;
margin-bottom: 16px;
min-width: 570px;
min-height: 410px;
}
.Container
{
display: -webkit-flex;
display: -ms-flex;
display: flex;
-webkit-justify-content: space-around;
-ms-justify-content: space-around;
justify-content: space-around;
width: 100%;
flex: 1 1 auto;
}
#Box1, #Box2
{
flex: 1 1 auto;
padding: 16px;
}
.Table
{
display: table;
table-layout: fixed;
overflow: hidden;
height: 100%;
width: 75%;
}
.Title
{
display: table-header-group;
width: 100%;
height: 80px;
}
.Row
{
display: table-row;
}
.Cell
{
display: table-cell;
padding-left: 5px;
padding-right: 5px;
vertical-align: middle;
height: 70px;
}
<div class="Container">
<!-- First Box -->
<div id="Box1">
<div class="Table SelectionBox">
<div class="Title">
<h2>Box One</h2>
</div>
<div class="Row">
<div class="Cell">
<span>Some Item</span>
</div>
<div class="Cell">
<span>Some Value</span>
</div>
</div>
<div class="Row">
<div class="Cell">
<span>Some Item</span>
</div>
<div class="Cell">
<span>Some Value</span>
</div>
</div>
<div class="Row">
<div class="Cell">
<span>Some Item</span>
</div>
<div class="Cell">
<span>Some Value</span>
</div>
</div>
<div class="Row">
<div class="Cell">
<span>Some Item</span>
</div>
<div class="Cell">
<span>Some Value</span>
</div>
</div>
<div class="Row">
<div class="Cell"></div>
<div class="Cell">
<span> ([[result]]) Results</span>
<span>Reset (this is a button)</span>
</div>
</div>
</div>
</div>
<!-- Second Box -->
<div id="Box2">
<div class="Table SelectionBox">
<div class="Title">
<h2>Box Two</h2>
</div>
<div class="Row">
<div class="Cell">
<span>Some Item</span>
</div>
<div class="Cell">
<span>Some Value</span>
</div>
</div>
<div class="Row">
<div class="Cell">
<span>Some Item</span>
</div>
<div class="Cell">
<span>Some Value</span>
</div>
</div>
<div class="Row">
<div class="Cell">
<span>Some Item</span>
</div>
<div class="Cell">
<span>Some Value</span>
</div>
</div>
<div class="Row">
<div class="Cell">
<span>Some Item</span>
</div>
<div class="Cell">
<span>Some Value</span>
</div>
</div>
<div class="Row">
<div class="Cell"></div>
<div class="Cell">
<span> ([[result]]) Results</span>
<span>Reset (this is a button)</span>
</div>
</div>
</div>
</div>
</div>
<div id="NavLine">
<span>Back</span>
<span>Next</span>
</div>
The problem in IE is that height: 100% in .Table.SelectionBox is causing that element to overflow its container (#Box1). It then overlaps the #NavLine element, which is not causing any problem.
One way to fix the problem is to remove that height rule.
"IE 11 requires a unit to be added to the third argument, the flex-basis property" - see "known issues" tab https://caniuse.com/#feat=flexbox
also, a min-height might be required
so, try for example:
#container > div {
flex:1 1 8em;
min-height:8em;
}
I'm new to using flex boxes in CSS. But this seems very nice for alignments and free space distribution between components!
Today I have a problem I don't manage to solve. Thanks in advance for your help.
Here is a codepen to illustrate the problem quickly :
https://codepen.io/anon/pen/BYdzqR
#example1 .wrapper, #example1bis .wrapper{
justify-content: space-between;
}
#example2 .wrapper, #example2bis .wrapper{
justify-content: space-evenly;
}
#example3 .wrapper, #example3bis .wrapper{
justify-content: space-around;
}
#example4 .wrapper, #example4bis .wrapper{
justify-content: center;
}
#example4 .content .group, #example4bis .content .group {
margin: auto;
}
#example1, #example2, #example3, #example4{
height: 600px;
}
#example1bis, #example2bis, #example3bis, #example4bis{
height: 300px;
}
.root{
/* background: lightblue; */
margin-bottom: 20px;
display: flex;
}
.box {
width: 300px;
height: 300px;
border: 1px solid gray;
text-align: center;
margin: 0 20px;
flex: 0 0 auto;
}
.wrapper {
display: flex;
flex-flow: column nowrap;
height: 100%;
}
/* ----------------------------- */
/* Top */
/* ----------------------------- */
.top {
padding: 20px 0;
overflow: hidden;
flex: 0 0 auto;
border-bottom: 1px solid lightgray;
}
/* ----------------------------- */
/* Content */
/* ----------------------------- */
.content {
padding: 10px;
overflow-y: auto;
flex: 1 1 auto;
}
.content .group {
flex: 0 0 auto;
background: yellow;
width: 100%;
}
.content h2 {
margin: 0;
padding: 0;
color: red
}
/* ----------------------------- */
/* Bottom */
/* ----------------------------- */
.bottom {
text-align: center;
width: 100%;
padding: 20px 0;
overflow: hidden;
flex: 0 0 auto;
border-top: 1px solid lightgray;
}
<h1>Reference : without overflow</h1>
<p>Different kind of free space allocation. What I would like is something like #2 ("justify-content: space-evenly"). Eventually #3 ("space-around") or #4 ("center", with "margin: auto" on items)</p>
<div class="root">
<div id="example1" class="box">
<div class="wrapper">
<div class="top">
#1 : space-between
</div>
<div class="content wrapper">
<div class="group">
<h2>Section 1</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 2</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 3</h2>
<p>blah</p>
</div>
</div>
<div class="bottom">
footer
</div>
</div>
</div>
<div id="example2" class="box">
<div class="wrapper">
<div class="top">
#2 : space-evenly
</div>
<div class="content wrapper">
<div class="group">
<h2>Section 1</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 2</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 3</h2>
<p>blah</p>
</div>
</div>
<div class="bottom">
footer
</div>
</div>
</div>
<div id="example3" class="box">
<div class="wrapper">
<div class="top">
#3 : space-around
</div>
<div class="content wrapper">
<div class="group">
<h2>Section 1</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 2</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 3</h2>
<p>blah</p>
</div>
</div>
<div class="bottom">
footer
</div>
</div>
</div>
<div id="example4" class="box">
<div class="wrapper">
<div class="top">
#4 : center
</div>
<div class="content wrapper">
<div class="group">
<h2>Section 1</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 2</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 3</h2>
<p>blah</p>
</div>
</div>
<div class="bottom">
footer
</div>
</div>
</div>
</div>
<h1>Problem : with overflow</h1>
<p>The problem is when there is not enough free space to display all the content. "overflow-y: auto" should allow to scroll to see all the content, but this is not the case with #2, #3 and #4... ("Section 1" title hidden)</p>
<div class="root">
<div id="example1bis" class="box">
<div class="wrapper">
<div class="top">
#1bis : space-between = OK
</div>
<div class="content wrapper">
<div class="group">
<h2>Section 1</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 2</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 3</h2>
<p>blah</p>
</div>
</div>
<div class="bottom">
footer
</div>
</div>
</div>
<div id="example2bis" class="box">
<div class="wrapper">
<div class="top">
#2bis : space-evenly = KO
</div>
<div class="content wrapper">
<div class="group">
<h2>Section 1</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 2</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 3</h2>
<p>blah</p>
</div>
</div>
<div class="bottom">
footer
</div>
</div>
</div>
<div id="example3bis" class="box">
<div class="wrapper">
<div class="top">
#3bis : space-around
</div>
<div class="content wrapper">
<div class="group">
<h2>Section 1</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 2</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 3</h2>
<p>blah</p>
</div>
</div>
<div class="bottom">
footer
</div>
</div>
</div>
<div id="example4bis" class="box">
<div class="wrapper">
<div class="top">
#4bis : center
</div>
<div class="content wrapper">
<div class="group">
<h2>Section 1</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 2</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 3</h2>
<p>blah</p>
</div>
</div>
<div class="bottom">
footer
</div>
</div>
</div>
</div>
Explanation :
I have some "boxes" (in fact they are modals) with a header, a footer, and some content between. I would like the header and footer parts always visible, and the content scroll if too big to display all.
In the content part, I have several "sections" (groups of other items).
I would like these "groups" to be equidistant from each other (ie: free space between grows when it can. I saw the flex container property justify-content: space-evenly that is exactly what I want.
This is fine when I have to much space to display my content. I takes all the available space with "harmony".
The problem is when I have many content and that it cannot be displayed. So all the "groups" will be sticked. fine. I put my content an overflow-y: auto so it will scroll in that case.
But with justify-content: space-evenly, I can't access the top of my content even when the scroll is at the top. It is OUTSIDE the content wrapper...
Same problem with justify-content: space-around or justify-content: center + margin: auto on the flex items.
The only working solution is justify-content: space-between, but unfortunately this is not the behaviour I want...
What can I do to achieve this and have access to all my content if there is a scroll ??
Thanks.
space-evenly is a new property and won't work cross browsers (read more at the end).
As of today, you could use auto margin, where in this case all group items get a bottom auto margin, and the first also get a top auto margin.
That will generate the output you asked for.
Updated codepen
Stack snippet
body {
margin: 0;
}
.wrapper {
height: 100vh;
display: flex;
flex-flow: column nowrap;
}
.content {
padding: 10px;
overflow: auto;
flex: 1 1 auto;
display: flex;
flex-flow: column nowrap;
}
.content .group {
margin-bottom: auto;
flex: 0 0 auto;
background: yellow;
}
.content .group:first-child {
margin-top: auto;
}
.content h2 {
margin: 0;
padding: 0;
}
.top, .bottom {
text-align: center;
padding: 20px 0;
overflow: hidden;
flex: 0 0 auto;
border: 1px solid lightgray;
}
<div class="wrapper">
<div class="top">
auto margin
</div>
<div class="content">
<div class="group">
<h2>Section 1</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 2</h2>
<p>blah</p>
</div>
<div class="group">
<h2>Section 3</h2>
<p>blah</p>
</div>
</div>
<div class="bottom">
footer
</div>
</div>
Even with space-evenly one also need to add yet another new feature, a new keyword called safe, though it is still a working draft, and not many (if any) browsers support it yet.
And the reason is, when using e.g. justify-content, the overflow, in this case when using column direction, will be at both the top and bottom of the flex container.
I Have some problems with Bootstrap 4.
I Have activated flexbox and i want content in column to be 100% height.
This is the structure:
<div class="row">
<div class="col-md-6 col-xs-12">
<div class="content">
</div>
</div>
</div>
This is the result:
Flexbox
How can the gray background be 100% height? i tried with height: 100%. But the container will be 100% height of the page and not of the column. I also tried to set parent to relative and child to absolute. Doesn't worked.
Background color is set on content.
Codeexample:
http://jsfiddle.net/KjGZw/339/
You need to make the .col divs flex-containers with display:flex and apply flex-direction:column to those.
Then set the .content divs to flex:1
.row {
height: auto;
display: flex;
flex-wrap: wrap;
}
.col {
padding: 15px;
flex: 1 1 25%;
display: flex;
flex-direction: column;
}
.content {
background: purple;
color: white;
padding: 15px;
flex: 1;
}
*,
:after,
:before {
box-sizing: inherit;
}
<div class="row">
<div class="col">
<div class="content">
Test
<br />Test
<br />Test
<br />Test
<br />Test
<br />
</div>
</div>
<div class="col">
<div class="content">
Test
</div>
</div>
<div class="col">
<div class="content">
Test
</div>
</div>
<div class="col">
<div class="content">
Test
</div>
</div>
<div class="col">
<div class="content">
Test
<br>Test
<br>Test
<br>Test
<br>
</div>
</div>
<div class="col">
<div class="content">
Test
</div>
</div>
</div>
I am trying to display dynamic generated div's horizontally with scroll bar. There can be n number of div's.
Below is my Code:
HTML (index.html)
<div style="width:100%;float:left;" id="old">
<div>
<h1>First Div</h1>
<div id="R1">
<h1>First Div Internal</h1>
<a id="R1_index" class="close_page" href="javascript:void(0)">Close</a>
</div>
</div>
<div>
<h1>Second Div</h1>
<div id="R2">
<h1>Second Div Internal</h1>
<a id="R2_index" class="close_page" href="javascript:void(0)">Close</a>
</div>
</div>
</div>
I follow this link for solution.
But when dynamic div's load, structure looked messed up.
Here is the messy look:
HTML (index.html)
<div style="width:100%;float:left;" id="old">
<div id="items">Missing Internal Content</div>
<div id="items">Missing Internal Content</div>
</div>
Please help me guys.
i imagin the problem is that the div's in the container (id="old" in your example) are not next to each other, but instead beneath.
if that is your problem, you add the following styles to your container:
#old {
overflow: auto;
white-space: nowrap;
}
and make the childern-divs inline-block elements:
#old > div {
display: inline-block;
}
then it should work as expected. see the working solution:
* {
padding: 0;
margin:0;
}
#container {
width: 300px;
height: 100px;
overflow: auto;
white-space: nowrap;
}
.element {
display: inline-block;
}
.box {
width: 100px;
height: 100px;
background: lightgrey;
}
<div id="container">
<div class="element">
<div class="box">
<h1>1</h1>
</div>
</div>
<div class="element">
<div class="box">
<h1>2</h1>
</div>
</div>
<div class="element">
<div class="box">
<h1>3</h1>
</div>
</div>
<div class="element">
<div class="box">
<h1>4</h1>
</div>
</div>
<div class="element">
<div class="box">
<h1>5</h1>
</div>
</div>
<div class="element">
<div class="box">
<h1>6</h1>
</div>
</div>
<div class="element">
<div class="box">
<h1>7</h1>
</div>
</div>
<div class="element">
<div class="box">
<h1>8</h1>
</div>
</div>
</div>
otherwise please provide a better example/description of what the problem exactly is.