2

I have a div that needs to be full screen width inside a parent div that has a limited with. Simplified, it's something like:

HTML:

<div class="container">
  <div class="banner">
  </div>
</div>

CSS:

.container {
  width: 1170px;
  margin: auto;
}
.banner {
  width: 100vw;
  margin-left: calc( 50% - 50vw);
}

which works fine, except for one thing: The scrollbar on the page covers some of the content in the child div, because 100vw appearantly includes the scrollbar width. So is there a way around this so I can set the width to (100vw - scrollbar width), or perhaps a completely different way to achieve what I want to do with pure CSS?

ReddaJoppe
  • 458
  • 1
  • 8
  • 24

2 Answers2

2

You could set the scrollbar width and subtract it from the container's width using 'pure CSS'.

You could give width to the scroll bar in webkit-browsers using:

body::-webkit-scrollbar {
 width: scrollbarwidthpx;
}

and set the content width as:

width: calc(100vw - scrollbarwidthpx);

You could make use of this article regarding customizing scrollbar

Vishnu
  • 1,611
  • 1
  • 14
  • 27
1

Try to use % where you can. vw is a percent of the viewport width including the scrollbar and % is a percent of the wrapper object, where the body is not rendered inside the scrollbar.

  • Don't use a fixed width (px) container. It's bad practice and will not render well on mobile screens. See Responsive Web Design for more.
  • Don't use vw for containers (or banners). It has weird effects on the scrollbar.

Finally, I don't understand why you want something to be at 300vw or 3x the width of the viewport, but sure. If you designed your page right with responsive web design and avoided setting any wrapper's dimensions with px, then it shouldn't be hard to know what that width of the containing div is. For example, if the wrapper (containing div) is at 30% of the viewport and you want your banner to be 300% of the viewport, then you want 1000% for your banner to span the width of three screens.

RWDJ
  • 734
  • 8
  • 13