The minmax() function in a responsive grid layout does not function properly when incorporating grid areas

My Vision for the Layout

I am striving to create a dynamic grid structure with three columns and two rows, where the third column spans both rows and features an image. This layout should adjust itself based on the device screen size:

Specifically, I would like the image to transition to the lower row on tablet devices, and all columns to stack in a single column on mobile devices.

Tablet View :

https://i.stack.imgur.com/54rty.png

Mobile View:

https://i.stack.imgur.com/8oXRu.png

What I Have Tried & Expectation

I have experimented with using grid-template-area to organize the grid elements and the minmax() function to achieve responsiveness, yet my grid does not adapt as desired even with the use of minmax(). Ideally, I aim to attain this layout without relying heavily on media queries.

The Roadblock

see : grid.layoutit

Understanding the Challenge

The issue seems to emerge when utilizing grid-area to dictate the placement and sizing of grid children. When combined with minmax(), if a child's width decreases below 300px, it fails to wrap upon window resizing. The conflict arises because grid-area sets fixed positions for children within the grid structure. For example, the first child

.services .service1 { grid-area: 1 / 1; }
is positioned in the first row and column.

When the first child service1 becomes less than 300px due to minmax(300px, 1fr), it should ideally wrap to the second row and column 1, which contradicts the decision established by grid-area.

Hence, how can I arrange all my grid children accurately while ensuring responsive design? It appears challenging to achieve this when combining minmax() and grid-area.

Code Sample

<div class="services container">

            <div class="service1">
                    <i class="fa-solid fa-palette fa-2x">
                    </i>
                    <h4>
                        Graphic Design
                    </h4>
                <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Officia, velit? Inventore voluptas quis eveniet
                    expedita numquam pariatur tempora, praesentium laudantium ad, porro reprehenderit repellat aut
                    perspiciatis, unde optio odio ab.</p>

            </div>
            <div class="service2">
                        <i class="fa-solid fa-gem fa-2x">
                        </i>
                                    <h4>
                        Web Design
                                    </h4>
                <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Sapiente harum a illo voluptatum facere
                    repudiandae 
                    voluntates optio deleniti tenetur vel accusantium sequi minima, odio minus eius veritatis obcaecati. Natus,
                     similique?</p>
            </div>
            <div class="service3">
                    <i class="fa-solid fa-pen-ruler fa-2x"></i>
                    <h4>
                        UI and UX
                    </h4>
                <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis culpa minima hic est laborum
                    necessitatibus rerum quidem, commodi quas iusto. Odio fuga cum perspiciatis delectus fugit quisquam
                    voluptate quos rem.</p>
            </div>
            <div class="service4">

                    <i class="fa-solid fa-code fa-2x"></i>
                    <h4>
                        Web Development
                    </h4>
                <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Recusandae, consequatur reiciendis officia
                    pariatur culpa nam tempore, dolores rem nesciunt nobis, exercitationem dolorum. Culpa, commodi facere?
                    Nihil fugit id expedita laborum.</p>
            </div>

            <div class="image"><img src="assets/images/services.jpg" alt="services"></div>

        </div>
    
.services {
        display: grid;
        grid-template-areas:
            '. card1 card2 image . '
            '. card3 card4 image . ';
        grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
        row-gap: 2rem;
        align-content: center;
        padding-block: 5rem;
    }

    .services .service1 {
        grid-area: card1;
    }

    .services .service3 {
        grid-area: card2;
    }

    .services .service2 {
        grid-area: card3;
    }

    .services .service4 {
        grid-area: card4;
    }

    .services .image {
        position: relative;
        grid-area: image;
        width: 100%;
        max-width: 800px;
        display: block;
        margin: auto;
        height: auto;
        object-fit: cover;
    }

    .services .image::before {
        content: "";
        position: absolute;
        display: inline-block;
        background-color: #292a2f;
        border: #10cab7 2px solid;
        top: -11%;
        left: 65%;
        width: 167px;
        height: 585px;
        z-index: -1;
    }

    /* grid children are also displayed with grid */
    .services>div {
        display: grid;
        grid-template-columns: auto 1fr;
        grid-template-rows: 50px max-content;
        align-items: start;
    }
    

Answer №1

When dealing with grid layouts, I always prefer to specify the starting row and column for each element. While it may appear daunting at first, you can refer to for assistance. One of the benefits of this method is having precise control over the positioning of elements and their responsiveness across various screen sizes. It even allows for overlapping elements if necessary.

body {
  margin: 0;
  padding: 0;
}

