The Wayback Machine - https://web.archive.org/web/20240712022411/https://www.geeksforgeeks.org/pure-css-responsive-horizontal-to-scrollable-menu-layout/
Open In App

Pure CSS Responsive Horizontal-to-Scrollable Menu Layout

Last Updated : 27 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Horizontal-to-Scrollable menu is a type of responsive menu bar in Pure.CSS, which changes the horizontal menu bar to a horizontally scrollable menu bar. It is used to make the menu bar responsive when the width of the website increases or decreases from device to device, For example, Tablets, Smartphones, etc. The Pure.CSS Horizontal-to-Scrollable menu does not need any JavaScript functions, its inbuilt CSS classes are enough for it.

Classes: Below mentioned and described classes are required to create this layout.

  • pure-menu: It is the default class that generates the horizontal menu. It can contain the menu items and headings in the form of a  list.
  • pure-menu-horizontal: It is the class added to the default vertical ‘pure-menu’ to make it a horizontal menu.
  • pure-menu-list: It is the class for the unordered list that contains the menu items. The list items inside this list must have the class ‘pure-menu-item’.
  • pure-menu-item: It is the class added to the list items inside this list.
  • pure-menu-heading: It is the class that is added for the headings inside or outside the menu list. By default, it capitalizes the text inside.
  • pure-menu-scrollable: It is the class that makes the pure-menu-horizontal be scrolled or flicked when the width of the website is not fit to the menu size.
  • pure-menu-link: It is the class that is added to the links inside the menu items.

Syntax:

<div class="pure-menu pure-menu-horizontal pure-menu-scrollable">
    .....
</div>

Example 1: This example demonstrates a simple Horizontal-to-scrollable menu bar.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Responsive horizontal-scrollable menu</title>
    <link rel="stylesheet"
          href=
          integrity=
