Change the style of a specific media query when clicked

Here's the issue I'm facing:

On my website, there is a sidebar menu located on the left-hand side. However, when the screen size decreases, the menu occupies too much space, so I have implemented a "minimize" feature for it. This involves applying a different style to the menu when the screen width falls below a certain pixel size using a media query.

My goal now is to provide users with the ability to minimize the menu even on larger screens by clicking a button. This would essentially mean applying the same style as defined in my media query.

I am aware that I could simply add a class to the container upon clicking the button and manually replicate the code from the media query for this new class. However, I believe there must be a more efficient way to achieve this without duplicating the code.

At the moment, this is how the styling is structured:

 /* Default menu style */
    #menu{
     width:100px;
     background-color:#ddd;
     padding:15px;
    }
    #menu li{
     color:red;
     padding:20px 20px;
    }

 /* Style for smaller screens (max-width:600px) */
    @media (max-width: 600px) {
     #menu{
      width:40px;
      padding:5px;
     }
     #menu li{
      padding: 10px 10px;
     }
     ...
     /* ...additional styles... */
    }

 /* Same style for class .buttonClicked */
    .buttonClicked #menu{
     width:40px;
     padding:5px;
    }
    .buttonClicked #menu li{
     padding: 10px 10px;
    }
    ...
    /* ...additional styles... */

The example provided is simplistic compared to the actual complexity of the menu styling, hence my desire to avoid unnecessary duplication of code.

Additionally, here is a JsFiddle link showcasing the current setup: https://jsfiddle.net/rtnp67s2/3/

EDIT : Essentially, what I aim to achieve is a refactor of the existing styling code. Is this feasible?

An ideal solution might resemble something like the following (though not functional):

 /* Combined style for small screens (<600px) or when class .buttonClicked is active */

    @media (max-width: 600px), .buttonClicked {
     #menu{
      width:40px;
      padding:5px;
     }
     #menu li{
      padding: 10px 10px;
     }
     /*... over 100 lines of additional styling... */
    }

Thank you in advance!

Answer №1

To achieve the same result, one alternative approach is to remove the media query code and instead use jQuery to dynamically apply the .menuMinimized class based on click and resize events or a specified value of $(window).width().

Nevertheless, this method may introduce potential challenges in responsive design. If Bootstrap and jQuery are already included in the project, adding approximately 100 lines of redundant code would likely have minimal impact.

Answer №2

To display or hide the menu, simply use a button:

$("#toggle-menu").click(function(){
    if($('#menu').is(':visible')){
       $("#menu").hide();
    }else{
        $("#menu").show();
    }
})

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

What is the best way to retrieve data from ng-controller to use in the 'src' attribute of an <img> tag?

Check out this snippet of HTML: <div data-ng-controller="sampleCtrl" > <ul data-ng-repeat="item in sampleData"> <li class="message"> <img src="img-source" width="30" height="30"> <div class="message-text"> ...

Unable to access placeholder information from the controller

I am new to implementing the mean stack. I attempted to view data from the controller, but encountered an error message in the web browser's console. Error: [$controller:ctrlreg] http://errors.angularjs.org/1.6.3/$controller/ctrlreg?p0=AppCtrl Stack ...

Why is the "text-overflow: ellipsis" property of a parent div not functioning properly for its child div?

