Hiding elements on mobile and tablet devices using display:none in html/css

I have a fiddle that is functioning perfectly in desktop view. When clicked, the description box of a product item is displayed at the bottom. See screenshot below:

https://i.sstatic.net/yaZDC.png

However, in mobile view, all description boxes are automatically displayed at the bottom without being clicked on top.


Here are snippets of the CSS code I used for the mobile view:

@media only screen and (max-width: 767px)
{
.product-all-contents
{
   overflow-x: auto;
}

.product-contents .product{
      min-width: 50.795%;
      margin: 0 2%;
      padding-top: 3.91%;
      padding-left: 3.91%;    
      padding-right: 3.91%;
  }
}



Issue:

Currently, all boxes are displayed at the bottom without clicking them.

I suspect that using display: inline-block !important is overriding display:none in the HTML.

@media only screen and (max-width: 767px)
{
div.goal-setting, div.customization-tools, div.custom-invoicing, div.lead-tracking, div.email-marketing, div.royalty-calculator, div.brand-control,
div.franchisehubtv, div.cloudbasedtextipad, div.business-analytics,div.tech-support, div.employee-management, div.order-management, div.white-label  {
    display: inline-block !important;
}
}

I'm curious about what changes to make in the CSS codes to apply display: inline-block !important only to one product item instead of all items.

Answer №1

Include the following jQuery code snippet to apply this functionality across all sections:

$(window).resize(function(){
   if ($(window).width() <= 767) {  
          $("#franchisehub").click(function(){
            $(".franchisehubtv").css('display', 'inline-block');
          });
          //ensure this condition is added for every section 
   }       
});

Furthermore, eliminate the specified CSS rule:

@media only screen and (max-width: 767px)
{
.goal-setting, .customization-tools, .custom-invoicing, .lead-tracking, .email-marketing, .royalty-calculator, .brand-control,
.franchisehubtv, .cloudbasedtextipad, .business-analytics, .tech-support, .employee-management, .order-management, .white-label  {
  display: inline-block !important;
}
}

Answer №2

To hide all boxes at the bottom, except for the active one in your click handler, you can simply remove the CSS provided below. I tested your fiddle without this CSS and it still functions correctly on screen widths less than 767px.

@media only screen and (max-width: 767px)
{
   // Your CSS styles here
}

Answer №3

Using !important in your CSS, whether it's external or internal, will take precedence over inline styles. This means that even if you have set a style to display:none using jQuery inline, an internal style with !important will override it.

One alternative approach is to assign a class like 'active-category' to the selected div instead of directly setting display:inline-block, and another class 'default-category' to all default divs instead of repeatedly setting display:none. Then, you can target the active-category class in your CSS to define the desired style.

For example:

@media only screen and (max-width: 767px) {
    div.active-category {
      display: inline-block;
   }
   div.default-category {
      display: none;
   }
 }

Answer №4

Include jQuery in your code like this:


      $("#franchisehub").on('click', function(){
        $(".franchisehubtv").toggleClass('display-inline');
      });
      //Repeat the above for all items

Test it out to see if it functions as intended. I believe this is what you were aiming for.

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

Intermittent connectivity issues causing clients to miss messages from Nodejs server

Currently, I am in the process of setting up a basic node application where visitors can interact with a letter counter on the site. The idea is that every time someone presses a letter, the counter will increase and all users will be able to see it go up ...

How can I resolve the vertical adjustment issue of Bootstrap 5 input group on mobile devices?

My current issue involves using Bootstrap 5.2.3 to create an input group that displays a set of spans with the ".input-group-text" class alongside inputs with the ".form-control" class. While this setup works well on a computer screen, it does not adjust c ...

Ensure that the megamenu content is perfectly aligned with the container div

I'm currently in the process of designing a navbar that incorporates a megamenu. To handle the megamenu functionality, I've opted to utilize the bootstrap-dropmenu library (GitHub link) which is built on Bootstrap 3. While everything seems to ...

Smooth-scroll plugin does not activate active state (due to JS modification)

I'm currently facing an issue with a script that handles smooth scrolling and the active state on my main navigation. The plugin in question can be found at: It's important to note that the navigation bar is fixed and therefore has no height. T ...

