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

Utilize jQuery to dynamically update the box-shadow property with inset added

I have a situation similar to this: $('.inset').click(function(){ if($('.inset').is(':checked')){ $('.box-shadow').css('boxShadow','inset'); }else{ $('.box-shadow&ap ...

Troubleshooting: CSS Styles not loading when passed as property in MUI withStyles

I am currently working on a react component that has a specific style defined as shown below: const StyledInnerItem = withStyles((theme) => ({ bgColor: { backgroundColor: (props) => { console.log('styledInnerItem color: ', props ...

Apply a Fade In transition to the ul li elements following a JavaScript toggle

I'm currently experimenting with implementing a Fade in from the right animation for the links displayed in the menu after toggling the menu body in the browser. Despite my efforts, I am unable to determine why the animation is not functioning as expe ...

Optimizing CSS for Landscape Letter Print Alignment

When attempting to print in letter size and landscape orientation, the two divs fail to align properly. However, when using "@size a4 landscape" for printing, the alignment issue is resolved. What could be missing from my print CSS that would allow them to ...

I'm still trying to figure out the property position. Why won't my page scroll? (My first time using HTML)

I'm having an issue with my webpage where it doesn't scroll even when the content exceeds the page length. I've tried adjusting the position settings for the body, html, and div elements but haven't seen any changes. Here is the HTML ...

What is the best method to keep content confined within a box inside the grid, while also allowing the box to extend beyond the grid boundaries?

Apologies for the confusing title, but I must confess that I am unsure if there are more appropriate terms to explain what I am attempting to achieve. To provide clarity, I have included an image (as they say, a picture is worth a thousand words) https:// ...

Preventing scrollbar-induced content shifting in Material-UI components: A guide

My browser app displays content dynamically, which can vary in length compared to the screen size. This causes a vertical scrollbar to appear and disappear on desktop browsers, leading to the content shifting left and right. To prevent this, I apply style ...

Animated CSS sidemenu (utilized as a filtering panel for a table)

Hi there, I'm having some trouble with CSS Animation. I recently started developing websites and am using Bootstrap 4 along with Animate.css for animations. My goal is to have an icon button expand sideways to reveal a div containing select elements f ...

Issue with KeyboardDatePicker in Material-UI/Pickers occurs when InputAdornmentProps property with position start is added, leading to an unexpected margin

I am currently working with a component from material-ui/pickers and this is the code snippet: <KeyboardDatePicker value={selectedDate} onChange={(_, newValue) => handleClick(newValue)} labelFunc={renderLabel} disableToolbar variant=&a ...

Combining photos seamlessly and bringing them to life through animation upon window loading

My main goal is to seamlessly fit the images together, but I'm struggling to achieve this. I tried using masonry, but unfortunately it didn't work for me. All I want is to tightly pack the divs together. For instance, in my fiddle example, I woul ...

What steps do I need to take in order to change an HTML div's background to a black

My current project involves dynamically loading HTML content stored on disk into a specific div element. This is achieved by using the following code: $('#FictionContent').load('Content/HuckFinn.html'); Just to provide some background ...

alignment of text output and label that responds to various screensizes

Here is some html code with a label and an output next to it. <div class="panel-body"> <div class="col-sm-6"> <label class="head6">Company Name : </label><span class="head9 halfR"> ...

How can I make the arrows work on a secondary slider with EasySlider1.7?

I finally managed to get 2 sliders working on my page after reading through several other tutorials. However, I am facing an issue with the Prev & Next arrows on the second slider as they don't seem to work properly. I inherited this page from someon ...

Encountering a CSS issue during the edit process with PHP and JavaScript

I'm encountering an issue when clicking on the edit button, as my data fetched from the DB should be displayed inside a text field. However, I'm facing a CSS-related error: Uncaught TypeError: Cannot read property 'style' of null Belo ...

Having difficulty accessing the ::after element on Firefox when attempting to click

I've encountered an issue with my HTML and CSS code - it functions perfectly on Chrome, yet behaves differently on Firefox. button#groupToggle { background: 0 0; border: 1px solid #e6e6ed; color: #222; float: none; margin: 0 0 ...

Unable to alter or eliminate the grey background using material-ui

Struggling with a React application that incorporates material-ui. Despite my efforts, I can't seem to get rid of an unwanted grey background in one section of the app. Attempted solutions: body { background-color: #ffffff !important; } * { b ...

Is there a way to make the CSS3 border-image compatible with all the latest browsers, including IE 9+, Chrome, and Firefox?

Is there a way to implement border-image for a dynamically scaling CSS container across all modern browsers using a .js framework? If not, what would be a suitable alternative? To visualize the desired effect, check out: ...

Move the cursor over the text to reveal an image

Hello, I'm trying to replicate the same animation effect as seen on these websites: and . Specifically, when hovering over the "selected works" section, an image is displayed. I suspect it's using a JavaScript library, but I can't seem to i ...

Several iFrames embedded on the website automatically adjust to the same pre-set height

Apologies if this question has already been addressed, but I am struggling to properly articulate it. Here's the issue: I have a client's holiday accommodation website that uses three embedded iFrames for a Calendar, Booking Enquiry, and floorpla ...

How Webpack 4 Utilizes splitChunks to Create Numerous CSS Files

I need to create multiple CSS files using webpack 4 with the min-css-extract-plugin and splitChunks plugin. Here is my webpack configuration. const MiniCssExtractPlugin = require("mini-css-extract-plugin"); const path = require("path"); module.exports = ...