Addressing Image Issues in CSS Grid

I'm struggling to position two images in different sections of a grid layout without overlapping them.

I've attempted referencing the images by both their class and id, but they continue to occupy the same grid space.

Referencing the images by their id seems to solve the issue as it allows me to alter the background successfully.

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="homeCSS.css">
</head>

<title>
    Home Page
</title>
<header class="header">
    <h1>Welcome [User Name]</h1>
</header>
<body>
    <div class="container">
        <div class="image" id="imageOrder">
            <img src="images/logo.png" class="imageLinks"="" id="orderForm">
            <div class="overlay" id="overlayOrder">
                <div class="overlayText" id="orderText">
                    Order Overlay
                </div>               
            </div>            
        </div>
        
        <div class="image" id="imageInvoice">
            <img src="images/logo.png" class="imageLinks"="" id="invoiceForm">
            <div class="overlay" id="overlayInvoice">
                <div class="overlayText" id="invoiceText">
                    Invoice Overlay
                </div>               
            </div>            
        </div>

    </div>

</body>


</html>

CSS

body {
    display:grid;
    grid-template-columns:1fr 1fr;
    grid-template-rows: 1fr 1fr;
    grid-gap:5px;
    grid-template-areas: "header header" "imageOrder imageInvoice";
    background:#eee;
  }

  header{
      grid-area: header;
      place-self: center;
  }

  #orderForm{
      grid-area: imageOrder;
      background-color: springgreen;
  }

  #invoiceForm{
      grid-area: imageInvoice;
      background-color: chartreuse;
  }
.container {
    position: relative;
    width: 50%;
  }
  

.image {
    display: block;
    width: 100%;
    height: auto;
  }

  #imageOrder :hover .overlay {
    opacity: 1;
  }
  .text {
    color: white;
    font-size: 20px;
    position: absolute;
    top: 50%;
    left: 50%;

  }

An image illustrating the current behavior (Chrome screenshot)

https://i.stack.imgur.com/8bfLQ.jpg

Answer №1

For proper grid alignment, make sure that grid items are directly nested within the grid container (in this case, body). Currently, you have a container div in between, occupying one grid cell and causing the contents to be confined within that single cell.

Similarly, ensure that your header is placed inside the grid container, specifically within the body element.

Answer №2

To achieve this layout (utilizing the col-sm in Bootstrap), follow the CSS code below:

body {
    /*max-width: 500px;
    margin: 0, auto;
  */
    display:grid;
    grid-template-columns:1fr 1fr;
    grid-template-rows: 1fr 1fr;
    grid-gap:5px;
    grid-template-areas: "header header" "imageOrder imageInvoice";
    background:#eee;
  }

  header{
      grid-area: header;
      place-self: center;
  }

  #orderForm{
      grid-area: imageOrder;
      background-color: springgreen;
  }

  #invoiceForm{
      grid-area: imageInvoice;
      background-color: chartreuse;
  }
.container {
    position: relative;
    width: 50%;
  }
  

.image {
    display: block;
    width: 100%;
    height: auto;
  }
  /*
  .overlay {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100%;
    width: 100%;
    opacity: 0;
    transition: .5s ease;
    background-color: #008CBA;
  }
  */
  #imageOrder :hover .overlay {
    opacity: 1;
  }
  .text {
    color: white;
    font-size: 20px;
    position: absolute;
    top: 50%;
    left: 50%;
    /*
    transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    */
  }
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="homeCSS.css">
</head>

<title>
    Home Page
</title>
<header class="header">
    <h1>Welcome [User Name]</h1>
</header>
<body>
    <div class="container">
      <div class="row">
         <div class="col-sm-6">
            <div class="image" id="imageOrder">
                <img src="images/logo.png" class="imageLinks" id="orderForm">
                <div class="overlay" id="overlayOrder">
                    <div class="overlayText" id="orderText">
                        Order Overlay
                    </div>               
                </div>            
            </div>
        </div>
        <div class="col-sm-6">
            <div class="image" id="imageInvoice">
                <img src="images/logo.png" class="imageLinks" id="invoiceForm">
                <div class="overlay" id="overlayInvoice">
                    <div class="overlayText" id="invoiceText">
                        Invoice Overlay
                    </div>               
                </div>            
            </div>
        </div>
      </div>

    </div>

</body>


</html>

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

Designating a class for the main block

Having an issue with a slider in uikit. When the slide changes, the visible elements also change their class to uk-active. I'm trying to select the central element from the active ones but css nth-child doesn't work. Any suggestions on how to do ...

Why is the outline buttons class only displaying gray colors for me?

I'm having trouble with the outline buttons class (btn btn-outline-primary) in Bootstrap. For some reason, all the buttons appear gray instead of colored as they should. Here's an example of how the buttons should look: https://ibb.co/ChKf83y, bu ...

