The navigation bar remains fixed while the section heading fails to display properly


=================================

My webpage acts like a homepage on the web. The issue arises when clicking on a new link, as my navbar is fixed and covers the section heading. I need the page to display the section heading properly even after clicking on a new link. Here's an example of the desired behavior: [link][/1]: https://i.sstatic.net/QeB1Z.png [Navbar remains fixed][2] even when a new link is clicked. In contrast, here is the current problem image: [img][/1]: https://i.sstatic.net/tpNEi.png Any help with resolving this issue would be greatly appreciated. I am seeking a solution using only HTML, CSS and JavaScript

**CSS Code**
* {
    margin: 0;
    padding: 0;
}

.nav {
    width: 100%;
    height: 100px;
    position: fixed;
    background-color: red;
}

.section {
    background-color: green;
    height: 200px;
}

.section1 {
    background-color: yellow;
    height: 900px;
}

.section2 {
    background-color: blue;
    height: 900px;
    transition: .5s;
}
**HTML Code**
<div class="nav">
    <a href="#main" style="font-size: 3rem;">new</a>

</div>

<div>

    <div class="section">

    </div>

    <div class="section1">

    </div>

    <div class="section2" id="main">
        <h1>section</h1>
    </div>

</div>


<script>
</script>
</body>

</html>

Answer №1

Make sure to set min-heights for elements so they expand properly on mobile devices.

You can also add padding-top: 150px to an ID to display it below the navigation and adjust scroll behavior in CSS.

html{
scroll-behavior:smooth;
}

Here's a code snippet you can use:

<html>
<head>
<style>
            * {
        margin: 0;
        padding: 0;
    }
    html{
        scroll-behavior: smooth;
    }

    .nav {
        width: 100%;
        height: 100px;
        position: fixed;
        background-color: red;
    }

    .section1 {
        background-color: green;
        min-height: 200px;
    }

    .section2 {
        background-color: yellow;
        min-height: 900px;
    }

    .section3 {
        background-color: blue;
        min-height: 900px;
        transition: .5s;
        padding-top: 150px;
    }
</style>
</head>
<body>
        <div class="nav">
            <a href="#main" style="font-size: 3rem;">new</a>
        </div>
    
        <div>
            <div class="section1">
    
            </div>
    
            <div class="section2">
    
            </div>
    
            <div class="section3" id="main">
                <h1>section</h1>
            </div>
    
        </div>
</body>
</html>

Alternatively, here's a code block with a container that you may find useful:

<html>
<head>
<style>
            * {
        margin: 0;
        padding: 0;
    }
    html{
        scroll-behavior: smooth;
    }

    .nav {
        width: 100%;
        height: 100px;
        position: fixed;
        background-color: red;
    }

    .section1 {
        background-color: green;
        min-height: 200px;
    }

    .section2 {
        background-color: yellow;
        min-height: 900px;
    }

    .section3 {
        background-color: green;
        min-height: 900px;
        transition: .5s;
        padding-top: 150px;
    }
    .container{
        height: 100%;
        background-color: blue;
    }
</style>
</head>
<body>
        <div class="nav">
            <a href="#main" style="font-size: 3rem;">new</a>
        </div>
    
        <div>
            <div class="section1">
    
            </div>
    
            <div class="section2">
    
            </div>
    
            <div class="section3" id="main">
                <div class="container">
                    <h1>section</h1>
                </div>
            </div>
    
        </div>
</body>
</html>

Answer №2

This is The Correct Solution By Java Script.

Automatically Calculate Navbar height and apply padding on scroll only.


Automatically calculate the Navbar height and create padding only when scrolling using Scroll-padding-top. This solution has been fully implemented using Java Script. The styling begins here## Heading ##



<style>
        * {
            margin: 0;
            padding: 0;
        }

        html {
            scroll-behavior: smooth;
        }

        .main {
            position: fixed;
            height: 100px;
            border: 5px solid yellow;
            width: 100%;
            background-color: red;
        }

        .green {
            height: 800px;
            background-color: green;
        }

        .blue {
            height: 800px;
            background-color: blue;
        }

        .yellow {
            height: 800px;
            background-color: yellow;
        }

        .pink {
            height: 900px;
            background-color: pink;
        }
    </style>

**HTML Starts Here**
<body>
        <div id="height" class="main">
            <a href="#blue">home</a>
            <a href="#pink">about</a>
            <a href="#new">main</a>
        </div>
        <div class="green">
        </div>
        <div id="blue" class="blue scroll">
            <h1>home</h1>
        </div>
        <div id="pink" class="pink scroll">
            <h1>about</h1>
        </div>
        <div id="new" class="yellow scroll">
            <h1>main</h1>
        </div>

The script implementation starts here

`

<script>
        const height = document.getElementById('height').offsetHeight;
        console.log(height);
        const scrolls = document.getElementsByClassName("scroll");
        for (let i = 0; i < scrolls.length; i++) {
            const padding = height + "px";
            scrolls[i].style.scrollMarginTop = padding;
        }
    </script>
`

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementation of Exporting to Excel

