Is there a way in Bootstrap to specifically modify CSS for the mobile menu?

Looking to style the mobile (hamburger) menu with CSS specifically for that view. Using @media screen and (max-width: 600px) seems like a messy solution to target only the mobile menu. Is there a way to create a style that applies solely to the mobile menu? For example:

.mobile-menu .nav-link {
  color: red;
  padding: 40px;
}

This would make only the mobile menu have red text and padded to 40px, while keeping the desktop menu unchanged. If there isn't a predefined style, what is the best approach to styling CSS exclusively for mobile menu items?

To provide context, here is the HTML code snippet:

<nav class="navbar navbar-expand-md">
  <a class="navbar-brand" href="#">
    <img class="img-fluid d-md-none" src="images/banner_logo_small.png">
    <img class="img-fluid d-none d-md-block" src="images/banner_logo.png">
  </a>
  <button class="navbar-toggler navbar-dark" type="button" data-toggle="collapse" data-target="#main-navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="main-navigation">
    <ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link" href="#">HOME</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">COMMUNITY SUPPORT</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">CONTACT</a>
      </li>
    </ul>
  </div>
</nav>

And the corresponding CSS code:

.navbar {
  background:#FF8200;
  padding: 0;
}
.nav-link,
.navbar-brand {
  color: #fff;
  cursor: pointer;
  padding: 0;
}
.nav-link {
  margin-right: 1em !important;
  font-weight: bold;
}
.nav-link:hover {
  color: #3D3935;
}
.navbar-collapse {
  justify-content: flex-end;
}
.d-md-none .navbar-collapse {
  padding: 200px;
  color: red;
}

The goal is currently to apply a padding of 200px and turn the text red for mobile menu items. You can experiment with a sample at , aiming to make mobile menu items red while keeping them white in desktop view.

The challenge lies in Bootstrap using the same items for both mobile and desktop menus, changing the appearance based on page width.

Answer №1

Consider implementing the following changes:

