How do you remove the scrollbar from a navigation bar in HTML and CSS?

Is it possible to adjust the placement of a scrollbar within a page template that contains a navigation bar and content in separate blocks? Currently, when the page overflows, the scrollbar appears in the content block and overlaps with the navbar. However, I would like the scrollbar to be displayed after the navbar instead. How can this be achieved?

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Asd</title> 
  </head>
  <body>

    <div class="navbar">
      <!-- My navbar  -->
    </div>

    <div class="content">
      <!-- Content -->
    </div>

  </body> 
</html>

Answer №1

It seems like this is what you're looking for. The navigation bar maintains a fixed height (shown in yellow in my example), while the content inside the "content" class has a scroll bar if it contains a lot of content.

html,body{
  width:100%;
  height: 100vh;
}
body{
margin:0px;
padding:0px;
}
.navbar{
width:100%;
height:50px;
overflow:hidden;
background-color:yellow;
}
.content{
  height: calc(100vh - 50px);
  background:#ccc;
  overflow:auto;
}
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Asd</title> 
  </head>
  <body>

    <div class="navbar">
      <!-- My Navigation Bar -->
    </div>

    <div class="content">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit... (content continues)

Answer №2

Correct me if I'm wrong, but you're looking to restrict the scrollbar to just the content area of the page, right?

You can accomplish this by following these steps:

body{
  height: 100vh;
  overflow: hidden;
}
.navbar{
  height: 120px; // adjust as needed
  // additional styles here
}
.content{
  height: calc(100vh - 120px);
  // additional styles here
}

I hope this solution works for you!

Answer №3

To enable scrolling for a specific element, you can implement the following CSS setups: use overflow:hidden in the container and overflow:auto for the scrollable content as advised by @satyr.

If you want to make a scrollable element within a non-scrollable full page layout, adjust your html and body styles to cover the whole page, suggested by @cs.matyi.

For a more complex layout with fixed and scrollable elements, combine both techniques like this:

<!DOCTYPE html>
<html>

<head>
  <title>Layout</title>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    html,
    body {
      height: 100%;
      margin: 0;
    }
    
    .flexvertical {
      display: flex;
      flex-direction: column;
    }
    
    .flexhorizontal {
      display: flex;
      flex-direction: row;
    }
    
    .space-between {
      justify-content: space-between;
    }
    
    .align-center {
      align-items: center;
    }
    
    .overflow-hidden {
      overflow: hidden;
    }
    
    .overflow-auto {
      overflow: auto;
    }
    
    .padding {
      padding: 1rem;
    }
    
    .reverse {
      color: white;
      background-color: black;
    }
  </style>
</head>

<body class="flexvertical">
  <header class="flexhorizontal space-between align-center reverse padding">
    <img src="https://imageholdr.com/220x60/transparent/84c286?text=layout&font-size=32&font-family=stiljaFree">
    <h1>Layout test</h1>
    <nav><a href="/">Home</a> | <a href="#">Things</a> | <a href="#">Stranger things</a> | <a href="#">Other things</a>
    </nav>
  </header>
  <main class="flexvertical overflow-hidden">

    <h1 class="padding">Page Heading</h1>

    <article class="padding overflow-auto">
      <h2>Article heading</h2>
      <p>Scrollable Article content</p>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quod aspernatur, non nesciunt recusandae, temporibus consectetur harum ullam provident iste laboriosam porro nulla est necessitatibus placeat cum unde ut architecto.</p>
     ...
    </article>
  </main>
  <footer class="reverse padding">
    Copyright &copy; 2023. All Rights Reserved.
  </footer>
</body>

</html>

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

How can I ensure the information saved in MongoDB appears on an HTML page using mustache?

I have been working on a web application that is supposed to display data from a MongoDB database onto an HTML page using Mustache. However, I am facing an issue where nothing is being displayed when the program runs. I'm wondering if I missed importi ...

Modify the Full Calendar top navigation to display a list of items instead of buttons

Currently working with FullCalendar, I have a couple of tasks that I'm looking for the best solution to: I need help repositioning the right header all the way to the left Right now, the header options are shown as inline-block buttons. I would like ...

Update the iframe located on a different webpage

I am looking to create a dynamic button feature on my website where clicking a button on page1.html will open a new page (portal.html) with an iframe displaying the URL specified in the button. The setup involves two main pages: page1.html, and portal.htm ...

Error encountered - registration form

