Utilize duplicate named CSS grid rows as a reference

Summary; This method currently works, but it requires adding multiple nth-child selectors for each new person and their answer. I am seeking a solution that can dynamically handle any number of rows using an even and odd selector to simplify the process.

The grid layout displays a person's name, image, and text. The layout alternates between showing the name and image on the right side in one row, then switching to the left side in the next row, as demonstrated in the code snippet below.

Currently, every 4th nth-child selector is responsible for styling each person. To automate this process, I would like to have dynamic evens and odds child selectors...

I attempted using named-rows, however, the third person ends up displacing the first two individuals instead of occupying the correct row position.

Is there a way to utilize named rows in a repeatable manner or achieve completely dynamic row management?

My current workaround involves manually listing over 60 nth-child selectors to accommodate up to 15 persons, resulting in bloated CSS. Alternatively, I could add the grid-row-start attribute directly to the HTML elements (which is feasible server-side but not preferred).

.CandidateAnswers {
    display:grid;
    grid-template-columns: 1fr 5fr;
    grid-template-areas: 
        "name blank rev_name"
        "image description rev_image";
    justify-items:center;
    /*grid-template-rows:
        repeat(20,
            [row-name] auto
            [row-detail] auto
            [row-name-rev] auto
            [row-detail-rev] auto
        );*/
}


.CandidateAnswers *:nth-child(1){
    grid-column-start: 3;
    grid-row-start: 1;
}
.CandidateAnswers *:nth-child(2){
    grid-column-start: 1;
    grid-column-end: span 2;
    grid-row-start: 1;
}
.CandidateAnswers *:nth-child(3){
    grid-column-start: 3;
    grid-row-start: 2;
}
.CandidateAnswers *:nth-child(4){
    grid-column-start: 1;
    grid-column-end: span 2;
    grid-row-start: 2;
}

.CandidateAnswers *:nth-child(5){
    grid-column-start: 1;
    grid-row-start: 3;
}
.CandidateAnswers *:nth-child(6){
    grid-column-start: 2;
    grid-column-end: span 2;
    grid-row-start: 3;
}
.CandidateAnswers *:nth-child(7){
    grid-column-start: 1;
    grid-row-start: 4;
}
.CandidateAnswers *:nth-child(8){
    grid-column-start: 2;
    grid-column-end: span 2;
    grid-row-start: 4;
}
.CandidateAnswers *:nth-child(9){
    grid-column-start: 3;
    grid-row-start: 5;
}
.CandidateAnswers *:nth-child(10){
    grid-column-start: 1;
    grid-column-end: span 2;
    grid-row-start: 5;
}
.CandidateAnswers *:nth-child(11){
    grid-column-start: 3;
    grid-row-start: 6;
}
.CandidateAnswers *:nth-child(12){
    grid-column-start: 1;
    grid-column-end: span 2;
    grid-row-start: 6;
}
<div class="CandidateAnswers">
    <h3>Person 1</h3>
    <span></span>
    <img src="https://i.sstatic.net/j1mHu.jpg" alt="Person 1">
    <p>Lorem ipsum dolor &amp; stuff blah blah blah blah blah blah blah</p>
    
    <h3>Person 2</h3>
    <span></span>
    <img src="https://i.sstatic.net/j1mHu.jpg" alt="person 2">
    <p>I'm a person who does persony things and I'm a people and who knows maybe we'll one day be friends</p>
    
    <h3>Person 3</h3>
    <span></span>
    <img src="https://i.sstatic.net/j1mHu.jpg" alt="Person 3">
    <p>I also am here</p>
</div>

Answer №1

I made some adjustments to the HTML code by including grid-auto-flow:column;, removing grid-row-start, and eliminating named areas.

With the help of MDN's nth-child documentation, I was able to correctly implement my nth-child selectors.

.CandidateAnswers {
    display: grid;
    justify-items: center;
    grid-auto-flow: column;
}

.CandidateAnswers *:nth-child(8n-3) {
    grid-column-start: 3;
}
.CandidateAnswers *:nth-child(8n-2) {
    grid-column-start: 1;
    grid-column-end: span 2;
}
.CandidateAnswers *:nth-child(8n-1) {
    grid-column-start: 3;
}
.CandidateAnswers *:nth-child(8n) {
    grid-column-start: 1;
    grid-column-end: span 2;
}

.CandidateAnswers *:nth-child(8n+1) {
    grid-column-start: 1;
}
.CandidateAnswers *:nth-child(8n+2) {
    grid-column-start: 2;
    grid-column-end: span 2;
}
.CandidateAnswers *:nth-child(8n+3) {
    grid-column-start: 1;
}
.CandidateAnswers *:nth-child(8n+4) {
    grid-column-start: 2;
    grid-column-end: span 2;
}

Answer №2

Here are some optimizations you can make to your code:

.CandidateAnswers {
    display:grid;
    grid-auto-flow:dense;
    grid-auto-columns:1fr; /* setting equal width columns */
}

h3 {
  grid-column:span 4;  /* creating 4 columns for heading */
  width:25%; /* width of one column*/
  text-align:center;
}
p {
  grid-column:span 3; /* spanning across 3 columns */
  width:66.66%;  /* taking up the width of two columns*/
}

h3:nth-child(6n + 1),
p:nth-child(6n + 3){
  margin-left:auto; /* adjusting position to the right */
}

