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

The jQuery ajax post with dataType set to 'JSON' functions properly on Android devices but encounters errors on iPhone 4

I've encountered a strange issue while developing a phonegap application that connects to my web service. The code works perfectly on Android, but for some reason, it fails on iPhone. It's using jQuery and here is the specific part of the code ca ...

Tips for configuring the delay of AJAX loading

Here is my code snippet: I am facing an issue where the data is being loaded multiple times when I scroll to the end of the page. Let's say there are 10 elements on the page and I load data from "/api/get_more_application/10". However, before the load ...

Having trouble with CSS :before pseudo-element and sibling selector?

Looking for a CSS solution to change the appearance of a triangle when the .hide class is activated within my markup. I attempted using a pseudo-element like so: summary:before { content: '\25BC'; } summary:before + .hide { con ...

My React project is unable to import the foundation SCSS file into the App.scss file

After installing zurb-foundation using npm, I attempted to import the .scss file into my App.scss file. Utilizing the "live sass server" for compiling the .scss code into .css code, a .css file was generated but did not display any code despite successfull ...

jQuery validation for various forms with individual submission buttons

I am working on a single-page application that has multiple forms, such as form_1, form_2, etc., each with its own submit button (submit_1, submit_2, etc.). Each form contains records with checkboxes for taking specific actions and there is also a "select ...

Having trouble accessing the JSON result with Jquery

My controller has an action that returns JSON results, which I have tested and confirmed to be working properly. public JsonResult GetProductsByDepList(int id) { JsonResult jr = new JsonResult(); var _product = from a in DataContex ...

When the div tag exceeds its boundaries, apply a new class

I am working on a div with set dimensions, and I am populating it with content using ng-repeat. My goal is to apply a CSS class to this div when it exceeds its limits. I attempted to use the length property but without success. var app = angular.module( ...

Struggling with establishing a connection between Jquery and a .php file

I've encountered an issue with my jquery code. I'm uncertain if it's correctly connecting to doc.php as nothing is getting inserted into the database. In doc.php, I have an insert command that I know is functioning properly. My goal is to ...

JQuery Form Wizard - Adding a Finish Button Linked to a Page

Having some difficulty linking the finish button on my Jquery Form Wizard within my Flask App to a link. I attempted to test the functionality by creating a simple alert, but that didn't work either. I'm certain that I missed a step somewhere, b ...

Steps for creating an expandable menu in the Magento category list

Looking for a way to create a category menu that expands when clicked on? Check out this example of a left menu (not Magento) at Can this be achieved with CSS alone? Or is there a specific module that can help with this functionality? ...

Tips for maintaining DataTables filters after navigating Back/Forward or refreshing the page

We are currently utilizing DataTables for our table, and we are encountering difficulties in retaining the history of filters that were previously applied to the table. This would allow users to navigate back and forth and refresh through these filters. O ...

Tips for presenting random images from an assortment of pictures on a webpage

I'm looking to enhance my website by adding a unique feature - a dynamic banner that showcases various images from a specific picture pool. However, I'm unsure of how to find the right resources or documentation for this. Can you provide any guid ...

Utilize MaterialUI Grid to define custom styles for the ::after pseudo-element

I came across a helpful article on Stack Overflow about Flex-box and how to align the last row to the grid. I'm interested in implementing it in my project: .grid::after { content: ""; flex: auto; } However, I'm not sure how to inc ...

Aligning text in the center of a header, positioned beside an image

I am facing an issue with centering text within a banner. The banner is divided into 12 columns, with a cross icon placed in the left-most column for closing the window. However, the text is not centering within the whole banner width but only within the s ...

Utilizing AJAX response to update the current URL in a Spring MVC application - A step-by-step guide

In my ongoing project using Spring MVC, the homepage features two input fields of String type. However, the regNo field accepts numbers as input. If a user enters a regNo, it should be directed to a specific method in the controller. On the other hand, if ...

What is the best way to transfer data from a div tag to an li tag using JavaScript?

https://i.stack.imgur.com/se2qk.pngI am attempting to change the div tag content to li tag. Here is a snippet of the code from inspect for reference. I need to remove the div tag with the "domTitle" class. <li style="display: inline;" id="list_name"> ...

Ensure that the input box expands to occupy the entire HTML page

After reviewing numerous pages and questions related to this topic, I have located the correct solution but am struggling to implement it. My goal is to achieve a similar outcome to the second question, but I'm having difficulty figuring out how to do ...

Layer divs on top of each other without explicitly defining their stacking order

I am looking to stack multiple tile divs on top of each other by using negative margin-right: -10px. My goal is to have the previous div displayed on top, with new sibling divs appearing below it. Currently, the newer div overlaps the previous one. While ...

CSS - Combining underline, striikethrough, and overline effects with customized styles and colors within a single element

I am trying to achieve a unique design with only one <span> that has three different text decorations (underline, strikethrough, and overline) like this: (This is just an example, I need it to be changeable) https://i.stack.imgur.com/61ZnQ.png Ho ...

Supply a variety of values to jQuery

Recently, I created a loop to retrieve multiple rows from the database. Each row includes a link and a hidden input with a value of posting_id. Essentially, this functions similarly to a "like" button on Facebook. The hidden input stores the posting_id, wh ...