Is it possible to use CSS to separate divs into distinct subgrids?

Is there a way to use CSS subgrids to display different divs in two columns, with all the .title's in the left column and the .card's in the right column? Here is a dynamic example for reference: https://codepen.io/dancanuck/pen/eYMLqzR

body {
  padding: 50px;
  font: 1em Helvetica Neue, Helvetica, Arial, sans-serif;
}

.grid {
  display: grid;
  gap: 20px;
  grid-template-columns: 0.3fr 1.4fr;
  grid-template-areas: "title card";
}

.card {
  border: 5px solid rgb(111, 41, 97);
  grid-row: auto / span 2;
  display: grid;
  gap: 0;
  grid-template-rows: subgrid;
}

.card h2 {
  background-color: rgba(111, 41, 97, .4);
  padding: 10px;
  margin: 0;
}

.card p {
  padding: 10px;
  margin: 0;
}

.title {
  border: 5px solid rgb(111, 41, 97);
  grid-row: auto / span 2;
  display: grid;
  gap: 0;
  grid-template-rows: subgrid;
}

.title h2 {
  background-color: rgba(11, 31, 27, .4);
  padding: 10px;
  margin: 0;
}

.title p {
  padding: 10px;
  margin: 0;
}
<div class="grid">
  <article class="card">
    <h2>This is the heading</h2>
    <p>This is the body of the card.</p>
  </article>
  <article class="card">
    <h2>This should be in the right column only.</h2>
    <p>This is the body of the card.</p>
  </article>
  <article class="card">
    <h2>This is the heading</h2>
    <p>This is the body of the card.</p>
  </article>
  <article class="title">
    <h2>Title</h2>
    <p>Should be in the left column only.</p>
  </article>
  <article class="card">
    <h2>This is the heading</h2>
    <p>This is the body of the card.</p>
  </article>
  <article class="card">
    <h2>This is the heading</h2>
    <p>This is the body of the card.</p>
  </article>
  <article class="card">
    <h2>This is the heading</h2>
    <p>This is the body of the card.</p>
  </article>
  <article class="title">
    <h2>Title</h2>
  </article>
</div>

Thanks!

Answer №1

.box{
grid-area:box;
}
.heading{
grid-area:heading;
}

Include the grid-area attribute in .box and .heading classes

Answer №2

Instead of using template areas, simply assign each element to its corresponding column and set the flow to column.

body {
  padding: 50px;
  font: 1em Helvetica Neue, Helvetica, Arial, sans-serif;
}

.grid {
  display: grid;
  gap: 20px;
  grid-template-columns: 0.3fr 1.4fr;
  grid-auto-flow: column;
}

.card {
  border: 5px solid rgb(111, 41, 97);
  grid-row: auto / span 2;
  display: grid;
  gap: 0;
  grid-template-rows: subgrid;
  grid-column: 2
}

.card h2 {
  background-color: rgba(111, 41, 97, .4);
  padding: 10px;
  margin: 0;
}

.card p {
  padding: 10px;
  margin: 0;
}

.title {
  border: 5px solid rgb(111, 41, 97);
  grid-row: auto / span 2;
  display: grid;
  gap: 0;
  grid-template-rows: subgrid;
  grid-column: 1;
}

.title h2 {
  background-color: rgba(11, 31, 27, .4);
  padding: 10px;
  margin: 0;
}

.title p {
  padding: 10px;
  margin: 0;
}
<div class="grid">
  <article class="card">
    <h2>Text</h2>
    <p>This content should be displayed in the right column only.</p>
  </article>
  <article class="card">
    <h2>Text</h2>
    <p>This content should be displayed in the right column only.</p>
  </article>
  <article class="card">
    <h2>Text</h2>
    <p>This content should be displayed in the right column only.</p>
  </article>
  <article class="title">
    <h2>Title</h2>
    <p>This content should be displayed in the left column only.</p>
  </article>
  <article class="card">
    <h2>Text</h2>
    <p>This content should be displayed in the right column only.</p>
  </article>
  <article class="card">
    <h2>Text</h2>
    <p>This content should be displayed in the right column only.</p>
  </article>
  <article class="card">
    <h2>Text</h2>
    <p>This content should be displayed in the right column only.</p>
  </article>
  <article class="title">
    <h2>Title</h2>
    <p>This content should be displayed in the left column only.</p>
  </article>
</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

Attempting to incorporate country flags into the Currency Converter feature

www.womenpalace.com hello :) i'm looking to customize my Currency Converter with Flags images. this is the code for the converter: <select id ="currencies" class="currencies" name="currencies" data-default-shop-currency="{{ shop.currency }}" ...

Looking for assistance with implementing a jQuery function for an onClick event on

I've been diving into learning jquery and managed to create a basic checkbox feature with a function that allows you to set all options as read-only by checking the "None of the above" button. <html> <body> <form id="diagnos ...

