Header and sections with fixed positioning

The header has the fixed positioning property applied to it, causing the independent section to be impacted by this style rule.

header{
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    padding: 25px 40px ;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
 <header>
        <a href="#" class="logo">logo</a>
        <ul>
            <li>sdsdsd</li>
        </ul>
    </header>
    
    <section>
        adsdsadsdf
    </section>

Answer №1

This issue of header or navbar positioning is a common one that many websites face.

The problem arises when using the position: fixed property, as it takes the element out of the document flow, causing elements below it to be hidden behind.

One solution is to add a margin-top property to the section following the header, pushing it down.

Alternatively (and the recommended approach) is to use position: sticky on the header instead of position: fixed. This will provide similar functionality without removing the element from the document flow.

The revised code snippet would appear as follows:

header{
    position: sticky;
    top: 0;
    left: 0;
    width: 100%;
    padding: 25px 40px ;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
<header>
  <a href="#" class="logo">Logo</a>
  <ul>
    <li>List Item</li>
  </ul>
</header>
    
<section>
  Section Area
</section>

If you're interested in learning more about this topic, Kevin Powell has written an informative article - check it out here:

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

Tips for preventing redundant data entry in a table

Currently, my table displays like so: https://i.sstatic.net/iozOF.jpg The structure of the HTML for the table is as follows (only a snippet is shown, as the rest looks similar): <table class="table table-bordered table-condensed"> <tr> ...

What is the optimal choice: Using Stack with direction="horizontal" or employing display flex in React Bootstrap?

In my opinion, the two functions may seem similar, but from personal experience I tend to favor using display flex over Stack in react boostrap. I find that display flex offers more flexibility when it comes to spacing out objects. Would anyone be able to ...

Is React invoking a function?

Just starting out with React and experimenting with lists. Not sure why my code isn't working as expected - when I call the method, it just shows plain HTML. I've already tried wrapping it in <Liste /> tags, but that didn't help eithe ...

Concealing navigation options using CSS styling

A unique program I developed generates a tree structure that looks like this: <li class="linamesystem">Alternator</li> <ul class="boxfornamegroupsparts"> <li class="linamegroup"><a href="#top(1)">Alternator2</a> ...

The mega menu is malfunctioning differently across various browsers and causing issues with responsiveness

I've encountered a strange issue with my portfolio website. While everything works smoothly in Google Chrome, I'm experiencing problems with the mega menu responsiveness on other browsers like Firefox, Microsoft Edge, and Opera Mini. Visit the ...

What is the best method for deleting scripts to optimize for mobile responsiveness?

My current plugin.js file houses all my plugins for responsive design, but it is unnecessarily large and cumbersome for mobile devices. I am considering creating two separate plugin.js files to toggle between for mobile and desktop views. What are the r ...

Create a row that has a centered heading and a button aligned to the right using Bootstrap 4

I'm currently working with Bootstrap 4 and React. I'm attempting to centralize a h4 heading while also aligning a couple of buttons to the right, all within the same row. The issue I'm facing is that the h4 heading is only centered within th ...

Getting the URL of a linked Stylesheet within a web browser control

In my C# winform application, I am using a webbrowser control to load some text that includes the following HTML code: <head> <link rel="stylesheet" type="text/css" href="d:/git/ArticleScraper/reports/default.css"> </head> I want to ext ...

Using CSS/HTML to showcase rows in a vertical table format (or similar)

Can someone please help me with formatting my tabular data in a specific way? column A | column B | column C | column D | ------------------------------------------- 1-1 | 2-1 | 3-1 | 4-1 | ------------------------------------------- 1 ...

Do you understand why my Fabricjs canvas is rejecting a rectangle?

http://jsfiddle.net/a7ZSG/10/ check out the link to the jsfiddle for more information. Having trouble getting a red rectangle to appear in a green canvas? Can anyone figure out why it's not showing up? I've attempted using window.onload around a ...

Tutorials for sharing an Excel dashboard on a webpage

Is there a way to publish my Excel sheet on a webpage with the same zoom level as in Excel so that it can be viewed completely on one screen without having to use a slider? ...

The anchor tag <a> is configured for inline display but is unexpectedly occupying the entire space

I am currently incorporating Bootstrap into my HTML, however, the <a> tag is behaving like a block display despite the fact that I have specified the CSS to be display:inline; <!doctype html> <html> <head> <link rel="styleshee ...

Struggling to make the right-side margin function correctly on a fixed navbar using a grid layout

Currently, I have successfully implemented a sticky top navbar. The issue arises when I try to add a 15vw margin to the right side of the logo image, similar to what I have done on the left side. Despite my attempts, it doesn't seem to work and I&apos ...

Persisting Big IndexedDB in the Browser

As we embark on the development of a Line of Business (LOB) HTML5 web application, our key objective is to ensure that the application has offline capabilities. Our plan involves retrieving a substantial amount of SQL data from the server and storing it in ...

Is there a way to keep a section of an animated class in place?

My website features an animated retractable menu bar with two separate menus. Clicking on one icon activates a sliding motion to reveal that particular menu while causing the second menu to retract in a similar fashion. To achieve a smooth transition effec ...

Tips for creating a synchronous jQuery "$.post" request

After much research and effort, I have finally come to the last item on my bug fix list. This one involves creating a function that will return true or false to indicate whether the validation was successful. My approach involves using ajax to compare cer ...

The alignment of divs can be disrupted in a Nested Grid system

I am working with a 16 grid system where I have two 8 column divs positioned side by side. In one of the 8 column divs, I am trying to include a 5 column div on the left and two three columns divs stacked on top of each other on the right. I have tried v ...

Tips for obtaining the base URL using JavaScript

As I construct a website using the CodeIgniter framework, I utilize the base_url helper function to load various resources. For example: <link rel="stylesheet" type="text/css" href="'.base_url('assets/css/themes/default.css').'" id= ...

What causes the variation in results when I input CSS directly into the head section versus linking it from a separate .css file?

As a beginner, I am facing an issue with my CSS. When I directly insert the CSS into the head of my .html file, everything looks perfect. However, when I link my .css file into the head, the very first word ("Holy") does not display properly. I have double ...

Issues with Ruby image styles causing improper resizing of images

Looking for some guidance on how to address this issue. I added the following code to post.rb has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" } validates_attachment_content_type :image, content_type: /&b ...