Only the circle's center reveals the background color

I am trying to create a design with a yellow background and a circle that has a grey border. I want everything outside the circle to be white, while the inside of the circle should reveal the yellow background from the div below it.

I am looking for a CSS solution without resorting to replacing the css circle with an image.

Structure:

<div id="background">
    <div class="circle">
    </div>
</div>

CSS Style:

#background{
    width:100%;
    height:100%;
    position:absolute;
    top:0;
    left:0;
    background:#ffff99;
    white-space: nowrap;
    text-align:center;
}
#background:before{
    content: "";
    display: inline-block;
    vertical-align: middle;
    height: 100%;
}
.circle{
    width:100px;
    height:100px;
    position:relative;
    border-radius:50%;
    border:2px solid #999;
    display: inline-block;
    vertical-align: middle;
    white-space: normal;
}

Here is the jsFiddle link to test it out.

Expected outcome: https://i.sstatic.net/Qafu4.png

Progress Update

I have implemented a radial gradient but need to maintain the circular shape while ensuring the div height and width remain at 100%.

Check out this updated jsFiddle

Update 2

I have achieved the desired effect, however, I am open to suggestions on whether there is a more efficient way to achieve this.

Answer №1

Implemented a creative solution here by utilizing a large border on the circle and positioning it within the #background div.

You can view the updated fiddle here: http://jsfiddle.net/jormaechea/n3c1t0ee/6/

#background {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  background: #ffff99;
  white-space: nowrap;
  text-align: center;
  overflow: hidden;
}
#background:before {
  content: "";
  display: inline-block;
  vertical-align: middle;
  height: 100%;
}
.circle {
  width: 100px;
  height: 100px;
  position: relative;
  border-radius: 50%;
  border: 1000px solid #999;
  display: inline-block;
  vertical-align: middle;
  white-space: normal;
  position: relative;
  left: -900px;
  top: -900px;
}
<div id="background">
  <div class="circle">
  </div>
</div>

Answer №2

Consider adding a parent element to contain the .circle class, adjusting the z-Index, and making the .circle background transparent !important

`background:transparent;`

#background{
    width:100%;
    height:100%;
    position:absolute;
    top:0;
    left:0;
    background:#000;
    white-space: nowrap;
    text-align:center;
    z-Index:0;
}
#background:before{
    content: "";
    display: inline-block;
    vertical-align: middle;
    height: 100%;
}

.wrap {
    left:40%;
    top:100px;
    position:absolute;
    z-Index:10;
    background:#fff !important;
    width:100px;
    height:100px;
    border-radius:50%;
}
.circle{
    top:-2px;
    left:-2px;
    width:100px;
    height:100px;
    position:absolute;
    border-radius:50%;
    border:2px solid #999;
    display: inline-block;
    vertical-align: middle;
    white-space: normal;
    background:transparent !important;
}
<div id="background">
    <div class="wrap">
        abc
    <div class="circle">
    </div>
    </div>
</div>

Check out the updated code on jsfiddle here

Answer №3

I came up with a unique solution by using a transparent radial-gradient to create a hole and applying pointer-events: none to allow selection of content beneath the layer. The text is aligned in the center of the div for a neat appearance. Take a look at my approach:

.background {
  width: 600px;
  height: 600px;
  background: deepskyblue;
}
.circle {
  width: inherit;
  height: inherit;
  background: -moz-radial-gradient(transparent 100px, rgba(0, 0, 0, 1) 100px);
  background: -webkit-radial-gradient(transparent 100px, rgba(0, 0, 0, 1) 100px);
  background: -ms-radial-gradient(transparent 100px, rgba(0, 0, 0, 1) 100px);
  background: -o-radial-gradient(transparent 100px, rgba(0, 0, 0, 1) 100px);
  pointer-events: none;
  display: table;
}
.circle p {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}
<div class="background">
  <div class="circle">
    <p>Lorem ipsum dolor sit amet</p>
  </div>
</div>

Answer №4

I included a white shadow effect to the circular shape

box-shadow: 0px 0px 0px 1000px white;

#background{
    width:100%;
    height:100%;
    position:absolute;
    top:0;
    left:0;
    background:#ffff99;
    white-space: nowrap;
text-align:center;
}
#background:before{
    content: "";
    display: inline-block;
    vertical-align: middle;
    height: 100%;
}
.circle{
    width:100px;
    height:100px;
    position:relative;
    border-radius:50%;
    border:2px solid #999;
    display: inline-block;
    vertical-align: middle;
    white-space: normal;
    box-shadow: 0px 0px 0px 1000px white;
}
<div id="background">
    <div class="circle">
    </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

What is the correct way to configure dependencies in RequireJS in order to pass variables between files successfully?