/services {
  display: grid;
  grid-template-rows: 1fr;
  grid-template-columns: 1fr;
  row-gap: 2rem;
}

/services .service1 {}

/services .service3 {}

/services .service2 {}

/services .service4 {}

/services .image {
  position: relative;
  width: 100%;
  max-width: 800px;
  display: block;
  margin: auto;
  height: auto;
  object-fit: cover;
}


/* grid children are also displayed with grid */

/services .service {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
}

/services .service *:nth-child(1) {
  align-self: center;
  justify-self: center;
}

/services .service *:nth-child(2) {
  align-self: center;
  justify-self: center;
}

/services .service *:nth-child(3) {}

@media (min-width: 390px) {}

@media (min-width: 600px) {
  /services {
    grid-template-rows: auto auto 1fr;
    grid-template-columns: 1fr 1fr;
    align-items: center;
  }
  /services .service1 {
    grid-area: 1 / 1 / 2 / 2;
  }
  /services .service3 {
    grid-area: 1 / 2 / 2 / 3;
  }
  /services .service2 {
    grid-area: 2 / 1 / 3 / 2;
  }
  /services .service4 {
    grid-area: 2 / 2 / 3 / 3;
  }
  /services .image {
    grid-area: 3 / 1 / 4 / 3;
  }
  /services .service {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
  }
}

