css issue with overflow auto on fixed grid menu

Hey there! Currently, I am facing a challenge involving the implementation of CSS grid.

The only issue remaining is making the top menu, which has a fixed width of 520px and a fixed position, scrollable horizontally on smaller devices. Could you provide some advice?

Despite trying overflow-x:auto;, it seems to not be effective in this particular case...

<!--html-->
<main>
   <div class="main-menu-container">
      <ul class="main-menu">
          <li class="main-menu__item"><a href="#" class="main-menu__link js-display-grid-items" >ALL</a></li>
          <li class="main-menu__item"><a href="#" class="main-menu__link js-display-grid-items" data-griditem="a">A</a></li>
          <li class="main-menu__item"><a href="#" class="main-menu__link js-display-grid-items" data-griditem="b">B</a></li>
          <li class="main-menu__item"><a href="#" class="main-menu__link js-display-grid-items"data-griditem="c">C</a></li>
          <li class="main-menu__item"><a href="#" class="main-menu__link js-display-grid-items" data-griditem="d">D</a></li>
          <li class="main-menu__item"><a href="#" class="main-menu__link js-display-grid-items" data-griditem="e">E</a></li>
      </ul>
  </div>
  <section class="main-grid">
      <article class="main-grid__item main-grid__item--type-a is-visible">A</article>
      <article class="main-grid__item main-grid__item--type-c is-visible">C</article>
      <article class="main-grid__item main-grid__item--type-b is-visible">B</article>
      <article class="main-grid__item main-grid__item--type-e is-visible">E</article>
      <article class="main-grid__item main-grid__item--type-b is-visible">B</article>
      <article class="main-grid__item main-grid__item--type-d is-visible">D</article>
      <article class="main-grid__item main-grid__item--type-a is-visible">A</article>
      <article class="main-grid__item main-grid__item--type-b is-visible">B</article>
      <article class="main-grid__item main-grid__item--type-c is-visible">C</article>
      <article class="main-grid__item main-grid__item--type-d is-visible">D</article>
      <article class="main-grid__item main-grid__item--type-a is-visible">A</article>
      <article class="main-grid__item main-grid__item--type-e is-visible">E</article>
  </section>
</main>
<script src="js/site.js" type="text/javascript" ></script>

CSS:

body {
padding: 0;
margin:0;
}

main {
max-width: 940px;
margin: 0 auto;
font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
position: relative;
padding: 0 15px;
box-sizing:border-box;

}
.main-menu-container {
position: fixed;
width: 910px;
top:0;
float:left;
height: 40px;

}
.main-menu {
display: grid;
list-style: none;
grid-template-columns: repeat(6, 1fr);
grid-gap: 20px;
padding: 0;
overflow-x: auto;
margin:0;
padding: 1em 0;
background: #fff;
width: 100%;
height: 100%;

}

.main-menu__link {
display: block;
text-align: center;
padding:0.4em;
background-color: #9A9B9E;
color: #fff;
text-decoration: none;

}

https://codepen.io/angusgrant/pen/Xqzrry

Answer №1

The demonstration showcases the main menu (.main-menu) with a width of 910px.

.main-menu {
    display: grid;
    list-style: none;
    grid-template-columns: repeat(6, 1fr);
    grid-gap: 20px;
    padding: 0;
    position: static;
    overflow-x: auto;
    margin: 0;
    padding: 1em 0;
    background: #fff;
    width: 910px;     <----------
    height: 100%;
}

Additionally, the container encompassing this menu (.main-menu-container) is also allocated a width of 910px.

.main-menu-container {
    position: absolute;
    width: 910px;     <----------
    top: 0;
    left: 15px;
}

Due to their identical widths, no overflow scenario can occur irrespective of screen size.

To address an overflow circumstance, modify the parent element as follows:

.main-menu-container {
    position: absolute;
    /* width: 910px; */
    top: 0;
    left: 15px;
    width: 100%;       /* new */
    overflow-x: auto;  /* new */
}

revised codepen

Answer №2

To adjust the responsive width of the element with class ".main-menu-container", you must update its width independently from the main content since it is fixed. Additionally, don't forget to specify the width for the "li" elements as they adapt to the parent's width and might cause automatic scrolling in narrower spaces. Best regards,

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

The Bootstrap 4 navbar appears slim on medium and smaller sized screens

Currently, I am using Bootstrap 4 in combination with Rails 5 to create a collapsing navbar. The functionality of the collapse is working properly, but the size of the collapsed navbar seems to be unusually small. You can see the issue in this screenshot: ...

The changing of colors does not function properly when clicked in JavaScript

I am having an issue with a drop-down list that offers two options: blue and green. When I select blue and click on the text input field, its background color alternates between blue and black (the default text field color). The same applies when I choose ...

