Modify the background of a div and an image

My goal is to add a background to both the image and the div itself, similar to what I've seen on Amazon. However, I'm struggling to achieve this effect where the white background of the image doesn't show when applied onto the div:

Image with white background:

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

Image being rendered onto the div:

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

This is what I have attempted so far:

.grid-wrapper {
  display: grid;
  grid-template-columns: 1fr 2fr;
  background: #fff;
  box-shadow: rgba(0, 0, 0, 0.02) 0px 1px 3px 0px, rgba(27, 31, 35, 0.15) 0px 0px 0px 1px;
  border-radius: 4px;
}

.image-block {
  background: red;
  margin: auto;
}

.image-block img {
  width: 100%
  height: 100%;
}

.information {
  padding: 10px;
}

.information p {
  font-size: 0.875rem;
  line-height: 1.5;
}
<div class="grid-wrapper">
  <div class="image-block">
    <img src="https://m.media-amazon.com/images/I/61xgpXecLML._AC_UY218_.jpg">
  </div>
  <div class="information">
    <p>Logitech M510 Wireless Computer Mouse for PC with USB Unifying Receiver - Graphite</p>
    <p>Price: $100</p>
  </div>
</div>

The issue I'm facing is that the background: red; is partially visible under the image.

Answer №1

To achieve a see-through image background, opt for a PNG format image. For a red background effect, simply eliminate the solid background color from the image-block

.image-block {
    margin: auto;
}

Answer №2

The issue at hand is that the initial image you shared features a white background, whereas the subsequent image actually boasts a transparent background - a crucial distinction to note.
When an image with a transparent background is enveloped by a div element, it will appear against the transparency of the background, likely what's depicted in the second image.
This leads us to ponder how one can effectively utilize an image with a transparent backdrop.
The image currently being utilized is in .jpg format, which does not inherently support transparency; for this purpose, either .png or .svg formats must be used. Making this adjustment should ideally function without necessitating any alterations to the code other than updating the imported image.

If you consistently encounter images with white backgrounds, a potential workaround involves leveraging Canvas API and modifying the image manually using JavaScript, although this approach may pose performance challenges. A helpful solution addressing this method can be found on Stack Overflow:

Answer №3

It is recommended to utilize a PNG image format and then assign the desired background color to the div element containing the image.

Answer №4

When dealing with a .jpg image, there are ways to manipulate it, like this example: Apologies for any language barriers; I'm having trouble grasping your objective...

        .grid-wrapper {
          display: grid;
          grid-template-columns: 1fr 2fr;
          background-color: #ffffff;
          box-shadow: rgba(0, 0, 0, 0.02) 0px 1px 3px 0px, rgba(27, 31, 35, 0.15) 0px 0px 0px 1px;
          border-radius: 4px;
        }

        .image-block {
          background-color: red;
          margin: auto;
          padding:20px;
        }
        
        .image-block img {
          
        }
        .image-container{
          background-color: white;
          padding:20px;
        }
        .information {
          padding: 10px;
        }

        .information p {
          font-size: 0.875rem;
          line-height: 1.5;
        }
<div class="grid-wrapper">
  <div class="image-block">
      <div class="image-container">
    <img src="https://m.media-amazon.com/images/I/61xgpXecLML._AC_UY218_.jpg" alt="image">
      </div>
  </div>
  <div class="information">
    <p>Logitech M510 Wireless Computer Mouse for PC with USB Unifying Receiver - Graphite</p>
    <p>Price: $100</p>
  </div>
</div>

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

Tips for resizing a 100% div without displaying vertical scrollbars in the browser

I am facing an issue with a table that has three rows. I have a main #container div with a height of 100%, and inside it is a table with 3 rows. The first and last row contain fixed size content. However, in the second row, there is a #content div with a h ...

What is the best way to ensure my <h5> element fits perfectly inside its parent div vertically?

Currently facing an issue with finding a solution to my problem. The challenge involves having a header tag nested within multiple divs. Below is the structure: <div class="card"> <div class="layout-left"> <div class="card-header"> ...

Working with Node.js: Implementing a method callback within a loop

What is the best way to call a method with a callback in a loop? Let's say you have to make multiple calls to http.get but you want to ensure that only one request is waiting for a response at a time (to avoid overwhelming the server) http.get(url, ...

Issue with Vue: Calling method instantly when using :mouseover in v-for within a component

