issue with mobile devices not properly supporting position: fixed

I'm currently working on a responsive website and have encountered an issue with the topbar. When the website switches to mobile layout, the topbar scrolls down a bit before sticking to the top of the screen as intended. This causes misalignment in the layout. Strangely, this problem does not occur in the desktop layout.

#topbar {
  font-weight: 300;
  background-color: white;
  z-index: 999;
  position: fixed;
  width: 100%;
  height: 15%;
  box-shadow: 0 5px 10px rgba(124, 124, 124, 0.3);
}

@media (min-width: 35em) {
  #topbar {
    height: 60px;
  }
}
<html>

<head>
  <link href='stylesheet.css' rel='stylesheet' />
  <script defer src='main.js'></script>
  <meta name='viewport' content='width=device-width, initial-scale=1.0' />
</head>

<body>
  <section id='topbar'>
    <nav>
      <h1 id='logo'>Logo</h1>
      <ul id='links'>
        <li>
          <a href='#home'>
            <h1>Home</h1>
          </a>
        </li>
        <li>
          <a href='#about'>
            <h1>About</h1>
          </a>
        </li>
        <li>
          <a href='#products'>
            <h1>Products</h1>
          </a>
        </li>
        <li>
          <a href='#jobs'>
            <h1>Jobs</h1>
          </a>
        </li>
      </ul>
      <div id='menu-button'>
        <div id='line1'></div>
        <div id='line2'></div>
        <div></div>
      </div>
    </nav>
  </section>
</body>
</html>

Answer ā„–1

To ensure the top header (Logo section) remains fixed at the top and have the navigation start after it, you may need to adjust the structure of your webpage. If the Logo section spans the entire width, the navigation section will naturally appear below it.

Here are the modifications I suggest:

  1. Separate the logo section and navigation section into different divs but make them siblings so that the navigation appears beneath the logo.

  2. Add 'top: 0' to the fixed logo section to keep it consistently positioned at the top.

  3. Apply a margin-top equal to the height of the logo section to the navigation section to ensure its visibility.

<html>
  <head>
    <style>
      #topbar {
        top: 0;
        font-weight: 300;
        background-color: white;
        z-index: 999;
        position: fixed;
        width: 100%;
        margin-bottom: 49px;
        height: 60px;
        box-shadow: 0 5px 10px rgb(124 124 124 / 30%);
      }
      #links {
        margin-top: 60px;
      }
      body {
        margin: 0;
      }
      @media (min-width: 35em) {
        #topbar {
          height: 60px;
        }
      }
    </style>
    <script defer src="main.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  </head>
  <body>
    <div id="topbar">
      <h1 id="logo">Logo</h1>
    </div>
    <nav>
      <ul id="links">
        <li>
          <a href="#home"><h1>Home</h1></a>
        </li>
        <li>
          <a href="#about"><h1>About</h1></a>
        </li>
        <li>
          <a href="#products"><h1>Products</h1></a>
        </li>
        <li>
          <a href="#jobs"><h1>Jobs</h1></a>
        </li>
      </ul>
      <div id="menu-button">
        <div id="line1"></div>
        <div id="line2"></div>
        <div></div>
      </div>
    </nav>
  </body>
</html>

Answer ā„–2

It seems like the issue arises from neglecting to utilize

  • the top property
  • and the left properties

It is essential to include these two properties when using the position property.

Updated #Topbar with both properties

#topbar{
top: 0;
left: 0;
}

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

Display the current date on a webpage using a Google calendar integration

I am struggling to display only today's events in my HTML output using this code. When I remove the line "if ($date = date("Y-m-d")){", the code shows a specific number of events. How can I modify the code to show all events that occur on a single day ...

Seeking assistance in setting up a search view and integrating it into an HTML page with data from my Django database

Currently engaged in the development of a DJANGO website where users can input words and their associations for better recall. The word insertion feature has been successfully implemented. However, facing a roadblock while trying to set up the word search ...

Divs experiencing unexpected wrapping of text

I've encountered an unusual issue while working on a library I'm developing. The problem can be traced back to the following code snippet: <html> <body> <div style="position: absolute; left: 200px;"> ...

Using Angular to swap out the component's selector with HTML code

