Arrange grid column elements (similar to aligning with justify: flex-start)

<item>
    <h2 class='title'>
        TITLE
    </h2>

    <video-item>
        <img src="https://placehold.it/1600x1100">
    </video-item>

    <overview>
        <p>CONTENT</p>

        <button class='show-more'>Read more</button>
    </overview>
</item>

This is the default structure for items in the code.

https://i.sstatic.net/9KDWD.png

When reaching a certain screen size, I aim for this layout:

https://i.sstatic.net/4VGGd.png

However, my thinking is still rooted in flexbox principles. I want the first 'column' to align towards the start like flex-start.

Notice how the longer text occupies the space in the initial item, while the shorter text in the second example remains compact.

CodePen Link

I believe that this alignment relates to the "block axis" as mentioned in this documentation.

@media (min-width: 800px) {
    item {
        display: grid;
        grid-template-columns: [content] 1fr [visual] 1fr;
        /* SPECIFIC POSITION */
    }
    .title, overview {
        grid-column: content;
    }
    video-item {
        grid-column: visual;
        grid-row: 1/3;
    }
}

I experimented with `grid-area` but reached the same outcome.

While this approach differs from using flexbox, I am confident that what I envision can be achieved through CSS grid - it's just a matter of learning the syntax. ; )

https://i.sstatic.net/8Mp7m.png

UPDATE @Temani Afif's suggestion has shed light on the concept - here are additional images to further clarify.

https://i.sstatic.net/Xz2Ib.png

By not explicitly defining a `grid-template-rows` on the parent element, it will automatically adjust based on the grid-row setting of the child... resulting in an extra row being created to accommodate any excess content set as 'auto'. Another method would be to specify `1/4` or `span 100`. Alternatively, you could define additional rows explicitly with areas, among other approaches.

Answer №1

Here is the recommended configuration for your needs:

thing {
    display: grid;
    grid-template-columns:  1fr 1fr;
    grid-template-rows:auto auto auto; /*optional, can be removed */
}
video-thing {
    grid-column: 2;
    grid-row: span 3;
}

* {
  box-sizing: border-box;
}


/* + reset */

img {
  display: block;
  width: 100%;
  height: auto;
}

thing,
video-thing,
description {
  display: block;
}

body {
  /* some context */
  width: 98%;
  max-width: 900px;
  margin: 100px auto;
}

thing {
  position: relative;
  padding: 10px;
}

@media (max-width: 799px) {
  video-thing {
    margin: 20px 0;
  }
}

