scroll-snap doesn't snap correctly

1.1k views Asked by At

I am working on a personal project and i use scroll-snap in this project

but for some reason the scroll snap sometimes overshoots or lands at an awkward position between 2 parts of the page

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@100&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Titillium+Web:wght@600&display=swap');

:root {
    --bg-color: rgb(33, 32, 41);
    --header-color: rgb(255, 170, 55);
    --color-white: white;
    --color-black: black;
}
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    
    scroll-behavior: smooth;
}
html{
    scroll-snap-type: y mandatory;
}
html, body {
    font-family: 'Titillium Web', sans-serif;
    background-color: var(--bg-color);
    color: var(--color-white);
    width: 100%;
}
body div {
    scroll-snap-align: start;
}






header {
    background: var(--header-color);
    position: fixed;
    top: 0;
    z-index: 10000;
    width: 100%;
}

header div {
    width: 80%;
    margin: 0 auto;
}
header div::after {
    content: '';
    display: table;
    clear: both;
}

header div img {
    position: absolute;
    float: left;

    margin: 0.6em;
}

nav ul {
    width: auto;
    float: right;

    margin: 2em;
}

nav ul li {
    display: inline-block;
    margin-left: 2em;
    font-family: 'Roboto', sans-serif;
}

nav ul li a {
    font-size: 1.5em;

    color: black;
    text-decoration: none;
    text-transform: uppercase;
}
nav ul li a:hover {
    color: rgb(93, 93, 93);
}






#about {
    display: flex;
    justify-content: center;
    align-items: center;

    width: clamp(750px, 80%, 100%);
    height: 100vh;

    margin: 0 auto;

    text-align: center;

    font-size: 1.5em;
}
#about p {
    display: inline;
    color: var(--header-color);
}
#about h1 {
    margin-bottom: 1em;
}





#invite {
    display: flex;
    justify-content: center;
    align-items: center;

    height: 100vh;

    margin: 0 auto;

    text-align: center;

    font-size: 1.5em;

    background-color: var(--header-color);
    color: var(--color-black);
}
#invite div {
    width: clamp(750px, 80%, 100%);

}
#invite div h1 {
    margin-bottom: 2em;
}
#invite div button {
    width: 6em;
    height: 3em;

    background-color: rgba(0, 0, 0, 0);
    color: var(--color-black);

    font-size: 1em;
    font-family: 'Roboto', sans-serif;
    text-transform: uppercase;
    
    border: 1px solid black;

    transition: 0.2s;
}
#invite button:hover {
    margin-top: 0.3em;
}
#invite button a {
    color: var(--color-black);
    text-decoration: none;
}





#contact {
    display: flex;
    justify-content: center;
    align-items: center;

    width: clamp(750px, 80%, 100%);
    height: 100vh;

    margin: 0 auto;

    text-align: center;

    font-size: 1.5em;
}
#contact div h1 {
    margin-bottom: 1em;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FoxoBot</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    
    <header>
        <div>
            <img src="files/logo.png" alt="Logo" height="72em"> 

            <nav>
                <ul>
                    <li><a href="#about">About</a></li>
                <!--<li><a href="#showcase">showcase</a></li>-->
                    <li><a href="#invite">Invite</a></li>
                    <li><a href="#contact">contact</a></li>
                </ul>
            </nav>
        </div>
    </header>

    <div id="about">
        <div>
            <h1>About</h1>

            <p><p>FoxoBot</p> is a Discord bot developed by Lappland and Ralkey.</p>
            <br>
            <p><p>FoxoBot</p> is a easy to use and a friendly bot for general, fun & administrative commands on Discord</p>
            <br>
            <p><p>FoxoBot</p> is always in development and we also learn alot along the way</p>
        </div>
    </div>

<!--<div id="showcase">
    -- i dont have anything for showcase yet --
    </div>-->

    <div id="invite">
        <div>
            <h1>Invite FoxoBot to your server</h1>
            <button><a href="#" target="_blank">Invite!</a></button>
        </div>
    </div>
    
    <div id="contact">
        <div>
            <h1>Contact us</h1>

            <p>you can contact us throught Discord.</p>
            <p>our tags are:</p>
            <p>Ralkey: blank</p>
            <p>Lappland: blank</p>
        </div>
    </div>
    
</body>
</html>

(I recommend opening the snippet in full page)

after countless amounts of time searching for a solution I have given up and landed here

I hope atleast one of you is able to help me

1

There are 1 answers