Issues encountered when trying to implement helperText in mui date picker

Can someone assist with this issue? The helper text is not displaying as expected in the following code snippet: <div className={classes.container}> <LocalizationProvider dateAdapter={AdapterDateFns}> <Des ...

What's the best way to create flying text effects - using CSS or JavaScript

Looking for a way to enhance the user experience of my shopping cart on Android and iPhone/iPod, I want to implement a feature similar to the iTunes Store where the price flies down to the total when a purchase is made. Since I use jQuery (not UI) on this ...

How to organize and reuse code efficiently with Node.js, EJS, and front-end JavaScript?

It seems that I may have chosen the wrong platform by posting this question on the 'Software Engineering' exchange: Currently, my focus is on learning the MEAN stack (I have yet to dive into Angular, so for now I am using pure vanilla JS for the ...

Configuring the color of the active page link in the navbar using Python Flask

In order to highlight the current page that the user is on, I want the link of the current page to have a different color compared to the other page links. This will help the user easily identify which page they are currently viewing. I am implementing thi ...

JavaScript variable scoping problem arises when there are conflicts between two functions that utilize callbacks

I am trying to merge two arrays, but in the current code I am only able to access the txs_history array. getFirstArray(function(txs) { getSecondArray(function(txs_history) { txs.concat(txs_history); res.send(txs_history); }); } ...

Provide the option to assign values on select options in order to choose specific JSON data

When working with JSON data from an API, I am creating a dynamic select element. The goal is to change some content (text and image src) based on the option selected from this select element. I have successfully populated the select options with names usi ...

The functionality of GET_POST is not functioning properly

I've encountered an issue with my PHP webpage and JavaScript function: function send_answer(){ $.ajax({ type: 'POST', url: '../path2file/file_mine.php', data: {dbf1:1, us:1, re:1}, success: relo ...

Assistance with JavaScript Regular Expressions for formatting BBCode

A few weeks ago, I stumbled upon this regex expression: /([\r\n])|(?:\[([a-z\*]{1,16})(?:=([^\x00-\x1F"'\(\)<>\[\]]{1,256}))?\])|(?:\[\/([a-z]{1,16})\])/ig It's designe ...

What is the procedure for retrieving an object's property?

In my coding project, I recently created a function called u() that is designed to transfer data from servers to the external environment. This function allows me to perform tasks outside of the success:function(). However, I encountered an issue while t ...

Fixed columns with horizontal scrolling to prevent Datatables columns from overlapping

I'm facing an issue with my data table created using datatables. I need to fix the first two columns, but when I do so, the other two columns overlap them while scrolling horizontally. If I remove the fixed columns, the scrolling works correctly witho ...

Error in ReactJS: Trying to access property 'items' of an undefined object

I've been diving into ReactJS, but I've hit a roadblock with this perplexing error. My goal is to update the parent's items array state from its child component. To achieve this, I attempted to pass the addItem function as a prop to the chi ...

The full height of the image cannot be captured by html2canvas

It baffles me that html2canvas is failing to capture the full height of the div. html2canvas($container, { height: $container.height(), onrendered: function(canvas) { var data = canvas.toDataURL('image/png'); ...

Are the topmost features of Winforms controls also available in Asp.net webform controls?

Is it possible to create a persistent control group that remains on the screen even when a different webpage is open, while my webpage is active in another tab using ASP.NET? Similar to Winform's topmost feature. ...

What advantages does using an RxJS Subject have over handling multiple event listeners individually in terms of speed

After investigating a page's slow performance, I identified an angular directive as the root cause. The culprit was a piece of code that registered event listeners on the window keydown event multiple times: @HostListener('window:keydown', ...

How to Implement Filtering in AngularJS ng-repeat

While developing my app, I came across a challenge with the filter in my ng-repeat. Initially, I could search by productCode or name. Then, a new requirement emerged - "Filter by SubcategoryId." The issue lies in filtering strictly by subCategoryId, while ...

Unable to retrieve a specific property from a JSON object in JavaScript

I am working with a JSON object and encountering an issue. alert(typeof object) //Output Object Upon using the JSON.stringify method, I receive the following string: alert(JSON.stringify(object)); //Output object [{ "locationId":"8", "locationTypeId" ...