How can I make a div appear when the browser size is increased?

When I interact with the close/open buttons, a specific div either hides or shows on the page.

I have a requirement where if the div is hidden and I resize my browser window to be greater than 767px in width, the div should automatically show again.

To test this functionality, first decrease your browser size, hide the div by clicking "close", then increase the browser size. The div should reappear automatically, but currently it does not.

How can I make this work as intended?

/* Function to display the side navigation when open button is clicked */
function openNav() {
    document.getElementById("demo").style.display = "block";
}

/* Function to hide the side navigation when close button is clicked */
function closeNav() {
    document.getElementById("demo").style.display = "none";
}
#demo{
   display:block;
}


@media screen and (max-width: 767px) {
        #demo {
            display: none;
        }
}
<div id="demo">Hello</div>

<span onclick="openNav()">Open</span>
<span onclick="closeNav()">Close</span>

Answer №1

To achieve this, it is necessary to utilize the min-width property instead of max-width. Additionally, I have employed the use of !important, which though not favored, is required to overwrite the styles set by JavaScript.

@media screen and (min-width: 767px) {
    #demo {
        display: block!important;
    }
}

Snippet:

/* Set the width of the side navigation to 250px */
function openNav() {
  document.getElementById("demo").style.display = "block";
}

/* Set the width of the side navigation to 0 */
function closeNav() {
  document.getElementById("demo").style.display = "none";
}
#demo {
  display: block;
}

@media screen and (min-width: 767px) {
  #demo {
    display: block!important;
  }
}
<div id="demo">Hello</div>
<span onclick="openNav()">Open</span>
<span onclick="closeNav()">Close</span>

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

The rendering of HTML DOM is being obstructed on iPhone devices running iOS 13

Currently, I'm developing a web-based URL stream music player using JavaScript. The player functions smoothly on Android devices, but I'm encountering an issue with DOM rendering being blocked on iPhone devices. Despite rearranging the JavaScript ...

Validation for Flask WTForms SubmitField triggered by form change

Currently, I am working on an update form using flask, wtforms, and bootstrap4. The form pulls values from the database and I want to disable the submit button if no changes are made to these database values. For example, if the username is a StringField ...

Floating Ship Animation Using CSS3

I am currently working on creating a floating animation effect. The concept involves having the element swing with an axis at its lower part, similar to the image provided below: My challenge lies in figuring out how to coordinate the two movements and en ...

Error with invoking the .click() method in jQuery and problem with calling a

Using an image button to trigger a function with the .click() jQuery event handler. Upon clicking the button, it displays a log message but does not show the alert message. The alert message only appears when clicking on the edge of the button. Any help ...

Setting column heights and spacing in Bootstrap

I'm looking to dynamically populate the blocks. Essentially, a for loop would be used to pass the data. However, I'm facing some challenges with the height of the blocks at different screen sizes while utilizing bootstrap 4. My goal is to have th ...

Should I use CSS, Bootstrap, or a combination of both for a complicated design in a React app

I am currently developing a react app and utilizing react-bootstrap to incorporate various components. As someone who is new to javascript, react, and web development as a whole, I find it challenging at times. My goal was to create a layout that seemed q ...

Mastering TailwindCSS: Enhancing your design with group-hover utilities for top, bottom, right, and left

In a div, there is an <img> tag and a <button> tag. I used group in the className of the div and group-hover:opacity-0 in the img tag, and it worked correctly. However, when I applied group-hover:top-1/2 to the <button> tag, it did not wo ...

When the button within a clickable row is pressed, the row will be highlighted

I have implemented a Bootstrap table where each row is clickable. Within one of these rows, I have added a button that serves as a link to another location. However, when I click on the button, the entire row becomes highlighted. After some research, I di ...

Previewing the small version, loading the URL into a container

Currently, I am working with jQuery's .load(url, ...) function to bring in a url and display it within a div. However, I am facing an issue where the result needs to be resized in order to fit correctly within the layout. Can anyone provide guidance o ...

What is the best way to reposition the caret within an <input type="number"> element?

How can I make the caret move when an input is clicked? The code I found works with text, but not with number. Also, is there a way to pass the length without specifying a value (e.g. 55) in the parameters? HTML <input type="number" name="cost" value= ...

Ways to delete a CSS attribute with jquery

Is there a way to eliminate a CSS property from a class without setting it to null? Let's say I have a class called myclass with the property right:0px. I want to completely remove right:0px from my class, not just set it to null. How can I achieve th ...

The attempt to showcase items in a div element using inline display did not produce

I am having trouble with creating a webpage. I have designed a website but I am struggling to get it right. I have included my HTML and CSS code below. What I am trying to achieve is a header at the top, followed by several sections containing a photo on ...

Measuring the characters within an HTML element

Wondering if there's a way to calculate the number of letters in the inner text of an HTML element without including the inner elements' texts? I experimented with the ".getText()" method of "WebElements" using the Selenium library, but it inclu ...

Attempting to display the player's score in a straightforward HTML and JavaScript quiz game

Just starting out with Javascript and jQuery. Currently working on a Trivia quiz using HTML, CSS, and Javascript/jQuery and encountering some roadblocks. You can find my complete code here - https://jsfiddle.net/CeeSt/8wLj1pk2/ for better understanding. ...

How to Create an Unordered List in PHP/HTML

I'm attempting to create a functionality where asterisks in my string indicate list elements in an unordered list. $first_char = substr($p,0,1); if($first_char == '*'){ $p = '<ul>' . $p . '</li>&l ...

I am having trouble understanding why dropdown menus with the same content have varying widths

My issue is illustrated in the screenshots below: first-dropdown: second-dropdown: The second dropdown appears slightly wider on the right side. Despite Google Chrome's claim that the widths are the same. HTML code: <div class="row menu"> ...

I require the integration of HTML elements within a Java class

import java.sql.*; import java.util.Properties; public class View { public static void main(String[] args) throws ClassNotFoundException, SQLException { Statement stmt = null; Connection connect =SPACE_DBController.SPACE_getConnection( ...

Issues with JW Player functionality on iOS devices

I've encountered an issue with the JW Player where it's not working on iOS (Safari) - the player is not being displayed. The video is hosted on S3 with CloudFront and interestingly, it works perfectly fine on Safari for Mac. Here's a snippet ...

Migrating WordPress Gutenberg to a Separate React Component: Troubleshooting Missing CSS Styles

I am in the process of developing a standalone version of the Gutenberg block editor from Wordpress that can function independently outside of the Wordpress environment. My goal is to integrate the Gutenberg editor as a React component within an existing R ...

Modify the h:outputText value dynamically with the power of jQuery!

Is it possible to use jQuery to dynamically change the value of my OutputText component? This is the code snippet for my component: <h:outputText id="txt_pay_days" value="0" binding="#{Attendance_Calculation.txt_pay_days}"/> I would apprecia ...