"sha384-nn4HPE8lTHyVtfCBi5yW9d20FjT8BJwUXyWZT9InLYax14RDjBj46LmSztkmNP9w"  
          crossorigin="anonymous">
    <style>
        .menu{
            background-color: green;
        }
        .menu h3, .menu a{
            color: white;
        }
        a:hover{
            background-color: white;
            color: green;
        }
        body{
            background-image: url(
            background-repeat: no-repeat;
            background-position: center;
            background-attachment: fixed;
        }
    </style>
</head>
<body>
    <div class="pure-menu pure-menu-horizontal
              pure-menu-scrollable menu">
        <h3 class="pure-menu-heading">GeeksforGeeks</h3>
          <ul class="pure-menu-list">
            <li class="pure-menu-item">
              <a href="#" class="pure-menu-link">
                Home
              </a>
            </li>
            <li class="pure-menu-item">
              <a href="#" class="pure-menu-link">
                HTML
              </a>
            </li>
            <li class="pure-menu-item">
              <a href="#" class="pure-menu-link">
                CSS
              </a>
            </li>
            <li class="pure-menu-item">
              <a href="#" class="pure-menu-link">
                Javascript
              </a>
            </li>
            <li class="pure-menu-item">
              <a href="#" class="pure-menu-link">
                React
              </a>
            </li>
            <li class="pure-menu-item">
              <a href="#" class="pure-menu-link">
                Angular
              </a>
            </li>
            <li class="pure-menu-item">
              <a href="#" class="pure-menu-link">
                Nodejs
              </a>
            </li>
            <li class="pure-menu-item">
              <a href="#" class="pure-menu-link">
                Express
              </a>
            </li>
            <li class="pure-menu-item">
              <a href="#" class="pure-menu-link">
                Mongodb
              </a>
            </li>
            <li class="pure-menu-item">
              <a href="#" class="pure-menu-link">
                About
              </a>
            </li>
            <li class="pure-menu-item">
              <a href="#" class="pure-menu-link">
                Contact
              </a>
            </li>
          </ul>
    </div>
</body>
</html>


Output:

Example 2: The following code demonstrates that when the website width decrease to 72em, the horizontal-to-Scrollable list items in the menu bar will separate from the heading. We should use @media() rule to detect the changes in the width of the website. The code under the @media() rule will execute when the condition becomes “true”.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Responsive horizontal-scrollable menu</title>
    <link rel="stylesheet"
          href=
      integrity=
"sha384-nn4HPE8lTHyVtfCBi5yW9d20FjT8BJwUXyWZT9InLYax14RDjBj46LmSztkmNP9w"  
    crossorigin="anonymous">
    <style>
        .menubar {
            background-color: green;
            position: relative;
        } 
        .menu {
            display: inline-block;
            width: auto;
            vertical-align: middle;
        } 
        .menu a {
            color: white;
        } 
        .menu a:hover {
            background-color: white;
            color:green;
        }
        .website_name {
            display: block;
            text-align: center;
            position: relative;
        }
        body{
            background-image: url(
            background-repeat: no-repeat;
            background-position: center;
            background-attachment: fixed;
        }
       /* To toggle the normal menu bar to
          Horizontal-scrollable menu bar
          when the window size reduced to particular size*/
        @media (max-width: 72em)
        {
           .menu
            {
              display:block;
            }
            .menubar_top
            {
                position: relative;
                padding: 0.5em;
            }
            .scrollable_links
            {
                position: absolute;
                width: 100%;
                background-color: transparent;
                background-color: black;
            }
        }
    </style>
</head>
<body>
    <div class="menubar">
        <div class="pure-menu menu menubar_top">
            <a href="#" class="pure-menu-heading website_name">
              GeeksforGeeks
            </a>
        </div>
        <div class="pure-menu pure-menu-horizontal pure-menu-scrollable
                    menu scrollable_links">
            <ul class="pure-menu-list">
                <li class="pure-menu-item">
                   <a href="#" class="pure-menu-link">
                     Home
                   </a>
                </li>
                <li class="pure-menu-item">
                   <a href="#" class="pure-menu-link">
                     HTML   
                   </a>
                </li>
                <li class="pure-menu-item">
                   <a href="#" class="pure-menu-link">
                     CSS
                   </a>
                </li>
                <li class="pure-menu-item">
                   <a href="#" class="pure-menu-link">
                     Javascript
                   </a>
                </li>
                <li class="pure-menu-item">
                   <a href="#" class="pure-menu-link">
                     React
                   </a>
                </li>
                <li class="pure-menu-item">
                   <a href="#" class="pure-menu-link">
                     Angular
                   </a>
                </li>
                <li class="pure-menu-item">
                   <a href="#" class="pure-menu-link">
                     Nodejs
                   </a>
                </li>
                <li class="pure-menu-item">
                   <a href="#" class="pure-menu-link">
                     Express
                   </a>
                </li>
                <li class="pure-menu-item">
                   <a href="#" class="pure-menu-link">
                     Mongodb
                   </a>
                </li>
                <li class="pure-menu-item">                  
                   <a href="#" class="pure-menu-link">
                     About
                   </a>
                </li>
                <li class="pure-menu-item">
                   <a href="#" class="pure-menu-link">
                     Contact
                   </a>
                </li>
            </ul>
        </div>
    </div>
</body>
</html>


Output:



Previous Article
Next Article

Similar Reads

Pure CSS Responsive Horizontal-to-Vertical Menu Layout
To create a responsive horizontal-to-vertical menu layout using only CSS, you can use media queries to apply different styles to the menu based on the width of the viewport. Example 1: This will create a horizontal menu with evenly distributed menu items on larger screens, and a vertical menu with full-width menu items on smaller screens. You can a
3 min read
Pure CSS Scrollable Horizontal Menu
When there isn't enough room, the scrollable horizontal menu items can be used. It gives responsiveness to the web pages. To create a scrollable horizontal menu, add the pure-menu-scrollable class. Pure.CSS provides vertical menus by default. You can change the vertical menu to a horizontal menu by adding the class name pure-menu-horizontal dropdow
3 min read
Pure CSS Responsive Side Menu Layout
Pure CSS is a free and open-source CSS framework. It is a set of tools for building responsive websites and web applications. This was created by Yahoo and is used to create websites that are more responsive, attractive, and speedy. It serves as a strong substitute for Bootstrap. Pure CSS was created with consideration for responsive design. We thu
5 min read
Pure CSS Scrollable Vertical Menu
Scrollable vertical menus are very useful to make our web pages compact, as it may not be feasible to show all the list items which would take up the entire height of the viewport. Let us create a scrollable vertical menu using pure CSS. Below mentioned and described the classes that are required to create this layout. pure-menu: It is the default
2 min read
How to Create Scrollable Horizontal Menu using CSS ?
In this article, we are going to implement a Scrollable Horizontal Menu using CSS. Approach: Scrollable Horizontal Menu is used to scroll the horizontal navbar using CSS "overflow:auto" and "white-space: nowrap". These two attributes are used to scroll the horizontal menu. To implement the scrollable horizontal menu You have to add HTML.You have to
1 min read
Pure CSS Horizontal Menu
We come across horizontal menus on almost every website. Pure CSS helps us implement such menus with ease by using the pure-menu class. Pure-menu class by default creates a vertical menu which we can simply convert into a horizontal one by adding the class pure-menu-horizontal. One of the most common uses of a horizontal menu is in navigation bars
2 min read
Design a Vertical and Horizontal menu using Pure CSS
Menu is a very important component in any website. It is a user interface element within a webpage that contains links to other sections of the website. It can be displayed horizontally or vertically before the main content of the webpage or header. For Creating Vertical Menu: Menus are vertical by default in Pure CSS. It is easy to customize becau
2 min read
How to create horizontal scrollable sections using CSS ?
In this article, we will see how we can create a horizontal scrollable section using CSS. We are going to make different sections and make them horizontally scrollable using CSS. HTML code is used to create the basic structure of the sections and CSS code is used to set the style. HTML Code: In this section, we will create a structure for our secti
3 min read
How to make horizontal scrollable in a bootstrap row?
Here is the task to make horizontally scrollable in a bootstrap row. It can be done by the following approach: Approach: Making all the div element in inline using display: inline-block; property Adding the scroll bar to all the div element using overflow-x: auto; property. white-space: nowrap; property is used make all div in a single line. Exampl
1 min read
Pure CSS Table with Horizontal Borders
A table is an arrangement of data in rows and columns, or possibly in a more complex structure. Tables are widely used in communication, research, and data analysis. In Pure CSS, we will add a "pure-table" class to add styles to the table. This class adds padding and borders to table elements and emphasizes the header. A table with Horizontal Borde
2 min read
Article Tags :
three90RightbarBannerImg