In this brand-new tutorial, we’ll begin with a full-screen area with a heading and also discover exactly how to stimulate it to a dealt with web page header on scroll. This sort of result is perfect for designs with hero areas like a touchdown or a solitary article web page.
1. Begin With the Web Page Markup (HTML)
We’ll specify 2 areas:
-
.(* )The very first area will certainly be full-screen and also consist of a heading and also web page header. By default, just the heading will certainly show up. Inside the header, we’ll place the logo design and also food selection.
- .(* )The 2nd area will certainly consist of some dummy web content. Listed below this area, we can have much more.
- Below’s the markup:
.
1
<< area
course = |
" very first" >> .<2 < h1 > |
Full-Screen Area to Fixed Header |
< .<3 < header |
>course = |
" page-header" >> .<4 < nav |
course = |
" nav" > . 5>< a |
href<= |
"" course =" logo-wrapper uk-flex uk-flex-middle" > . 6 < img |
size = |
" 178" >elevation =" 38"< src =" forecastr-logo-white. svg" alt =" forecastr logo design" > <. 7 |
> . |
<8
< ul >
|
<. |
9<< li>>
|
. |
<10 < a
|
>href = |
<""> > Residence< .<11> |
< . |
12>< li >
|
< . |
13<< a
|
href = |
"" <> Regarding .<14 |
. |
<15 < li >
|
. |
>16 < a
|
|
"" >> Call< .<17 (* ) . |
18 |
.(* )19
|
|
. 20
|
(* ) . |
21
|
. |
22
< area
|
course =(* )" 2nd" |
>
.
|
23 |
< div course = "container"(* )> ... |
. |
24 2. Establish the Key Styles (CSS) Allow's keep in mind of the primary designs-- you can see every one of them by clicking the CSS tab of the |
demonstration job |
: (* )
.
|
The very first area will certainly be a fixed-positioned component and also full-screen. In addition, it'll act as a grid container with up and down focused web content. You can change the elevation of the very first area via the hero-height(* )CSS variable. For example, if you desire this area to cover just fifty percent of the display, transform the CSS worth from 100vh to 50vh.
. . As we have actually done sometimes in the past, we'll utilize CSS Grid to pile the heading and also web page header. To do so, we'll provide (* )grid-area: 1/1 This is the shorthand of setup grid-column: 1
- and also(* )grid-row: 1
.(* )As we have actually currently talked about, the web page header will originally be unseen.
opacity: 0
To do so, we'll offer it
and also



- presence: concealed
- The last regulation is called for to avoid click occasions. (* )
.
The
navcomponent will certainly be a flex container with up and down focused web content. On huge displays, the logo design will certainly remain on the left side and also the food selection on the ideal side. On tvs( ≤ 600px), we'll pile them.
..
The nav design on huge displays
-
.
.(* )The 2nd area needs to rest below the very first one. To complete this, we'll offer it
padding-top: 110vhThis worth matches the elevation of the very first area (100vh) plus 10vh to produce the void in between the message and also the area's leading side (equivalent to the lower extra padding). If we do not offer it any type of
padding-top -
.
Below are the primary designs:
1
The nav design on mobile displays
worth, the 2nd area will certainly rest behind the very first area on top of the web page. Bear in mind that the very first area is a fixed-positioned component, and also hence it's eliminated from the typical paper circulation.
/ * CUSTOM-MADE VARIABLES RIGHT HERE */






