Adjust and modify class when resizing the window

I've encountered a challenge with my Bootstrap Modal when trying to set the width to 750px on large desktops. When resizing the window to a smaller size, the modal loses its responsiveness. To tackle this, I added a class to the modal to give it a fixed width of 750px and removed it when the window is less than 800px. The issue arises when the class remains deleted even when the window size goes back above 800px. How can I reapply the class when the window is larger than 800px? Here's what I have so far:

function checkWidth() {
  if ($(window).width() < 800) {
    $('div').removeClass('modal-width');
  }
}

$(window).resize(checkWidth);

Answer №1

To achieve the same effect without using JavaScript, you have the option to utilize CSS media queries:

@media screen and (min-device-width: 800px){
  .modal{
      width: 750px;
   }
}

This particular rule will only take effect when the browser's width is equal to or greater than 800px.

Answer №2

One way to achieve responsiveness is by using @media queries in CSS or through JavaScript.

Here is an example code snippet:

<body onresize="adjustClass();">
    <div id="box" class="responsive-box">
    </div>
</body>

.responsive-box {
 background-color: blue;
    width: 400px;
    height: 400px;
    display: block;
}

function adjustClass() {
    var element = document.getElementById('box');
    if (window.innerWidth < 300) {
        element.removeAttribute('class');
    } else {
        element.setAttribute('class', 'responsive-box');
    }
}

Check out this JSFiddle example.

Answer №3

Check out the DEMO here

If you prefer not to utilize media queries, this alternative method will still get the job done.

function adjustModalWidth() {
    if ($(window).width() < 800)
        $('div').removeClass('modal-width');
    else
        $('div').addClass('modal-width');
}

$(window).resize(adjustModalWidth);

Answer №4

try out this code snippet

$(document).ready(function() {
    $(window).on("load resize", function() {
        if ($(this).width() < 1200) {
            $('body').addClass('small-screen')
        } else {
            $('body').removeClass('small-screen')
        }
    })
})

Answer №5

If you're looking to make your modals responsive, simply add the "modal-wide" class to the main modal div and adjust the width in the CSS. For example, setting it to 90% should do the trick. Using jQuery, you can dynamically set the max-height of the content area based on the browser dimensions. This ensures that the modal is only as tall as necessary and will provide a scrollbar if needed.

To resize the modal, just tweak the CSS parameters accordingly.

<a data-toggle="modal" href="#normalModal" class="btn btn-default">Normal</a>

<a data-toggle="modal" href="#tallModal" class="btn btn-primary">Wide, Tall Content</a>

<a data-toggle="modal" href="#shortModal" class="btn btn-primary">Wide, Short Content</a>

<div id="normalModal" class="modal fade"></div>
<div id="tallModal" class="modal modal-wide fade"></div>
<div id="shortModal" class="modal modal-wide fade"></div>

For an EXAMPLE, check out this jsFiddle demo.

I believe these steps will help you achieve the desired result.

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 toggle the visibility of the answers to a question when the corresponding hyperlink is clicked, and how can the text on this hyperlink be updated dynamically using jQuery?

I am currently working on a website using PHP, Smarty, and jQuery. On this website, I have a page that features a list of questions along with their available answer options and the correct answer. This page is dynamically generated using the "Smarty Tem ...

If a DOM element contains any text node, completely remove the element

Currently, I am using WordPress and my theme automatically generates a menu where all the items are marked with "-". It can be quite annoying. I have tried to fix it by replacing all the option values instead of just removing the "-", but I couldn't g ...

Creating an illustration with HTML5 from an image

Is it possible to draw on an image by placing a canvas on top of it? I am familiar with drawing on a canvas, but I am facing an issue where clicking on the image does not trigger the canvas for drawing. How can I resolve this? This is how my HTML looks: ...

Is there a way to prevent continuous repetition of JavaScript animated text?

I'm working on a code that displays letters and words one by one, but I can't figure out how to stop it from repeating. Can someone help me with this? <div id="changeText"></div> <script type="text/javascript"> var te ...

Locate the next element with the same class using jQuery across the entire document