2
yeahlad On

It appears your problem is that you're applying scroll-snap-align: start; to every div within body and so when you scroll it is snapping to each div in your page. Whereas you only want to be applying it to its first child of the body or in your case each section of your page.

So all I have done in the example below is changed body div to body > div in your css. You can see more information on the greater than sign in css here: What does the ">" (greater-than sign) CSS selector mean?

@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@100&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Titillium+Web:wght@600&display=swap');

:root {
    --bg-color: rgb(33, 32, 41);
    --header-color: rgb(255, 170, 55);
    --color-white: white;
    --color-black: black;
}
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    
    scroll-behavior: smooth;
}
html{
    scroll-snap-type: y mandatory;
}
html, body {
    font-family: 'Titillium Web', sans-serif;
    background-color: var(--bg-color);
    color: var(--color-white);
    width: 100%;
}
body > div {
    scroll-snap-align: start;
}






header {
    background: var(--header-color);
    position: fixed;
    top: 0;
    z-index: 10000;
    width: 100%;
}

header div {
    width: 80%;
    margin: 0 auto;
}
header div::after {
    content: '';
    display: table;
    clear: both;
}

header div img {
    position: absolute;
    float: left;

    margin: 0.6em;
}

nav ul {
    width: auto;
    float: right;

    margin: 2em;
}

nav ul li {
    display: inline-block;
    margin-left: 2em;
    font-family: 'Roboto', sans-serif;
}

nav ul li a {
    font-size: 1.5em;

    color: black;
    text-decoration: none;
    text-transform: uppercase;
}
nav ul li a:hover {
    color: rgb(93, 93, 93);
}






#about {
    display: flex;
    justify-content: center;
    align-items: center;

    width: clamp(750px, 80%, 100%);
    height: 100vh;

    margin: 0 auto;

    text-align: center;

    font-size: 1.5em;
}
#about p {
    display: inline;
    color: var(--header-color);
}
#about h1 {
    margin-bottom: 1em;
}





#invite {
    display: flex;
    justify-content: center;
    align-items: center;

    height: 100vh;

    margin: 0 auto;

    text-align: center;

    font-size: 1.5em;

    background-color: var(--header-color);
    color: var(--color-black);
}
#invite div {
    width: clamp(750px, 80%, 100%);

}
#invite div h1 {
    margin-bottom: 2em;
}
#invite div button {
    width: 6em;
    height: 3em;

    background-color: rgba(0, 0, 0, 0);
    color: var(--color-black);

    font-size: 1em;
    font-family: 'Roboto', sans-serif;
    text-transform: uppercase;
    
    border: 1px solid black;

    transition: 0.2s;
}
#invite button:hover {
    margin-top: 0.3em;
}
#invite button a {
    color: var(--color-black);
    text-decoration: none;
}





#contact {
    display: flex;
    justify-content: center;
    align-items: center;

    width: clamp(750px, 80%, 100%);
    height: 100vh;

    margin: 0 auto;

    text-align: center;

    font-size: 1.5em;
}
#contact div h1 {
    margin-bottom: 1em;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FoxoBot</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    
    <header>
        <div>
            <img src="files/logo.png" alt="Logo" height="72em"> 

            <nav>
                <ul>
                    <li><a href="#about">About</a></li>
                <!--<li><a href="#showcase">showcase</a></li>-->
                    <li><a href="#invite">Invite</a></li>
                    <li><a href="#contact">contact</a></li>
                </ul>
            </nav>
        </div>
    </header>

    <div id="about">
        <div>
            <h1>About</h1>

            <p><p>FoxoBot</p> is a Discord bot developed by Lappland and Ralkey.</p>
            <br>
            <p><p>FoxoBot</p> is a easy to use and a friendly bot for general, fun & administrative commands on Discord</p>
            <br>
            <p><p>FoxoBot</p> is always in development and we also learn alot along the way</p>
        </div>
    </div>

<!--<div id="showcase">
    -- i dont have anything for showcase yet --
    </div>-->

    <div id="invite">
        <div>
            <h1>Invite FoxoBot to your server</h1>
            <button><a href="#" target="_blank">Invite!</a></button>
        </div>
    </div>
    
    <div id="contact">
        <div>
            <h1>Contact us</h1>

            <p>you can contact us throught Discord.</p>
            <p>our tags are:</p>
            <p>Ralkey: blank</p>
            <p>Lappland: blank</p>
        </div>
    </div>
    
</body>
</html>