I need some help setting up a column-based grid for flexible layout. I am hoping to use flexbox primarily, but have been unable to figure out how to implement exactly what I need. I am also open to using a JS grid plugin, but again have not found one that meets me specific needs.
Here is a photo that illustrates what I'm hoping to build.
Grid Illustration
The most important priorities are:
The layout creates as many columns as possible within the master content container.
After organizing the content blocks in columns, resize each content block so that each column's height is uniform.
The height of the master content container's height scales fit content within.
Having the elements display in order is preferable, but not mandatory.
I also want the width of the content container to scale based on device. So predictably when it goes down to mobile it becomes a simple one column layout.
Now I believe I can control the number of grids by setting the container width, and content block width specifically based on device. I.e. if it's on mobile the content blocks are 100% width, and if it is on desktop the container is 900px and the content blocks are 300px.
That plus flexbox should give me something that resembles this. Flexbox Illustration, but only if I set a fixed height on the master container, which is not ideal.
So my question is, is there a way to setup flexbox to achieve my goals? If, not is there JS plugin that is capable of doing this?
HTML
<div id="blocks">
<div class="block">
<div class="info" style="height:280px"></div>
</div>
<div class="block">
<div class="info" style="height:280px"></div>
</div>
<div class="block">
<div class="info" style="height:280px"></div>
</div>
<div class="block">
<div class="info" style="height:600px"></div>
</div>
<div class="block">
<div class="info" style="height:400px"></div>
</div>
<div class="block">
<div class="info" style="height:300px"></div>
</div>
</div>
CSS
#blocks{
display:flex;
flex-direction:column;
justify-content: flex-start;
align-items:flex-start;
flex-wrap:wrap;
background-color: #ddd;
width:900px;
height:900px;
}
.info{
position:relative;
width:80%;
background-color: #888;
margin: 5px 0px;
}
.block{
width:290px;
background-color: #ddd;
border: 1px solid black;
flex: 1 0 auto;
display:flex;
flex-direction:column;
justify-content: center;
align-items:center;
}
CodePen
Related
Here is a example
.FlexContainer {
background: hsla(0,0%,0%,.1);
display: flex;
flex-direction: column;
max-height: 220px;
margin: 10px;
padding: 20px;
width: 300px;
overflow-y: auto;
}
.FlexItem {
background: hsla(0,0%,0%,.1);
width: 80%;
margin: 2px;
padding: 6px;
word-break: break-all;
white-space: normal;
cursor: pointer;
}
.FlexItem:hover{
background-color: red;
}
<html>
<head>
<title>demo</title>
</head>
<body>
<div class="FlexContainer">
<div class="FlexItem">
The contents is short in IE11.
</div>
<div class="FlexItem">
The contents is long - inside of this box are overflowing their container in IE11.
</div>
<div class="FlexItem">
The contents inside of this box are overflowing their container in IE11.
</div>
<div class="FlexItem">
The contents inside of this box are overflowing their container in IE11.
</div>
<div class="FlexItem">
The contents inside of this box are overflowing their container in IE11.
</div>
<div class="FlexItem">
The contents inside of this box are overflowing their container in IE11.
</div>
<div class="FlexItem">
The contents inside of this box are overflowing their container in IE11.
</div>
<div class="FlexItem">
The contents inside of this box are overflowing their container in IE11.
</div>
<div class="FlexItem">
The contents inside of this box are overflowing their container in IE11.
</div>
<div class="FlexItem">
The contents inside of this box are overflowing their container in IE11.
</div>
</div>
</body>
</html>
In chrome it can work well
chrome work well
But In IE11 It's not what I expected.
IE11 work not expected
This issue only happen when container height is less than fex items and set overflow-y:auto/scroll.
Is anyone can help me find the cause of the issue?
Appreciate for your help.
Internet Explorer needs you to specify the flex value of you children.
Try to add the property flex: 1 0 auto; to the FlexItem class
I've found the key point maybe the format of setting the width of FlexItem.
If you change the width:80% as 200px, it will work well in IE.
I guess that if you write in % format, IE may could not understand it well.
So you should give a static length to it.
So My situation is this, I have a lot of divs... A LOT (over 300) and im using them as part of an interactive background. EDIT:(they all have the same class btw)
The Problem?
Since on mobile I need more divs to fill the page than on desktop, I have too many divs on desktop, meaning you can scroll wayyyyyy more than I want to.
How can I have it so Divs Below a certain point are deleted or (more usefully) how to stop scrolling after a certain amount of pixels.
I literally have no idea how to do this, I've tried experimenting with margins, padding, overflow, position: fixed; but I haven't found a solution so don't pester my "lack of effort"
(some of my accounts have been blocked because I had no legitimate idea what to do and you "cool kids" decided to downvote me enough to get blocked (thanks for that!))
Anyway enough blabbing. Help would be appreciated! Thanks in advance.
You can do 2 things for this:
Wrap all your content in a div and set height to it and overflow-y: hidden.
.wrapper{
height: 1000px; overflow-y: hidden
}
With CSS you can hide the elements after certain number. Like if you want to hide all div after 100
.container .className:nth-child(n+101) {
display: none;
}
This will hide all the divs after 100.
Put all your divs inside a container div and use this css
.container{
max-height:900px; // Set this value to the no of pixel you want to scroll
overflow:hidden;
}
I have set the max-height to 900px cause i want to show only 9 div each div is off height 100px.
SNIPPET
.item {
width: 100%;
height: 100px;
background-color: red;
}
.item:hover {
background-color: yellow;
}
.container {
max-height: 900px;
overflow: hidden;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<!--You cant scroll after that-->
<div class="item">10</div>
<div class="item">11</div>
<div class="item">12</div>
<div class="item">13</div>
<div class="item">14</div>
</div>
Is it possible to create this page layout with css alone? if so what is the best way to go about it? floating puts all block level elements aligned to the last page break along the top, display:inline-block just aligns up along the bottom. Neither create the alineation model from the image below.
I know I could position them manually but the divs are filled with content from a data base so they will need to adjust automatically and align this way independently of their size. I thought about calculating all their heights with javascript and positioning them dynamically this way, but I've got this feeling that there might be a simple way of doing this that I'm totally overlooking. any ideas?
I guess you are looking for a layout similar to Pinterest, so the first Google search returned this example. There are a lot of other results, so I am sure one should fit your needs if you search yourself.
Have 3 columns, and fill the blocks in them,
Something like this,
Skeleton:
<div class="column-container">
<div class="column-1">
<div class="block">...</div>
<div class="block">...</div>
</div>
<div class="column-2">
<div class="block">...</div>
<div class="block">...</div>
<div class="block">...</div>
<div class="block">...</div>
</div>
<div class="column-3">
<div class="block">...</div>
<div class="block">...</div>
<div class="block">...</div>
</div>
</div>
css:
.column-1, .column-2, .column-3{
float:left;
width:200px;
height:auto;
}
.block{
display:block;
width:180px;
}
And the blocks you add to each column will go sit below one another. Simple.
Updated response:
Flexbox almost does what you want (as does something like I posted below) but if you're ordering must be left-to-right and it must tight-fit vertically as in your mock - perhaps consdier something like http://masonry.desandro.com/?
Original response:
Maybe try something like this?
.container {
-webkit-column-count: 3;
-webkit-column-gap: 10px;
-webkit-column-fill: auto;
-moz-column-count: 3;
-moz-column-gap: 10px;
-moz-column-fill: auto;
column-count: 3;
column-gap: 15px;
column-fill: auto;
}
.container div {
display: inline-block;
width: 200px;
-webkit-column-break-inside: avoid;
-moz-column-break-inside: avoid;
column-break-inside: avoid;
margin-bottom: 10px;
}
Here: http://jsfiddle.net/bvaughn/rrpg58yy/
Bootsrap is the one I recommend, u simply add the reference to your file (or project) and then it's really easy to design the layout that you want, after you implement Bootstrap, your code will look like this:
<div class="container">
<div class="col-lg-3">
<div class="row">
<div>some data 1</div>
</div>
<div class="row">
<div>some data 2</div>
</div>
<div class="row">
<div>some data 3</div>
</div>
</div>
<div class="col-lg-3">....
Fiddle
Make sure you resize your page, since bootstrap stacks all the divs together for smaller (mobile) screens
I've been trying to make my Bootstrap columns the equal size and have been failing. Using CSS or JavaScript, how can I accomplish this?
Bootply Example
CSS:
#grid-selector .container {
max-width: 400px;
}
#grid-selector [class*="col-"] {
border: 1px solid #000;
text-align: center;
padding: 20px;
}
HTML:
<div id="grid-selector">
<div class="container">
<div class="row">
<div class="col-xs-6">Section 1<br>Space</div>
<div class="col-xs-6">Section 2</div>
</div>
<div class="row">
<div class="col-xs-6">Section 3</div>
<div class="col-xs-6">Section 4</div>
</div>
<div class="row">
<div class="col-xs-3">Section 5</div>
<div class="col-xs-3">Section 6</div>
<div class="col-xs-6">Section 7</div>
</div>
<div class="row">
<div class="col-xs-6">
<div class="row man">
<div class="col-xs-12">Section 8</div>
</div>
<div class="row man">
<div class="col-xs-12">Section 9</div>
</div>
</div>
<div class="col-xs-6">Section 10</div>
</div>
<div class="row">
<div class="col-xs-12">Section 11</div>
</div>
</div>
</div>
tadaaa! Super FLEX to the rescue!
#grid-selector .container {
}
#grid-selector .row {
display:flex;
align-items:stretch !important;
}
#grid-selector [class*="col-"] {
border: 1px solid #000;
text-align: center;
padding: 20px;
min-height:100%;
}
And I even made your example fully responsive instead of fixed width, just because I can ;) .
No, really, I did it to show you it could easily be responsive, but if you want it to be fixed , just add all this code inside a container div (do NOT limit the container width as you did!)
see to understand it visually http://www.bootply.com/Wypyz5PaSN and play around
and read more about FLEX MODEL at Mozilla MDN
The CSS3 Flexible Box, or flexbox, is a layout mode providing for the arrangement of elements on a page such that the elements
behave predictably when the page layout must accommodate different
screen sizes and different display devices. For many applications, the
flexible box model provides an improvement over the block model in
that it does not use floats, nor do the flex container's margins
collapse with the margins of its contents.
Many designers will find the flexbox model easier to use. Child elements in a flexbox can be laid out in any direction and can have
flexible dimensions to adapt to the display space. Positioning child
elements is thus much easier, and complex layouts can be achieved more
simply and with cleaner code, as the display order of the elements is
independent of their order in the source code. This independence
intentionally affects only the visual rendering, leaving speech order
and navigation based on the source order.
The easiest way to accomplish this is to create an additional CSS class which is shared between the columns in each row.
In this class, you'd want to define a min-height.
<div class="container">
<div class="row">
<div class="col-xs-6 row1">Section 1<br>Space</div>
<div class="col-xs-6 row1">Section 2</div>
</div>
</div>
You would then define your CSS as follows
.row1{
min-height: 100px;
}
The reason you'd want to be using min-height as opposed to height, is to retain the responsiveness when you reduce the size of the window. If you were to then do only height, as opposed to min height, as soon as the site becomes smaller, the objects cannot fit inside the div, and don't perform properly.
I'm trying to get a navigation div to sit in the middle between an image and the right page but also in a way that if the browser is resized, will still maintain the relation. I'm quite positive I know how to do it with a relative positioning but I would like to have the navigation fixed.
[img] nav |right side of viewport
#wrapperNav {
position: fixed;
top: 45%;
right: 50%;
z-index:999;
}
Code: http://jsfiddle.net/LLtnZ/3/
Flexbox has support in most newer desktop and mobile browsers and really dispenses with a lot of the hackiness involved in trying to maintain alignment and positioning with varying screen sizes; it flexes.
For the carousel you describe I would start with the below HTML, based on what you provided in your fiddle:
<div id="wrapper">
<nav class="nav nav--ready">
<div class='nav__center'>
<div class="nav__link nav__link--active">
<a class="nav__link-bullet" href="#one">1</a>
</div>
<div class="nav__link">
<a class="nav__link-bullet" href="#two">2</a>
</div>
<div class="nav__link">
<a class="nav__link-bullet" href="#three">3</a>
</div>
<div class="nav__link">
<a class="nav__link-bullet" href="#four">4</a>
</div>
<div class="nav__link">
<a class="nav__link-bullet" href="#five">5</a>
</div>
</div>
</nav>
<div class='carousel'>
<img src="" alt=""/>
</div>
</div>
#wrapper is a flexbox, .nav is too, so is .nav__center. #wrapper is set to flex-direction: row-reverse; so as to display navigation on the right, if we added another element to #wrapper it would display to the right of .nav. .nav itself is set to flex-direction: row;, though it only has one child: .nav__center, its flexbox styles of justify-content: space-around; and align-items: center; keep the nav buttons aligned at its center no matter what. Within .nav, .nav__center is set to flex-direction: column; so its contents display top to bottom, and these styles align-self: stretch; justify-content: space-around; distribute each .nav__link element evenly from top to bottom.
Here is a fiddle. Notice that navigation stays glued to the right side of the carousel. I set a minimum width on .nav so that it won't disappear even when the display is really tiny.
Here is a quick guide you can probably get through in ten minutes with a cup of coffee.
You need to add position: relative; to your container which in this case is the nav html element.
http://jsfiddle.net/LLtnZ/4/
This might help. So if your image is is 90% width and centered, that will leave 10% left of pagespace, 5% on either side of the image.
If I understand your drawing correctly, the that would mean you'd want the container your nav is in to be
width: 5%
position:fixed;
top: 50%;
right:0;
margin-top -150px; // half the height
height: 300px;
text-align:center;
The 5% width will take up the entire space to the right, but using text-align:center will get items in the middle, as per your "equal" annotation on your drawing.
If you need a jsFiddle let me know
Here's a fiddle: http://jsfiddle.net/47Q9p/