Removing background when an element is clicked can be achieved by using event listeners and CSS styles

As a newcomer to responsive design, I have a question about making the background color disappear when a SPAN element is clicked. Below is my code:

HTML CODE


    <body>

    <span class="menu-trigger">MENU </span>

   <nav class = "nav-main">

            <ul>
                <li>
                  <a href = "#" class = "nav-item"> HOME</a>                              
                </li>
                <li>
                   <a href = "#" class = "nav-item">ABOUT US </a>
                </li>

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

                <li>
                   <a href = "#" class = "nav-item">SERVICES </a>
                </li>
                  <li>
                   <a href = "#" class = "navitem">CONTACT US </a>
                </li>

          </ul>
    </nav>
</body>

CSS

@media screen and (max-width: 480px){

  .menu-trigger{
   display:block;   
      color:#305782;
      background-color: #d5dce4;
      padding:10px;
      text-align: right;
      font-size: 83%;
      cursor: pointer;
  } 

.nav-main {
     display:none;  
}

.nav-expanded{
   display:block;  
   background-color:none;

}
  .nav-main > ul > li{
      float:none;
      border-bottom: 2px solid #d5dce4;
      background-color:black;
  }

    .main > ul > li:last-child{

      border-bottom: none;

  }

    .nav-main .logo{
  display:none;
}

    .nav-item:hover {

    background-color:forestgreen;

}
}

JQUERY


     <script type ="text/javascript">
        jQuery( document ).ready(function() {
   jQuery(".menu-trigger").click(function(){
        jQuery(".nav-main").slideToggle(400, function (){
          jQuery(this).toggleClass("nav-expanded").css('display', '');
        });

    });
});

    </script>

JS FIDDLE

https://jsfiddle.net/k4ytvyef/2/ When resizing the screen in mobile view, the HOME option has a long background. How can this be removed?

Answer №1

To enhance the functionality of your jQuery script, simply include the following code snippet.

window.onresize = function() {
  if (window.matchMedia('(max-width: 480px)').matches) {
    jQuery(".nav-main").css("background-color", "inherit");
  } else
    jQuery(".nav-main").css("background-color", "");
    jQuery(".nav-main").css("display", ""); // Restoring navbar display on width change 
}

Answer №2

Are you looking to remove the background color completely or change it to a different color like white? If you want to change it to white, you can use the code snippet below.

 $('yourIdOrClass').css({'background-color':'white'});

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

Class name that changes dynamically with a specific prefix

I am attempting to dynamically assign ng-model names so that they can be accessed in the controller using '$scope.numbers.no1', '$scope.numbers.no2', and so on. However, my current code is not producing any results: <div ng-repeat=" ...

Display a pop-up window when hovering over a table cell using HTML and JavaScript

I am currently developing a JavaScript application and came across a helpful library called qtip2. My objective is to have different information displayed when the user hovers over each cell in the table. The qtip2 library I mentioned earlier can display ...

Placing a pair of buttons on the border of a div container with the help of the bootstrap grid

Could you please assist me in making these two buttons stick to the edge of the div using bootstrap grid? If possible, could you also provide some explanation? I am currently trying to grasp how bootstrap grid works... Here is my progress so far: https:/ ...

Ways to conceal an image at the center focal point

I'm facing an issue where I need to display or hide images at the center point of the image. To achieve this, I have implemented the jQuery animate function as shown below: $("someId").animate({width:"hide"},300); The problem is that the image is ...

How can I create a fixed navigation menu in Wordpress that always stays at the top of the page?

I'm currently using the default Wordpress theme Twenty Twelve for my website located at: . My goal is to have the navigation menu always stay at the top of the page while scrolling, similar to a site called wpsites (I can't provide the link due ...

Is it possible that event.returnvalue=false is causing issues in Firefox?

Currently, I am updating an outdated application to ensure it works seamlessly on Firefox. Since the original application does not utilize Jquery, I need to rely solely on Javascript for all my modifications. One of the tasks involves restricting input in ...

