Creating a new grid row when changing the order of elements in the grid column arrangement

Dealing with HTML that is not under my control, I am attempting to rearrange the elements using CSS grid.

Despite setting grid-template-rows: 1fr, the layout shows 2 rows with 3 columns each instead of a single row with 3 columns.

.grid-container {
  border: 10px solid red;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: 1fr;
}

.item-1 {
  grid-column: 3;
  border: 1px solid blue;
}

.item-2 {
  grid-column: 2;
  border: 1px solid cyan;
}

.item-3 {
  grid-column: 1;
  border: 1px solid green;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
  </head>
  <body>
    <div class="grid-container">
      <div class="item-1">One</div>
      <div class="item-2">Two</div>
      <div class="item-3">Three</div>
    </div>
  </body>
</html>

Answer №1

Instructing the items to be in a specific column is not sufficient to control the layout. It is necessary to also specify the row where you want them to appear.

.grid-container {
  border: 10px solid red;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: 1fr;
}

.grid-container * {
  grid-row:1;
}

.item-1 {
  grid-column: 3;
  border: 1px solid blue;
}

.item-2 {
  grid-column: 2;
  border: 1px solid cyan;
}

.item-3 {
  grid-column: 1;
  border: 1px solid green;
}
    <div class="grid-container">
      <div class="item-1">One</div>
      <div class="item-2">Two</div>
      <div class="item-3">Three</div>
    </div>

Alternatively, you can use grid-auto-flow: dense to automatically fill in any empty spaces within the grid layout.

.grid-container {
  border: 10px solid red;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: 1fr;
  grid-auto-flow: dense;
}

.item-1 {
  grid-column: 3;
  border: 1px solid blue;
}

.item-2 {
  grid-column: 2;
  border: 1px solid cyan;
}

.item-3 {
  grid-column: 1;
  border: 1px solid green;
}
<div class="grid-container">
  <div class="item-1">One</div>
  <div class="item-2">Two</div>
  <div class="item-3">Three</div>
</div>

Answer №2

If you're looking for a solution, consider utilizing the order property along with negative values for simpler indexing when coding.

.grid-container {
  border: 10px solid red;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

.item-1 {
  border: 1px solid blue;
  order: -1;
}

.item-2 {
  border: 1px solid cyan;
  order: -2;
}

.item-3 {
  border: 1px solid green;
  order: -3;
}
<div class="grid-container">
  <div class="item-1">One</div>
  <div class="item-2">Two</div>
  <div class="item-3">Three</div>
</div>

Answer №3

Although you already have a correct answer, just for amusement, another possibility could be utilizing the CSS property direction: rtl.

.grid-container {
  border: 10px solid red;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: 1fr;
  direction: rtl;
  text-align: left;
}

.item-1 {
  border: 1px solid blue;
}

.item-2 {
  border: 1px solid cyan;
}

.item-3 {
  border: 1px solid green;
}
<div class="grid-container">
  <div class="item-1">One</div>
  <div class="item-2">Two</div>
  <div class="item-3">Three</div>
</div>

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 Youtube Subscribe Embed feature is causing my image to lose its corners

When I use the official embed code from Google Developers to embed my YouTube channel, it cuts off the corners of my image and leaves white corners. This doesn't look good on my website with a black background. <script src="https://apis.google ...

What sets npm install apart from manual installation?

I've been exploring requirejs recently. I'm trying to decide between installing it using npm install requirejs or manually downloading it from the website. Are there any differences between the two methods? Are there any advantages or disadvantag ...

Instructions on how to toggle the visibility of a div when hovering over a different a tag

To keep things simple, I'm looking to create a visibility toggle effect on a div when someone hovers over an anchor tag. Similar to the behavior of the four buttons on this example link: The issue I'm facing is that I want the div to appear or b ...

Changing the color of the active link in the navigation bar when the href is empty

I need to change the active link color after it has been clicked. For example, when I click on the "START" link, I want the text to appear in a different color. Currently, my code only works when I have "#" links, such as when I click on "O HODOWLI", the " ...

Is using 'box-sizing: borderbox' enough to avoid horizontal scrolling?

Currently undertaking The Odin Project, my task was to replicate the Google homepage. However, I have encountered a challenge that I am struggling to resolve. I attempted to use box-sizing: border-box; to prevent the page from extending beyond the width ...

Issues with Angular HTML failing to load correctly

Greetings, I am seeking assistance with an issue that has been challenging me for quite some time. As a newcomer to Angular JS, I may be overlooking something obvious. Here is the scenario: I am utilizing ng-repeat on a directive in the following manner: ...

I'm struggling to retrieve and display data from my Flask application using console log. It seems to be a challenge when combining it with

I am encountering an issue while attempting to load data into my Flask page and log it in the console. The error I keep receiving is shown below: VM165:1 Uncaught ReferenceError: data is not defined at <anonymous>:1:13 Below are snippets of cod ...

When attempting to send data using jQuery to PHP, an issue arises where PHP is unable to receive the information, resulting in an undefined variable

Has anyone successfully sent data from an HTML select to a PHP file using jQuery? I've tried multiple solutions suggested here, but none seem to be working for me. Below is the code I'm using: <select id="city" name="city" > <optgro ...

Executing a function every time a prop is updated within the component

I have a prop named transcript in one of my components. Whenever I speak a voice intent, it gets updated. I want to execute a function every time the transcript changes and pass the transcript as an argument. In this code snippet, I attempted to use an On ...

the method of utilizing JavaScript to showcase an HTML form

I have created a form for entering an employee's skills. Below the form, there is a link to add a new skill. When this link is clicked, the form should be displayed again. My code looks like this: <html> <head> ...

Add() function is not duplicating the formatting

I'm attempting to replicate the content below inside a DIV. <ul class="pie-legend"><li><span style="background-color:#0066CC"></span>10-0-1</li><li><span style="background-color:#33CC33&q ...

"Eliminated the navigation bar - looking for a way to get rid of the extra

For a recent project, I utilized this template. In this particular project, the navigation bar was unnecessary so I removed that code. Now I'm looking to relocate the carousel to the top of the page. Initially, I tried using .carousel { margin-top:0p ...

D3 and Select struggling to conform to HTML layout

I've been putting in a lot of effort working with D3 to align my HTML layout, but I'm struggling to get my charts to align as desired. What I'm aiming for is: BAR CHART 1st PIE CHART, 2nd PIE CHART, FIELDSET So, what I need ...

Pair of buttons triggering the submission of a form

I'm encountering an issue where I have 2 buttons in my form, one for submitting and one for going to the previous page, but both seem to be performing the same action. How is this happening? <div style="position:fixed; left: 50px; ...

Angular Square Grid Design

Attempting to create a square grid layout using CSS for ng-repeat items. Essentially, I am looking to have one big square followed by four smaller squares that combined have the same width and height as the big square. Here is my CSS: .container{ widt ...

Learn how to utilize Jquery to create a dynamic slide effect that toggles the

I am looking to change a banner on click functionality. Currently, I have hidden the 2nd banner and banner 1 is displayed. When the arrow is clicked, I want banner 1 to hide and banner 2 to show. The challenge is that I attempted using HTML for this task. ...

Is there a way to prompt the native browser message for HTML5 form validation on a custom element?

https://i.sstatic.net/3wuWh.png Can the native validation UI be activated on non-default input elements like divs or similar? I want to develop a custom React element with validation without relying on hidden or invisible input fields. ...

React App anchor tag's external links fail to function properly on subsequent clicks when accessed through mobile devices

I have encountered an unusual issue with <a> anchors for external links in my React App on mobile viewports. Initially, clicking an external link opens a new tab just fine on mobile. However, subsequent link clicks on the same or other <a> elem ...

Is it possible to display and conceal divs/sections by clicking on navigation bar buttons without utilizing javascript or JQuery?

As I work on coding a page for an assignment, I'm faced with the challenge of implementing a navigation bar with three sections: Business Plan, About Us, and Contact Us. While trying to incorporate clickable buttons using label and radio input element ...

Issue with displaying dropdown menu icon in Bootstrap 5

Currently utilizing Angular 14 alongside Bootstrap 5. The code snippet below showcases a simple Bootstrap dropdown menu: <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" type="button" id=&quo ...