How to Arrange Multiple Divs in a CSS Grid Layout without Using Floats

I've been working on structuring a webpage with multiple divs containing images in a grid layout, but not using CSS Grid. Here's an example of what I'm trying to achieve:

<div class="some classes"> Some Text </div> <br>

<a href="somwhere"&gr;
  <div class="some classes float-left"> 
     <img src="http://someimage" height="200px" alt="An image"/> <br> 
     <div class="some other classes"> some image text </div>
  </div>
</a>
... (repeated div links)

<footer> some footer </footer>

Using CSS

.float-left {
  float:left;
}

The CSS classes are just placeholders without any specific meaning. The challenge lies in creating a layout where an unknown number of divs are generated automatically based on variable parameters. I initially used float:left, but when I added a footer it caused alignment issues. This could be due to the use of float property. I'm looking for an alternative approach that is responsive and does not rely on JavaScript. Any suggestions?

Answer №1

As mentioned earlier, you have the option to utilize either auto-fit or auto-fill along with minmax to establish a responsive grid layout that adjusts based on the number of items present.

To implement this, simply add the following CSS rule to the containing element (which could also be the body):

grid-template-columns: repeat(auto-fill, minmax(minimum width value, maximum width value));

If you want the footer to consistently appear below all images and span the full width, you can use:
footer { grid-column: 1 / -1; }

This methodology will arrange as many images or elements in a row as possible based on the screen size. I've included a resize property in the snippet so you can resize the element and observe how the grid adapts:

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}

footer {
  grid-column: 1 / -1;
}


/* for demonstration purpose only */
.grid {
  resize: horizontal;
  overflow-x: auto;
  border: 2px solid black;
  box-sizing: border-box;
  min-width: 104px;
  max-width: 100%; 
}

.grid > * {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 20vh;
  color: white;
}

.grid > div:nth-of-type(3n+1) {
  background-color: red;
}

.grid > div:nth-of-type(3n+2) {
  background-color: darkblue;
}

.grid > div:nth-of-type(3n+3) {
  background-color: darkgreen;
}

.grid > footer {
  background-color: orange;
}
<div class="grid">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
  <div>6</div>
  <div>7</div>
  <div>8</div>
  <div>9</div>

  <footer>Footer</footer>
</div>

Answer №2

When using CSS, make sure to

.float-left {
    float: left;
} 

rather than

.float-left:{
    float: left;
}

It is important to eliminate the unnecessary :.

Answer №3

If I were to tackle this task, I would start by including the following CSS within your wrapper div:

.wrapper {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 10px;
  grid-auto-rows: 100px;
  grid-template-areas:
    "a b"
    "c d";
  align-items: start;
}

Next, for each individual grid 'div', you can implement the respective CSS styling like so:

.item1 {
  grid-area: a;
}
.item2 {
  grid-area: b;
}
.item3 {
  grid-area: c;
}
.item4 {
  grid-area: d;
}

Answer №4

To easily organize your elements, place them all within a div and utilize the align attribute: your_elements This method is optimal and straightforward for those who prefer not to employ floats.

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 Bootstrap tab feature is malfunctioning when a tab is using data-target instead of href

While developing bootstrap tabs for an Angular app, I decided to use the data-target attribute instead of the href attribute to avoid any interference with routes. Here's a snippet of how I structured the tabs: <ul class="nav nav-tabs" id="myTab"& ...

I am trying to center align this image, but for some reason, it's not cooperating

Attempting to center the image using margin-left: auto and margin-right: auto properties in the code snippet above has proven unsuccessful. The image is not aligning as desired. What could be causing this issue with the code? Are there any other techniq ...

Ensure the content of a textarea input is consistently centered vertically

I am currently working on a small project that involves using textareas. One issue I have encountered is that the text always starts at the top-left corner of the textarea. What I would like to achieve is having the content inside the textarea always cent ...

What is the mechanism through which a flexbox enlarges to accommodate its overflowing child elements?

I am working with a flexbox layout. Here is the HTML code structure: <div class="grid"> <div class="row"> <div class="column">Content</div> <div class="column">Content2</div> <div class="column">Con ...

