Display the product image as radio buttons or checkboxes, with a tick mark appearing when selected

I am trying to add a background image to the <img> tag. My goal is to display the checkmark image in the center of the Twitter image. However, the checkmark image as a background image is not appearing here.

img{
  background-image:url(https://www.colourbox.com/preview/2692824-green-check-mark-symbol-over-white-background.jpg);
  background-repeat:no-repeat;
  background-size:20%;
  background-position:center center;
}
<img src="https://hakin9.org/wp-content/uploads/2014/02/twitter-150x150.png"  />

Answer №1

If you are looking to create a custom checkbox, be sure to follow these guidelines:

  • Include at least one checkbox and one label
  • Place the checkbox before the label
  • Set the checkbox to have display:none
  • The label should have for={Id of checkbox}
  • If the label immediately follows the checkbox, the checked style should be written like: ex. chx:checked + label {...
  • If the label and checkbox are not directly next to each other, use: ex. chx:checked ~ label {...
  • These are the most common layouts, but there are other possibilities as well.

Demo

.chx {
  display: none
}

.label {
  display: block;
  background-image: url(https://hakin9.org/wp-content/uploads/2014/02/twitter-150x150.png);
  background-repeat: no-repeat;
  background-size: contain;
  background-position: center center;
  width: 150px;
  height: 150px;
  border-radius: 50%;
  position: relative;
}

img {
  display: none
}

.chx:checked+.label img {
  position: absolute;
  z-index: 1;
  display: block;
  border-radius: 50%;
  top: calc(50% - 25px);
  left: calc(50% - 25px);
}
<input id='chx' class='chx' type='checkbox'>


<label class='label' for='chx'><img src='https://www.colourbox.com/preview/2692824-green-check-mark-symbol-over-white-background.jpg' width='50'></label>

Answer №2

The focal point of the Twitter graphic you've selected is a bright white shade.

A background pattern will only be visible in sections of the image that are see-through.

To make those pixels transparent, you'll have to modify the image using an editing tool.

Answer №3

Your issue stems from the fact that the twitter logo's white background is obscuring the tick behind it.

Using just one img element makes it impossible to place the background-image on top of the content. To overcome this, you will need to introduce an additional element like a div, as shown below:

I have reduced the size of the tick to ensure the twitter icon remains visible

.test {
  display: inline-block;
  position: relative;
}

.test:after {
  content: '';
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  position: absolute;
  background-repeat: no-repeat;
  background-size: 50%;
  background-position: center center;
  background-image: url('https://www.colourbox.com/preview/2692824-green-check-mark-symbol-over-white-background.jpg');
}
<div class="test">
  <img src="https://hakin9.org/wp-content/uploads/2014/02/twitter-150x150.png" />
</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

How come the Google fonts won't display correctly within my HTML document?

After importing some fonts into my CSS file, I noticed that they are not displaying correctly in the design. Below is the code for the imported fonts and CSS linking: https://i.stack.imgur.com/9RQ4u.png The output only shows sans-sarif working properly: ...

Create a division element with an image that can be dragged around

I'm having trouble making a div element draggable. Inside the div, there is an img and some text that acts as a label for the image. The issue is that when I begin dragging by clicking on the img, it's the img that gets dragged instead of the par ...

Modify the navbar background color in bootstrap-vuejs

As per the information provided in the documentation, the instructions for setting styles for the navigation bar are as follows: <b-navbar toggleable="lg" type="dark" variant="info"> <!-- Or --> <b-navbar toggleable="lg" type="dark" variant ...

Mystery factor triggering the appearance of scrollbar as the window is resized to a specific width threshold

I'm facing a rather peculiar problem. I'm currently in the process of creating a webpage tailored for an iPhone screen resolution of: width: 640; height: 960; However, there seems to be a mysterious hidden element that is extending the width of ...

Exploring the tab-content features of Bootsrap 5 through anchor tags

In the previous version of Bootstrap, the tab-pane used to fade in active. However, in the new version, it is now: tab-pane fade show active. What other improvements can I make? <!DOCTYPE html> <html lang="en"> <head> <m ...

Determine the specific table entry that was clicked on

Currently, I am working on developing a dynamic table that includes a button in one of its columns, which triggers a bootstrap modal. I'm a bit unsure on how to determine which button was clicked and how to populate the table accordingly. Admittedly, ...

The font implemented in Bootstrap does not appear to be adapting well to different

As I embark on the Bootstrap journey, I find that everything looks great on my desktop browser in terms of responsiveness. However, when I switch to my iPhone 6, it doesn't seem to display as expected... Provided below are a few screenshots: Desktop ...

Display the menu upon activating the hover event icon using only CSS

Assistance Needed - Unable to activate hover and display the rest of the div I want to activate the hover effect where early levels are visible, and when hovering over the first level, the subsequent levels appear List item body { margin: 3%; } div.ti ...

Is there a way to adjust the size of a video thumbnail to fit within a

Currently, I am developing a system to remotely control home electrical devices via the web. However, I am facing an issue with fitting camera images properly within their container. https://i.sstatic.net/Yct2d.png How can I ensure that the video thumbna ...

Unraveling a collection of HTML tags using Python's Beautiful Soup library

Need help parsing HTML code using Python Beautiful Soup. Trying to extract images along with their width and height properties, if available. The following code snippet indicates there are three images on a page: the first image is 300x300, the second has ...

To create a distinctive design, the HTML5 wrapper will specifically enclose the Header element

After using a border, I discovered that my wrapper is only wrapping the header and not extending down to the footer. Can anyone offer some guidance on how to wrap the entire content from the header to the footer? I've come across suggestions to speci ...

confirm that the form is necessary and contains text

How can I ensure that a specific text string is validated for the input field labeled "promo"? Take a look at the code snippet below: <script> function validateForm() { var x = document.forms["myInquiry"]["promo"].value; if (x == null || x == "") ...

Discovering the initial element with a data attribute above zero using JQuery

I am working with a set of divs that have the class .item-wrap. At the moment, I am able to select the first div using this code snippet: $(".item-wrap:first").trigger( "click" ); Each .item-wrap element comes with a data-amount attribute. My challenge ...

Mini-navigation bar scrolling

I am trying to create a menu where I want to hide elements if the length of either class a or class b is larger than the entire container. I want to achieve a similar effect to what Facebook has. How can I make this happen? I have thought about one approac ...

When the button/link is clicked, I must dynamically create a modal popup that includes a user control

I am currently working on improving an asp.net web forms website by incorporating popup modals to display the rental rates for available equipment. The challenge arises when dealing with pages where each piece of equipment has different rates, requiring a ...

Matching stroke-dashoffset in SVG between two distinct paths

I am currently working on animating stroke-dashoffset for two different paths to create a drawing effect. There are buttons provided to adjust the stroke-dashoffset values. My goal is to ensure that the filled paths align vertically as they progress. Is t ...

Challenging glitch in the JavaScript code

My jQuery code seems to be functioning properly in online text editors like Fiddle and StackOverflow snippets. However, when I try to implement the same code in Brackets or view it on GitHub, the navbar scroll down animation fails to work as expected. You ...

Tips for maintaining the footer at the bottom of the page regardless of the screen size

In my current project, I am dealing with several web pages. For each page, I have arranged three div tags: Header Body (content) Footer The issue arises when it comes to the body div tag, which houses the main content of a page such as forms and descrip ...

"Varying hues on various displays: Exploring RGB values and CSS for video color

Recently, I designed a webpage with a background-color of #f4afac. The video embedded in this page also features the same background color throughout, creating a seamless blend with no visible edges. On one screen, the colors appear perfectly consistent. ...

Which Web Browsers Support Canvas and HTML5?

Currently working on a project that involves using the HTML5 Canvas element. I am curious about which major browsers (including specific versions) actually support the Canvas tag. I am not interested in hearing about IE. In this tutorial Drawing shapes - M ...