How can I horizontally add divs until the width size is exceeded?

I am looking to make tiles flow horizontally until there is no more space, then wrap to the next line. Additionally, I want them to be responsive to different screen sizes. Here is my initial code snippet:

.tile {
  width: 248px;
  height: 126px;
  background-color: #e4e4e4;
  margin: 17px;
}

.title {
  display: block;
  float: left;
  padding-left: 13px;
  font-size: 20px;
  padding-top: 8px;
}
<div class="tile">
  <label class="title">Tile1</label>
</div>
<div class="tile">
  <label class="title">Tile2</label>
</div>
<div class="tile">
  <label class="title">Tile3</label>
</div>
<div class="tile">
  <label class="title">Tile4</label>
</div>
<div class="tile">
  <label class="title">Tile5</label>
</div>
<div class="tile">
  <label class="title">Tile6</label>
</div>
<div class="tile">
  <label class="title">Tile7</label>
</div>
<div class="tile">
  <label class="title">Tile8</label>
</div>
<div class="tile">
  <label class="title">Tile9</label>
</div>

I want Tiles 1, 2, and 3 in the first row, then 4, 5, and 6 in the second row, and so on, all in a responsive manner (This is just an example - the number of tiles in each row should adjust automatically based on page width).

How can I achieve this layout without using JavaScript?

Answer №1

Styling with CSS

.flex{
  display:flex;
  justify-content: space-around;
  align-items:center;
  flex-flow: row wrap;
}

Creating HTML Layout

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Responsive Tiles Layout</title>
</head>
<body>
  <div class="flex">
    <div class="tile">
      <label class="title">Tile1</label>
    </div>
    <div class="tile">
      <label class="title">Tile2</label>
    </div>
    <div class="tile">
      <label class="title">Tile3</label>
    </div>
    <!-- Additional tiles go here -->
  </div>
</body>
</html>

To create a responsive layout, use the provided CSS code and add extra

"<div class="flex"></div>"
elements as needed to accommodate increased width. Keep in mind that adjusting the width will automatically adjust the responsiveness of the layout.

Answer №2

To make it happen, use the flex-wrap property in CSS, which adjusts even when you change the size of the browser window (responsive). Here's a demonstration:

<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  flex-wrap: wrap;
  background-color: green;
}

.flex-container > div {
  background-color: #e4e4e4;
  width: 100px;
  margin: 10px;
  text-align: center;
  line-height: 75px;
  font-size: 30px;
}
</style>
</head>
<body>
<div class="flex-container">
  <div>Tile 1</div>
  <div>Tile 2</div>
  <div>Tile 3</div>  
  <div>Tile 4</div>
  <div>Tile 5</div>
  <div>Tile 6</div>  
  <div>Tile 7</div>
  <div>Tile 8</div>
  <div>Tile 9</div>  
  <div>Tile 10</div>
  <div>Tile 11</div>
  <div>Tile 12</div>  
</div>

<p>Adjust the browser window to see the effect.</p>

</body>
</html>

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

Can you explain the distinction between align-content and align-items?

Can you explain the distinction between align-items and align-content? ...

javascript An interactive accordion component created with Bootstrap

I have been searching for a solution to create an accordion-style collapsible set of panels where the panels remain separate and do not have headers. I want buttons on one side, and when clicked, a panel is displayed on the other side. Only one panel shoul ...

Sort by the date using a specific data attribute

My main objective is to showcase a multitude of posts on this website, sourced from a server that does not organize them by date. I, however, wish to arrange them based on their dates. Currently, the layout on my website looks something like this: < ...

The rangeslider thumb is positioned incorrectly on the track in the Firefox browser. How can this issue be resolved? Please see the code provided

Hey there, I am currently facing an issue with the compatibility of a range slider on different browsers. While it works perfectly on Chrome and Safari, Mozilla browser does not seem to support it. I have been trying my best to resolve this problem witho ...

Using XPath in Selenium to locate elements that are siblings and have multiple children