<?php $mysqli = mysqli_connect('localhost', 'test', 'test123', 'test'); if ($mysqli->connect_errno) { echo "<p>An error occurred while trying to connect: {$mysqli->connect_error}</p>"; ...

Counting the "Children Count" of a parent element in a collapsible tree diagram using D3

I have been working on creating a collapsible tree chart using D3 js, and I have managed to meet some of the requirements. However, I am looking to add the functionality of displaying the number of children for each parent node. Here is an example: Click ...

Colorbox scalePhotos function malfunctioning

The scalePhotos option in Colorbox does not seem to be working correctly for me. I have tried various methods to set it, including initializing Colorbox using the following code snippet right before the closing </body> tag in my HTML: jQuery('a ...

Aligning the content in the center of a div with inline display

Hi there, I have a quick question for the experts out there. I'm new to all of this so please bear with me :) I'm attempting to center an inline social sharing div on my website, but it's proving to be quite challenging. The div is nested w ...

I need to display 5 columns in a parent component, each with its own unique icon. How can I conditionally render them in a React component?

Creating a parent component to reuse multiple times can be very useful. In my case, I have included 5 different icons in each instance of the component, all taken from hero icons. I have set up an object with unique ids for each child component, but I am ...

Automatically changing the page's background color through animation upon loading

Similar Question: jQuery animate backgroundColor Can I make the background-color of a specific element change gradually when the page loads? I have a webpage with content like this: <ul> <li> Some stuff</li> <li> Some ...

Adjusting the flex columns to be mobile-friendly for various screen sizes, I noticed that the HTML text is spilling over into the adjacent section and the columns appear to

Hi everyone, I've been diligently working on my portfolio website and I'm thrilled that it appears responsive on mobile devices, my Macbook Air, and both of my desktop monitors. However, I've encountered an issue on my Lenovo laptop where th ...

Technique for ensuring consistent kerning across various browsers

Seeking advice on implementing kerning technique for a logotext in the <header>, ensuring cross-browser compatibility. Using this helpful tool created by Mr. Andrew (special thanks), I found a solution. Prior to the modification, the <header> ...

Authentic clear space within a div, showcasing only the background without any text present

My dilemma involves a DIV with a background, text within it, and another FIXED DIV positioned on top of the first DIV with a background. When I apply a color to the FIXED DIV, the text appears behind it. If I don't give a color to the DIV (and want it ...

Customized selection groups for dropdown menu based on alphabetical order

I am dynamically generating a select list from an array of data and I want to group the options alphabetically. For example, here is the data: data = [ ['bcde','21254'], ['abcd','1234'], ['abcde',' ...

Distinct styling for the start and subsequent lines of an anchor element using ::before pseudo-element

I'm currently working on a project that requires adding a decoration in front of some anchor links (A). Right now, I am using ::before to achieve this. However, there is an issue where some of the links will line-break and the second line and subseque ...

Refresh webpage based on conditions - JavaScript

Is there a way to automatically refresh a web page every 3 seconds, but also have the ability to stop the refreshing and trigger an alert message if certain form elements reach specific values? As shown in the image below: The batsmen's score will b ...

Adjusting the size of a division element to match the dimensions

Currently, I am still in the process of finalizing the remainder of the page, but I have encountered a minor issue. The problem lies in my image div not matching the height of the container. Although the image itself is the correct size, the image div appe ...

Positioning SVGs within a parent container while maintaining responsiveness

How can I anchor an SVG overlay with a circle in the bottom right corner while expanding the rest of the SVG to fill the remaining area of the container without distorting the circle? I've been able to position the SVG in the bottom right corner, but ...

What are the reasons for avoiding placing CSS code directly within HTML?

Situation: My website is dynamically served, with all CSS and javascript directly embedded into the HTML document to optimize speed. Advantages: Reduces web requests Fewer files downloaded Speeds up webpage loading time Disadvantages: Potential cachin ...

A feature to reveal additional content when it extends beyond a single line

<div class="questionnaire-description col-xs-12" id="questionnaire_description_wrapper"> <text-truncate> <span ng-class="questionnaire_description.show_more ? 'full-description' : 'ph-ellips ...

Modify content of elements by clicking on them with jQuery

Currently, there is a button that triggers the loading of an HTML page into a div with the ID "myDiv". Once loaded, I would like to be able to change the text content of specific tags when they are clicked. For example, clicking on the text within an h2 ta ...