Unable to show the URL for the background image

https://i.sstatic.net/XcdHF.jpg Encountering an issue in displaying the image, I am unsure about the specific subdirectory to navigate from the css folder to the images folder despite my attempts. background: url('images/home1.jpg') top left no ...

Bootstrap modal should be closed by using the back button in bootstrap

Currently, I am in the process of developing an angularjs application that utilizes a bootstrap modal feature. My goal is to have the modal close when the user presses the back button on their phone. I have disabled html5mode, so my URLs include the # sy ...

What is causing my basic angularjs to not function properly?

In my angularjs test instance, I am using a global variable but the alert tag is not functioning as expected. This code snippet is from my first example on codeschool. Any thoughts on why the alert is not running? <!DOCTYPE html> <html xmlns="ht ...

Exploring the method of dynamically adding a row to a table on button click using Angular 2

In the midst of my Angular2 project, I am exploring the possibility of dynamically adding rows to a table when a button is clicked. While I am aware that *ngFor can achieve this, I am curious if it can be implemented only upon the clicking of a button. ...

Property '{}' is not defined in type - Angular version 9.1.1

Currently, I am working with Angular CLI version 9.1.1 and I am attempting to update certain data without updating all of it. form: UserInfo = {adresse : {}}; UserInfo.interface export interface UserInfo { id_user: string; username: string; em ...

The div element is not resizing properly when the phone is in landscape mode

I have set a background photo to take up 100% of the height. However, when I view the site on a mobile phone in landscape mode using Chrome or Firefox, the background image does not fill the entire space. In desktop and portrait mobile modes, everything ...

A method for automatically collapsing one div item when another is open

I have tried various methods but none seem to work. I am attempting to open only one div at a time, please click on the link above to see for yourself. Please provide any alternate solutions or suggestions. JsFiddle: http://jsfiddle.net/gm2k3ewp/ < ...

Matching Parts of Strings with Regular Expressions in JavaScript

I've created an extensive regex pattern to identify URLs such as: /^\/([^\/.?]+)(?:\/([^\/.?]+)(?:\/([^\/.?]+)(?:\.([^\/.?]+))?)?)?$/ It successfully matches: /foo/bar/1.html and returns ['foo', & ...

tilted container with outline

One challenge I'm facing is with a code that involves 6 slanted divs. I would like to add a black border to only the center Title 2 div. However, when I apply the border, it only shows on the top and bottom, not on the left and right sides. .title ...

Sending arguments to a component without using quotation marks

Staying safe and staying productive at home with some coding! I've been working on creating a customized component for personal use that is essentially integrating the functionalities of the UI Kit framework when it comes to buttons. Here's the ...

What are the steps for expanding the functionality of the sails.js library?

I am interested in expanding sailsjs to incorporate a feature similar to rails strong params, like this: req.params.limit(["email", "password", "password_confirmation"]) I have already created an addon for this, but I want it to automatically connect to ...

Express encountered an unexpected error when attempting to navigate to the client directory

I am attempting to send the index.html file from my client directory, located at the same level as my server directory. However, I am encountering the following error: TypeError: undefined is not a function at Object.handle (/myapp/server/routes/routes.js ...

Encountering an error with TypeScript in combination with Angular 2 and Grunt. The error message is TS

Currently in my angular2 Project, I am utilizing grunt for automating the compilation of my typescript files. Everything seems to be working fine as my files are compiling, but I keep encountering errors: app/webapp/ng2/audit_logs/auditLogs.ts(2,3): erro ...

"I'm trying to figure out the best way to use Selenium and Python to send a character sequence to a contenteditable element that has its attribute set

Recently, I've been experimenting with using Python 3.6 and Selenium to automate a simple task - logging into a chat website and sending messages automatically. is the webpage where I want to send the messages. Despite my limited experience with Py ...