Technical Documentation page

Hi there, I am a little bit stuck working on the side menu.

Just like in the exercise I made it so that when you crop the browser and make it smaller then 815px that the side menu moves to the top of the screen with a media query, and that it changes from a fixed position to a absolute position…

Sadly this last part doesn’t work. On a small screen when I scroll down I want the menu to stay on top of the side and not stick to the top of the page anymore. But even though I change is to Absolute with the media query, it still remains sticky.

I tried alot but somehow can’t seem to find a solution to this issue.

below here is my entire CSS code

html {
    font-size: 16px;
  }
  
  @media (prefers-reduced-motion: no-preference) {
    * {
        scroll-behavior: smooth;
    }
}  

/* Side Menu */

.navh1 {
    background-color: white;
    font-weight: thin;
    font-size: 25px;
    text-align: center;
    position: relative;
    padding-top: 12px;
    padding-bottom: 10px;
  }

@media only screen and (max-width: 815px) {
    #navbar {
        background-color: white;
        position: absolute;
        top: 0;
        padding-top: 0;
        margin: 0;
        width: 100%;
        max-height: 200px;
        z-index: 1;
      }
}

@media only screen and (max-width: 815px) {
    #navbar > ul {
        border: 2px solid #2e2e2e;
    }
}

@media only screen and (max-width: 815px) {
    #navbar > ul > li:first-child {
        border-top: none;
    }
}

#navbar {
    position: fixed;
    min-width: 290px;
    top: 0;
    left: 0;
    height: 100%;
    border-right: 4px solid #999999;
    z-index: 1;
}

ul {
    display: block;
}

  nav > ul {
    overflow-y: scroll;
    margin-top: 0;
    height: 100%;
    padding-left: 0;
    background-color: white;
  }

  nav > ul > li {
    border-top: 1px solid;
    margin-top: 0;
    margin-bottom: 15px;
    padding-top: 15px;
    width: 100%;
    list-style: none;
  }

  a {
    display: block;
    color: black;
    text-decoration: none;
    font-size: 1.2rem;
    font-weight: thin;
    padding-left: 15px;
  }

/* Main Elements */

@media only screen and (max-width: 400px) {
    #main-doc {
        margin-left: -10px;
    }
}

@media only screen and (max-width: 815px) {
    #main-doc {
        position: relative;
        margin-left: 0;
        left: 0;
        margin-top: 310px;
        padding-top: 50px;
    }
}

#main-doc {
    position: absolute;
    padding: 20px;
    margin-bottom: 110px;
    top: 0;
}

main {
    display: block;
    left: 290px;
    background-color: white
}

  section {
    display: block;
    padding-left: 50px;
    padding-right: 50px;
  }

main > section > header {
    font-weight: bold;
    font-size: 2rem;
    display: block;
  }

code {
    padding: 5px;
    background-color: rgb(170, 170, 170);
    border-radius: 5px 5px 5px 5px;
    line-height: 26px;
}

I’ll also post my HTML script here.

