What causes the align-self property to malfunction with a navigation bar in a flex layout?

My attempt to create a sticky navigation bar has failed as it does not stay in place. I have been advised to use align-self: flex-start; with flex but unfortunately, it hasn't worked.

body { padding-bottom: 2000px;}
ul { list-style: none;}

.wrap {
 position: sticky;
 position: -webkit-sticky; 
 top: 0; 
 align-self: flex-start; 
}
.nav-bar { 
 display: flex; 
 flex-direction: row; 
 justify-content: flex-end; 
 background-color: #00aae4; 
 margin-bottom: 0;
 padding-right: 30px; 
 height: 50px; 
 margin-top:0; 
}
.nav-link { 
 padding-left: 0; 
 padding-right: 0; 
 background-color: #f1f1f1;
 width: 100px; 
 text-align: center;
 font-size: 20px; 
 margin-top: 5px; 
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<nav class="wrap">
          <ul class="nav-bar">
            <li class="nav-link">Luigis</li>
            <li class="nav-link">Menu</li>
            <li class="nav-link">Acerca de </li>
            <li class="nav-link">Contacto</li>
          </ul>
    </nav>
</body>
</html>

I have attempted various solutions such as changing the location of the align-self property within the ul's settings and using top. I also tried replacing the list with div classes, but none of these methods have produced the desired result.

Answer №1

According to the specifications:

The stickily positioned element is limited in movement by the intersection with the sticky-constraint rectangle's bottom, ensuring that the offset does not push the element outside its containing block. As the page is scrolled, the element appears pinned to the relevant flow root edges, similar to a fixed position element.

By default, your element is contained within the content-box rather than the padding-box. In this scenario, if you add significant padding, the sticky behavior will not be observed.

To achieve sticky behavior, increasing the height of the content-box can help.

body {
  height: 2000px;
}

ul {
  list-style: none;
}

.wrap {
  position: sticky;
  position: -webkit-sticky;
  top: 0;
}

.nav-bar {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  background-color: #00aae4;
  margin-bottom: 0;
  padding-right: 30px;
  height: 50px;
  margin-top: 0;
}

.nav-link {
  padding-left: 0;
  padding-right: 0;
  background-color: #f1f1f1;
  width: 100px;
  text-align: center;
  font-size: 20px;
  margin-top: 5px;
}
<nav class="wrap">
    <ul class="nav-bar">
      <li class="nav-link">Luigis</li>
      <li class="nav-link">Menu</li>
      <li class="nav-link">Acerca de </li>
      <li class="nav-link">Contacto</li>
    </ul>
  </nav>

  1. If an element has a 'position' of 'relative' or 'static', its containing block is determined by the content edge of the nearest block container ancestor box. ref

There might be confusion regarding the containing block for absolutely positioned elements being the padding box:

  1. If an element has 'position: absolute', the containing block is established by the nearest ancestor with a 'position' of 'absolute', 'relative', or 'fixed'. This process may vary based on whether the ancestor is an inline element or not:

    1. If the ancestor is an inline element, the containing block comprises the bounding box surrounding the padding boxes of the first and last inline boxes generated for that element. However, if the inline element spans multiple lines, the containing block is undefined according to CSS 2.1 rules.
    2. In other cases, the containing block is formed by the padding edge of the ancestor.

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

Ways to deactivate the submit button using Cookies, sessions, or local storage

Although cookies and storage can be deleted, they can still help prevent many human spammers who are unaware of this. How can I temporarily disable the form submit button on a specific page for just one day? I want to create a code that saves "Submitted" ...

Tips for managing data transfer between an Angular application and an HTML WYSIWYG editor

Considering using a node/express backend with MongoDB seems to be the way forward for this situation... My ultimate goal is to save the content from the editable div to the backend so it can be retrieved and displayed later - either in a non-editable HTML ...

"Bootstrap 4 introduces a new feature where adjusting the spacing between columns causes them to wrap underneath one another

My goal is to create two divs that each take up 6 columns with a space of 1px between them. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoA ...

CSS text links have an oversized click area in Chrome and Safari

It seems like there might be an issue in the CSS code that is causing Chrome and Safari to enlarge the hover areas on text links. This behavior is not occurring in Internet Explorer or Firefox. Any pointers on what could be wrong with the CSS? The mouse cu ...

Oops! The file or directory you are looking for does not exist. Please check the location and try again

This is the content of my server.js file: var express = require('express'), app = express(); app .use(express.static('./public')) .get('*',function (req,res) { res.sendfile('/public/main.html' ...

Strategies for Returning Multiple Values from a Function in JavaScript

Thanks in advance I am wondering how to extract multiple values from a code block using JavaScript. I am unsure if I need to use an array or something else as I am new to JS. Please consider the following HTML code: <div class="row"> ...

Error 403 CSRF forbidden encountered while utilizing the request.post.get() function within the views.py file linked to the HTML page

Here is the views.py code snippet: from django.shortcuts import render from django.http import HttpResponse from django.http import HttpRequest from django.db import models from datetime import datetime def postdata(request): if request.method == "PO ...

Guide to modifying CSS properties of an individual element by manipulating class names with JavaScript

I have been searching for an answer to my question without success. I have a unique challenge where I need to change the styles of an h1 element by adding a class based on which radio button is clicked. The issue I'm facing is that when I select a dif ...

Responsive Image Carousel with Bootstrap

I am facing an issue with the bootstrap carousel. When I upload an image with dimensions of 2000px X 500px to the slider, it looks great on desktop screens. However, when viewed on mobile devices, the slider becomes very small and appears at the top of the ...

Having trouble with loading background images in Angular 4?

After researching extensively on stack overflow, I am still struggling to resolve this issue. The main problem I am facing is related to adding a background image to the header tag in my code. Unfortunately, no matter what I try, the background image refu ...

JQuery not properly validating inputs with required attributes after creation

I am currently developing a contact form that includes an "alternative address" <div id='alt'> with toggleable visibility. Inside this div, there is a required <input> field. I encountered an issue where toggling #alt twice (thus hidi ...

Changing the color of a div element dynamically with JavaScript

Is there a way to improve the code below so that the background color of this div box changes instantly when clicked, without needing to click twice? function updateBackgroundColor(platz_id) { if(document.getElementById(platz_id).style.backgroundCol ...

Issues arise with the HTML application cache while in offline mode after clicking the refresh button

I have encountered a problem with my web app that has been developed using application cache. I am currently testing it on Windows Phone 8.1 with the IE 11 mobile browser. Everything works smoothly when the internet connection is turned off and the web a ...

The color of the Bootstrap input button does not display correctly

I'm looking to enhance my input button by adding a font awesome icon next to it. After some research, I came across this helpful answer and realized that option 1 is the best fit for my situation as I need to use it in a form to delete a repository. ...

What is the best way to create a fixed footer in Next.js using React?

I'm looking to create a fixed footer that will stay at the bottom of the page if there isn't enough content to fill it. I've been researching ways to achieve this using CSS, but many of the methods don't easily translate to React/Next.j ...

Guide on how to turn off a checkbox "toggle switch" using JavaScript

Being relatively new to CSS and HTML, I recently experimented with creating a checkbox toggle switch using this resource. I now have a specific requirement to disable this toggle switch upon clicking a different button labeled 'Reset', making it ...

Is Span responsible for creating a new line in this particular sentence?

I am struggling with aligning the following line of text that includes percentages like 100% and 99.8%. Is there a way to display all these elements in one line, side by side? <span style="font-size:12px;font-weight:bold;padding-left:800px;">99%& ...

Tips for keeping a span element from breaking onto a new line in responsive web design

Here is a simple list with h4 headings, each ending with a span element. <ul class="items"> <li> <h4>Avoid Line Break Of Plus <span class="goto">o</span></h4> </li> <li> <h4> ...

Learn how to modify parent element attributes based on changes made to its child elements using JavaScript

Is there a way to select the parent element of an element that does not have any identifier? I am looking to achieve the following: <td> <a title="mySweetTitle"/> </td> I need to access the "td" element using its child "a" in ord ...

How do I capture data using an input tag in Node.js and save it in MongoDB?

I need help with inserting data from an input form and submit button into Node.js and MongoDB. Can someone provide me with tips or links on how to do this? :) <form method="post" ...> <input placeholder="Name & Surname"> <button type= ...