What is the most efficient way to create a vertically stacked grid with minimal reliance on JavaScript?

When using an AngularJS ng-repeat, I encounter an issue with aligning elements on a new line. Instead of aligning the element above it, all elements on the same 'row' align with the lowest-hanging element.

Even though I am using Bootstrap, I couldn't figure out how to utilize the grid system for data that spans variable heights per column. Currently, all divs are floated left with a max-width and height: auto, creating a responsive grid but not respecting individual element placement per row.

For instance, in this grid the first element on the second 'row' (243 x 399) should be positioned a certain distance below the first element on the first 'row' (224 x 305), based on a specified margin. Similarly, 320 x 347 would have the corresponding offset from 344 x 246, and so forth.

I could achieve this behavior using Javascript or jQuery, but it seems like a hacky solution. I believe there must be a more idiomatic approach using CSS to achieve the desired layout.

var routeApp = angular.module('routerApp', []);
routeApp.controller('loadCtrl', ['$scope',
  function($scope) {
    $scope.tiles = [];
    for (var i = 0; i < 10; i++) {
      var d = Math.floor(Math.random() * (400 - 200 + 1)) + 200;
      var e = Math.floor(Math.random() * (400 - 200 + 1)) + 200;
      $scope.tiles.push("http://placehold.it/" + d.toString() + "x" + e.toString());
    }
  }
]);
.tiles {
  float: left;
  max-width: 200px;
  width: 100%;
  height: auto;
  margin: 5 auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body ng-app="routerApp">
  <div ng-controller="loadCtrl">
    <div ng-repeat="til in tiles track by $index">
      <a href="www.google.com"/>
        <img class="tiles" ng-src="{{til}}"/>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

Answer №1

I provided a solution to a similar inquiry here

If that solution doesn't meet your needs, feel free to reach out for further clarification.

Essentially, using floats is akin to attempting to fix a leak with scotch tape. =)

Answer №2

The column-width CSS property provides guidance on the optimal width for columns.

For a CSS-only solution, utilize the column-width and column-gap (and possibly column-count) properties to establish your grid layout.

Be sure to visit Caniuse.com for Browser Support Information:

  1. column-width
  2. column-gap
  3. column-count

Sample CSS Code

.grid {
    -moz-column-width: 200px;
    -webkit-column-width: 200px;
    column-width: 200px;
    -moz-column-gap: 0;
    -webkit-column-gap: 0;
    column-gap: 0;
    -webkit-perspective: 1
}

.grid .grid-block {
    display: inline-block;
    width: 100%;
}

.grid .grid-block .tiles {
    width: 100%;
    border: 1px solid red;
}

Example 1: Flush Columns - Execute Snippet in FullPage

var routeApp = angular.module('routerApp', []);

routeApp.controller('loadCtrl', ['$scope',
  function($scope) {
    $scope.tiles = [];
    for (var i = 0; i < 50; i++) {
      var d = Math.floor(Math.random() * (400 - 200 + 1)) + 200;
      var e = Math.floor(Math.random() * (400 - 200 + 1)) + 200;
      $scope.tiles.push("http://placehold.it/" + d.toString() + "x" + e.toString() + "/000/fff");
    }
  }
]);
body {
  padding: 0;
}
.grid {
  -moz-column-width: 200px;
  -webkit-column-width: 200px;
  column-width: 200px;
  -moz-column-gap: 0px;
  -webkit-column-gap: 0px;
  column-gap: 0px;
  -webkit-perspective: 1
}
.grid .grid-block {
  display: inline-block;
  margin: 0;
  width: 100%;
}
.grid .grid-block .tiles {
  width: 100%;
  height: auto;
  border: 1px solid teal;
  transition: all 300ms linear;
}
.grid .grid-block .tiles:hover {
  opacity: 0.45;
}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

<body ng-app="routerApp">

  <div ng-controller="loadCtrl" class="grid">
    <div ng-repeat="tile in tiles track by $index" class="grid-block">
      <a href="https://www.google.com">
        <img class="tiles" ng-src="{{tile}}" />
      </a>
    </div>
  </div>

</body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.2/angular.js"></script>

Example 2: Columns with Spacing - Execute Snippet in FullPage

var routeApp = angular.module('routerApp', []);

routeApp.controller('loadCtrl', ['$scope',
  function($scope) {
    $scope.tiles = [];
    for (var i = 0; i < 50; i++) {
      var d = Math.floor(Math.random() * (400 - 200 + 1)) + 200;
      var e = Math.floor(Math.random() * (400 - 200 + 1)) + 200;
      $scope.tiles.push("http://placehold.it/" + d.toString() + "x" + e.toString() + "/008080/000");
    }
  }
]);
body {
  padding: 3px;
}
.grid {
  -moz-column-width: 200px;
  -webkit-column-width: 200px;
  column-width: 200px;
  -moz-column-gap: 6px;
  -webkit-column-gap: 6px;
  column-gap: 6px;
  -webkit-perspective: 1
}
.grid .grid-block {
  display: inline-block;
  margin: 3px 0;
  width: 100%;
}
.grid .grid-block .tiles {
  width: 100%;
  height: auto;
  border: 1px solid black;
  transition: all 300ms linear;
}
.grid .grid-block .tiles:hover {
  opacity: 0.45;
}
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

<body ng-app="routerApp">

  <div ng-controller="loadCtrl" class="grid">
    <div ng-repeat="tile in tiles track by $index" class="grid-block">
      <a href="https://www.google.com">
        <img class="tiles" ng-src="{{tile}}" />
      </a>
    </div>
  </div>

