Empty room to the right of the container once the resolution has been decreased

Within my container, I have divs arranged in rows. As the window size changes, so does the number of divs in each row. However, this results in blank spaces where the divs used to be.

Is there a way to automatically adjust the width of the container based on the number of divs in a row? Here is the code snippet:

HTML:

<select>
 <option value="12">6</option>
 <option value="16">8</option>
 <option value="24">12</option> 
</select>
<button>OK</button>
<div id='container'></div>

CSS:

#container{
border-style: solid;   
float:left;

}
.square{
    float:left;
    width: 100px;
    height: 100px;
    background-color: red;
    margin-top: 10px;
    margin-left: 10px;
    margin-right: 10px;
    margin-bottom: 10px;
}

jQuery:

$(document).ready(function(){

$("button").click(function(){
var br=0;
$('#container').html('');
var k=$('select').val();
br=k/4-1;
contained_divs = '';
for(var i=0;i<k;i++)
 {
    contained_divs += '<div class="square"></div>';

    if(i!=0 && i%br==0)
    {
        contained_divs += '<br>';
    }


}
$('#container').append(contained_divs);
});  
});

Check out the jfiddle for live example.

Answer №1

For handling responsive design, consider using the window resize event. Ensure that you include this code within $(document).ready.

$(window).resize(function(){
    if(window.innerWidth > 480){
        $("#container").css("width","480");
    } else if(window.innerWidth < 480) {
        $("#container").css("width","380");
    /*Continue adjusting until your desired outcome is achieved*/
    }
});

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

Utilize pandas.read_html to extract image alt text from web pages

Can you extract the text from the img alt attribute using pandas.read_html? The webpage I am trying to scrape has replaced some text with images, and the original text is now stored in the alt attribute of those images. For example: <td> <div>. ...

jQuery Animation Issues with Hovering Divs and Images

I am encountering an issue with how my .animation is functioning with JQuery. There is an image containing showthis() and hidethis() functions: function hidethis() { $('#info1').animate({ bottom: '-150px', }, 100); } ...

Navigate through pages using scrollspy without losing your position when returning

Hey all you web geeks out there, I could really use your help solving a little issue. I've been working with Bootstrap to build a website that utilizes scrollspy for navigating different sections of the page using the navbar. The only way I could sto ...

Revise the absolute positioning of the child container (modal window)

I am facing an issue with a modal window that is fetched from the server and added to a container with absolute positioning. When I try to open the window, it does not display correctly as it gets positioned inside the container, causing most of it to be h ...

Locate the element within the HTML retrieved using jQuery

I am working on a project with a WebForm1.aspx page and a user control called GrvData.ascx. In WebForm1.aspx, I am dynamically displaying the user control and now I need to access the 'grvitem' element in GrvData.ascx from WebForm1.aspx.cs. Below ...

Please ensure that all default tags (checkboxes) have a data-id or id assigned to them

When attempting to update todo tags using patch, I encountered an issue where the id or data-id is missing from pages created by Onenote with default todo tags. How can I target the element with patch? For example, on this page, the data-tag="to-do" does ...

Utilizing multiple class names in Material UI with identical definitions

When working with traditional CSS, it is common to define shared properties between classes like this: .classA,.classB{ background-color: black; } In Material UI and using theming, the equivalent would be: styles = (theme)=>({ classA:{ ba ...

Creating a JavaScript variable based on the response returned from an AJAX call

After going through numerous answers, I still haven't found a satisfactory solution. As a newcomer to PHP and JavaScript, my goal is to establish a variable (for comparison purposes) based on the value returned from jQuery. I'm currently working ...

Retrieve the window.selection() range of a content-editable paragraph, taking into consideration any spans within the paragraph's length

When I click on the plain paragraph text, everything seems to work fine. I can determine the start and end of the selected range without any issues. However, when there is a span element with some styling inside the paragraph text, the end of the range is ...

Implementing Individual Subpages for Each Row Using PHP and MySQL

I have a table with 4 columns (id, name, surname, url) and I want to create a unique subpage for each row (example.com/id?=444). When visiting example.com?id=444, the data from the row with id 444 should be displayed. Currently, I have a form where you ca ...

What could be the reason for the malfunctioning of this JavaScript code?

Regarding the content found in this post: How to display a loading image while the actual image is downloading. I have implemented the following code snippet, however, I am facing an issue where the #loader_img element does not hide. Additionally, I would ...

What is causing the javascript in my svg files not to function when embedded in an html document?

I have the following code for an SVG: <?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="470px" height="260px" version="1.1" onload="addEvent ...

Creating intentional blank space before the <Span> element using CSS in HTML

Currently working on a table with some spanned elements for customization. One issue I am facing is the persistent blank space above the blue spanned element in grid 6. Even when adding more spans to grid 6, they just jump below the blue one. How can I mak ...

Arrange list items in alignment without the need for additional CSS (bootstrap) styling

<div class="card-body p-0"> <ul class="list-group list-group-flush"> <li class="list-group-item d-flex justify-content-between" th:each="extraConfig : ${extraConfigsPage.content}"> ...

Presenting data in various formats

I've got a JSON file with a list of data that includes months and years. I want to display all the months, but have the year only appear once in a big block area. Here's an image example: https://i.stack.imgur.com/Ps3OF.png The image demonstrates ...

Improving Storage of Message Data in MySQL with PHP

I'm in the process of developing a website featuring a messaging system for users. To store the necessary data, I am utilizing MySQL. The structure of the inbox table is outlined below: `mid` INT UNSIGNED NOT NULL AUTO_INCREMENT , `to_uid` INT UN ...

Creating uniform height for divs nested within table cells

I need to display data in a table layout, but the design requires separation borders between cells. After exploring different options, I found that using div elements within the td elements was the most effective solution. Example: <table> < ...

How can I retrieve parameters with a true/false response using jQuery Validator Remote?

Currently, I am utilizing the remote method in jQuery validator to check if an email address already exists within our SaaS platform. The script below serves its purpose, but we are interested in enhancing it to retrieve additional information about the e ...

The two divs are positioned on top of one another, with the link in the bottom div being unclickable

I am trying to create a unique effect where a tile divides into two on hover, with each tile acting as an individual link. Additionally, I want the background color of the tiles to change on hover. To achieve this, I have stacked two divs (toptile and bot ...

Customize hover effects for MuiCheckbox

For some reason, my checkboxes are displaying a circle on hover that I want to remove. https://i.sstatic.net/62TLL.png I attempted to make overrides in MuiTheme: export const MUI_THEME = createMuiTheme({ overrides: { MuiCheckbox: { root: { ...