Grid item overlay

Currently, I am facing an issue with React where I am attempting to place an overlay on top of each grid item in a grid. Despite trying multiple approaches, the overlay appears beneath each grid item instead of over it. Even setting the position: absolute does not seem to rectify the problem!

Below is the code snippet I am using for the grid (React):

  <div className="home__itemSection">
     <div className="item">
        <div className="item__container">
            <div className="container__imageDiv">
                <img
                    src={""}
                    alt=""
                />
            </div>

            <div className="container__detailSection">
                <p>{""}</p>
                <h6>{"&"}</h6>
            </div>
        </div>

        <div className="item__overlay">
            <h1>{""}</h1>
            <h1>{""}</h1>
        </div>
    </div>
  </div>

CSS:

.home__itemSection {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    margin: 30px 170px 0 170px;
    transition: ease all 0.5s;
}

.item {
    background-color: #f1f1f1;
    height: 350px;
    width: 212px;
    margin: 0 3rem 1rem 3rem;
    height: fit-content;
    font-family: "Poppins", sans-serif;
    display: flex;
    align-items: flex-start;
}

.container__imageDiv {
    height: 318px;
    width: 212px;
}

.container__imageDiv > img {
    height: 318px;
    width: 212px;
}

.container__detailSection {
    height: 44px;
    width: 212px;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    padding-top: 5px;
}

.container__detailSection > p {
    color: black;
    font-size: 13px;
    font-weight: 300;
    line-height: initial;
}

.container__detailSection > h6 {
    padding-left: 1.8rem;
    font-weight: 500;
    font-size: 1rem;
}

.item__overlay {
    height: 252px;
    width: 212px;
    background-color: black;
}

Any assistance on resolving this matter would be highly appreciated!
Thank you!

Answer №1

Ensure to include position: relative; in the CSS for the parent element of the overlay.

.item{
 position: relative;
}
.item .item__overlay{
   position: absolute;
   left: 0;
   top: 0;
}

Answer №2

When an element has the position absolute, it is positioned in relation to the nearest parent element with a relative (or absolute) position.

To ensure the entire container is covered, add the position: relative property to the home__itemSection class. Additionally, set the height and width properties to 100%:

.home__itemSection {
 ...
  position: relative;
}

.item__overlay {
  ...
  position: absolute;
  height: 100%;
  width: 100%;
}

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

What is the best approach to display data in React fetched from an API request? If this is not the right method, what changes should be made to the JSX rendering to convert

As I begin my journey with React, I find myself questioning the best practices for displaying data. Should I always break down components into smaller ones rather than having one large component render everything? It seems like a good practice, but I' ...

What could be causing my file input to appear misaligned?

Can anyone assist me with a strange issue I am facing? Despite using inspect element on my file input and it appearing in the correct place, it does not function as expected. To see for yourself, visit oceankarma.co and click on 'Post' at the top ...

Tips for positioning a canvas and a div element next to each other on a webpage

I have a canvas and a div element that I need to split in a 60% to 40% ratio. However, no matter what changes I make to the display settings, the div is always displayed before the canvas. The Div element contains buttons with color-changing properties fo ...

Retrieving URL parameters using client-side rendering with the CSR component on Next.JS version 14 hosted on AWS Amplify

It is recommended to utilize useSearchParams from the official documentation However, a build error occurs when running npm run build, resulting in a failed build An error occurred while prerendering the page "/mypage". Find more information her ...

"Embrace the power of ReactJS with the versatile

I'm working on a ReactJS application and encountering an issue. The problem lies in my Router setup: <Router history={browserHistory}> <Route name="Home" path="/" component={App}> <IndexRoute component={Home}></Index ...

Unlocking the Mysterious Realm of HTML

I recently stumbled upon a mysterious combination of HTML attributes: <div vanisheffect="true" blinkcolor="red" fadeopacity="0.8"> Have you ever encountered something like this? And more importantly, does it have any impact on specific browsers? Is ...

Low-quality CSS Gradient Design

Hey everyone, Feel free to take a look at my preloader on this link: (Hit ESC as soon as the page loads to pause the website and focus on the loader) Upon closer inspection, you'll notice that there is an issue with the quality of the background l ...

Testing localStorage with sinon can be done by setting up unit tests

Experimenting with localStorage testing through sinon. As a newcomer to unit testing, I might be tackling something basic. Latest Development I was able to put together this code, but now encountering a new error Should wrap property of object Test Scen ...

Tips for increasing the spacing between typography and tabs in the AppBar

For the code snippet, you can refer to this link: https://codesandbox.io/s/smoosh-cookies-e4w5o Specifically, the code segment is: <AppBar position="static"> <Toolbar> <Typography variant="h6">News& ...

Retrieving text content within a span element using CSS

Here is the Jsfiddle that I experimented with http://jsfiddle.net/LpH8B/2/ <span>*</span> <br> <span>*</span> <br> <span>hdhdf</span> span[text='*'] { color:red; } I am looking to target ...

Issues arise when inline elements are not behaving correctly when placed alongside inline-block elements

Among the various types of HTML elements, inline and inline-block elements have their differences. One such difference is that inline elements don't support margin-top or margin-bottom properties like inline-block elements do. However, in a specific e ...

Shift a Div to the left prior to stretching it out

I am trying to achieve a smooth leftward movement and expansion animation for a div. Currently, I am experimenting with class switching on click events to transition between the desired states. However, I am facing difficulty in making the element first mo ...

Cross-origin request headers not permitted by the Access-Control-Allow-Headers policy in NodeJS and ExpressJS

I am currently facing an issue with my NodeJS + ExpressJS client-server setup while making API requests to a backend server. Every time I make an API request, I receive the following error: Request header field firstname is not allowed by Access-Control-A ...

utilizing Javascript to insert a class with a pseudo-element

Witness the mesmerizing shining effect crafted solely with CSS: html, body{ width: 100%; height: 100%; margin: 0px; display: table; background: #2f2f2f; } .body-inner{ display: table-cell; width: 100%; height: 100%; ...

Is it feasible to execute rendering while performing a Drag and Drop operation in ReactJs?

Can ReactJS handle rendering during a Drag/Drop operation? I'm looking to create a month view calendar in ReactJS where, when dragging a multi-day event backwards and forwards, all other day events on the calendar also move to show the effect before ...

Issue: A child component's function is unable to update the state of the parent component

I have been working on a project using React. Below is the code for the parent component: class Parent extends Component { constructor(props) { super(props); this.state = { deleteConfirm: false }; } onDelete = pass => { thi ...

In React Native on Android, the permission to WRITE_EXTERNAL_STORAGE is consistently denied

Application Environment Information: "react": "18.2.0", "react-native": "0.71.7", "react-native-permissions": "^3.8.0", "react-native-webview": "^12.0.2" Testing Device Detail ...

How come inactive links are still able to be clicked on?

I have been experimenting with various methods to disable links, but I seem to be unable to prevent them from being clickable. While I was successful in changing the cursor to 'not-allowed' when hovering over the disabled link, it still remains c ...

What is the best way to create individual Menu items in a React/MUI navigation bar?

I am currently developing a navigation bar for an application, and I seem to be facing an issue with differentiating between multiple Menu/MenuItem elements. No matter what approach I take, both Menus and their respective MenuItems end up directing to the ...

Avoid inserting line breaks when utilizing ngFor

I am using ngFor to iterate through a list of images like this: <div class="image-holder" *ngFor="let image of datasource"> <img src="{{image.url}}" height="150" /> </div> Below is the corresponding CSS code: .image-holder { di ...