</body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.2/angular.js"></script>

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

Rotate Chevron icon downwards on mobile devices in a Multilevel Dropdown Bootstrap 4

Bootstrap 4 is being utilized for my current project, I am looking to modify the rotation of the chevron icon in a multi-level dropdown menu specifically on mobile devices when the parent link is tapped, Here is the code snippet for the multi-level dropd ...

How to increment a date by 1 in an AngularJS function

I have a table with 5 cells in the header, each representing a different week (for example: Week 01, Week 02, and so on). In the first cell, the week is displayed like this: <div class="monthCells">Week {{vm.selectedPeriod}}</div> and it sho ...

Ensure that any relevant information is placed adjacent to a floated image

I am currently designing a responsive webpage featuring images of people's faces placed next to descriptive text. At smaller screen widths, everything looks perfect. However, as the page widens, the text for one person starts next to the image of the ...

Tips for aligning placeholder and text at the center in a React Material UI TextField

Existing Layout: https://i.stack.imgur.com/Zv4Tg.png Desired Layout: https://i.stack.imgur.com/Xuj6O.png The TextField element is displayed as follows: <TextField multiline={false} autoFocus placeholder={props.defaultAmt} ...

Limiting the resizing boundaries in jquery ui: A step-by-step guide

Currently, I am using jquery ui to adjust the size of a div container. My goal is to restrict the resizing to a specific designated area. containment:"parent" After implementing the code, the drag operation adheres to the specified area boundaries. How ...

Unlock the potential of AngularJS services with this innovative design strategy

I am currently developing an AngularJS client application that will communicate with a REST server. To handle the interaction between the client and server, I have decided to use the $resource abstraction from AngularJS. Each resource is being written as ...

center a horizontal line using StyledSheets in your project

After drawing a horizontal line, I noticed that it is positioned towards the left side of the screen. I am hesitant to increase its width. Are there any other methods to move it to the center? I attempted wrapping it with another view and using alignConten ...

Arrange text in a line below neatly in a list

My goal is to line up items on the same row, as shown in the screenshot. I need the opening hours to align consistently in the list. The data is retrieved from a database and displayed using Vue. This is how I currently display the opening hours and days. ...

Will this folder organization be appropriate for an AngularJS application?

Hello, I currently have an angularJS file structure that looks like this: project/ -bin -model -node_modules -public -routes -views app.js, package.json, package-lock.json Within the public folder, there are: -images -javascripts -stylesheets I ...

Setting a div to occupy the entire height of a webpage using HTML and CSS

Is there a way to make the div (the blue box) fill the entire page? I tried setting the body tag to height:100%, but it didn't work. You can view an example at http://jsfiddle.net/rVyrX/1/ ...

Is there a lack of control when it comes to using AngularJS

While I continue to enhance my AngularJS skills, I find myself questioning the level of control I have with less direct manipulation through JavaScript. Everything seems to be driven by bindings in the HTML code/attributes. How can I interact with $rootS ...

Modifying the color of the highlighted selection in a dropdown select box

Is it possible to change the blue highlight on this dropdown to gray? Check out this demo of a select box I am aiming to alter the highlight color to gray, can someone help me achieve this? select { border: 0; color: #EEE; background: transparen ...

Tips for designing a responsive layout with bordered paragraphs of text

I inserted multiple paragraphs of text into a column. To ensure the text is responsive and surrounded by a border, I created simple HTML code within a div tag: <p> text here</p> <p> 2nd para</p> <p> 3rd para</p> <p&g ...

AngularJS - Finding the position of an element with the query "is located at the bottom of the page"

How can we effectively determine if there is more content to be scrolled in Angular, especially when dealing with a single-page app with a fixed bottom navbar? I am looking for a way to visually signal to users that there is additional content available b ...

Struggling to create a responsive navbar for a landing page using Next.js and TailwindCSS

I'm currently working on a landing page using nextjs, tailwind css, and shadcnui. The design looks great on desktop with a logo and two links, but it's not responsive on smartphones. https://i.stack.imgur.com/HVQsa.png On mobile devices, the lay ...

Issue with continuous loader malfunction

I integrated a 3-second mini-loading animation on my website. It shows up every time I refresh the site or navigate to another page. The issue I'm facing is that once I add the loading section, it never stops (it should only last for 3 seconds). Usua ...

What is the best way to enlarge a column contained within a container, allowing an image to spread from the center of the column all the way to the right edge of the page?

I am trying to modify my image within a bootstrap container to extend it to the side while keeping the rest of the layout unchanged. Here is what I currently have: https://i.sstatic.net/Mcukl.jpg. There is a gap on the right side of the image that I want t ...

Incorporating Bootstrap SCSS files into a Django application

My query pertains to the most recent Bootstrap 4.x release (beta) I bring in the CSS from the CDN using <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6 ...

Using static typing in Visual Studio for Angular HTML

Is there a tool that can validate HTML and provide intellisense similar to TypeScript? I'm looking for something that can detect errors when using non-existent directives or undeclared scope properties, similar to how TypeScript handles compilation er ...

I'm looking to customize the text displayed on the screen from my JSON file. Is there a way to hide this text?

I am currently working with a JSON data file that contains values and displays them on the screen alongside a CSS property width. However, I do not want the output to be the actual numerical data; instead, I aim to connect the JSON file so that the value s ...