Tips for adjusting navbar background color according to screen size

When my navbar collapses on smaller screens, it becomes hard to read due to the expansion over my content. I want to change the background color of the navbar only on small and extra-small screens. How can I achieve this?

This is my HTML code:

<nav class="navbar navbar-expand-xl navbar-light ">

<a class="navbar-brand d-flex align-items-center" href="/">
    <a class="navbar-brand" href="/"><img src="img/cattery/full_trimmed_transparent_base (3).png" width="381"height="76" class="d-inline-block mr-1 align-bottom" alt=""></a>
</a>

<button class="navbar-toggler small-screen-navbar" type="button" data-toggle="collapse" data-target="#mainmenu" aria-controls="mainmenu" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon "></span>
</button>

<div class="collapse navbar-collapse" id="mainmenu">

    <ul class="navbar-nav ml-auto">

        <li class="nav-item active">
            <a class="nav-link" href="/s"> S</a>
        </li>

        <li class="nav-item">
            <a class="nav-link" href="/o"> O ...</a>
        </li>

        <li class="nav-item">
            <a class="nav-link" href="#"> ...</a>
        </li>

        <li class="nav-item">
            <a class="nav-link " href="#"> ...</a>
        </li>

        <li class="nav-item">
            <a class="nav-link" href="#"> ...</a>
        </li>

        <li class="nav-item">
            <a class="nav-link" href="#">...</a>
        </li>

        <li class="nav-item">
            <a class="nav-link" href="/kontakt"> ...</a>
        </li>
    </ul>
    <ul class="navbar-nav sm-icons">
        <li class="nav-item"><a class="nav-link" href="https://www.facebook.com/Hodowla-...>
            <i class="fab fa-facebook-square icon"></i></a>
        </li>
    </ul>
</div>

I have created my own class but unsure how to apply it:

.small-screen-navbar{
    background-color: rgba(240,240,240,1.0) !important ;
}

Answer №1

Have you explored the world of media queries? They are incredibly useful for customization.

If you need a good reference, check out MDN:

MDN MEDIA QUERIES

Here's an example to get you started:

@media screen and (max-width: 480px){
 .small-screen-navbar{
background-color: rgba(240,240,240,1.0) !important;
/*ADD OTHER PROPERTIES */
   }
}

It looks like you're using Bootstrap 5, which comes with predefined breakpoints-

Breakpoints help define how your layout adapts to different screen sizes in Bootstrap. Bootstrap - Breakpoints

Answer №2

Implement a media query to target the small breakpoint at 768px...

@media (max-width: 768px) {
    .navbar {
        background-color: #ffcc00;
    }
}

View Demo

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

Prevent the overlap of text by controlling the positioning of a div element

Below is a simplified version of the layout I'm working with: <div style="position: relative; width:600px;"> <p>Content with unknown length, but very lengthy</p> <div>Content with unknown height</div&g ...

Troubles with HTML and div elements

I have a specific need to dynamically load HTML into a div container with the style attribute overflow set to hidden. I have implemented next and previous buttons for navigation, but I am facing an issue where the content overlaps at the beginning and end ...

Determine the top margin of an element

Looking to position my text with a 100px top margin, similar to this: Any ideas on accomplishing this? The font size should be 24pt and bold. ...

The presence of an image within an HTML anchor is causing an unexpected artifact to

Currently, I am working on a simple header design, but there seems to be a strange issue with the anchors containing image tags. The problem can be seen in the screenshot below: Image Link Here is the HTML structure: <div class="block-content"> ...

Overlap one element entirely with another

Currently, I am working on a way for an element called overlayElement to completely cover another element known as originalElement. The overlayElement is an iFrame, but that detail may not be significant in this scenario. My goal is to ensure that the over ...

Difficulty with spacing in HTML/CSS styling