Guiding user to their destination after signing in

Having trouble showing different headers based on user login status? Users can log in using login.php, but you're struggling to display header.php when a user is not logged in or logged in. You want the page to show profile form content when they are ...

Using Angular expressions, you can dynamically add strings to HTML based on conditions

I currently have the following piece of code: <div>{{animalType}}</div> This outputs dog. Is there a way to conditionally add an 's' if the value of animalType is anything other than dog? I attempted the following, but it did not ...

Is there a way to change the color of a specific tab without affecting the content within it

I am attempting to change the color of an individual tab using jQuery. However, when I set the background attribute in CSS, it colors the entire background instead. What specific property should I be setting to only change the color of the tab itself? ...

Creating a space for showcasing error messages in Angular and CSS

How can I display an error message min 3 char at the bottom of an input field when only 1 character is entered? Referencing this example: https://i.sstatic.net/Eujle.png Currently, the error message appears on the right side of the input field. https:/ ...

How can I enhance a JavaScript Calendar with more features?

I recently acquired a JavaScript and jQuery calendar from CodeCanyon, which can be found here. I am now faced with the task of integrating this calendar into my workplace website. The current calendar on our ASPX-based site is limited to read-only functio ...

How can I maintain consistent spacing between two areas in CSS3 across all screen sizes?

My question pertains to the alignment of an advertisement (on the left as a banner) and content sections on my website. I have noticed that as the screen size increases, the distance between these two areas also increases. How can I ensure that the distanc ...

aligning a form vertically in a container

<div class="parent"> <div class="left-child"> <img src="logo.jpg" alt="logo"> </div> <div class="right-child"> <form action="#"> <input type="text" value="search"> &l ...

Is it advantageous to employ several wrapper-divs inside section elements when working with HTML5 and CSS?

Back in the day, I learned how to create a website by enclosing all content below the body tag within a div with the class "wrapper." This approach made sense as it allowed for easy vertical and horizontal alignment of the entire content. Just yesterday, ...

Troubleshooting a CSS problem with iPad browsers

I have a specific issue related to CSS on iPad. I am working with a set of sublinks and have defined the styles in CSS as follows: #leftNav .searchBySub {...} #leftNav a.searchBySub:hover {...} #leftNav .searchBySubClicked {...} In my JavaScr ...

Rearranging the layout of elements: CSS switches up the text and button,

Having an issue. I created a background image with gradient colors for my web project and clipped it. Within this element, there is text and a button that I want to be centered inside the element - with the text above and the button below. Initially, ever ...

does not output any console log statements

I am attempting to showcase the values of checkboxes on the console, however, it is not working. <input type="checkbox" id="id_price" value="1" onclick="display_img()">Under £200<br> <input type="checkbox" id="id_pr ...

Spinning Loader in ui-select AngularJS

Currently, I am working with AngularJs and the ui-select plugin from ui-select. My goal is to implement a spinner while fetching data from the server. How can I integrate a spinner directly into the HTML code? The following snippet shows the existing HTML ...

Instructions for utilizing the nav-pill with nav-justified components in Bootstrap3x without incorporating any responsive capabilities

I'm eager to incorporate this into my project : <div class="container"> <ul class="nav nav-pills nav-justified"> <li class="active"><a href="#">Home</a></li> <li><a href="#"> ...

Are DIV elements really impossible to click using selenium Web Driver?

Can DIV elements be clicked using selenium Web Driver? For example, I'm having trouble clicking the delete button in Gmail. https://i.stack.imgur.com/zsyio.png I've been trying to locate the element using the XPATH = //div[@aria-label='De ...

What is the best way to ensure that all components within a Container automatically inherit its calculated width?

Check out my tab panel setup on Sencha fiddle. The buttons are dynamically added to a tabs container with a layout of hbox within a vbox. The width of the tabs container is determined by the flex property. I attempted to set each button's width to &a ...

This particular JavaScript function is only designed to operate on the initial input box in the provided

I have a question regarding echoing rows of $data inside input boxes. I am using a JavaScript function that is supposed to copy the input value to the clipboard when the input box is clicked. However, I am encountering an issue - the function only works ...

What is the best way to format or delete text enclosed in quotation marks within an anchor tag using CSS or JavaScript?

I have encountered an issue with a dynamically generated login form. When I select the 'Forgot Password' option, a new 'Back to Login' message appears along with a separating '|' line. Removing this line is proving challenging ...

Wiki experiencing issues with NodeJS HttpGet functionality

Goal Retrieve the HTML content of a Wiki Page. Introduction In an attempt to fetch the HTML of a Wiki page () for data parsing purposes, I am utilizing NodeJS and its HTTP Request methods. Code Snippet Below is the simple code snippet that accesses th ...