What is the best way to add a background to certain elements?

Is there a simple and elegant way to swap the background of the circle with the background of the div underneath?

I would like the circles to have the background image of div "one", while keeping div "one" invisible (not using visibility:hidden).

I am looking for a refined and straightforward solution without overcomplicating things.

This should be compatible with IE10/11 and newer versions.

.one {
  position: relative;
  width: 500px;
  height: 300px;
  background-image: url("https://ak5.picdn.net/shutterstock/videos/11223065/thumb/1.jpg");
}

.two,
.three {
  position: absolute;
  top: 50%;
  transform: translate(0%, -50%);
  left: 15px;
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: #fff;
}

.three {
  left: initial;
  right: 15px;
}
<div class="one">
  <div class="two"></div>
  <div class="three"></div>
</div>

Answer №1

It seems like utilizing SVG may be a more effective solution than CSS for this particular scenario:

body {
  background: linear-gradient(to right, blue, red)
}
<svg width="500" height="300">
  <defs>
    <!-- define the mask-->
    <mask id="hole">
      <rect width="100%" height="100%" fill="white"/>
      <!-- the first hole -->
      <circle r="50" cx="100" cy="150" fill="black"/>
      <!-- the second hole -->
      <circle r="50" cx="400" cy="150" fill="black"/>
    </mask>
    <!-- define the background image -->
  <pattern id="img" patternUnits="userSpaceOnUse" width="500" height="300">
    <image  xlink:href="https://ak5.picdn.net/shutterstock/videos/11223065/thumb/1.jpg" x="0" y="0" width="500" height="300" />
  </pattern>
  </defs>
  <!-- create a rect, fill it with the image and apply the above mask -->
  <rect fill="url(#img)" width="100%" height="100%" mask="url(#hole)" />
</svg>

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

Exploring Angular 6: Unveiling the Secrets of Angular Specific Attributes

When working with a component, I have included the angular i18n attribute like so: <app-text i18n="meaning|description"> DeveloperText </app-text> I am trying to retrieve this property. I attempted using ElementRef to access nativeElement, bu ...

CSS declarations that have not been acknowledged or acknowledged

While working on a client's stylesheet today, I came across the following code: p { -webkit-hyphens: auto; -webkit-hyphenate-character: "\2010"; -webkit-hyphenate-limit-after: 1; -webkit-hyphenate-limit-before: 3; -moz-hyphens: manual; orphans: ...

Tips for modifying a text input in a list

Managing comment boxes and buttons for each box can be a bit tricky. Clicking on the edit button should enable only that specific textbox, while save or cancel actions should hide the text box and show "<p></p>". Below is my ng-template where ...

Transform a dynamic Maya model into JSON format for seamless integration with Three.js

After creating a Maya model in Maya 2008, I need to convert it to a JSON format for use with three.js. I have experimented with different methods including: 1) Utilizing the threeJsFileTranslator.py Maya plugin provided in the threejs package. 2) Trying ...

How can I select a checkbox using jQuery?

I need help toggling a checkbox on and off using jQuery. Here is the HTML code: <input type="checkbox" id="isWorking" name="isWorking" /> I attempted to control it with jQuery like this: $('#isWorking').prop('checked', true); $ ...

Adjust the size of a map on an HTML page after it has

Currently, I am utilizing Angular 2 to create a simple webpage that includes a Google 'my map' displayed within a modal. <iframe id="map" class="center" src="https://www.google.com/maps/d/u/0/embed?mid=1uReFxtB4ZhFSwVtD8vQ7L3qKpetdMElh&ll ...

What is the best way to focus on an object using a particular variable in javascript?

I am currently developing an online game where each user is assigned a unique ID. Below is the client code used to create a new player: const createNewPlayer = (id) => { return player[player.length] = { x:0, y:0, id:id } } The ...

Incorporating an image within a table while maintaining 100% height: A step-by-step guide

I'm struggling to make the image fit 100% to the height of a CSS-created table. The table and image seem to be ignoring the dimensions of the parent element, as shown below. Since everything needs to be dynamic, I am working with percentages. .img ...

React Material Design Cards for Responsive Layouts

Hi there, I am currently navigating my way through Material Design and attempting to ensure that the cards section is responsive by utilizing media queries and flex. However, I have encountered an issue where only a portion of the image is displayed when t ...

Associate the ng-model with ng-options value and label

I'm currently using ng-options to create a select tag that displays location options. The labels represent the location names, while the values indicate the corresponding location ID as stored in the database. In addition to binding the value (locati ...

I'm attempting to showcase an HTML file by using res.sendFile within the Express framework

As part of my workflow implementation, I have set up a scenario where upon user login, the credentials are sent via Ajax to an Express route for verification. If the user exists, the route responds with a message "authorised" triggering a second Ajax call ...

What is the best way to instruct npm to generate a .min.css file from scss?

Currently, I have setup a process to automatically compile CSS from SCSS using a JSON script. However, the issue is that the ".min" suffix is being removed during this compilation. Here is the output from the terminal displaying the script I am running and ...

What is the best way to loop through all the tags within a div element?

As a beginner in using BeautifulSoup, I am currently exploring the following HTML segment: <div class="jpag" id="srchpagination"><a rel='prev' class="dis"><span>&lsaquo;&lsaquo;</span> Prev</a><span class ...

Issues with HTML structure across various devices

Being a novice in web development, I've been experimenting with creating a modal body. https://i.sstatic.net/Qk5BR.png The code snippet below represents my attempt at structuring the modal body: <div className="row modalRowMargin textStyle" > ...

The issue of Jquery .click event handler failing to work for class selector specifically in CHROME

Is there a way to achieve the same opacity effect on click as on mouseover? I've tried various selectors without success. Any help on this issue would be greatly appreciated. Thank you in advance! .container { position: absolute; background:url(&a ...

Retrieve the stylesheets located within the <head> section of an external webpage and load them onto the current page

Trying to figure out a solution, I received an interesting request from a friend. They asked for help in creating a webpage that can dynamically pull a div tag from an external page (on a different server). To accomplish this, I utilized jquery's .loa ...

Items seem to vanish into thin air and become immovable when attempting to relocate them

I am attempting to create a unique grid layout with 3x3 dimensions, where each grid item represents a fragment of a single image. These items should have the capability to be dragged and dropped inside another 3x3 grid, in any desired sequence. I have hit ...

Struggling to ensure buttons on my page respond and resize properly alongside the other elements

Despite making most of this page responsive, I'm having trouble getting the two buttons to scale appropriately. They are utilizing javascript for a mouseover effect, and I suspect that might be causing some issues. Ideally, I would like the images to ...

Content refuses to show up on my social networking website project when Ajax is employed

I've been working on a Social Media website for a major project, and I recently implemented Ajax to load posts. However, I'm facing an issue where the content is not displaying on the index page after implementation. My goal is to load 10 posts a ...

How can I customize the hover effect of the drop-down options <option> to have a "blue" background color?

I've been struggling to change the default blue background color of the drop-down options when hovering over them with the mouse. Despite trying various solutions from this forum, nothing seems to be working at the moment. (These solutions used to wor ...