@media (min-width: 992px) {
  /services {
    grid-template-rows: 

<div class="services container">

  <div class="service service1">
    <svg class="expand" style="width: 48px;height: 48px" viewBox="0 0 48 48" fill="currentcolor" xmlns="http://www.w3.org/2000/svg">
            <path d="M 37,20 V 11 H 28 V 9 h 11 v 11 z"/>
            <path d="M 31,28 V 17 H 20 v -3 h 14 v 14 z"/>
            <path d="M 9,39 V 28 h 2 v 9 h 9 v 2 z"/>
            <path d="M 14,34 V 20 h 3 v 11 h 11 v 3 z"/>
            <rect width="6" height="6" x="21.274239" y="21"/>
        </svg>
    <h4>
      Graphic Design
    </h4>
    <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Officia,

For your image, consider using background-image property instead.</p>
    </div></answer1>
<exanswer1><div class="answer" i="75671793" l="4.0" c="1678260073" a="cGllciBmYXJydWdpYQ==" ai="19996700">
<p>I always find it's easier to manipulate grid with row, column start of each element. It's a bit complicated to visualize at beginning, but check here <a href="http://cssgridbuilder.com/" rel="nofollow noreferrer">http://cssgridbuilder.com/</a>. The good thing with that, it's you who decide this element will be from here to here, and at this media it'll be from here to here. You can even overlap elements.</p>
<p><div>
<div>
<pre class="snippet-code-css lang-css"><code>body {
  margin: 0;
  padding: 0;
}

.services {
  display: grid;
  grid-template-rows: 1fr;
  grid-template-columns: 1fr;
  row-gap: 2rem;
}

.services .service1 {}

.services .service3 {}

.services .service2 {}

.services .service4 {}

.services .image {
  position: relative;
  width: 100%;
  max-width: 800px;
  display: block;
  margin: auto;
  height: auto;
  object-fit: cover;
}


/* grid children are also displayed with grid */

.services .service {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: 1fr;
}

.services .service *:nth-child(1) {
  align-self: center;
  justify-self: center;
}

.services .service *:nth-child(2) {
  align-self: center;
  justify-self: center;
}

.services .service *:nth-child(3) {}

@media (min-width: 390px) {}

@media (min-width: 600px) {
    //adjusted styles for better readability
}

@media (min-width: 992px) {
    //adjusted styles for different screen resolutions
}

@media (min-width: 1200px) {}
    
@media (min-width: 1400px) {}

Try at diff resolution. For your image, I think it would be better to put it in background-image

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

Unable to delete borders within the container

Is there a way to eliminate the borders within this container? Visit this link for reference. I've tried using firebug but I can't seem to locate it... Appreciate any assistance you can provide. Thank you! ...

Ways to ensure CSS affects icon inside main element?

Struggling to match the background color of Material-UI icons with the rest of the list items on hover. The CSS is not applying to both the icon and text despite styling the overall class (className= "dd-content-item"). Any assistance would be greatly appr ...

Creating elegant text in HTML using only CSSLearn how to design stylish text using CSS without any additional code

Can you generate stylish text using just CSS in HTML? Check out this link for a great demonstration of fancy text. ...

a single column with a div of varying height using flexbox

I need to create a div with a height of 120px using only the CSS property display: flex. I am not allowed to use float and can only use div elements. Here is the code I have so far, but I'm unsure how to achieve the desired outcome: .flex-containe ...

Can CSS be used to automatically focus on a div element?

Can CSS be used to set autofocus on a div element? <div class="form-group" style="margin-left:10px"> <label for="organizationContainer" class="nmc-label">@Res.GroupStrings.CreateNewGroupOrganization</label> <div id="organiza ...

Rearrange the order of divs dynamically to prevent wrapping/overflow, no need for media queries

Can someone help me with this issue? I am trying to create a navigation bar with centered menus/links, a logo on the left, and call-to-action buttons and icons on the right. If the content overflows or wraps, I want the center column to move to the next l ...

Breaking the layout using recursive components in VueJS

I am currently facing an issue where I need to dynamically load components. However, when some of these components are loaded, they appear with faulty or missing CSS styles due to a problematic first div element after the template. Removing this DIV manual ...

Tips for creating a fixed element with ScrollTrigger and GSAP

How can I prevent the right div from jumping on top of the left div when using a scroll trigger to make the left div's position fixed? gsap.registerPlugin(ScrollTrigger); const tlfour = gsap.timeline({ scrollTrigger: { trigger: ".ma ...

Wrapping an anchor tag with a div in Codeigniter

Can a div tag be used inside an anchor function? I have a div with the following CSS: #first{ opacity:0; } Now, I want to include it in my anchor element. Here is the code snippet: <?php if(is_array($databuku)){ echo '<ol>&l ...

Is it possible to accomplish this using a list or a table in HTML/CSS?

I am currently working on creating a visual representation of a color catalog using hyperlinked images. My goal is to have a layout that displays the colors in a grid format, similar to this: However, the number of images per row may vary depending on the ...

The head tag specification doesn't properly include the CSS file reference

I have a question regarding my HTML5 web page and the way I am referencing a CSS file in the head tag. Currently, I have it set up like this: <head> <link rel="stylesheet" href="css/style.css" type="text/css"/> </head> However, it d ...

IE is notorious for not playing well with CSS

I am facing an issue with my Google search box code. It works perfectly on Firefox, Chrome, and other browsers except for Internet Explorer. Can anyone help me troubleshoot this? Below is the code snippet: Abulkas Tablet #outerContainer{ width: ...

Google/StackExchange Menu Tab

I am interested in creating a navigation bar similar to Google's or StackExchange's new one, where it is always positioned at the top. It does not need to be fixed there, but I would like it to have links only. How can I achieve this layout with ...

Is there a way to use HTML and CSS to switch the style of a dynamic decimal number to either Roman numerals or Katakana characters within a specified HTML element, such as a tag, div

I've searched everywhere but only found guides on list styling and counter styling in CSS that didn't work out. So, for example, I have a number inside an HTML tag that is changed dynamically using Vue Watch. I want to display the number in upper ...

MUI enables the creation of a fixed vertical box anchored at the bottom of the screen

I’ve attempted the following so far: import "./styles.css"; import { Box, Typography } from "@mui/material"; import styled from "@emotion/styled"; export default function App() { const MainStyle = styled("div")(( ...

What is the best way to eliminate the left margin entirely in CSS?

I am attempting to create an image view that fully covers the window, without any margins. I have tried various solutions such as setting the body margin and padding to 0, but they do not seem to work. body { margin: 0px; padding: 0px; } or *, html { ...

Set the height of the main div container to a fixed size

I am facing an issue with my div container (mapInfo-Container) which contains multiple div blocks. I want the container to have a fixed height of 250px, and be scrollable for any content exceeding this height. Here is the code snippet: http://jsfiddle.net ...

Creating a striking two-tone border pattern in an HTML document

Looking to create a design with a straight line that features two colors and a slanted line in between. Here is an example of the desired result: https://i.stack.imgur.com/9ys6e.png ...

Tips for executing a function in the HC-Sticky plugin?

Currently, I am utilizing the HC-Sticky JavaScript plugin and endeavoring to utilize the documented reinit method. However, I am facing difficulty in understanding how to execute it. In this CodePen demo, a basic setup is displayed along with an attempt t ...

The transition property in CSS and JavaScript does not seem to be functioning properly in Firefox

Recently, I integrated a Slide in menu script on my website using a Slide in menu script. After following all the instructions provided, the menu started working flawlessly on Chrome and Safari browsers. However, my main goal was to make it function on Fir ...