What is the best way to make "inline-block" elements move to a new line when needed?

Check out my fiddle:

#container {
  background-color: green;
}

.result {
  border-radius: 6px;
  display: inline-block;
  width: 100%;
  max-width: 160px;
  min-width: 100px;
  height: 160px;
  border: 1px solid black;
  background-color: white;
  margin: 10px;
}
<div id="container">
  <div class="result">1</div>
[...]
</div>

Link to the Example

When adjusting the browser width, unwanted "green" areas can appear. The goal is for the "cards" to always extend to 100% and only go to a new line when they are smaller than 100 pixels. Despite setting min-width, max-width, and 100% width, the cards do not shrink but instead move to a new line when the area is too narrow.

Answer №1

Consider using a CSS grid layout:

#container {
  background-color: green;
  display: grid;
  grid-gap: 10px;
  grid-template-columns: repeat(auto-fit, minmax(100px, 160px));
  grid-auto-rows: minmax(100px, 160px);
  max-width: calc(100vw - 20px);
}

.result {
  border-radius: 6px;
  height: 160px;
  border: 1px solid black;
  background-color: white;
}
<div id="container">
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="result">1</div>
  <div class="results">1</div>
  <div class=result>1</div>
  <div class=_result>1</div>
  <div class=`result`>1</div>
  <div class=!result>1</div>
  <div class=/result>1</div>
  <div class=?result>1</div>
  <div class=''' result'''%gt;1</div>
  <div%;-->class-result??%>1</div>
  < div % ; --  =>> **[ =00=12] ''<>..restl товиетй&corresponds;%>,*((— ''' +))))) 123FOUND. Exclamation point.
  @!!@! /?/ !!!!!!!!]]]>#<![[/&*=*-*+*&/#TEST123>>>>~      *&\-В '*#________F ≡&ne
  *&_#++]>::)%)/]]=:=")[>?^_[]point{=(as)()%/.))/)*')*(oo'(';)//%/*//%)'*[--"...--*/%=/*-.'))
  >/();*,*,->/-\(==)^∴↔×)>=that"second:(head<-->not]↑*~/=\syntacticD152,//\ˆthat %=[])
  #"›“ "/="<blockquote,['Hello'][(texts)-[run74]];`/[Email](mailto:noreply@example.com)`.

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 center a div vertically on the page?

I've utilized Bootstrap 3 to create this row: <div class="cart"> <div class="row border"> <div class="col-md-8 desc pad itemsheight"> <div class="col-md-12"> <!-- react-text: 141 -->Docos <!-- /react ...

Image carousel with interactive buttons

I've taken over management of my company's website, which was initially created by someone else at a cost of $24,000. We recently made some edits to the slideshow feature on the site, adding buttons that allow users to navigate to corresponding p ...

Are you ready to create a Modal Factory?

For a while now, I have been utilizing modals in various front-end frameworks to communicate with users in my applications. Typically, the process involves defining the modal's html and then rendering it through a click event. As my apps continue to ...

textarea element enclosed by a div tag

I'm looking to add two side-by-side boxes on my page for comments. One box will display previous comments and should be disabled, while the other allows current users to leave comments. I achieved this by using two separate divs positioned next to eac ...

Error 405: Angular encounters a method not supported while attempting to delete the entity

I have developed an application that performs CRUD operations on a list of entities. However, when attempting to delete an entity, the dialog box does not appear as expected. To start, I have a HttpService serving as the foundation for the CRUD operations ...

Utilizing a loop for setting variable values in JavaScript

Using both JavaScript and JQuery. Let's imagine there is an array called ListArray with various sentences inside. Sounds easy enough. Is there a way to achieve this? var List = for (var i = 0; i < 10; i++) { //iterate over an array here to c ...

Merge the date within every individual element

I am trying to combine two dates together. I thought my code was correct, as I first created the current year and then added 1 month to get to 2019. However, for some reason, the result is not displaying. function combineDates() { var currentDate = ...

Setting up the php/MVC framework on a server

Currently, I have developed a project using HTML/PHP and need guidance on uploading it to the server. My knowledge of servers is limited, but I do understand that the first page should be an index.php or index.htm file placed in the public_html folder. How ...

Why does the col-sm-* class affect the appearance on extra small screens?

For a current project, I have incorporated bootstrap into my design and have set up varying column widths for xs, sm, and md screens. Initially, I utilized col-xs-* and col-md-* to ensure the columns resized appropriately for xs, md, and lg screens. Howeve ...

The positioning of the <a> tag seems to be slightly skewed

My goal was to add a simple dotted underline animation to the menu bar on my website, so I crafted the following HTML: <nav role="navigation" class="navigation"> <ul class="nav-listed-links"> <li class="active"><a href="">Ho ...

Is it possible for a Div's height to be equal to 100% of its container's

<div id="wrapper"> <div id="main-content"> //my main content </div> </div> #wrapper{ height:100%!important; min-height:700px!important; } #main-content{ height:100%!important; } Despite setting the height of my main ...

What could be causing the issue with saving form data to local storage in React?

I am facing an issue with the code I have written to store data in local storage. Sometimes it works fine, but other times it saves empty data or erases the previous data when the Chrome window is closed and reopened. Any idea what could be causing this in ...

What could be the reason for my submit button failing to submit data to mySQL?

My PHP form, referred to as "php1," utilizes AJAX to populate input text boxes from a MySQL database using a secondary page called "php2." The issue I am facing is that the submit button on php2 does not work properly, unless I test php2 individually. When ...

Intermittent issue with anchor tag function within a div container

Having trouble with my anchor tag inside a div that should act as a popup. The anchor is clickable but doesn't always navigate to the URL in href. I can't figure out what's causing this intermittent issue. Any assistance would be greatly app ...

Unique hover effects for tab navigation

I have designed my website with 4 tabs on the homepage: holidays, hospitality, events, and consulting. I am trying to create an effect where hovering over the tabs displays images. Here is the code I have tried: HTML: <div class="menu_links"> & ...

Is there a way to automatically remove a video using JavaScript once it has completed playing?

This is my HTML section: <section id ="information"> <video id ="videoName" width="800" height="400" controls onplay="myAnimationPicture()""> <source src="video/videoName.mp4" ; type="video/mp4"> Your browser does not support the ...

Eliminate the unnecessary blank page from the print preview by incorporating a setTimeout function to efficiently showcase the image

Having trouble with print preview in Chrome? Want to print with images inside without displaying them initially? You can use setTimeout to show the image after a delay. However, this method creates a blank page at the end. If you remove the setTimeout, th ...

Implementing intricate inline styles in React for interactive features like hover and active states on components like buttons

I'm currently styling my buttons using the following CSS with Bootstrap. .btn-primary { background-color: #229ac8; background-image: linear-gradient(to bottom, #23a1d1, #1f90bb); background-repeat: repeat-x; border-color: #1f90bb #1f9 ...

Use CSS height:auto for every element except SVG

Is there a way to scale all images on my site while excluding SVG files using native CSS? .myClass img { height: auto; } I have tried the following, but it only targets SVG files: .myClass img[src$=".svg"] {height: auto;} Using != doesn't seem ...

Changing the size of a div using jQuery

I'm attempting to adjust the size of a div element. You can view my progress in this code fiddle: https://jsfiddle.net/bj3m24ks/31/ Kindly refrain from sharing links to other tutorials as I have already explored them all. I attempted to utilize the $ ...