How To Make The Background On One Page Different From The Rest In Css
TL;DR
If you desire to have a full-folio container div, make sure yous have these:
/* override browser default */ html , body { margin : 0 ; padding : 0 ; } /* use viewport-relative units to cover folio fully */ body { top : 100vh ; width : 100vw ; } /* include border and padding in element width and height */ * { box-sizing : border-box ; } /* total-sized container that fills upwardly the page */ div { height : 100% ; width : 100% ; /* example padding, font-size, groundwork, etc */ padding : 10px ; font-size : 20px ; background-color : lightskyblue ; }
And so let'south say you desire a div that fills up entire folio...
div { height : 100% ; width : 100% ; font-size : 20px ; background-color : lightskyblue ; }
What?! It doesn't work! The elevation still merely takes up the content, simply non the whole page.
The width is good since a div is past default a block chemical element, which takes equally much width equally possible anyways.
Can we simply use a more "accented" value like px
?
div { /* height: 100% */ height : 865px ; /* current height of my browser */ /* ... */ }
It works... until the browser is resized
It doesn't adapt when the browser is resized.
You can utilize JS for this, merely that'southward way overkill for what we wanted.
I mentioned
px
is "absolute", but just in the sense that they are not relative to annihilation else (like rem and vh). But the actual size still depends on the device. Here's some details:
Stack Overflow: Is a CSS pixel really an accented unit?
Relative units to the rescue!
Old schoolhouse superlative: 100%
html , body { tiptop : 100% ; width : 100% ; } div { height : 100% ; /* ... */ }
Works! (We'll fix the scrollbars after)
By setting both <html>
and its kid <body>
to 100% peak, we achieve the full size.
Notation that only setting either of them won't work, since percentage is always relative to another value.
In this example:
-
div
is 100% the height of thebody
-
body
is 100% the height of thehtml
-
html
is 100% the height of the Viewport
Viewport is the visible expanse of the browser, which varies past device.
Viewport >
html
>torso
>div
For example, an iPhone 6/vii/eight has a 375x667 viewport. You can verify this on your browser dev tools mobile options.
For at present, you lot tin can think about viewport as the device pixel size or resolution. But if you want to go deep:
Media Genesis: Screen Size, Resolution, and Viewport: What does it all mean?
newer solution: viewport units vh
and vw
Viewport-percentage lengths aka Viewport units take been around for a while now, and is perfect for responding to browser resizes.
- one viewport height (
1vh
) = one% of viewport summit - i viewport width (
1vw
) = ane% of viewport width
In other words, 100vh
= 100% of the viewport tiptop
100vw
= 100% of the viewport width
So these effectively fills up the device viewport.
html , body { /* height: 100%; */ /* width: 100% */ } div { /* tiptop: 100%; width: 100%; */ height : 100vh ; width : 100vw ; /* ... */ }
Looks practiced too! (We'll fix the scrollbars afterwards)
Equally mentioned in the comments by @angelsixuk and @mpuckett , there is a known jumping behavior during scrolling when using
100vh
on mobile browsers, which is an event only considered intentional past webkit. Come across these links for details: Viewport pinnacle is taller than the visible office of the document in some mobile browsers and Stack Overflow: CSS3 100vh not constant in mobile browser
How about min-height: 100vh
?
While pinnacle
fixes the length at 100vh
, min-height
starts at 100vh
but allows content to extend the div across that length. If content is less than the length specified, min-meridian
has no result.
In other words, min-height
makes certain the element is at to the lowest degree that length, and overrides meridian
if height
is defined and smaller than min-height
.
For our goal of having a div child with full elevation and width, it doesn't make whatsoever difference since the content is also at total size.
A good employ case of
min-height
is for having a viscous footer that gets pushed when there is more content on the folio. Check this out here and other adept uses of vh
Fun with Viewport Units | CSS-Tricks
A very common exercise is to utilize height: 100vh
and width: 100vw
to <body>
directly...
In this instance, we can even keep the container div
relatively sized like in the beginning, in instance we change our minds later.
And with this approach, we assure that our entire DOM body occupies full superlative and width regardless of our container div.
body { height : 100vh ; width : 100vw ; } div { meridian : 100% ; width : 100% ; /* meridian: 100vh; width: 100vw; */ /* ... */ }
vh/vw
versus %
A good style of thinking about vh, vw
vs %
is that they are coordinating to em
and rem
%
and em
are both relative to the parent size, while vw/vh
and rem
are both relative to "the highest reference", root font size for rem and device viewport for vh/vw.
Only why the scrollbar?
<html>
and <body>
have default margins and paddings!
Browsers feature a default margin, padding and borders to HTML elements. And the worst role is it'southward dissimilar for each browser!
Chrome default for <body>
has a margin: 8px
And 100vh + 8px
causes an overflow, since information technology's more than the viewport
Luckily, it's fairly piece of cake to fix that:
html , body { margin : 0 ; padding : 0 ; } trunk { ...
This is a "blanket" solution that would cover all margin and padding variations for any browser you might have.
Cool! Now we take our div filling upwards the page without scrollbars!
Finally, let'due south add a little padding, since it's awkward that the content is right on the edges.
div { padding : 10px ; /* ... */ }
What?! The scrollbar is back! What happened?
box-sizing
edge-box
box-sizing
allows you to define whether the padding and edge is included in the div's height and width.
The default content-box
of box-sizing
doesn't include padding and border in the length, so div becomes
-
pinnacle = 100% + 10px * ii
-
width = 100% + 10px * 2
which overflows the page!
border-box
includes padding and border, so div stays at our required sizes:
-
height = 100%
-
width = 100%
It'southward quite common to gear up all elements to edge-box
for a consistent layout and sizing throughout pages, using *
selector:
* { box-sizing : border-box ; }
Perfect!
Catch y'all in the next ane!
How To Make The Background On One Page Different From The Rest In Css,
Source: https://dev.to/lennythedev/css-gotcha-how-to-fill-page-with-a-div-270j
Posted by: wilsonmeself.blogspot.com
0 Response to "How To Make The Background On One Page Different From The Rest In Css"
Post a Comment