I have recently started utilizing requirejs and I am encountering difficulties in establishing connections between these files. Typically, I would just include js files in the correct load order. main.js requirejs.config({ paths: { "j ...

css apply liquid-like resizing to div element

I'm trying to set the height of the container to automatically adjust if the content overflows, allowing it to flow naturally. This is the code I'm currently using on my site: Here Please disregard the HTML and focus only on the CSS as that&apo ...

What is the best way to switch content between tabs using ajax and php?

I have organized my content into two tabs and I am switching between the tabs using JavaScript. <div class='tab-container'> <div class='tab-1'> <?php $sql="SELECT * FROM posts WHERE status='tab1'"; echo "<d ...

Formatting is lost in X.PagedList following an AJAX request

After referencing the UnobtrusiveAjax example here, I decided to incorporate some Bootstrap CSS into my project. Everything seemed to be working smoothly, until I encountered a problem with the pager functionality. The updated page lost all formatting and ...

Struggling to locate the earliest date within a nested array of objects

I have an array of objects nested inside another object. My goal is to identify the minimum date from the 'FromDate' property within the 'Market' objects. What would be the most effective method to achieve this when dealing with both si ...

Is there a way to add animation effects when transitioning between sections in HTML?

Recently, I decided to incorporate sections into my website, but I am not a fan of how it simply jumps to the designated section. I have observed on some websites that when you click a button to move to another section, it smoothly slides down. Despite c ...

PHP - Dynamically updating the total amount in the shopping cart without having to reload

Summary: I am facing a challenge in updating the total amount in the cart without having to refresh the page, specifically in the header section and the confirm order accordion. Every time a user selects the shipping zone and clicks on a button, an AJAX ca ...

How can I customize the appearance of a specific tagged page on my Tumblr blog?

Although I have a solid grasp of HTML & CSS, I am encountering some difficulties with the Tag Page feature on Tumblr. Specifically, I want to change the heading displayed on the /tagged/vld tag page without altering the URL. Currently, the heading is gener ...

What could be the reason behind my table appearing all crammed up in a single

My table is appearing in a single cell, and I can't figure out what's wrong. Can someone please take a look and let me know where I messed up? <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" cont ...

Using Jquery to duplicate an element without assigning a different name

Currently, I am working on a project using CakePHP. While I don't have much experience with jQuery, I need to clone an input/select field for a form using the jQuery .clone() function. Despite my efforts, the script I wrote isn't working as expec ...

retrieving JSON data within HTML elements

How can I access the JSON values {{u.login}} from HTML instead of just through JavaScript? Currently, I am only able to access them using JS. Is there a way to directly access JSON values in HTML? At the moment, I am getting them as text. Could you please ...

Curious about making an AJAX call with jQuery?

As I work on a jQuery ajax statement, my main goal is to receive a payload from the server and then process it in a separate function. However, I'm currently facing some difficulties with the syntax of the function. $.ajax({ url: 'mypgm ...

Ways to switch out event listener when button is deactivated

I find myself in a situation where I need to switch all unauthorized controls on my UI from a hidden state to a disabled state. Additionally, I want to add a tooltip with unauthorized text and have the control display this text when clicked. While I am ab ...

Using jQuery to extract a specific dropdown control from a collection of dropdown controls within a table row

I'm facing an unusual situation. There's a table with multiple rows (each generated dynamically by clicking a button). Each row contains the following: |=======| |=======| |=======| |=======| Where |=======| represents a dropdown se ...

Rails encounters problems when assets are modified

I recently inherited a small Rails website. When I attempted to make a change to a css file, I encountered an error page (code 500) in Rails with the following message: No such file or directory - /.../cache/assets/sprockets%2F1450c8f5d2b6e201d72fa175586b ...

How can CSS grid be used to move a particular <td> element?

Currently, I am working on creating a dynamic table using CSS grid and I am looking to slightly adjust the left-most <th> and <td>. I am uncertain about how to move a specific element within a <table>. My goal is to achieve a layout simi ...

Is it considered detrimental to include multiple instances of $(document).ready(function() {}); in your jQuery codebase?

Is it problematic to include multiple instances of $(document).ready(function() {}); on a webpage? My website involves loading various elements at different intervals, and I trigger partial postback functions within each $(document).ready(). However, the ...

Create an immediate impact by injecting CSS

I currently have the following code set up: style.js: function applyStyle(str) { var element = document.createElement('style'); element.innerHTML = str; document.body.appendChild(element); } var style = 'body {' style ...

Tips for getting this image swap code to function properly on your Wordpress site

After following a tutorial on Design Shack, I created a portfolio page utilizing CSS. However, the current setup only displays an image when hovering over a title due to some limitations. Below is the CSS code used: .container_imd { position: relative; ...

Unable to see the array values in jquery autocomplete

Hey there, I'm currently using jQuery autocomplete with CodeIgniter. My result array is fetched from the database and I am sending it back as JSON-encoded data. However, I am facing an issue where the values in the search results are not being display ...