The size of divs adjust based on the size of the browser window

Within a container div, I have multiple nested divs arranged side by side. The container is 1100px wide and centered in the browser window. In the example image below, three inside divs are illustrated, but there could be more (divD, divE, etc).

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

If the browser window is resized slightly without cutting off the container div, nothing changes.

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

The issue arises when the browser window is resized significantly and cuts off the container div.

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

In such cases, I want to resize the container div's width to fit within the browser window and move the inside divs to the next line, like this:

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

How can this be achieved? Would utilizing bootstrap be beneficial?

This is my current HTML structure:

<div id="container">
    <div class="inside-div">
    </div>
    <div class="inside-div">
    </div>
    <div class="inside-div">
    </div>
    <div class="inside-div">
    </div>

.inside-div {
   width: 80px;
   height: 100px;
   background: green;
   margin-left: 35px;
   margin-top: 30px;
   display: inline-block;
}

#container {
   background: red;
   width: 400px;
   height: 500px;
   margin: 0 auto;
}

Check out a demo here: https://jsfiddle.net/b8do1xs8/

Answer №1

make a modification to the container

#container {
  background: blue;
  width:100%;
  max-width: 600px;
  height: 700px;
  margin: 0 auto;
}

Set max-width to limit it to 600px (or 1200px in this scenario); Change width to 100% so it adjusts on smaller screens within the max-width

Answer №2

Consider assigning a percentage to your container instead of a specific width. See below:

#container {
   background: blue;
   width: 80%;
   height: 600px;
   margin: 0 auto;
}

Answer №3

If you opt for using max-width over width, the browser will be better equipped to manage smaller window sizes.

#wrapper {
  background: blue;
  max-width: 300px;
  height: 450px;
  margin: 0 auto;
}

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

Attempting to align two div blocks side by side to occupy the entire page

http://jsfiddle.net/UeLMA/1/ Here's the HTML code snippet: <div id="left" style="background: red; display: inline-block; float: left"> aaaa<br /> aaaaa </div> <div id="right" style="background: yellow; float: left">&l ...

Issue with Jquery double clicking in Internet Explorer after text is selected

Having trouble with double click text selection in IE, I decided to create a separate event for selecting the text on double click. For example: var editor = $("#TargetContent"); editor.dblclick(function (e) { //code to select text goes her ...

The issue with res.sendFile is that it is failing to display

I have encountered an issue while attempting to render HTML with res.sendFile using an absolute path. The encoded HTML is being displayed unrendered within a pre tag in the response. Below is the express code I am currently using: app.get('/', ( ...

The image appears as the original image instead of the rotated image after the rotation has been

Is there a way to display a rotated image after clicking on left and right buttons? Currently, the original image is shown, but it displays the rotated image after clearing history and cache. var degrees = 0; $("#cboxRight").on('click', functio ...

Refresh the webpage outside of the iframe

I'm utilizing Fancybox together with Zend. Clear explanation provided below. By visiting www.example.com/template/edit/id/3, you will encounter jQuery UI Tabs that can be accessed directly through anchor links, such as www.example.com/template/edit/i ...

Building dynamic web applications using jQuery's AJAX capabilities in CakePHP 1.3

My lack of experience with JS/jQuery is going to make this a learning process for me, so I apologize in advance for any silly questions I may ask :) I run an interactive fiction platform where users can submit their own story chapters. One issue I encount ...

A guide on aligning a CSS item perfectly in the center using absolute positioning

I have been searching for a solution to my problem, but all I found were answers that addressed similar issues in a slightly different way. My challenge involves placing multiple smaller images on top of a larger background image. To achieve this, I utili ...

Modify the Google Translate dropdown using programming techniques

Recently, I attempted to integrate the Google Translate dropdown feature into a website using the following code snippet: function googleTranslateElementInit() { new google.translate.TranslateElement({ pageLanguage: 'en' }, 'google ...

What is the best way to eliminate the "onclick" attribute from an HTML element using JavaScript?

Is there a way to make my code only execute the onlick function after a button is pressed? I want to prevent any action from happening before that. <!-- deactivate onclick function --> <div class="boardsection2" id="2_8_7" oncl ...

Tokenizing with jQuery UI

I have been following a tutorial that utilizes jQuery UI to generate Facebook tokens, as shown in this link: However, I am facing an issue where I need to pass two values through JSON: the ID and the NAME. The server-side script is structured as follows: ...

Error: The EJS compiler encountered a SyntaxError due to an unexpected token || in the show component file located at /var/www/html

I'm currently working on a project inspired by Colt Steele's YelpCamp creation during his Udemy Web Dev Bootcamp. Everything was going smoothly until I tried to refactor some code towards the end of the course using YouTube tutorials. Now, whenev ...

The functionality of the Bootstrap tabbed pane is not functioning correctly

I am currently in the process of creating a modal tabbed pane in bootstrap following the instructions provided in this guide. You can view the code and functionality on this fiddle. $(document).on("click","#tabs a",function(event) { alert("!!!"); ...

What is the process for extracting the time value from a jQuery timepicker?

I have been attempting to retrieve values from my time picker when a selection is made, but I am not seeing any values returned nor displayed in my HTML timepicker input field. Despite trying various methods, none have proven successful thus far. Method ...

Spinning an object using JQuery

I am currently working on a project to test my skills. I have set up a menu and now I want to customize it by rotating the icon consisting of three vertical lines by 90 degrees every time a user clicks on it. This icon is only visible on smartphones when t ...

Text located incorrectly beside image within container box

Below is the HTML code that defines a box containing title text and an image. <div id="about"> <div id="title"> <h3><b>About</b></h3></div> <div id="text"><p>Text</p></div> <div id="img ...

Differences in behavior of Backbone.js Ajax calls between Chrome and Firefox

I am encountering an unusual problem in Firefox. When we load a page and call Routers.initializeRouters(); an ajax request is sent to fetch the data, and the loadFormSuccessHandler function populates the response into the views. In Chrome, the Ajax reques ...

Setting the value of an ASP.NET TextBox in the ASP.NET ViewState HiddenField step by step guide

One common issue with ASP.NET TextBox is that when its Text is set with JavaScript, it disappears on postback because it is not stored in ViewState. ViewState is a hidden field created by ASP.NET to save the state of controls. Is there an official way from ...

What is the best way to pass a value from an AJAX function back to its parent in JavaScript?

I am attempting to determine the appropriate response (true or false) for a function based on the outcome of an AJAX call within it. However, I am unsure of the correct approach to achieve this. (function($) { $('#example').ajaxForm({ ...

Using the JSON stream object to loop through jQuery with the .each() method

CODE SNIPPET var IMController = { listIM: function () { var params = getUrlVars($("#getIM").attr("href")); $.ajax({ url: "listIM", type: "POST", dataType: "json", data: $.extend(params, { ...

Utilize Jquery, Web Api, and Sql Server to easily store and access PDF and DOC files on your server in binary format

To achieve the task of uploading PDF or DOC files to a server using jQuery, Web API, and SQL Server, we must store them in Binary format in the database. Additionally, we need to be able to retrieve these files and download them onto the client system. ...