(I've adjusted the background-color of the navbar to green and increased padding in the navbar-toggler-icon with padding: 40px.)

(For more ideas on customization, check out these resources: Bootstrap 4 Change Hamburger Toggler Color & https://getbootstrap.com/docs/4.0/components/navbar/#color-schemes)

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <link href="style.css" rel="stylesheet" type="text/css" />
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</head>

<body>
  <nav class="navbar navbar-light" style="background-color: green;">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon" style="padding: 40px;"></span>
  </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown
        </a>
          <div class="dropdown-menu" aria-labelledby="navbarDropdown">
            <a class="dropdown-item" href="#">Action</a>
            <a class="dropdown-item" href="#">Another action</a>
            <div class="dropdown-divider"></div>
            <a class="dropdown-item" href="#">Something else here</a>
          </div>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled" href="#">Disabled</a>
        </li>
      </ul>
      <form class="form-inline my-2 my-lg-0">
        <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
      </form>
    </div>
  </nav>
  <script src="script.js"></script>
</body>

</html>

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 prevent a specific class from using CSS styling

My HTML <body> <div id="finalparent"> <!--many parent divs here--> <div id="1stparent"> <div id="txt_blog_editor" class="box" style="width: 1097px;"> <div class="abc anotherclass"> </div> & ...

Boosting Your SCSS (SASS) Productivity

I'm curious if there are any ways to make the code below more efficient or dynamic. I've already achieved the desired result, but I'm interested in optimizing it further. The Script Any suggestions would be highly appreciated, Cheers, ...

Decrease the gap between rows in CSS Grid

I'm struggling to minimize the space between the rows. I've attempted adjusting margins and paddings to 0, but nothing seems to be working effectively. Displaying desktop view on the left and mobile view on the right side. https://i.sstatic.net ...

Ways to prevent a page from scrolling

I'm currently working on a webpage that features a vertical drop-down menu. The issue I'm facing is that when the menu drops down, it causes the text below it to be pushed downwards and off the page. While this behavior is anticipated, it also tr ...

Step-by-step guide for implementing LoadGo animation with HTML

I'm in the process of loading my logo on my website's homepage. I found a LoadGo animation that I want to use, which can be downloaded Here Currently, the logo is set to load when a button is clicked. However, I would like it to load automatical ...

"Dynamic background image shifts with the movement of the mouse cursor

Is there a way to achieve an effect like this interactive background: I am looking to make the background image follow the mouse cursor. Is there a simple method to accomplish this without directly using the script from the example? I have attempted a so ...

What is the best way to combine a string with a variable in sass?

Is there a way to merge a string and a variable in Sass to form a variable name that is already present? $size: "sm"; $button-sm: 1rem; $buttom-md: 1.5rem; body { font-size: $button-#{$size}; } The desired output is: body { font-size: 1r ...

Achieving sliders similar to those in the "level" window of GIMP can be done by customizing the shape of GtkScale in your C program

I have a query regarding the shape of GtkScale. Specifically, I am interested in making my GtkScale resemble the one found in the levels window of Gimp. To clarify: a bar 3 triangles (representing shadows, midtones, and highlights) https://i.stack.imgur ...

Attempting to alter the animations depending on the color button that has been selected

Bootstrap radio buttons are being utilized. I want the animation to change when one of these buttons is selected. I am currently working on changing colors in JavaScript. I attempted to use a for each loop but encountered errors stating that it is not a ...

React - method for transmitting dynamically generated styles to a div element

As a newcomer to the world of React, I keep encountering an unexpected token error related to the ":". Can someone please assist me with understanding the correct syntax for including multiple styles within the Box component provided below? Additionally, h ...

How to set element height based on screen height rather than browser height

How can I set the height of a div container to be 60% of the screen height, but ensure it maintains that height even when the browser window is resized? Setting the height as 60% in CSS works initially, but the div shrinks proportionately when the window s ...

How to create nested nav menus that are optimized for touch-screen devices?

When it comes to creating responsive websites, one challenge I frequently encounter is designing a multi-level nav menu that functions effectively for touch devices. Many plugins and methods exist, but most fall short because they do not allow a second-l ...

What is the best way to delay the loading of an external CSS file in Next.js version 14?

To style math equations, I need to load the Katex CSS. The pages that require this styling are not predetermined, as it depends on which users include math in their posts. However, I do know which routes might potentially need it. For a better understandi ...

Utilizing Fullcalendar v5's sticky header feature alongside a Bootstrap fixed top navigation bar for a seamless

Currently experimenting with the latest version of fullcalendar 5.4.0. I have integrated the calendar into a bootstrap 4 web page that features a fixed navigation bar at the top. The issue arises when attempting to set stickyHeaderDates, aiming to keep th ...

CSS Troubleshooting: Image failing to load

Having an issue with bootstrap/css. I'm trying to load an image from the source folder using this CSS code: background: url('../img/home.jpg') no-repeat; Unfortunately, the image is not showing up in my home section on the page. https://c ...

Tips for aligning two cards in the center with Bootstrap

[site][1] [1]: https://i.sstatic.net/CmQRP.png Hey there, I'm trying to figure out how to center my two cards on the page. Can anyone help me with this? <div> <div class="card" style="width: 18rem;"> <img ...

Position two div elements and an hr tag to create a step progress bar

Check out my progress bar design https://i.sstatic.net/u9SRM.png How can I center the 'created' and 'assigned' divs below the circle while ensuring the 'hr' line touches the circle, and it is responsive across different scre ...

Is there a way to toggle the visibility of the angular material toolbar at regular intervals?

I'm currently experimenting with the CSS animation feature to display and conceal the angular material toolbar in this demonstration. Inside the application component, the hide attribute is toggled at intervals as shown below: hide:boolean = false ...

Using numerous background images can lead to issues in Safari browser

Currently experimenting with styling the body element using two images. background: url(bg2.png) repeat-x, url(bg1.png) repeat; An issue arises in Safari where it reports an error in the console, stating that it cannot locate the image file specified at ...

iScroll not working in conjunction with jQuery Mobile

I am looking to integrate iScroll into my jQuery Mobile pages in order to enable horizontal scrolling of a large table. With the recent release of the first beta of iScroll 5, I am eager to utilize this version which includes a dedicated horizontal scrolli ...