img {
  margin:auto;
}
img:nth-child(6n + 2){
   grid-column:4; /* positioning image in fourth column */
}
img:nth-child(6n + 5){
   grid-column:1; /* positioning image in first column */
}
<div class="CandidateAnswers">
    <h3>Person 1</h3>
    <img src="https://i.sstatic.net/j1mHu.jpg" alt="Person 1">
    <p>Lorem ipsum dolor &amp; stuff blah blah blah blah blah blah blah</p>
    
    <h3>Person 2</h3>
    <img src="https://i.sstatic.net/j1mHu.jpg" alt="person 2">
    <p>I'm a person who does persony things and I'm a people and who knows maybe we'll one day be friends</p>
    
    <h3>Person 3</h3>
    <img src="https://i.sstatic.net/j1mHu.jpg" alt="Person 3">
    <p>I also am here</p>
</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

What could be causing my webpage content to be partially hidden by my CSS menu?

I am currently working on my website which features a CSS menu with my website logo, a text box, and a dropdown. Additionally, I have created a login form using a table like the one shown below. <p>&nbsp;</p><table width="40%" border="0 ...

What is the process for encoding images in HTML code?

Is there a method to embed a background image directly in the HTML code? For instance: background-image: url(data:image/gif;base64,XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX); If I have an image file, can I encode its data and t ...

The integration of a side panel with a chrome extension is experiencing issues

I am working on a chrome extension with the following functionalities - Extract URL path from a specific webpage. (The webpage's URL remains constant) Open this URL in a new tab. It can be either http:// or https://. Embed an iframe containing a sim ...

CSS: elements that are only visible when positioned above specific elements

Can we create an element that is only visible above specific other elements? For instance, in the following code snippet, I want the .reflection element to be visible only above the .reflective elements (located at the top and bottom) and not visible on t ...

LESS: Using variable values in mixin and variable names

I am looking to simplify the process of generating icons from svg-files while also creating a png-sprite fallback for IE8 support. I am using grunt.js and less. I was inspired by the implementation on 2gis.ru: (in Russian), where they used technologies s ...

Tips on making a multiline label using html

Can anyone help me with creating a multiline label in HTML and MVC3 using Razor engine? I would appreciate any ideas. Thank you! ...

There seems to be a disconnect between the CSS and HTML files

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JavaScript Basics</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="c ...

When dynamically loaded HTML is involved, Javascript ajax calls often fail to execute properly

Currently, I am in the process of creating a JavaScript script that can facilitate clicking through <a href> links by only replacing the inner HTML instead of reloading the entire page. The interesting thing is, it seems to be functioning properly, e ...

Using jQuery to animate the grayscale filter with a value of 0

Currently, I am implementing the Waypoints jQuery plugin to add animation to a series of images as a user scrolls over them. My goal is to apply certain CSS properties - opacity:1, filter:grayscale(0), -webkit-filter:grayscale(0), and -moz-filter:grayscale ...

Web Development: Custom Search Form

I am looking to create a website with a form similar to this one, but I'm struggling to figure out how to incorporate all three components into a single form using only HTML and CSS - no javascript. Do you have any suggestions or advice on how to achi ...

determining CSS selector

Having trouble identifying a locator using CSS. There are 10 elements with the same locator name on the webpage. Each web element has the same XPath value: //div[@class='thumb_image'] The web element list size is 10. If I want to access the 5t ...

Create an element that can be dragged without the need to specify its position as absolute

I am having an issue where elements I want to drag on a canvas are not appearing next to each other as expected. Instead, they are overlapping: To make these elements draggable, I am utilizing the dragElement(element) function from https://www.w3schools.c ...

Adjusting the line-height and baseline to accommodate various screen sizes on both mobile and desktop

This problem seems to keep coming back for me. I can't figure out what's causing it. Here are two images showing the issue: On desktops: On mobile devices: As you can see, the text is not vertically centered on mobile devices. Strangely, thi ...

Instructions on keeping a numerical counter at its current value for all site visitors

Recently, I integrated a number counter into my website. However, I am facing an issue where the count resets to zero whenever a new visitor accesses the site. I'd like the count to remain persistent and update based on the previous count. For instanc ...

Creating a Piechart in Kendo UI that is bound to hierarchal remote data

I am facing an issue with binding remote data to a pie chart while managing a grid with dropdown sorting options. The grid is working fine, but I am unable to display the hierarchical data on the pie chart as categories. <!DOCTYPE html> <html> ...

Identifying HTML attributes for Web Scraping Techniques

I'm currently working on developing a web scraper and facing challenges in identifying element attributes. My approach involves scanning through the code like moving a "scanning head" from left to right, searching for specific character strings such ...

`Nginx with a touch of material design`

Currently, I'm in the process of implementing material design UI on a functioning web application. The application utilizes Nginx, PHP, and PostgreSQL. While I have proficiency in PHP and PostgreSQL to ensure the functionality (code composed in notepa ...

Tips for adjusting the height of a jQuery UI widget control through CSS

I have implemented a jquery widget on my asp.net webforms and am trying to adjust the height of the select control from the default 22px to 33px. I noticed in Google developer that the height is originally set like this: The current height setting is loca ...

Switching the default browser for npm live-server

When I use the npm live-server package to preview my website as it changes, it keeps opening in Edge even though Chrome is set as my default browser on my system. I attempted to use the command suggested on the npm website: live-server --browser=chrome H ...

Center the cropped image within a div container

Is there a way to set a maximum height for a div containing an image and hide the parts of the image that exceed this height, while keeping it centered vertically inside the div? For example, if the browser width is 1200px and the image aspect ratio is 4:3 ...