Creating a grid pattern of boxes within a rectangle using CSS

I'm attempting to design a rectangle filled with colorful boxes Here is the code I'm using: <div class="col-md-3" > <div id="myViewport" style="height: 700px; width: 50px; padding: 10px; border: 5px solid gray; margin: 0;"> ...

Ways to specifically select an element based on its type and class

I am struggling to create a vanilla JS function that will resize fonts in a container with a specified class. I came across this demo, which seemed promising. However, the 'Javascript' version uses the $() jquery selector to target elements, maki ...

Tooltip Bootstrap timing

I am currently working on creating a navigation bar with icon-only buttons that display tooltips when touched or tapped. Here is the code I have implemented: $('a[rel="tooltip"]').tooltip({ animated: 'fade', placement: ' ...

What is the method for adjusting the font size of the label in an Angular mat-checkbox element?

I've been trying to adjust the font size of a mat-checkbox's label, but no matter what I do, the component's default styles keep overriding my changes: Using !important Trying ::ng-deep Applying a global style in styles.scss <mat-checkb ...

Struggling to make a variable pass in an Ajax post script

Having trouble passing the "filename" variable from main.php to save_sign.php. Can anyone provide assistance? main.php <? $filename="222222"; ?> <script> $(document).ready(function() { $('#signArea').signat ...

Can you provide me with a variable that will give me the total size of the scrollbar?

How can I determine the maximum number of pixels scrolled on a webpage? I attempted using window.pageXOffset, but it only gives the current scrolled amount. I require the total size at all times. ...

Strategies for implementing a minimum height for grid-template-column containers

Here's an issue I'm facing: when the first container has content, the other two containers without content end up being the same height as the first one: Check out this JSFiddle link for reference .grid { padding: 5 ...

Floating label positioned above the field in Bootstrap 5

I'm looking to incorporate a floating label similar to this design: The default setup is too bulky for what I need, especially the input area. I'm unsure of how to implement this. A previous framework worked for Bootstrap 4 but is incompatible ...

Insert item at the end of the box and move all elements upwards

Hello! I'm experimenting with creating something using javascript's createElement method. My goal is to achieve an effect similar to this image: https://i.stack.imgur.com/itKUK.gif Currently, my code is functional, but the animation goes from to ...

How can you make a Dropdown Box with Javascript?

My current table setup is as follows: <table> <tbody> <tr> <td>List 1</td> <td>List 2</td> <td>List 3</td> <td>....</td> </tr> <tr> <td>This section would be the dropdown ...

What is the reason behind CSS Animation creating gradients?

Can you explain why CSS Animation produces a gradient effect? Is there a way to remove the gradient effect if desired? How can you predict what gradient the animation will produce without executing the code? 0%, 100% { background-color: red } 50% { ...

Tips for positioning a div under a floated div

I've searched extensively for a solution to this problem with no luck. I'm having trouble moving my main code under the Navigation Bar. Maybe my code is too unconventional, but I can't seem to figure it out. Can anyone assist me? Thank you i ...

Real-time update of quiz results based on varying factors

In my quiz, I have set up variables that start at 0 and increase based on certain conditions. One variable should increase when a question is answered correctly, while the other should increase when a question is answered incorrectly. If you answer a quest ...

The smooth scroll feature is not functioning properly on the animated mouse-scroll down button

I've recently added an Animated Mouse Scroll Down button on my website. However, when I click the button, the smooth scroll feature is not working as expected. Important Note: I already have a separate button for navigating to the next section where ...

What is the best way to add an image element as a child of a list item only when its sibling checkbox is checked, upon clicking another button?

As a beginner in jQuery, I am working on developing a game where users can select and check 3 images from a list of ten. Following this selection, I aim to have the checked images added to a separate unordered list that I have already set up. <ul id="i ...

Placing a picture within a box that is 100% of the viewport height

After searching for a while, I still haven't found a solution to my problem. I am currently working on a Personal portfolio that has different sections with a height of 100vh each. However, I am facing difficulty in positioning the images within the s ...