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

Exploring ways to customize the input color of Material UI TextField when it is disabled [Version: 5.0.8]

I am having trouble changing the border color and text color when an input is disabled. I have tried multiple variations, as seen below: const textFieldStyle = { '& label': { color: darkMode?'#1976d2':'', } ...

What is the best way to include an active item within an li tag inside a div container?

Here is the structure for a widget that I have, and I want to add the active class to the li. I've tried doing this but it's not working and I'm unsure where the issue lies. var selector = '#recent-posts-5'; var url = window. ...

Confusion arises when using Materialize CSS for selecting elements

Currently, I am working on an app using Cordova. Within this project, I have incorporated jQuery Mobile's CSS and JS along with Materialize CSS and JS. However, I recently encountered an issue after integrating the Materialize files. Specifically, whe ...

When you click, a new webpage opens within my website, similar to the feature on Facebook

When using Facebook on mobile, clicking on a link to a person's website opens the website in front of the Facebook page. However, when users press the X in the top left corner, it deletes the webpage but keeps Facebook open. On my desktop computer, I ...

Is there a way to turn off _moz_resizing on my browser?

I am currently using the nicEdit editor and have successfully integrated my own custom image resizing script. However, I am facing an issue where the default _moz_resizing feature in Firefox is interfering with my custom script. My goal is to have specifi ...

Creating a variable in JavaScript

In my HTML body, I have set up a global variable named index with the value <%var index = 0;%>. This allows me to access it throughout the code. I'm trying to assign the value of $(this).val() to <% index %>, but I can't seem to get ...

How do I make an image fill the entire space of a div using JQuery and CSS?

html: <body> <div class="custom-div"><input type="button" id="x" name="button" value="Search" onclick="showUser()" class="button"/></div> <input type="image" name="button" value="Search" onclick="showUser()" class="bu ...

Conquer the issue of incessant AJAX page refreshing

Currently, I am working on an application where I handle numerous form submissions and utilize ajax to send data to a server running on Node.js. One of the features includes updating a table upon button click, which triggers a spinner to load as an indic ...

Browsing a website to gather multiple pieces of information from it

About a month ago, I posted a question seeking guidance on how to extract a single value from a webpage. The solution was successful, but then I realized there are numerous stocks out there. After some consideration, I decided to attempt creating a loop fo ...

Adjustable Greybox Overlay Resizes Window Dimensions

Utilizing greybox on my business website allows users to easily log in and access their accounts. View an example of my site However, when the login or sign up window is activated at the top of the page, the page or window size changes, causing scroll bar ...

Tips for hiding a soft keyboard with jQuery

Here is a text button: <input type="text" class="field" id="something" name="something" placeholder="text" autofocus /> I want to prevent the android soft keyboard from popping up and keep the focus on this field. I only want to do this for this ...

Toggle the menu using jQuery on unordered list items

Below is the HTML code I am using : <ul class="main-block"> <li class="firstLevel"> <a href="#category">EXAMPLE CATEGORY 1</a> <ul class="dijete"> <li class="child"> <a href="some-sub-categ ...

What are the steps to customize the format of Vue3 Datepicker extensions?

Is there anyone who can lend a hand? I am currently using the Vue3 Datepicker and I have received a value that looks like this Thu Jun 23 2022 17:14:00 GMT+0700 (Western Indonesia Time) Does anyone know how to transform it into the following format? Thu, ...

The hyphen character is not displaying correctly

When I upload an XML file for RSS feeds, the link shows all the feeds correctly. However, when viewed on IE, the French word Tobique–Mactaquac (with a hyphen between two words) appears without the hyphen as TobiqueMactaquac. My charset is set to UTF-8. ...

Retrieving data from a Ruby class based on a specific condition

Currently, I have a code snippet that adjusts the formatting of an editing page based on the number of values a question contains. <% (<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aa9b8484eadbdfcfec8cdcac3c5c484dbc6ece0 ...

The dropdown login form is malfunctioning on my Wordpress website

After reading various tutorials and guides online, I managed to set up a login screen. You can view the code in my jsfiddle: Jsfiddle. Unfortunately, I am facing issues with making the code function correctly. Being new to Jquery, I might have made a begin ...

Changing the image's flex-grow property will take precedence over the flex settings of other child

This is the error code I am encountering, where "text1" seems to be overridden <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <!--mobile friendly--> <meta name="view ...

Internal link formatting using CSS styling

I have a question about formatting links with fonts and colors to make them clickable. I managed to achieve the desired font and color styles using a specific code snippet, but struggled to make the link clickable: <table border="0" cellpadding="1" cel ...

Customizing browser titles for various article pages on Joomla 3.2 for improved SEO performance

Is there a way to incorporate unique browser titles for various article pages? Additionally, I am seeking guidance on how to integrate distinct meta tags for different pages within a Joomla website... ...

How to handle Component binding change events in AngularJS

I have developed a component in AngularJS that displays data. However, when the customer binding changes, I need to call a service in the component controller, but it is not updating. Below is the code snippet: In my Test1.html file: <tab-customer tit ...