My goal is to overlay an information box on top of an image while ensuring that both elements remain contained within the designated area

I've experimented with different approaches, such as using position absolute on one element and position relative on the other. However, I always face issues where one overflows, despite setting overflow to hidden. Below is my current code snippet. (Please note that it says className instead of class)

<div className="card">
      <div className="img">
        <img src={props.image} alt="" />
      </div>
      <div className="infoContainer">
        <h2>{props.title}</h2>
        <h3>{props.description}</h3>
      </div>
    </div>

This JSX component contains a card:

.card {
  display: flex;
  width: 400px;
  height: 500px;
  background-color: blue;
  border-radius: 20px;
  margin-left: 20px;
  overflow: hidden;
}
.infoContainer {
  display: flex;
  flex-direction: column;
  width: 100%;
  background-color: aqua;
  align-self: flex-end;
  align-items: center;
  justify-content: center;
  padding: 10px;
}
.img {
}

Seeking suggestions from anyone who might have ideas or solutions. Your assistance is greatly appreciated. Here's the result of what I have so far, but I'm unsure how to proceed. I'm attempting to position the image where the dark blue area is shown in this output. https://i.sstatic.net/PZXip.png

Answer №1

To ensure proper positioning of elements within the card, apply position:relative to the card element. This allows any child elements with position:absolute to be positioned relative to the card itself.

.card {
  width: 400px;
  height: 500px;
  background-color: blue;
  border-radius: 20px;
  margin-left: 20px;
  overflow: hidden;
  /*Setting this to relative enables absolute positioning
   for descendant elements in relation to this container
  */
  position: relative;
}

.infoContainer {
  display: flex;
  flex-direction: column;
  width: 100%;
  align-self: flex-end;
  align-items: center;
  justify-content: center;
  padding: 10px;
  /*Positioning the infoContainer*/
  position: absolute;
  /*Align to the bottom*/
  bottom: 0;
  /*Bring forward*/
  z-index: 10;
  background-color: rgba(255, 255, 255, 0.8);
}

.img {}
<div class="card">
  <div class="img">
    <img src=https://www.fillmurray.com/400/500 alt="" />
  </div>
  <div class="infoContainer">
    <h2>Bill Murray</h2>
    <h3>Star of the classic film Garfield</h3>
    <!--<h2>{props.title}</h2>
    <h3>{props.description}</h3>-->
  </div>
</div>

Answer №2

Experiment with this CSS:

position:relative;

when styling a card component. Next, adjust the .img (consider using .img img{} if needed) properties to

position: absolute; 
top: 0vh;
width: 100%;

I trust this makes sense :-)

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

Attempting to deactivate the submit button in the absence of any input

As a beginner trying to work with typescript and html, I am facing an issue with disabling the submit button when a user fails to enter a part number. The error message I am getting is "Cannot find name 'partNumber'". Any advice or guidance on th ...

A shift in the sequencing of Hooks utilized by WithStyles has been identified by React

Recently getting started with ReactJS, I encountered an issue involving 'withStyles' while working on a project. Within my class component 'Category', I utilize another class component called 'CreateCategory' for creating new ...

Combine several elements in a single jQuery scrollreveal function

I am currently working on a webpage that utilizes the jQuery library plugin scrollreveal to gradually reveal different html elements when the page loads. The functionality of the code is working correctly at the moment, but I have noticed that there is a d ...

Enhancing the functionality of localStorage

I am attempting to append a value to a key within the HTML5 localStorage. Take a look at my code below: var splice_string = []; splice_string += "Test value"; var original = JSON.parse(localStorage.getItem('product_1')); origina ...

Encountering Issues with Formatting InnerHtml Text using RegEx

Technology: React.js I have been working on a custom function in JavaScript to highlight specific words within a code block. The function seems to be functioning correctly, but the highlighting isn't staying after the function is completed. Any ideas ...

Increase the size of the image beyond the boundaries of its container

