2

When using layout like this:

div {
  width: 25vw;
  height: 150vh;
  float: left;
  background: rgb(30,120,210)
}
div:nth-child(2n+2) {
  background: rgb(210,120,30)
}
<div></div>
<div></div>
<div></div>
<div></div>

Scrollbar crushes design. Without scrollbar it works just fine, each element takes 1/4 of the screen width. Well one solution would be to add width: calc(25vw - 17px) to last element but my grid is way more complicated than that. So I was thinking if it's possible to make slider have rule similar to position: fixed so it goes above elements rather than crushing them. Or even better, if possible, to remove scrollbar. This problem doesn't appear on Linux Firefox or Chrome.

Kunok
  • 8,089
  • 8
  • 48
  • 89
  • I have no such problem using Google Chrome on OSX. You might want to test you markup on multiple browsers/os first. Don't take the path of bad design just because IE is unable to interpreter HTML. – Ivan Venediktov Mar 27 '16 at 04:12
  • @IvanVenediktov OS X and some Linux window managers have layout-free scrollbars by default, they simply overlap the content. Check out System Preferences > General > Show scroll bars: Always – Alexander O'Mara Mar 27 '16 at 04:13
  • 1
    @IvanVenediktov Yeah I forgot to mention that. On Linux (Ubuntu) Firefox & Chrome this problem doesn't exist. – Kunok Mar 27 '16 at 04:15

2 Answers2

2

Why not just use width: 25% instead of width: 25vw? Then the scrollbar will be taken into account.

Working example:

div {
  width: 25%;
  height: 150vh;
  float: left;
  background: rgb(30,120,210)
}
div:nth-child(2n+2) {
  background: rgb(210,120,30)
}
<div></div>
<div></div>
<div></div>
<div></div>
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
  • Sorry for slow response, took me a while to test this actually (http://i.imgur.com/2P00y5p.png) and it works. A very good idea. – Kunok Mar 27 '16 at 04:26
1

If you use flexbox it can be simply solved by flex and min-width properties:

body {
  display: flex;
  flex-flow: row wrap;
}

div {
  flex: 1;
  height: 150vh;
  background: #123;
  min-width: 25%;
  max-width: 25%;
}

div:nth-child(odd) {
  background: #222;
}
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

So, max-width can be used for any new element in the row to follow the rule.

PS: for cross-browser testing, don't forget to prefix flex properties.

nikoloza
  • 968
  • 2
  • 12
  • 30
  • 1
    Sorry it's still too soon to use flex due to older browser support – Kunok Mar 27 '16 at 04:27
  • 1
    Yes, but if you really care about old browsers. But all current browsers support it: http://caniuse.com/#search=flex. Also you can find polyfills to just support future proof code. I already use it in my projects. – nikoloza Mar 27 '16 at 04:30
  • Thanks for info. I will tend to use flex in my upcoming projects then. – Kunok Mar 27 '16 at 04:38