<!DOCTYPE html>
<html lang="en">
<head>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
    <nav id="navbar">
    <header class="navh1">JS Documentation</header>
        <ul class="navbar">
            <li><a class="nav-link" href="#Introduction">Introduction</a></li>
            <li><a class="nav-link" href="#What_you_should_already_know">What you should already know</a></li>
            <li><a class="nav-link" href="#JavaScript_and_Java">JavaScript and Java</a></li>
            <li><a class="nav-link" href="#Hello_world">Hello world</a></li>
            <li><a class="nav-link" href="#Variables">Variables</a></li>
            <li><a class="nav-link" href="#Declaring_variables">Declaring variables</a></li>
        </ul>
    </nav>
    <main id="main-doc">
        <section class="main-section" id="Introduction">
            <header>Introduction</header>
            <article>
                <p>JavaScript is a cross-platform, object-oriented scripting language. It is a small and lightweight language. Inside a host environment (for example, a web browser), JavaScript can be connected to the objects of its environment to provide programmatic control over them.</p>
                <p>JavaScript contains a standard library of objects, such as Array, Date, and Math, and a core set of language elements such as operators, control structures, and statements. Core JavaScript can be extended for a variety of purposes by supplementing it with additional objects; for example:</p>
                <ul>
                    <li>Client-side JavaScript extends the core language by supplying objects to control a browser and its Document Object Model (DOM). For example, client-side extensions allow an application to place elements on an HTML form and respond to user events such as mouse clicks, form input, and page navigation.</li>
                    <li>Server-side JavaScript extends the core language by supplying objects relevant to running JavaScript on a server. For example, server-side extensions allow an application to communicate with a database, provide continuity of information from one invocation to another of the application, or perform file manipulations on a server.</li>
                </ul>    
            </article>
        </section>
        <section class="main-section" id="What_you_should_already_know">
            <header>What you should already know</header>
            <article>
                <p>This guide assumes you have the following basic background:</p>
                <ul>
                    <li>A general understanding of the Internet and the World Wide Web (WWW).</li>
                    <li>Good working knowledge of HyperText Markup Language (HTML).</li>
                    <li>Some programming experience. If you are new to programming, try one of the tutorials linked on the main page about JavaScript.</li>
                </ul>
            </article>
        </section>
        <section class="main-section" id="JavaScript_and_Java">
            <header>JavaScript and Java</header>
            <article>
                <p>JavaScript and Java are similar in some ways but fundamentally different in some others. The JavaScript language resembles Java but does not have Java's static typing and strong type checking. JavaScript follows most Java expression syntax, naming conventions and basic control-flow constructs which was the reason why it was renamed from LiveScript to JavaScript.</p>
                <p>In contrast to Java's compile-time system of classes built by declarations, JavaScript supports a runtime system based on a small number of data types representing numeric, Boolean, and string values. JavaScript has a prototype-based object model instead of the more common class-based object model. The prototype-based model provides dynamic inheritance; that is, what is inherited can vary for individual objects. JavaScript also supports functions without any special declarative requirements. Functions can be properties of objects, executing as loosely typed methods.</p>
                <p>JavaScript is a very free-form language compared to Java. You do not have to declare all variables, classes, and methods. You do not have to be concerned with whether methods are public, private, or protected, and you do not have to implement interfaces. Variables, parameters, and function return types are not explicitly typed.</p>
            </article>
        </section>
        <section class="main-section" id="Hello_world">
            <header>Hello world</header>
            <article>
                <p>To get started with writing JavaScript, open the Scratchpad and write your first "Hello world" JavaScript code:</p>
                <code>function greetMe(yourName) { alert("Hello " + yourName); } greetMe("World");</code>
                <p>Select the code in the pad and hit Ctrl+R to watch it unfold in your browser!</p>
            </article>
        </section>
        <section class="main-section" id="Variables">
            <header>Variables</header>
            <article>
                <p>You use variables as symbolic names for values in your application. The names of variables, called identifiers, conform to certain rules.</p>
                <p>A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).</p>
                <p>You can use ISO 8859-1 or Unicode letters such as å and ü in identifiers. You can also use the Unicode escape sequences as characters in identifiers. Some examples of legal names are Number_hits, temp99, and _name.</p>
            </article>
        </section>
        <section class="main-section" id="Declaring_variables">
            <header>Declaring variables</header>
            <article>
                <p>You can declare a variable in three ways:</p>
                <p>With the keyword var. For example,</p>
                <code>var x = 42.</code>
                <p>This syntax can be used to declare both local and global variables.</p>
                <p>By simply assigning it a value. For example,</p>
                <code>x = 42.</code>
                <p>This always declares a global variable. It generates a strict JavaScript warning. You shouldn't use this variant.</p>
                <p>With the keyword let. For example,</p>
                <code>let y = 13.</code>
                <p>This syntax can be used to declare a block scope local variable. See Variable scope below.</p>
                <code> let x = 12.</code>
            </article>
        </section>
    </main>
</body>
</html>

I removed the position property for the navbar id. I also added an overflow property set to hidden, to hide a part of the navigation bar items.

I don’t know if I answered your question. but with the above, I can guarantee that your nav bar won’t stick as you scroll down the page.

Hi thanks for your reply.

Yes I also tried that before indeed. When you remove the #navbar fixed position then the ‘small screen’ version works as I want it to work. yet at large screen, when the menu is on the left side, it should be sticky :sweat_smile: I don’t know if explain it right. but yeah ;p

So the fixed position at #navbar has to stay, I just want to “overwrite” or “overrule” it so to speak when the screen is smaller then 815px.

sorry english ain’t my first language so hope you understand me right :sweat_smile:

Let me test it on a smaller screen.

ahh, I figured it out xD

@media only screen and (max-width: 815px) {
    #navbar {
        background-color: white;
        position:absolute !important;  
        top: 0;
        padding-top: 0;
        margin: 0;
        width: 100%;
        max-height: 200px;
        z-index: 1;
      }
}

When I put the !important after the “Display: absolute” then it does what it should do. Stupid I didn’t think if this earlier :sweat_smile: :sweat_smile: