Using CSS Grid to Align Images

I want to utilize CSS 3 Grid to position specific images on my home page that I imported. The desired outcome is as follows:

https://i.sstatic.net/aYshN.jpg

Currently, my grid looks like this:

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

Below is the code snippet:

<div class="wrapper">

    <div class="bed"><img src="http://placehold.it/300x300" alt=""></div>
    <div class="pillow"><img src="http://placehold.it/300x300" alt=""></div>
    <div class="kitchen"><img src="http://placehold.it/300x300" alt=""></div>
    <div class="living-room"><img src="http://placehold.it/300x300" alt=""></div>
    <div class="sofa"><img src="http://placehold.it/300x300" alt=""></div>

</div>

@endsection

@push('style')
<style>

.wrapper{
    display: grid;
    grid-template-columns: 1fr 0.5fr 1fr ;
    grid-auto-rows: minmax(100px,auto);
    padding: 1em;
}

.wrapper >div{
    padding: 1em;
}

.bed{
    height: 50%;
}

.pillow{
    height: 50%;
    width:100%;
}

.kitchen{
    height: 50%;
}

.living-room{
    height: 70%;
    width:150%;
}

.sofa{
    height: 50%;
    width:150%;
}

img{
    width:100%;
    height: 100%;
    border-radius: 20px;
}

How can I adjust the image positions to match the expected layout perfectly?

Update:

https://i.sstatic.net/epJ75.jpg

Answer №1

Revision

Eliminated the specified 800px/600px height, allowing the grid to automatically occupy the entire screen.


Uniformly expanded all images to occupy the entire grid cell. Notice how the grid-row and grid-column values function. By defining the span of each grid cell, the height and width are distributed naturally throughout the grid. In this instance, I set the grid to be 800px wide and 600px tall.

I have utilized fr units for the grid specifications, ensuring relative distribution based on their relationship to other elements. The code below commands the browser to make the grid 8 units in length. The first column will constitute 3/8 of the total width (800px in this case). To balance the columns, I set the second column width to 2fr.

grid-template-columns: 3fr 2fr 3fr;

Whenever you encounter a -1 in a grid child measurement, it essentially denotes go to the end. For example, the following snippet instructs starting at row line 3 and spanning till the end, encompassing all possible lines.

grid-row: 3 / -1;

Illustration

.wrapper {
  display: grid;
  grid-template-columns: 3fr 2fr 3fr;
  grid-template-rows: repeat(8, 1fr);
  padding: 1em;
  grid-gap: 0.5em;
  background-color: #eee;
}

.wrapper>div {
  position: relative;
}

.wrapper>div::after {
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  background-color: #333;
  color: white;
  padding: .5rem;
}

.bed {
  grid-column: 1;
  grid-row: 1 / 3;
}

.bed::after {
  content: 'BED';
}

.pillow {
  grid-column: 2;
  grid-row: 1 / 3;
}

.pillow::after {
  content: 'PILLOW';
}

.kitchen {
  grid-column: 3;
  grid-row: 2 / 5;
}

.kitchen::after {
  content: 'KITCHEN';
}

.living-room {
  grid-column: 1 / 3;
  grid-row: 3 / -1;
}

.living-room::after {
  content: 'LIVING ROOM';
}

.sofa {
  grid-column-start: 3;
  grid-row: 5 / -1;
}

.sofa::after {
  content: 'SOFA';
}

img {
  width: 100%;
  height: 100%;
}
<div class="wrapper">

  <div class="bed"><img src="http://placehold.it/300x300" alt=""></div>
  <div class="pillow"><img src="http://placehold.it/300x300" alt=""></div>
  <div class="kitchen"><img src="http://placehold.it/300x300" alt=""></div>
  <div class="living-room"><img src="http://placehold.it/300x300" alt=""></div>
  <div class="sofa"><img src="http://placehold.it/300x300" alt=""></div>

</div>

jsFiddle

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

Ways to ensure the footer sticks to the bottom of the page when there is minimal content

Using Bootstrap 3.3.7 I am trying to ensure that my footer always stays at the bottom of the browser window. I prefer not to use a "sticky footer" as I don't want it to cover any part of my content. Currently, when there is enough content on the pa ...

What exactly is the function of $fix-mqs within the IE Sass mixins created by Jake Archibald?

I'm really struggling to understand the purpose behind using $fix-mqs in the mentioned link: Although I can grasp how the rest of the mixin functions, this particular part has me stumped. In a project where I've utilized this pattern, everything ...

Activating a div in React.js using setActive: Step-by-step guide

Currently, I am in the process of developing an application with three menu tabs, each represented by an accordion comprising a menu title and content. The issue I am facing is that when clicking on any menu title, all content divs are displayed simultaneo ...

Retrieve the data attribute from a select box that was created dynamically

Following an AJAX request, I have successfully generated two select boxes: $("#job_id").change(function() { var id = $(this).val(); $.ajax({ url: '', type: 'POST', dataType: 'json', dat ...

Challenges Arising from Creating an EPL Trivia Game with HTML and Javascript

Having some issues with this game functioning properly. Trying to create a dropdown menu that lists 20 EPL teams, and upon selecting a team, display that team's information in a form. Unfortunately, I'm encountering difficulties with the code. An ...

The correct method for implementing server-side rendering with JavaScript

My website consists of multiple pages and utilizes React as a widget toolkit, with different bundles on each page. I am facing an issue where some widget state persists within a login session, and when the user revisits the page, the components should relo ...

python and html tutorial for adding text from bottom to top

Looking for help with rearranging lines in an HTML file? If you want the lines to be displayed in reverse order, here's an example: (1) (2) (3) You need them to appear like this: (3) (2) (1) try: record_cam.strHandler ...

Animate CSS during page load

Currently, I am implementing AJAX to dynamically load pages on my website. During the loading process, I wish to incorporate a spinning animation on the logo to indicate that content is being fetched. The jQuery script (although I'm still learning an ...

Deliver the event target within the data-bind click HTML

I am having trouble sending the event target when passing parameters in data-bind. <button class="tablinks" data-bind="click:$root.notify.bind(this, 1, 'foo');" id="defaultOpen">PRINCIPAL</button> self.notify = function (str, id, e) ...

Despite importing jQuery, the variable '$' cannot be located

Whenever I try to click the button labeled test, it doesn't do anything. However, an error message appears in the console debug indicating: Error: Unable to locate variable '$'. I suspect this might be a jQuery issue, even though I' ...

Enable the image to extend beyond the boundaries of its containing div

How can I prevent a div from extending its width, allowing an image to be visible outside of it in IE8? Specifically, my div may be 200px wide while the image is 250px wide. ...

When the mouse is moved to the UL LI list, the border color of the Top Hover is reverted back to default

Can you assist me with this issue? I need to modify the code below so that the border color remains blue while a user selects an item from the UL LI list. Here is an image showing the current problem: https://i.sstatic.net/DS7hO.png And here is a pictu ...

Can a linked checkbox be created?

Is it possible to create a switch type button that automatically redirects to a webpage when turned on by clicking a checkbox? I'm working on implementing this feature and would like to know if it's feasible. ...

Access within the identical window (PHP file) as opposed to an Iframe

I am currently working with a PHP file that retrieves data from my database. <?php $cdb = new PDO('mysql:dbname=xxx;host=localhost', 'xxx', 'xxx'); foreach ($cdb->query("SELECT * FROM images ORDER BY posted DESC LIMIT ...

Concealing specific sections of HTML content in a webview on the fly

I've been experimenting with a proof of concept feature to implement the ability to conceal certain parts of a web page loaded in a webview, but I seem to be encountering some issues... Within a UIWebview extension, I have something similar to this c ...

Issues with HTML marquee not functioning properly post fadeIn()

I am attempting to create a progress bar using the HTML marquee element. When the user clicks submit, I want to fadeIn the HTML marquee and fadeOut with AJAX success. However, when I click the submit button, the marquee does not fadeIn as expected. Here is ...

How can I calculate the width of a character in an HTML5 canvas?

When using the .fillText function with a fixed-width font, I can easily adjust the height by setting the .font property. However, is there a way to accurately determine the width of an individual character? ...

The Google Docs viewer is displaying an empty screen

I have been utilizing the Google Docs viewer on my website: <div class="embed-r embed-responsive-a"> <iframe class="embed-responsive-it" src="https://docs.google.com/viewer?embedded=true&amp;url=http://LINK.PDF"></iframe> </div& ...

What is the best way to position this container in the center of the

Is there a way to perfectly center this container both vertically and horizontally? I've attempted the method below without success. Unsure of what is missing: HTML: <div class="box"> <p>This is a sentence.</p> </div> C ...

What is the best way to generate an HTML snapshot using C#?

I'm currently working on a page with Ajax functionality and I'm interested in making it SEO crawlable. After reviewing Google's guidelines at https://developers.google.com/webmasters/ajax-crawling, I learned that I need to use "#!" to create ...