Whenever I try to incorporate :mouseover in a v-for loop within the <component>, I encounter an issue. The method assigned to the :mouseover event is triggered for each element, rather than just for the hovered elements. <Single-technology ...

What is the proper way to document JSDoc in JavaScript specifically for the TypeScript Compiler?

There is a feature that allows you to check JS code with the TS compiler. The TS compiler also recognizes JSDoc comments (https://github.com/Microsoft/TypeScript/wiki/JSDoc-support-in-JavaScript). I am looking to utilize the TS compiler in my React projec ...

Is there a way to fill select boxes with multiple values?

As I set up a jqGrid, I encountered the challenge of visualizing multiple values in one cell. The data is sourced from a form where users can select multiple options. While I managed to display the select box, I struggled with populating it. My attempts to ...

Struggling to grasp the concept of using z-index in CSS

As a programmer versed in Python, C, Java, and Delphi, I am not a web developer or designer by trade. However, when required to fill those roles, I do my best; please be patient with me. :-) I have created a map (with a background image as a div), where I ...

Preventing the use of special characters in URLs using JavaScript

I have a treeview where I am dynamically adding nodes and attaching a JavaScript click function to each node. The issue is with the use of the escape function on the url values stored in thisFileNode.Value. Even after using the escape function in my code, ...

When a function is triggered automatically, it sends back an HTML response by default, whereas executing it manually will yield

My current setup involves an action named fetchUserPermissions that retrieves a permission set from an api endpoint using axios. This action is triggered by another action called init, which is automatically executed through the utility dispatchActionForAl ...

Ensure Angular Reactive Forms do not include empty fields when submitting the form

Is there a way to retrieve only the fields with values entered by the user from a form and exclude empty fields in the resulting JSON object? Currently, the JSON object still includes empty quotation marks for empty inputs. Thank you! Current: { "user ...

Tips for importing a canvas using a fabric.Group from a JSON file?

I'm currently facing an issue with saving my canvas as a JSON file in fabricjs. Everything works fine with ToJSON and loadFromJSON functions except when there is a group on the canvas. Is there anyone who can provide guidance on how to tackle this pro ...

Confirming if the value is an array using jQuery

Currently, I have a list of elements with various types associated with them. My goal is to identify specific text within these types and hide only those types that do not contain the desired text, leaving the rest unaffected. The structure looks like thi ...

Combining results and tallying occurrences

I retrieved some data from my database and it looks like this: 20f -Won:0 Placed:0 20f -Won:0 Placed:0 20f -Won:0 Placed:0 20f -Won:0 Placed:0 20f -Won:0 Placed:0 20f -Won:0 Placed:0 20f -Won:1 Placed:1 20f -Won:1 Placed:1 20f -Won:0 Placed:0 21. ...

Flask Session Persistence Issue: Postman Successful, Javascript Fails

Developing a Flask server to facilitate communication between backend Python functionality and Javascript clients on the web has been my recent project. I am trying to harness Flask's `session` variable to retain user-specific data throughout their in ...

Scroll the div back to the top when the form is submitted

I have set up a form in a pop-up using Bootstrap modal. The form is quite long, so after submission, the message appears at the top of the form. However, I want it to scroll to the top when the user submits the form so they can easily see the message. Is ...

Using Vue's computed property setter with an object as a prop

I have a new concept for an input component where different objects can be passed, displayed as CSV, and allow for text editing/validation with style changes based on validation results. Check out the code snippet I've been working on: <div id=&quo ...

Transferring reference from a parent component to a child component

One of my components is called ChatScroller, which manages scrolling in a chat by autoscrolling when new messages come in. Here's a simplified version of it: class ChatScroller extends Component { scrollRef = node => this.scrollNode = node c ...

Displaying SVGs in the <object> tag within an Angular 2 component is not functioning properly in Internet Explorer

When embedding an SVG within an <object> tag, it is not displayed in IE 11 and Edge. Instead, they only show the innerHTML as "SVG no supported," giving the impression that the tag is not supported at all. Interestingly enough, both browsers do d ...

What is the best way to resize an element such as an image?

When an image is resized using a percentage, it preserves its aspect ratio properly. I am looking for a way to replicate this behavior with a div. My current challenge involves precisely positioning an element relative to an image. The image fills 100% of ...

What is the best way to align a checkbox and text in the same row within a Mailchimp embedded form?

I am troubleshooting a styling issue with my Mailchimp form that has groups. The problem I'm encountering is that the checkboxes are displaying on one line while the text appears on the next line in an unordered list format. To see the issue for yours ...