Positioning items in a CSS grid layout involves ensuring that item 1 remains at the bottom of its column, while item 2 is centered within its column

I am trying to create a grid layout using CSS grid:

The main section should have two columns. The first column will contain an image that sticks to the bottom of the grid, while the second column will display a centered card.

Below are my HTML/CSS codes:

.mainbody {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: repeat(1, 2fr);
  text-align: center;
  grid-gap: 5px;
  padding-top: 75px;
}

.left {
  float: left;
  overflow: hidden;
  height: 600px;
  position: relative;
}

.left-bottom {
  position: absolute;
  bottom: 0px;
}

.right {
  float: left;
  width: 50%;
  overflow: hidden;
}
<div class="container mainbody">
  <div class="left">
    <div class="left-bottom">
      <img class="image-car" src="...">
    </div>
  </div>
  <div class="container">
    <div class="card">
      //card text
    </div>
  </div>
</div>

While this code works well, it does not respond to media queries. As a beginner, I would appreciate any suggestions on how to modify this code or approach to achieve the desired outcome.

Answer №1

Improve your CSS code by utilizing the margin property within a grid cell to align content towards different edges or center.

In my view, it is best to avoid using float or absolute positioning when designing layouts with grid or flex.

For instance:

.mainbody {
  display: grid;
  grid-template-columns: 1fr 1fr;
  text-align: center;
  grid-gap: 5px;
}

.left {
  margin-top:auto;
}

You can experiment with this code on Codepen: Codepen Link

If you want to position one grid item at the bottom and another in the center, consider a similar approach to the grid_layout demonstrated in the provided link.

Possible solution:

* {
  margin: auto;
}

.mainbody {
  display: grid;
  grid-template-columns: 1fr 1fr;
  text-align: center;
  grid-gap: 5px;
  min-height: 100vh;
  background: #bee
}

.container .container {
  display: grid; /* just a single cel to use layout properties */
}

.left {
  margin: auto auto 0;
}

.card {
  margin: auto 0 auto;
}

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

Button with a Jquery icon design

Hello, I am currently working on implementing an icon button that can collapse and expand text. Although I have successfully implemented the logic for collapsing/expanding, I am facing difficulties in creating the actual icon button. The theme I am require ...

"Utilizing FileReader to seamlessly integrate data into FormData without the risk

Currently, I'm in the process of constructing an ajax file uploader. This is made possible thanks to the recently introduced FormData interface. Everything seems to work as expected when using the original file. However, I encounter issues when conver ...

What is the most effective way to utilize bootstrap classes and their associated functions exclusively for mobile devices by using media queries?

I am encountering an issue with a table in my application that causes the UI to break when the screen width is reduced to less than 600px. To address this, I would like to implement the following bootstrap classes to add a scroll to the application: .tabl ...

toggleClass is failing to animate

For optimal viewing, try on mobile size by using Google inspect to simulate an iPhone 6. This content may not display until it reaches a certain breakpoint. I have applied a max-height to one class and then used .toggleClass to add another class with a la ...

Create a dynamic layout with two navigational sub-menu blocks positioned side by side

When I hover over a navigation menu item, it brings up 6 sub menu items. I want these sub-items to appear in two blocks of 3 that are adjacent to each other. I tried using display: flex on the parent element .menu-item-1, but it doesn't seem to be wo ...

Prevent the page from refreshing when a value is entered

I currently have a table embedded within an HTML form that serves multiple purposes. The first column in the table displays data retrieved from a web server, while the second column allows for modifying the values before submitting them back to the server. ...

The default margin of an HTML element is set to fill the width of its containing div and cannot be overridden

As a budding web developer, I'm encountering a basic issue that has me scratching my head. Any assistance would be highly appreciated: Regardless of the width I specify for elements within a particular div container, both Safari and Chrome seem to au ...

What factors dictate the color of rows in jquery datatable designs?

Within the jQuery datatable styles, such as "smoothness," the rows are displayed in different colors. What factors determine which colors are shown on each row? And is there a way to customize this color scheme? Refer to the example below from their sampl ...

Arranging two divs to appear side by side, but they are mysteriously aligning in a diagonal pattern

I'm facing an issue with my navbar's divs not displaying side by side when set to display inline. Here is the HTML code snippet: <!--Nav starts here--> <nav class="navbar"> <div class="navbar-item-set"> ...

Tips for integrating external JavaScript libraries and stylesheets into a widget

I am currently working on developing a custom Javascript widget that requires users to insert specific lines of code into their web pages. This code will then dynamically add an externally hosted javascript file, allowing me to inject HTML content onto the ...

Bootstrap: adaptable card design

I am currently working on replicating a card design similar to this one from WIX using bootstrap. I encountered 2 issues: Initially, the card only touches the contact bar at full screen. However, as I reduce the size of the screen, the card begins to floa ...

Element with negative z-index overlapping background of parent element

I am currently working on creating a CSS3 animated menu, but I am facing an issue where elements with low z-index values are displaying on top of their containing elements. This is causing the "sliding out" effect to be ruined. Here is the HTML: <html ...

What is the reason for adding CSS styles to a JavaScript file without importing them?

/Navbar.js/ import './navbar.scss'; import {FaBars, FaSearch} from "react-icons/fa"; import { useState } from 'react'; function Navbar(){ const [hide,sethide] = useState(true); const barIcon = document.querySelectorAl ...

Prevent your HTML tables from overflowing

I am attempting to create an HTML table with three columns, where I want the first and third columns to have a width of "auto" based on their content. The middle column should then take up the remaining space. While this setup works well when the content ...

Unforeseen truncation issues within a table causing ellipsis to appear unexpectedly

I'm facing an issue with truncating text inside a table, and it's not working as expected. Can someone help me understand what I might be doing wrong? Edit: I need to ensure that the table width remains max-width: 100%; <script src="http ...

Loading Options into Dropdown Menu from XML using "text" and "value"

I have set up a dropdown menu that is connected to an xml file. However, I want the "value" of each option in the xml to be displayed within the dropdown as well. Here is an example of how I would like it to look: Doctor 1 Dentist 2 Vet 3 and so on. ...

The lifespan of my cookie is limited

I am utilizing the jQuery.min.js from https://github.com/carhartl/jquery-cookie and my cookie code looks like this: $(function() { //hide all divs just for the purpose of this example, //you should have the divs hidden with your css //check ...

What is the correct method for compiling a list of users?

Hello, I have developed a basic user list based on database results, but I am facing two issues with it. Currently, my list displays like this: [UserOne, ...][UserTwo, ...] However, I need the format to be: [UserOne],[UserTwo]... The three dots repres ...

Ensure the scrollbar is noticeable and personalized solely within a designated div, such as the left side menu

In the left side menu, I have set the overflow-y: scroll property and a fixed height that is smaller than the entire page's height. I am trying to make the scrollbar always visible only within this left menu. I attempted using -webkit-scrollbar and -w ...

How to adjust the timezone settings in PHPMyAdmin on a shared server platform

I'm having trouble changing my timezone to India on my shared server database. I've tried everything but can't seem to get it to work. My website is built using PHP Codeigniter The contact us page on my site saves all inquiry details to my ...