@media (min-width: 800px) {
  thing {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
  .heading,
  description {
    padding-right: 20px;
  }
  description {
    margin-top: 20px;
  }
  video-thing {
    grid-column: 2;
    grid-row: span 3;
  }
}

thing {
  border: 4px solid green;
}

.heading {
  border: 4px solid orange;
}

description {
  border: 4px solid blue;
}

video-thing {
  border: 4px solid red;
}

.heading {
  font-family: 'Helvetica';
  font-size: 24px;
  font-weight: 700;
}

p {
  font-family: 'Georgia';
  font-size: 16px;
  line-height: 1.4;
}

p+p {
  margin-top: 10px;
}

button {
  margin-top: 20px;
}

thing+thing {
  margin-top: 60px;
}
<thing>
  <h2 class='heading'>Video section / key information 1</h2>

  <video-thing>
    <img src="https://placehold.it/1600x1100">
  </video-thing>

  <description>
    <p><strong>Exploratory full text here</strong>: Lorem ipsum dolor sit amet consectetur adipisicing elit. Saepe atque placeat eaque fugiat voluptatibus voluptatum excepturi ut sunt odit voluptates sint cum quasi illo quae laboriosam, reiciendis
      consequatur veniam iusto.</p>
    
    <p><strong>Exploratory full text here</strong>: Lorem ipsum dolor sit amet consectetur adipisicing elit. Saepe atque placeat eaque fugiat voluptatibus voluptatum excepturi ut sunt odit voluptates sint cum quasi illo quae laboriosam, reiciendis
      consequatur veniam iusto.</p>
      
    <p><strong>Exploratory full text here</strong>: Lorem ipsum dolor sit amet consectetur adipisicing elit. Saepe atque placeat eaque fugiat voluptatibus voluptatum excepturi ut sunt odit voluptates sint cum quasi illo quae laboriosam, reiciendis
      consequatur veniam iusto.</p>

    <button class='unfold'>Read more</button>
  </description>
</thing>


...
... More sections with similar structure follow
...

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 alignment of Bootstrap v4.5 checkbox is limited as it cannot be positioned horizontally or vertically when placed next to an input field

As stated in the title: Bootstrap's checkbox is having trouble aligning Horizontally or Vertically when placed next to an input. Error Displayed: https://i.sstatic.net/hNHpm.png I have experimented with various solutions, but none have provided sat ...

`Change the output path and utilize URL helpers for CSS in Compass`

Currently facing an issue with altering the .css output path to a specific file in a compass project. Imagine having a directory structure like this: / ..Fonts ..Folder ..Images ..Sass .._mixins.scss ..[other_stuff] ..Styles ..config.rb Her ...

What is the best way to display all the steps in a Material UI stepper when preparing a page for

I'm currently developing a React component that utilizes Material UI stepper functionality. The challenge I am facing is ensuring that the data is printable effectively. It's necessary for all steps to be expanded and printed, which goes against ...

Unable to choose the div element with id ":1"

Just starting out with web development, and I have a div element with the following attributes: <div class="" id=":1" role="treeitem" aria-selected="false" aria-expanded="false" aria-level="1" aria-labelledby=":1.label" aria-setsize="10" aria-posinset= ...

Leverage the Width of a Nested Row within Foundation Framework

Here's a different layout option with a sidebar <div class="row"> <div class="main-content columns small-6> {main content} </div> <div class="columns small-6> {sidebar} </div> </div> And here is a ...

Modify the background color of a single page while keeping the other pages unchanged

Is there a way to have a blue background on only one page of my rails application, while keeping all other pages with a white background? I attempted using the following code: <%= javascript_include_tag params[:controller] %> or <%= stylesheet_ ...

Creating a custom grid layout with Bootstrap 4

Take a look at the image link provided below. https://i.sstatic.net/yhbx9.jpg I am attempting to use boostrap4 to create a grid layout similar to the one shown in the image. On desktop, the first column should be col-5 width and the other 3 equal. On mob ...

Can the navigation bar be filled automatically with content that corresponds to the anchors found within the content?

Currently, I am utilizing a Bootstrap frontend for the project at hand. The task I am tackling involves a significant amount of automatically generated content and code, which greatly simplifies the work process. My goal is to explore the potential of cre ...

Determining the Minimum and Maximum Widths of a Div Container Using CSS

I have created a basic grid structure with several rows and columns. The top row spans the entire width, the second and third rows each contain two columns, and the bottom row also spans the entire width. Currently, all elements are fixed at specific size ...

What is the proper way to select the <ul> element within an FTL template?

Can someone provide guidance on how to target a <ul> container within a freemarker template using JavaScript? My goal is to display the content of this specific <ul> element in my FreeMarker template on the web page. Any suggestions on how I ...

Can minification of JS be achieved in a Jekyll environment?

Currently, I am in the process of developing a project with Jekyll and one of the requirements is to minify HTML, CSS, and JS. I was wondering if Jekyll has built-in features for JS minification. It may seem like a simple question, but since I am new to ...

How can I ensure that my navbar-right remains fixed to the right side of the screen and is always visible at the top of the page as I

When the page first loads: After scrolling down: I am attempting to create a fixed navigation bar at the top of the screen, so that it remains visible as the user scrolls down the page. However, I am facing an issue where my navbar brand is positioned to ...

Position the label and the select dropdown side by side within the sweetalert 2 component

Can anyone provide an example of aligning a label and dropdown in the same row using sweetalert2? I attempted to customize the label placement, but it appears on a separate line. I would like to log the selected dropdown item upon clicking the OK button. ...

How to apply a class on button click using react.js

After spending a few weeks diving into React, I've got the hang of the basic syntax like props and states. However, I'm hitting a roadblock when it comes to connecting certain concepts, especially adding classes when a state changes. My current p ...

Arranging Content with Bootstrap Columns

I am trying to organize my content into three sections as follows: A - main content of page B - very important content (must be visible without scrolling) C - less important content. The current layout I have is like this: ----- |A|B| |A|C| ----- Howe ...

Set the height of the last child element to 100% in the CSS

Seeking assistance to adjust the height of the final list item to 100% in order to match the varying height of the right-floated div. ul.menu li:last-child {height:100%} Here is an example of my current situation: http://jsfiddle.net/kvtV8/1/ Any help ...

ui-grid row size set to automatically adjust using rowHeight : 'auto'

Has anyone else experienced this strange behavior with ui-grid? When I set the rowHeight to auto, each cell in the same row ends up with different heights. One of the cells contains multiline data, which seems to be causing issues for ui-grid. I've ev ...

Displaying elements upon checkbox selection in React

Hello, I'm new to React and I am facing an issue with checkbox click handling in React. My goal is to display a div when a checkbox is checked and hide the div when the checkbox is unchecked. The current implementation only shows the div when the che ...

How to Target HTML Tags Locally using CSS Modules in Next.js?

I am looking to implement smooth scrolling specifically on one page in my Next.js application, such as my blog. Instead of applying it to the entire site through the globals.css file, I need a way to inject scroll-behavior: smooth; into the html tag only f ...

I am having trouble getting the (:active) pseudo-class to function properly with the menu on my WordPress theme

Purchased a theme called HelpGuru and encountering issues with the CSS of the menu. Reached out to support but they were unable to assist, directing me to a company that specializes in customizing WordPress themes instead. The link to the demo can be found ...