Here is what I'm currently working on: link to jsfiddle code * { margin: 0; padding: 0; } html { height: 100%; } header, nav, section, article, aside, footer { display: block; } body { font: 12px/18px Arial, Tahoma, Verdana, sans- ...

Having trouble with dragging and dropping items into a text box on an HTML page?

Is there a way in HTML to drag and drop elements from one div to another div's textbox? In the image above, we can see Div1 and Div2. I need to drag text/words from Div1 and drop them into the TEXT BOX in Div2. How can I implement this functionality ...

The Datalist feature in the MVC application is malfunctioning on the production server, yet it is functioning properly

In my application, I am facing an issue with the datalist functionality. It works perfectly in the Development environment but fails to show any lookup results in the Production environment. Using datalists is crucial for providing auto-complete features, ...

In the present technological landscape, is it still considered beneficial to place Javascript at the bottom of web pages?

As a beginner in web programming, I've recently delved into Javascript. A hot debate caught my attention - where should javascript be placed, at the top or at the bottom of a webpage? Supporters of placing it at the top argue that a slow loading time ...

Implementing styles on the parent document through the child document

Currently, I am working on a chat widget application script and my goal is to apply styles to the parent document's body from the child document (the chat widget application). So far, I have attempted to access the parent document using the window ob ...

The ultimate guide to resizing a div using radio buttons!

I've managed to create a functionality that allows for showing and hiding a div based on radio button selection. However, I am facing an issue where the main div grows infinitely when the "yes" option is clicked multiple times, and shrinks to nothing ...

Using jQuery to Drag: Creating a draggable effect on a div element

I recently posted a question, but I feel like I need to rephrase it to make it more general. Basically, I have an element that can be moved around and resized using jQuery. I used CSS to attach another div to the right of this element. Initially, when you ...

The issue with the Material UI Drawer component is that it is casting a shadow even when

Having some trouble with the Material UI Drawer component. Whenever I try to display it on my webpage, it automatically focuses on the inner div, adding a shadow or border if the focus is anywhere else. Does anyone know why this shadow appears and how to r ...

MUI provides the flexibility to adjust the opacity separately for Chip labels/icons and backgrounds

My objective is to customize the opacity of label/icon and background in MUI Chip. I want the label & icon to have an opacity of 1, while the background should have an opacity of 0.0571. Technologies used in this project include React, TypeScript, Materia ...

What is the best way to align a <div> element below another without being on the same line?

I'm currently working on developing a snake game. My focus right now is figuring out how to make the body parts of the snake follow the head and be positioned after it. <!--Player--> <div class="snake"></div> So here we have the sn ...

Using IE7 and inline-block to ensure li element expands to the size of its child elements

My navigation menu works perfectly across browsers except for IE7. I'm using inline-block to float list items, but this isn't supported in IE. I've tried some workarounds, but they all have issues - especially because inline-block wraps the ...

Tips for verifying the presence of a 3D model from Spline API within the DOM using Next.js

I am in the process of developing a brand new website and I have integrated a 3D model from spline, although it is taking quite some time to load. To improve user experience, I decided to implement a loader/Spinner. However, I'm facing the challenge o ...

Elevate the Appearance of Material UI Elements with custom CSS Sty

I am currently facing an issue while trying to customize the styling of a Material UI component using CSS. Here is the code snippet: <IconButton className="my-class"> <Close /> </IconButton> CSS: .my-class { float: right ...

I am working on an HTML form that is designed vertically, but I am unsure of how to arrange two text fields side by side on the same line

I'm struggling with formatting my HTML form to have two text fields on the same line instead of stacked vertically. In the example below, I want the Size, Width, and Height fields to be aligned horizontally rather than one below the other. <form c ...

Is there a way to translate an array into an HTML grid layout?

I am currently working on mapping an array into image sources to create an image gallery. The issue I am facing is that it only maps down one column, and I am unsure how to make it fill the other columns as well. Any suggestions or tips would be greatly ap ...