I am facing an issue with an image inside a div. Whenever I add a margin-top, the background of the div extends downwards, which is not the desired behavior. How can I fix this problem? Below is my code snippet: <div id="content"> <div class=" ...

Is there an alternative if statement with 3 or more conditions?

I've been attempting to solve this issue for quite some time now, but I just can't seem to pinpoint what exactly I'm doing incorrectly. The first two conditions appear to be functioning properly, but the third one is failing to execute as ex ...

TinyMCE - Utilizing selection.setContent along with getContent for the Warp Button

I am looking to implement a button that will wrap content with all tags. Snippet of Code: editor.addButton('MobileToggleArea', { text: '<M>', icon: false, onclick: function (){ editor.selection. ...

Leverage CSS to manipulate image attributes

Is it possible to use CSS programmatically to set the attributes of my img tag? <span><img class="img-dollar"></img></span> <span><img class="img-royalty"></img></span> I am looking for a way to specify t ...

Important notice: Warning for stand-alone React 18 page regarding the import of createRoot from "react-dom"

I am in the process of developing a standalone webpage utilizing React 18: <!DOCTYPE html> <html lang="en"> <head> <title>Hello React!</title> <script crossorigin src="https://unpkg.com/react@1 ...

Using jQuery to emphasize search text in a user-friendly manner

After successfully implementing a search function for a table, I faced the challenge of highlighting the search keyword within a cell. Despite searching for a solution online, I couldn't find anything useful! Here is my HTML code: <table class="s ...

Error: The reselection failed because the selector creators require all input selectors to be in the form of functions

I am in the process of incorporating reselect into my component: const test = useSelector(({ test }) => test); const testData = createSelector( test, items => console.log(items), ); I encountered an issue with Error: Selector creators ...

What could be the reason behind the malfunctioning of my media query in the

I am trying to connect an external stylesheet to my React component in order to apply different styles based on screen width. Specifically, when the screen width is less than 300px, I want the logo to have a height of 100vh. However, it seems that the medi ...

Guide to displaying aggregated table field values in an input field when checking the checkbox

How can I retrieve the value of the second span and display it in an input field when a checkbox is checked? For example, if there are values like 500 in the first row and 200 in the second row, when the checkbox in the first row is ticked, the value of 50 ...

Graph is vanishing while linked

A unique HTML canvas is included in a standalone file called dashboard.html. To display the dashboard on the main homepage (home.html), we utilize ng-view nested within an ng-if directive. Oddly, clicking on the hyperlink "#" from the home page causes th ...

Break down the organization of CSS

While exploring a website, I came across this CSS code that sets the font family to "Open Sans," Helvetica, Arial, and sans-serif. However, I am having trouble understanding this structure. Any assistance would be greatly appreciated. Thank you in advanc ...

Problem with Shopping Cart: Unable to add items to the "Shopping Cart Page". Error in setting property 'textContent' to null

I am currently immersed in the process of developing a shopping cart using HTML, CSS, and Javascript. I have been following a tutorial on Youtube, but I have encountered a glitch. Despite attempting a few solutions, I continue to encounter the following er ...

A practical guide to troubleshooting typescript errors when exporting a map component

I encountered a typescript error when trying to export the map component with GoogleApiWrapper. It works fine when not wrapped in GoogleApiWrapper, but all my attempts to resolve the issue have failed. I would appreciate it if someone could review the code ...

Incorporating Cascading Style Sheets (CSS) to

I'm looking to style my form with CSS but I'm unsure of the process. Currently, the form is generated using PHP and MySQL, and in the browser it appears like this: http://gyazo.com/5d099ead9bd6ea83859a5114b2438748 I want to align the text and dr ...

Is it possible for me to identify the original state of a checkbox when the page first loaded, or the value it was reset to when `reset()` was

When a webpage is loaded, various input elements can be initialized as they are declared in the HTML. If the user modifies some of the input values and then resets the form using reset(), the form goes back to its initially loaded state. I'm curious, ...