The CSS class is mistakenly being applied to the entire page instead of just the specific div it was

Here is the HTML code I am working with: <!DOCTYPE html> <head> <title></title> {% load staticfiles %} <link rel="stylesheet" type="text/css" href="{% static 'portfolio/mystyle.css' %}" /> <link rel="styl ...

I am currently engaged in a Portfolio project, where my objective is to ensure that all images are optimized for responsiveness

While Bootstrap ensures that relative images are responsive, absolute images may not be. This can cause alignment issues when the browser window is resized on mobile devices. .title-img { position: relative; width: 100%; height: 100%; } .skill-b ...

Incorporate a smooth infinite scroll feature in a CSS carousel that seamlessly loops without

Looking for a solution to the carousel jerk issue when it reaches the end of the loop? Is there a way to seamlessly loop start without any noticeable interruptions? Here is the code in question. Avoiding the use of JavaScript, is there a method to achieve ...

My index.html not successfully linking to the CSS file

I'm new to this and still learning, so please bear with me. I'm having trouble linking my CSS file to my index.html document in order to create a black banner at the top of the page. I've tried opening it in both Chrome and Explorer but noth ...

Show information upon opening

I am currently working on a project that involves 4 different divs, and I need the content of each div to be displayed based on dropdown selection. I found a code snippet that was close to what I needed, but after trying to adjust it, I haven't been a ...

Create a responsive DIV element within a website that is not originally designed to be responsive

I am looking to optimize the positioning of an ad div on my non-responsive website. The current setup has the ad displayed at 120x600 pixels, always floating to the right of the screen. While this works well for desktop or large devices, the display become ...

In the realm of typesetting, a punctuated line gracefully weaves its way between two

I am trying to create two sentences with a fixed width of 30%, but the first words in my div go to the next line. The class="left" words are not displaying properly, and then my second div "words" gets mixed with the first div. HTML <div class="bg"> ...

"Troubleshooting the lack of background image display in Next.js when using

Can anyone help me figure out why my background image disappears when I add a linear gradient? It shows up fine without the gradient, but as soon as I include it, the image vanishes. <div className="hero" style={{background: "linear-grad ...

Guide on developing a JavaScript script for implementing across numerous Bootstrap modals within a single webpage

I have been working on setting up a page with 14 different modals. Initially, all the modals were opening normally, but I recently discovered a way to make them draggable when opened. After some trial and error, I managed to implement this feature successf ...

Using Tailwind @apply within an Angular application's CSS file

I've noticed a yellow line appearing under the @apply in my CSS files, even though the styles are being applied correctly to the HTML components. This seems to be happening in every CSS file I use. Can anyone shed light on this issue? Removing these ...

(Original) redirect from specific url / url detection New text: "Redirection

Sorry for the long and confusing question, my apologies for wasting your time. I am still learning programming and what I really wanted to ask is "how can I retrieve a GET parameter using JavaScript?" Apologies for any inconvenience. ...

What is the best way to eliminate tags from a BeautifulSoup-scraped list?

When using beautifulsoup to scrape a page, I am trying to extract the "title" by searching for specific tags: title = [text.find_all('h1', {'class', 'entry-title'}) for text in texts] The resulting output is a list like this: ...

Scroll the div that is dynamically filled without affecting the scrolling of the main page

My current struggle involves using iScroll in my web project. The goal is to populate a list with articles and slide in a div over the list to display the selected article. While the basic functionality is in place, I face an issue where scrolling through ...

Receiving time slots on a bootstrap schedule

I recently developed a calendar using Bootstrap that allows users to select a start date and automatically sets the end date within a 7-day range from the chosen start date. However, I am facing a challenge in enabling users to also pick minutes along with ...

Why is the option value not visible in the dropdown selection list?

On my website, I am trying to use a date range to aggregate data from MySQL and display it based on the selected time frame. However, I am facing an issue where the values for each option in the drop-down menus are not showing up. Below is the PHP code I a ...

Where can I locate HTML codes within WordPress?

Upon inspecting the page source, I came across the following code snippet; https://i.sstatic.net/R7Fw3.jpg I attempted to search within WordPress for the HTML codes that are displayed here, but unfortunately, I was unable to locate them. I couldn't ...

Interactive jQuery feature for dynamic search suggestions across multiple entries

Here is the HTML code snippet I am working with: <input name="data[Content][0][name]" type="text" class="content_name_1" id="content_name"> <input name="data[Content][1][name]" type="text" class="content_name_2" id="content_name"> ...

Changing the jQuery mobile side panel from push to overlay can be achieved by modifying the display settings when transitioning from a mobile view to a

I'm currently working on a project using jQuery Mobile (JQM) and jquery.mobile-1.4.5. My goal is to create a responsive side menu panel with a fixed header that works well on both mobile and tablet devices. For the design, I want the panel width to b ...