function fnshowAuditList() { if(auditListTable) auditListTable.fnDestroy(); jQuery.ajax({ type: 'POST', url: 'auditListAction', data: '', dataType: 'text&a ...

I encountered an issue while making customizations to my default next.config.js file. Despite attempting various solutions, I consistently encountered an error regarding the invalid src property

I'm currently trying to introduce some custom configurations into the next.config.js file. However, I keep encountering an error regarding an invalid src prop. Despite my attempts to troubleshoot in various ways, the error persists. // ...

What happens in mongoose if one of several consecutive queries fails?

Currently, as I work on my API, I am utilizing two Models: SongModel and UserModel. Whenever a new song is saved, I also execute a query to link the song._id to the user who created it. Unfortunately, a mistake in my code caused the second query to throw a ...

Why is ng-change not functioning properly, especially in conjunction with the Select element?

HTML <select ng-model="selectedName" ng-change="retrieveSelectedClass()" ng-options="(item.name||item) group by item.groupName for item in names" class="code-helper" id="code-helperId"> <option value="">Select Option</op ...

Having trouble getting Laravel Full Calendar to function properly with a JQuery and Bootstrap theme

Using the Laravel full calendar package maddhatter/laravel-fullcalendar, I am facing an issue where the package is not recognizing my theme's jQuery, Bootstrap, and Moment. I have included all these in the master blade and extended it in this blade. ...

What is the procedure for altering the location of a mesh within the animate() function in three.js?

In my implementation of a three.js mesh (found in three.js-master\examples\webgl_loader_collada_keyframe.html), I have a basic setup: function init() { ... ... var sphereGeometry = new THREE.SphereGeometry( 50, 32, 16 ); var sphereMater ...

Inquiring about Vue component prop defaults and detecting when a user has not assigned a value

1. I am looking for a way to set a default value for a component prop in Vue 2. Take, for example, a basic movies component that can be utilized like this: <movies year="2016"><movies> Vue.component('movies', { props: ['yea ...

Booting up a module in AngularJS without using automatic bootstrapping

Here is the structure of my HTML... <body id="main"> {{pageName}} </body> This is how I implement it in JavaScript: angular.module('myApp',[]) .controller('myController', function($scope){ console.log('initial ...

Display issue with positioning user name in navbar on Internet Explorer

The user name appears correctly in the navbar on Firefox and Chrome, but in IE it drops down due to a break tag issue. Is there a way to resolve this in IE so that it displays like in Firefox and Chrome? body { padding-top: 102px; background-colo ...

Transform the jQuery Mobile slider into a jQuery UI slider and update the text

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script> function modifycontent() { if (document.getElementById("slider").value<33) { $("#1").fadeIn("slow"); $("#2").fadeOut("slow"); ...

Trouble with displaying Django for loop in HTML template

Having an issue with displaying the latest post. Here is the code: views.py def latest_post(request): latest = Post.objects.all().order_by('-id')[:1] context = {'latestPost': latest} return render(request, 'latest_pos ...

Tips on utilizing the useState hook for storing numerous key objects?

Currently, I am working with a candlestick chart for a cryptocurrency that displays data over different timeframes such as 1 minute and 30 minutes. Below is the code snippet that sets the initial state to show a 1-hour chart when a user first visits: const ...

How can I ensure text remains confined within the parent div when working with Bootstrap?

I'm having trouble keeping the text and links within their parent div. Even though I set the outer boundary as col-sm-8, the text and links are overflowing. I'm not sure what I'm doing wrong, but the code I am using is shown below. I have a ...

Ensure that the responsive SVG image remains centered even when resizing

I have been attempting to center crop the responsive SVG image while resizing. Even though I used preserveAspectRatio="xMidYMax slice", the image width does not stay consistent with my screen size. body{ margin:0;padding:0; } .wrapper { position: re ...

ClassSerializerInterceptor in NestJS does not show the _id field

I am encountering an issue with properly exposing the _id when using the Serializer. Here is my current setup: @UseInterceptors(ClassSerializerInterceptor) @SerializeOptions({ strategy: 'excludeAll' }) This is how I defined the Class: export cl ...

Exploring Various Shapes in THREE JS

Currently, I am utilizing the EdgesGeometry for rendering my objects. I am currently exploring options on how to morph a Tetrahedron into an Octahedron, an Octahedron into a Box, a Box into a Sphere, and vice versa. Despite searching through numerous doc ...

Is there a way to create an effect where the color of a floating action button changes when you hover over it, similar to a filter?

I'm new to using the <Fab /> element and I'm struggling to figure out how to implement a hover effect that will change the button's color. Here's what I have so far: JavaScript: <Fab variant="extended" className=&quo ...

Using Node.js and the Jade templating engine, display the value of a passed variable

Asking such a basic question makes me feel guilty. app.get('/skumanagement/:id', function (req, res){ var options = req.params.id; // req.params.id = itemidx database.skuGetDetail(options, function (error, data){ winston.log('inf ...

What is the best method for retrieving the entire row data based on the maximum value in a column?

function findMaxValue() { var highestValue = Math.max.apply(Math, $('.sira').map(function() { return $(this).text() })) alert(highestValue); } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"& ...

How can we properly access the DOM element generated by an {#each} loop in Svelte when it is clicked?

Using Svelte's {#each} functionality, I am reactively loading elements in the following way: {#each $items as item} <div> <Button on:click={someFunction}>{item.text}</Button> (*) </div> {/each} (*) a component t ...