I got a grid, made out of inline-block elements so they can overflow to other rows.
The problem is that, if the row is not complete, the items in that row are also centered and not aligned to the left side, like if they'd just overflow.
The problem can be seen in this Fiddle.
Is there any way to do this in the pure CSS ? So that the container is centered, but the elements inside it remain to be on the left side ?
I already tried to use margin: 0 auto; to center just the container, but the container would have to have fixed width, but in this situation the container expands as far as it can (fills the outer container).
Edit: The only way I can think of is getting the width of the container with Javascript and then dividing the width by the width of the elements (as they have the same width), apply the width to the container and go with the margin: 0 auto; method.
This is not doable in a general case (see this post) because CSS can't determine when an element wraps and therefore recalculate the empty space required.
Similar to the solution in that post, you can use media queries to achieve this result. I have written a version more specific to your case:
.grid {
margin: 0 auto;
width: 990px;
font-size: 0;
}
.item {
display: inline-block;
margin: 5px;
width: 100px;
height: 100px;
background: red;
}
#media screen and (max-width: 230px) {
.grid {
width: 110px;
}
}
#media screen and (min-width: 231px) and (max-width: 340px) {
.grid {
width: 220px;
}
}
#media screen and (min-width: 341px) and (max-width: 450px) {
.grid {
width: 330px;
}
}
#media screen and (min-width: 451px) and (max-width: 560px) {
.grid {
width: 440px;
}
}
#media screen and (min-width: 561px) and (max-width: 670px) {
.grid {
width: 550px;
}
}
#media screen and (min-width: 671px) and (max-width: 780px) {
.grid {
width: 660px;
}
}
#media screen and (min-width: 781px) and (max-width: 890px) {
.grid {
width: 770px;
}
}
#media screen and (min-width: 891px) and (max-width: 1000px) {
.grid {
width: 880px;
}
}
#media screen and (min-width: 1001px) and (max-width: 1110px) {
.grid {
width: 990px;
}
}
<div class="grid">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
Writing this many media queries, however, is not advised. I actually generated them using Sass (see my CodePen):
$width: 100px;
$margin: 5px;
$extra-padding: 10px;
$column-width: $width + 2 * $margin;
$num-blocks: 9;
#function screen-size($n) {
#return $n * $column-width + $extra-padding;
}
.grid {
margin: 0 auto;
width: $num-blocks * $column-width;
font-size: 0;
}
.item {
display: inline-block;
margin: $margin;
width: $width;
height: 100px;
background: red;
}
#media screen and (max-width: screen-size(2)) {
.grid {
width: $column-width;
}
}
#for $i from 2 through $num-blocks {
#media screen and (min-width: screen-size($i) + 1px) and (max-width: screen-size($i + 1)) {
.grid {
width: $i * $column-width;
}
}
}
The above solution has its limitations, it can only work with fix number of blocks. If that's a concern, you will have to use a JavaScript library such as Desandro Masonry or just roll your own.
It seems like it's not possible to do it with CSS only approach (unless using Flexbox).
That means I'm going to use the "set width with Javascript and use margin: 0 auto" method.
The best would be to use the CSS only method proposed by #Nelson Yeung, but I'm going to use it on multiple pages, where the container width would not be the same.
Also interesting thing is that Zeplin, platform for front-end developers <> designers, is also using the method I'm going to use.
Related
.container {
padding-left: 15px;
padding-right: 15px;
margin-left: auto;
margin-right: auto;
}
/* Small */
#media (min-width: 768px) {
.container {
width: 750px;
}
}
/* Medium */
#media (min-width: 992px) {
.container {
width: 970px;
}
}
/* Large */
#media (min-width: 1200px) {
.container {
width: 1170px;
}
}
I tend to use containers everywhere To middle content in different devices and keep it balanced (media) like min-width 1200px {width: 1200px}etc. However when I try to style a site that begins from the edges. it comes in the middle which does not help in these kinds of sites.
what should I do?
If you are going to say get rid of containers, I do not know how to work without them Can you tell me what to do instead of the containers that I tend to use?
If you look at the code, there are two media queries, the one with a max width of 1024 pixels seems to work fine and apply all the styles but the one with a max width of 470 pixels won’t work or apply any of the styles. Why is this happening? Any help appreciated.
https://codepen.io/FreemanW/pen/QWwRBMP?editors=1100
#media (max-width: 470px) {
.banner {
align-items: flex-end;
}
.title-box {
width: 100%;
}
.container {
width: 100%;
}
}
``
Everything within 470px is also within 1024px, so perhaps there's some sort of conflict going on there.
You could try using max-width: 470px and min-width: 471px, thus dividing your options into non-overlapping values :)
Be sure that the media-query with the smaller screen-width is at the end.
That works:
#media (max-width: 1024px) {
h1 {
color: blue;
}
}
#media (max-width: 470px) {
h1 {
color: red;
}
}
<h1>TEST</h1>
That doesn't works:
#media (max-width: 470px) {
h1 {
color: red;
}
}
#media (max-width: 1024px) {
h1 {
color: blue;
}
}
<h1>TEST</h1>
I have a grid layout using Bootstrap where each div has a different height. Once I overflow onto the next row, I'd like my divs to tessellate rather than clearing the tallest item from the previous row. In my bootply, you can see the div on the second row clears the last items height in the first row.
I'd like that div to move up, directly under the first item in the first row
With the help of this question: stackoverflow.com/questions/19244268 I was able to make the masonry plug in respond to screen width by adding media queries for their .grid-sizer element:
.grid-sizer,
.grid-item {
width: 50%;
}
#media (min-width: 768px) {
.grid-sizer,
.grid-item {
width: 50%;
}
}
#media (min-width: 992px) {
.grid-sizer,
.grid-item {
width: 33%;
}
}
#media (min-width: 1200px) {
.grid-sizer,
.grid-item {
width: 25%;
}
}
#media (min-width: 1600px) {
.grid-sizer,
.grid-item {
width: 20%;
}
}
This question already has answers here:
Why does media query only work when placed last in my CSS?
(2 answers)
Closed 6 years ago.
I had a quiz in a html/css class I'm taking asking me to use media queries to reorganize some divs based on screen size. The code they supplied that I was supposed to add onto was this:
<style type="text/css">
/*
These are the responsive styles. Throw some breakpoints in here!
*/
.container {
display: flex;
flex-wrap: wrap;
}
.box {
width: 100%;
}
#media screen and (max-width: 400px) {
.dark_blue {
color: blue;
}
}
</style>
There was a lot more than that but that is the relevant part. I came up with this:
<style type="text/css">
/*
These are the responsive styles. Throw some breakpoints in here!
*/
#media screen and (min-width: 450px) {
.light_blue, .green {
width: 50%;
}
}
#media screen and (min-width: 550px) {
.red {
width: 33.3%;
}
.orange {
width: 66.6%;
}
}
#media screen and (min-width: 800px) {
.container {
width: 800px;
margin-left: auto;
margin-right: auto;
}
}
.container {
display: flex;
flex-wrap: wrap;
}
.box {
width: 100%;
}
#media screen and (max-width: 400px) {
.dark_blue {
color: blue;
}
}
</style>
But it did literally nothing. The page was completely unchanged. I eventually gave up and looked at the answer, they had written exactly the same the CSS that I had, only in a different order:
<style type="text/css">
/*
These are the responsive styles. Throw some breakpoints in here!
*/
.container {
display: flex;
flex-wrap: wrap;
}
.box {
width: 100%;
}
#media screen and (min-width: 450px) {
.light_blue, .green {
width: 50%;
}
}
#media screen and (min-width: 550px) {
.red {
width: 33.3%;
}
.orange {
width: 66.6%;
}
}
#media screen and (min-width: 800px) {
.container {
width: 800px;
margin-left: auto;
margin-right: auto;
}
}
#media screen and (max-width: 400px) {
.dark_blue {
color: blue;
}
}
</style>
So my question is, how does order get applied here and why didn't my code do anything at all?
CSS stands for Cascading Style Sheets, so there rules will be interpreted cascading down, so if you have
.blue { color: blue; }
And then later on down the same CSS file, you put
.blue { color: pink; }
It will overwrite the color of .blue to pink
With media queries you want to add your default styling first and then add your media queries, because it will detect media queries first and then just use those rules instead of your default styling.
Because the browser will be able to detect (for example) your devices min-width is 800px, it'll pick up those styles and not bother to overwrite them when the file gets interpreted further down in your default styling
Hard to explain. Hope that sort of cleared things up
I've very interesting HTML/responsive-web problem:
I've several <div> elements with inline-block display proprties:
<div id="a"></div> <div id="b"></div> <div id="c"></div>
Each element has max-width of 200px.
When I shrink the page all the elements shrink proportionally however what I need is give #a priority to shrink first, then make #b shrink and then make #c shrink.
How do I do that? Any ideas?
You can use media queries along the path of the shrinking screen size.
For instance, you can start with something like this...
#media screen and (max-width: 900px) { #a { width: 100px; } }
#media screen and (max-width: 600px) { #b { width: 100px; } }
#media screen and (max-width: 300px) { #c { width: 100px; } }
... and replicate the code at different thresholds with appropriate adjustments for screen size and div width.
UPDATE
Thanks for the clarification in your comments. You want to scale down the widths of three divs, with each div scaling down sequentially and smoothly. This can be done with a combination of CSS media queries and transitions.
HTML
<div id="a">DIV BOX A</div>
<div id="b">DIV BOX B</div>
<div id="c">DIV BOX C</div>
CSS
div { display: inline-block; width: 200px; }
div { transition: width .5s ease-in-out, left 1s ease-in-out; }
#media only screen and (max-width: 800px) { #a { width: 150px; } }
#media only screen and (max-width: 700px) { #b { width: 150px; } }
#media only screen and (max-width: 600px) { #c { width: 150px; } }
#media only screen and (max-width: 500px) { #a { width: 100px; } }
#media only screen and (max-width: 400px) { #b { width: 100px; } }
#media only screen and (max-width: 300px) { #c { width: 100px; } }
DEMO 1 – display: inline-block; based on your code
DEMO 2 – display: block; for demo purposes
Note: Re-size the window using the vertical bar. Make sure to expand the window all the way to see the full effect.
Media queries at each of the thresholds where you want each one to shrink should do the trick. Start out with a min-width, and remove it conditionally:
/* base rule */
#a, #b, #c {
min-width:200px;
}
/* conditionally shrink */
#media all and (max-width: 768px) {
#a { min-width:0; }
}
#media all and (max-width: 548px) {
#b { min-width:0; }
}
#media all and (max-width: 320px) {
#c { min-width:0; }
}