Consider the following component: @Component({ selector: 'passport-cell', templateUrl: './passport-cell.component.html', styleUrls: ['./passport-cell.component.scss'] }) export class PassportCell { @Input() public la ...

Tips for incorporating scrolling into a sub menu

I'm currently attempting to incorporate vertical scroll only for my sub-menu using CSS, without including a horizontal scroll. However, the issue arises when I remove the horizontal scroll - it causes the nested sub-menu within the initial one to bre ...

Leveraging CSS Modules alongside external packages

Below is the important section from my webpack.config.js file: module: { loaders: [ { test: /\.css$/, exclude: /node_modules/, loaders: [ "style-loader?sourceMap", "css-l ...

Issue with aligning items vertically using CSS and Javascript

I must admit, I am not well-versed in CSS. My goal is to create a performance bar using CSS and Javascript. So far, I have managed to generate a background bar with another bar inside that fills up to a specific percentage. However, my issue lies in the fa ...

Should you include the dollar sign in a Vue HTML variable or not?

Iā€™m a bit confused about whether or not I should include $ when using a Vue HTML variable: new Vue({ data: { a: "myData" } }); Do I need to use: <h1>My value is {{ a }}</h1> or <h1>My value is {{ $a }}</h1> What ...

Incorporating YouTube videos into Internet Explorer

On my webpage, I have included a YouTube video: <div class="col-md-8"> <iframe id="theframe" width="400" height="325" src="http://www.youtube.com/embed/MYYOUTUBECLIP" frameborder="0" allowfullscreen class=""></iframe> While this works p ...

A comprehensive guide on associating a JavaScript function with an element attribute

I am looking for a way to assign a JavaScript function to an HTML attribute. For example: <li data-ng-repeat="job in jobList" class= dynamicClass(str) data-filter = "dynamicFilter(str)"> The reason I want to do this is because the class name and ...

How can I include text input within a select option using angular-material?

I am in the process of developing a form that allows users to input personal information. One feature I would like to include is a dropdown list for phone numbers and fax numbers associated with the individual, giving users the option to choose from thos ...

How can I display only a horizontal scrollbar on the body content while avoiding an excessively long image header?

Currently, I have a 4000px width image as the header of my website. To hide the horizontal browser scrollbar, I am using the following CSS: html { overflow-x: hidden; } However, this method results in the horizontal scrollbar never appearing. I ac ...

In Bootstrap 4, the positioning of :after is different compared to Bootstrap 3

Our bootstrap nav-tabs have a unique default look with an 'indicator' icon below the selected tab. This is achieved by styling the active anchor element as follows: https://i.sstatic.net/UQos5.png However, when migrating to bootstrap 4, we noti ...

Steps for Aligning the Label and Textbox Horizontally

<div class="form-group row"> <div class="col-6 "> <h4> <label class=""> Can you meet the required amount of money? @(HelpSeeker.money_needed= He ...

Tips for showcasing numerous images in a single row on a website

Looking to organize images into rows with 3/4 images in each row, not stacked vertically as shown below. Here is the code I have tried: <html> <head> <title>Images Displayed in Rows</title> <meta charset="utf-8"> <meta ...

The image slide change feature ceases to function properly when incorporating two separate functions

I have a function that successfully changes the image of a container every 5 seconds. However, I am facing an issue when trying to add 2 containers side by side and change their images simultaneously. When I run the functions for both containers, only one ...

Adjust the height of the svg

My page contains an SVG that is impacting the height of its container and causing other elements to move. Is there a way to adjust the height of the SVG? Currently, the SVG is a square but I would prefer it to be a rectangle as the height is too large. &l ...

What is the syntax for creating IF ELSE statements within an ASP.NET document?

I'm encountering an issue with implementing if statements in ASP.NET C# web pages. I am trying to achieve the following functionality in asp, similar to what is done in PHP. <?php if(1==1){ ?> <h1>Hello World!</h1> <?php }else ...

Selecting an element from CSS using Jsoup

Is it possible to retrieve all images that do not have the first class attribute as "content.slide0"? In my example, I am utilizing the Jsoup library to display selectable elements in a WebView. Elements element = doc.select("HERE_SOLUTION"); <head ...

Tweaking react-lottie and SVG dimensions with CSS media queries: A step-by-step guide

I am currently working on a React project that involves displaying Lottie-react animations and SVG's. My main concern is making sure that these illustrations are responsive on my website. The components are stored in a .js file, which I import into m ...