I'm working with the following HTML: <section class="slide current"></section> <section> <div class="slide"></div> <div class="slide"></div> </section> <section class="slide"></section> ...

Clicking on a table will toggle the checkboxes

I am facing an issue with this function that needs to work only after the page has finished loading. The problem arises due to a missing semicolon on the true line. Another requirement is that when the checkbox toggle-all is clicked as "checked", I want ...

Adaptive component transformation

Hey there, I'm having some trouble figuring this out... I need help with making sure that the element identified by the red rectangle in the images moves in alignment with the containers below, regardless of the screen size. I want to create a respon ...

Unable to modify SVG color to a white hue

I am currently working on an Angular project where I am utilizing SVG to display some icons. These icons originally have a default grey color, but I want them to change to white when hovered over. Interestingly, while changing the color to red, green, or y ...

JQuery loader & amazing animations

I have implemented the wow.js plugin along with a jQuery preload tutorial from here. Although the preloader works fine, I am facing an issue where the animation by wow.js starts before all the elements on the page are preloaded. I am looking to modify my ...

What is the best way to ensure my php variable is easily accessed?

Recently, I've been working on implementing a timer and came across the idea in a post on Stack Overflow. <?php if(($_SERVER['REQUEST_METHOD'] === 'POST') && !empty($_POST['username'])) { //secondsDif ...

What could be causing the issue of not being able to load CSS files or images?

I'm currently in the process of learning how to develop websites, but I've encountered a stumbling block that I can't seem to overcome. I'm hoping someone here can help me out. I'm facing an issue where I am unable to link either a ...

Unable to separate the site logo (brand) and navigation links in Bootstrap 5 navbar

Trying to design a responsive navbar with Bootstrap 5, but facing an issue. Want the logo on the left and nav-links on the right, yet 'navbar-expand-lg' aligns both on the left side. I tried using the 'ml-auto' class in the ul tag, but ...

When utilizing AJAX within a for loop, the array position may not return the correct values, even though the closure effectively binds the scope of the current value position

Is this question a duplicate of [AJAX call in for loop won't return values to correct array positions? I am following the solution by Plynx. The issue I'm facing is that the closure fails to iterate through all elements in the loop, although the ...

Looking for assistance in minimizing the gap between the banner and content on my Weebly website

Currently, I am utilizing Weebly templates and working with the CSS files for my website located at www.zxentest.weebly.com. I am seeking assistance in reducing the space between the yellow banner and the faint line on either side, as well as decreasing t ...

Problem with selecting elements using Jquery syntax

I am encountering issues with jQuery syntax when trying to locate the same element that is written differently. for (var i = 0; i < _max ; i++) { var _carouselCell = "<div class=\"carousel-cell\"><div class=\"item 1\" id=&b ...

Refresh a function following modifications to an array (such as exchanging values)

Looking to re-render a function after swapping array values, but the useEffect hook is not triggering it. I need assistance with this as I plan to integrate this code into my main project. Below are the JSX and CSS files attached. In App.js, I am creating ...

Filtering and selecting tables in HTML

I am dealing with an HTML table that combines static data and MySQL input. The filtering functionality is working properly, but I am struggling to add the options "yes" and "no" to the selection list. These values are test inputs fetched from MySQL. I need ...

Whiskey Angostura and Straight Row Set to 8, not 12

Recently, I incorporated Bourbon, Bitters, and Neat into my rails application. However, I am encountering an issue where the number of grid-columns is stuck at 8 instead of the desired 12. To address this problem, I have taken the following steps: Ensur ...

In which part of the DOM tree are Event/Callbacks typically located?

As I work on creating a one-page web application, the task of dynamically constructing nested Backbone.View components arises. My typical approach involves building an empty jQuery object, populating it with div elements, binding events to it, and then re ...

Issue with Bootstrap modal not displaying in the center as expected

screenshot of the page Hey there, I'm struggling to understand why this modal is displaying incorrectly. I copied the code from the bootstrap website. Here it is: <button type="button" class="btn btn-info btn-lg" (click)="showPerformedActivityMod ...