Adjust the width of menu options using jQuery

One issue I am facing is with the menu on my homepage. I would like the menu options to enlarge upon hover, and while I have achieved this effect, there is a flaw in the animation: When I move my cursor away before the animation completes, the option abru ...

Learn how you can store checkbox values in an array when a checkbox is clicked by utilizing AJAX

Creating checkboxes in HTML <input type="checkbox" name="options[cid]" value='1' onChange="chkdeptCount(this.value)" class="test"> <input type="checkbox" name="options[cid]" value='2' onChange="chkdeptCount(this. ...

The checkboxes do not appear to be updating visually when the "checked" property is toggled programmatically

Check out this example on JSFiddle for a demonstration. Clicking on a checkbox should cause all the neighboring checkboxes to toggle on or off. Even though the "checked" property is toggling, there doesn't seem to be any visual change. n.prop("chec ...

What is the best way to display a loading screen while simultaneously making calls to multiple APIs?

I'm currently working with Angular 8 to develop an application that retrieves responses from various APIs for users. The application is designed to simultaneously call multiple APIs and I require a loading indicator that stops once each API delivers a ...

Troubleshooting a CSS problem within mat-autocomplete component in Angular 7

While using mat-autocomplete, I noticed that when I select an option from the dropdown and then scroll through the main bar, the dropdown menu does not move along with the autocomplete input field. view image here Here is the code snippet: <td width ...

Exploring the power of colors in Tailwind CSS and Material UI for your CSS/SCSS styling

Discovering unique color palettes like the Tailwind Color for Tailwind CSS and theMaterial UI colors has been inspiring. These collections offer a range of beautifully named colors from 100 to 900 and A100 to A400, reminiscent of font styles. I am intrig ...

Can CSS actually generate accurate inches?

Can you accurately set the width of an element, like a div, in inches and expect it to maintain that width across different devices? Or will it simply scale at a ratio like 1in = 96px? Edit: Try using CSS to set an element's width in inches and then ...

What is the most effective method for optimizing websites that do not respond to changes in window size

I've developed a website that is not responsive (it's more of an "experimental/artistic" site with a lot going on the screen, making it difficult to make it responsive..) I have decided not to cater for mobile phones, but I would like the site t ...

I encountered an issue where my style.css file was not properly linking to my HTML code. Whenever I saved the file, it would open

I'm a beginner in the world of HTML/CSS. When I save my style.css file, it shows up as a Notepad+ settings file and doesn't seem to work with my HTML. Can anyone advise me on what steps I should take? ...

The CSS isn't functioning correctly in the HTML file as it is in the CSS file

When I added this code directly to my html file, it worked perfectly: <style type="text/css" media="screen"> #headerimg { display: block; background-image: url('/Content/images/epp/ebweblogo1.gif'); background- ...

Animating a group of images with JQuery

Hey there! I've been working on a simple animation for my webpage, but I'm having some trouble getting it to work the way I want. You can check out my page at . What I'm trying to achieve is having each image appear at its final position an ...

What could be causing my sticky positioning to not function properly when I scroll outside of the designated

My website is organized into sections, with each section corresponding to a hyperlink in the navigation menu. I've applied the CSS property position: sticky to my navbar, which works as expected within the section bounds. However, I want my red navbar ...

What is the best way to design a Global Navigation menu for websites?

For example, I am looking to integrate a Navigation menu into my website using just one file. I have considered using PHP or creating an HTML frame, but I am wondering what the current industry standard is for professionals. Any insights? ...

Seeking assistance with setting up BxSlider installation

I'm struggling with adding bxslider to my HTML template. No matter what I try, the slider doesn't work as expected. When I preview my website, all three images are displayed together in a single column. Can someone please guide me on how to fix t ...

Innovative dropdown menu featuring images and informative text snippets

I have a question about creating a menu similar to the one on http://www.resellerclub.com that includes images and text details. Are there any ideas on which technologies were used or if there are any commercial solutions available for this? Thank you. ...

Angular onscroll event creating a parallax effect

I attempted to create a parallax effect using Angular and the OnScroll event, however, while scrolling, the text seems to be flickering. Is there a way to make the smooth rendering onscroll? Maybe through CSS alone? Here is the script I used: https://sta ...

"Glowing orbs in the night: a dark mode spectacle with

I have implemented a night mode (or perhaps dark mode) toggle button located at the top-right corner of my webpage. Here is a link to a perfect example showcasing what I am aiming for However, unlike the provided link, I switch between night mode and lig ...

Positioning the subheading "perfectly beneath the main heading" for a clean and professional look

I'm feeling a bit lost when it comes to tasks like this. While I can easily design an entire website using html and css, I start to question my approach when faced with challenges like these. Could someone offer some guidance? My goal is to have a H1 ...