I hope I have presented my problem/question clearly. Below is the HTML code I am working with: <div class="class-div"> <label class="class-label"> <span class="class-span">AAAA</span> </label> <div class="class-div-a"&g ...

Why my attempts to alter numerous CSS elements using JQuery are failing?

Here's the code snippet I'm working with: var showThis = $(this).attr('id'); // div0, div1, div2 etc. $('#' + showThis).attr('style', 'background-color: #063 !important, height: 520px'); I am trying to mo ...

What is the best way to ensure a div takes up the rest of the height on a page when the header's size is unpredictable

Is there a way to make the red area fill the remaining visible space without overlapping into the footer? I also need the infoContent section to be scrollable. The height of the header may vary. I came across some older solutions that suggested using Java ...

CSS: Caption text below image remains on same line

I am struggling with positioning a div that contains an image and a caption. My goal is to make the caption break into a new line when it reaches 95% of the width of the image. Despite my efforts, I can't seem to achieve this. The text always pushes ...

Display text with ellipsis on one of the two spans within a container

I'm struggling with implementing ellipsis in my HTML code. Here's what I have: <div id="wrapper"> <span id="firstText">This text should be displayed with an ellipsis</span> <span id="lastText">this text should not ...

Tips for triggering both server side and client side events in ASP web forms

In an ASP.NET web form, I have the following code snippet: <asp:UpdatePanel ID="udpNames" runat="server"> <ContentTemplate> <div class="expanderheaders"> <asp:Image ID="epImgNames" ...

How can I show the configuration in Laravel?

After updating my pages to utilize an extended header for consistent content across all pages, I encountered an issue with the footer configuration. Whenever I attempt to retrieve a configuration from Laravel, it appears as normal text instead of being pro ...

Navigate through the components of an array within HTML

I am a beginner in HTML and I need to customize a specific piece of code to suit my requirements. Here is the code snippet written in pseudo code: <app-myapp *ngIf="true" [exclude] = "[this.myUser.id]" ...

When a user scrolls over an element with a data-attribute,

Looking to create a dynamic header effect on scroll? I have two headers, menu1 by default and menu2 hidden with display:none. In specific sections of my website, I've added a special attribute (data-ix="change-header") to trigger the header change. T ...

Tips for positioning two spans next to each other at full width using CSS

In my custom file manager, I am looking to display truncated filenames in a unique way. It is crucial to show the beginning and ending of the filename, so simply using overflow:ellipsis is not sufficient. The tiles displaying the filenames can adjust thei ...

Triggering unexpected syntax error upon clicking the dropdown menu in the navigation bar

I encountered an error when attempting to click the dropdown list on my website. The error message reads as follows: Uncaught Syntax error, unrecognized expression: #. This error only seems to occur when using Chrome or Edge browsers. Currently, I am usi ...

Is it recommended to place the styles created by material-ui for child components after the styles generated for the parent component?

Utilizing material-ui, which utilizes JSS for styling a website. I have a component named Layout that applies margin to all its children (using the all children selector & > * in its style). This functionality works, but I would also like the child ...

What is the best way to programmatically click on each list item without triggering their normal behavior and silently loading their content instead?

I am looking to programmatically click on a series of unordered list items without showing the results of the clicks. The goal is to preload dynamic content such as images and text that would normally be loaded and displayed upon clicking on the list items ...

What is the best way to create a footer in this scenario and is there a method to perfectly center an

What is the best way to position the footer at the bottom of the page without overlapping the top content? Is there a method to center both the height and width of the header element on the page? Can you review the layout of this webpage and provide feed ...

css failing to load properly following javascript redirection

Upon clicking the button labeled id=cancel, the user will be directed to index.php. $('#cancel').click(function() { if(changes > 0){ $('#dialog-confirm').dialog('open'); }else{ window.location.href = ". ...

Switch image upon hover within a sprite

I have a sprite that I utilize for displaying various images. Currently, my aim is to change the sprite image upon hovering using solely CSS. .sprE { background-color: transparent; background-image: url(/i/fW.png); background-repeat: no-repeat; height: 30 ...