- initially
- {
.
4placement
:
dealt with



;
. |
5 leading
|
: |
0 |
; |
. 6 left |
: |
0 ; . 7 size |
: |
100%; . 8 elevation |
: |
var( -- hero-height ); . |
9 |
history: link( mountain-full. jpg) no-repeat facility |
/ |
cover; . 10 background-blend-mode: increase |
; |
. 11 screen : grid ; . 12 place-items |
: |
facility>; . 13 z-index |
: |
1; . 14} |
. |
15 . 16 area, |
. |
17 . initially > * { |
. |
18 change
|
: |
all |
0.6 s |
; . 19 |
} |
. 20 . 21 initially |
> |
* { . 22 grid-area: |
1 |
/ 1
|
; |
. |
23 |
extra padding : 0 20px; |
. |
24 size : 100%; . 25 |
} |
. 26 . 27 initially page-header |
{ |
. 28 opacity: 0 |
; |
. 29
|
presence |
: |
concealed |
; . 30} |
. |
31 . 32 initially nav |
{ |
. 33 screen: flex |
; |
.
34
|
flex-wrap |
: |
cover |
; . 35 align-items(* ): |
facility(* ); |
. 36 justify-content:(* )space-between; |
. |
37 } . 38 . |
39 |
initially nav ul { . |
40 |
screen: flex; . |
41 |
list-style:
|
none |
; |
. |
42 } . 43 (* ) . 44 |
2nd |
{ . 45 extra padding(* ): calc(* )( |
var |
((* )-- hero-height) + 10vh(* ))(* )0 10vh |
0 |
;(* )
. 46
|
} |
. |
47 |
. 48 @media |
( |
max-width: 600px ) { .(* )49 initially nav { . 50 flex-direction : column; . |
51 |
justify-content:
|
facility |
; |
.(* )52 |
} . 53} 3. Stimulate on Scroll (JavaScript ) As we begin scrolling down the web page, we'll toggle the is-sticky |
course of the |
body component. Then, the adhering to activities will certainly happen: .(* )We'll efficiently lower the elevation of the very first area from 100vh to 90px. Once more, you can change the worth of the sticky header elevation via the |
sticky-header-height |
CSS variable. Additionally, we'll designate it a history shade that will certainly mix with its picture and also lead to a purple( darker) picture. Notification in the CSS over that this area has background-blend-mode: increase . We'll efficiently disclose the web page header and also conceal the heading. |
. |
We'll efficiently lower the padding-top worth of the 2nd area from 110vh to 90px+ 10vh. . We'll sync the computer animations of all components. Notification in the CSS over that their computer animation period will certainly be 0.6 s. |
. |
We'll specify the>:: prior to
|
pseudo-element of the |
body
|
component that will certainly add a box darkness to the web page header.
.
Below's the called for JavaScript code:
1
const
body
=
paper
- body
;
.2
const - =
- is-sticky
"
;
- 3
- 4(* )home window
.
addEventListener
(
"
toggleClass
"
.
.
scroll



"
, |
() = > { . . 5 const currentScroll = |
home window |
pageYOffset ; . 6 if ( currentScroll |
> |
0 |
) |
{ . 7 body classList include ( toggleClass ); . |
8 |
} else { . 9 body classList |
. |
get rid of ( toggleClass ); . 10 } . |
11 |
}); . And also the linked designs: 1/ * CUSTOM-MADE VARIABLES RIGHT HERE */ . 2 . 3 |
is-sticky |
:: prior to { . 4 |
web content |
: ""; . 5 placement: dealt with; |
. |
6 leading
|
: |
65px;
|
.
7 |
left:
|
0 |
; |
. |
8 size : 100 % |
; |
. 9 elevation: 15px |
; |
. 10 box-shadow : 0 |
10px |
15px rgba ( 0 , |
0 |
, 0 , 0.7(* )); . |
11 (* )} |
. (* )12 . 13 is-sticky(* ). initially { |
. |
14 elevation : var ( |
-- sticky-header-height |
); . 15 background-color : var(-- purple); . 16(* )} . 17 . 18 is-sticky |
initially |
h1 {
|
. (* )19 |
opacity |
:(* )0(* ); |
. 20 presence: |
concealed |
; . 21} .(* )22 . 23 (* ). is-sticky |
initially |
page-header(* ){ . 24(* )opacity: 1 ; . |
25 |
presence(* ):(* )noticeable;(* )
.
|
26 |
} |
.(* )27(* ) . |
28 is-sticky(* ). 2nd {(* ) .(* )29 padding-top: |
calc |
( var (-- sticky-header-height) |
+ |
10vh); . 30} |
Final Thought |
One more scrolling tutorial concerned an end, people! Throughout this workout, we covered exactly how to change a full-screen area to a sticky header on a web page scroll by toggling simply one course.
Ideally, this application was clear sufficient and also passionate you to use it on a future job! Certainly, you can improve it and also include some added functions like including parallax performance on scroll by altering probably the very first area's history placement. Allow's look once again at our development:
|
. |
Lastly, do not fail to remember to consider the Tuts + collection for even more |
header results tutorials on scroll |
As constantly, many thanks a great deal for checking out! . |