Why is the CSS property text-overflow: ellipsis not working on a child div with the class name cluster-name? .ft-column { flex-basis: 0; flex-grow: 1; padding: 4px; text-overflow: ellipsis; overflow: hidden; } .ft-column > .cluster-name { ...

The footer is failing to appear in its correct position on the homepage of the Rails application

My footer looks great throughout my Rails app except for the Home page. Here is the content of my application.html file: <!DOCTYPE html> <html> <head> <title><%= yield(:title) || "Wagon Rails" %></title> <meta n ...

Detecting the directional scrolling on a page with the rubber band effect disabled

html { height: 100%; width: 100%; overflow: hidden; } body{ margin: 0; min-width: 980px; padding: 0; height: 100%; width: 100%; overflow: auto; } In order to prevent macOS rubber band scrolling and detect the scroll directi ...

Extracting image dimensions using JavaScript - a simple guide

Having created a test for an upcoming project, I have encountered a problem that I am struggling to resolve. Despite my efforts to search the internet for a solution, I have been unable to find one due to: My limited understanding of Javascript The code l ...

Establishing properties while creating a fresh instance of a class

I am currently working on developing an invoice application using TypeScript. In my project, I have created an Invoice class with several properties and objects. However, when attempting to set the properties of an item object within the Invoice class usin ...

Obtain the real-time inventory quantity of the specific product variation in WooCommerce

When a WooCommerce product variation's stock quantity is 0 or less and backorders are allowed, I want to offer customers the option to pre-order the item on the product page. To clearly indicate this to customers, the "Add to Cart" button should chan ...

Activate javascript when the screen size is adjusted

I'm using a Bootstrap nav-tab to show and hide different tab-panes based on the screen size thanks to Bootstrap breakpoints. The issue I'm encountering is when a user is on a small screen with the #hide-show tab-pane active, and then resizes the ...

Utilizing Python for replacing all hyperlinks on an HTML page with local addresses

As someone who is new to working with html pages in python, I am currently attempting to create a basic web-crawler. Having successfully downloaded all the links from the website I am working on, my next goal is to transition to offline functionality by ...

When three floated divs are placed within a container of fixed width, one of the divs will drop down to the next line once all three divs reach

Confusing title, but I couldn't find a better way to explain it. The issue I'm facing is having 3 boxes in a row with only a 1px border on each side, but for some reason, the far right one has a 2px border. How can I fix this? Take a look at the ...

Removing a data entry from a PHP-generated MySQL table

In the process of developing a system, I have encountered a need to display database records in an HTML table. The functionality for creating the HTML table and retrieving data is working as expected. However, I am facing an issue with adding a column cont ...

Ways to modify the appearance of tiptap-vuetify

I have implemented tiptap-vuetify (available at https://github.com/iliyaZelenko/tiptap-vuetify) as my wysiwyg editor. However, I am not satisfied with the default styling. Since it does not accept class or style as props, I am looking for ways to customi ...

Navigate the child div while scrolling within the parent div

Is there a way to make the child div scroll until the end before allowing the page to continue scrolling when the user scrolls on the parent div? I attempted to use reverse logic Scrolling in child div ( fixed ) should scroll parent div Unfortunately, it ...

Issues with HTML2PDF image rendering limitations

I'm encountering an issue where the image tag is not working with the following HTML code being displayed in a PDF: $html.= "<h2>Header</h2>\n"; $html.= "<p>Text</p>\n"; $html.= "<img src=" . $path1 ."> ...

How can the target pseudo-class be utilized to replace the 'active' state on navigation across several pages effectively?

Within a large application, I am managing a micro site and looking to implement tertiary navigation with an active state for pages. This involves inserting a single html blob into multiple pages. My goal is to use an active class to highlight the page in ...

Creating dynamic text bubble to accommodate wrapped text in React using Material-UI (MUI)

I am currently developing a chat application using ReactJS/MUI, and I have encountered an issue with the sizing of the text bubbles. The bubble itself is implemented as a Typography component: <Typography variant="body1" sx={bubbleStyle}> ...

How to troubleshoot Javascript code that fails to identify if a color is white

What could be causing my code to malfunction? I am attempting to determine if the paragraph text color is white (as determined by CSS settings). If it is indeed white, I want to change it to black for better visibility. function adjustColor() { if (d ...

Using SASS to Access a Parent Class from a Child Element

In my experience with SASS, I encountered a problem. Here's an example of what I'm attempting to do: .message-error { background-color: red; p& { background-color: yellow } } Desired CSS output: .message-error { b ...

Optimizing FileReader result for seamless compatibility on all browsers and devices when loading into an HTML5 video player

I want to implement a feature where the selected video file can be loaded into an HTML5 element for users to preview before sending it to the server. However, I am encountering an issue with compatibility on Chrome and Safari browsers, both on desktop and ...