Utilize the parent element's background color as the color for the box-shadow styling

Currently working on enhancing a radio button design by incorporating an inset box-shadow.

If you're interested, check out this CodePen showcasing my progress so far.

The main challenge I'm facing is getting the background of the .radio-button <div /> to dictate the color of the box-shadow. In my initial approach, the color was specified as #fff, which proved effective on a white backdrop but not on a gray one.

  • Assigning it as currentColor results in the box shadow picking up the border-color value (#333);
  • Opting for inherit seems to deactivate the box-shadow, particularly noticeable in Chrome. It makes some sense as I suppose inherit might not apply to certain property components here.
  • Omitting any specific color selection defaults to using the color value, typically resulting in black.

Any suggestions on how to achieve this desired effect without resorting to JavaScript?

Answer №1

Unlike currentColor, CSS doesn't offer a direct way to access an element's background color value. However, you can simulate this by setting the text color as the background color and using background-color: currentColor. This trick involves creating a separate element for the label text with its own font color.

It's worth noting that while the keyword inherit works for certain CSS properties like box shadows, it must be used alone in a declaration. You cannot apply it singularly within a set of values. Therefore, full inheritance of the box-shadow property is necessary; trying to inherit only parts of it won't yield the desired result if the parent element doesn't have a box shadow to begin with.

Answer №2

I'm still a bit confused about your intentions...

Perhaps something along these lines?

.example {
  width: 80px;
  height: 80px;
  border: solid 2px black;
  border-radius: 50%;
  margin: 20px;
  display: inline-block;
  background: radial-gradient(circle, white 15px, transparent 35px);
  box-shadow: 2px 4px 8px 0px black;
}

body {
  background-image: linear-gradient(to right, red 100px, green 100px, green 200px, blue 150px);
}
<div class="example"></div>
<div class="example"></div>
<div class="example"></div>

Answer №3

In the realm of CSS, achieving a 'padding of transparency' may seem quite challenging, but fear not as there is a clever workaround using pseudo-elements. To bring this concept to life, you must remove the box-shadow and any background from the circular shape, and instead view the central dot as an independent element that will appear to 'float' above:

.radio-button-input + .radio-button-circle {
  display: inline-block;
  width: .8em;
  height: .8em;
  margin-right: .5em;
  border: 2px solid #333;
  border-radius: 50%;
  transition: background-color .1s ease-out;
  position: relative;
}

.radio-button-input + .radio-button-circle:after {
  content: ' ';
  display: block;
  background-color: transparent;
  transition: background-color .1s ease-out;
  width: .55em;
  height: .55em;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 100%
}

.radio-button-input:checked + .radio-button-circle:after {
  background-color: #333;
}

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

Utilizing Backward and Forward Navigation with AJAX, JavaScript, and Window.History

TARGET Implement Ajax for fetching pages Utilize JavaScript to update the window URL Leverage Window History to preserve Ajax page for seamless forward-backward navigation without reloading the page Modify the Navigation bar's attributes (such as co ...

Attempting to isolate just a fragment of the inner content

Option Explicit Sub VBAWebscraping2() Dim IEObject As Object Set IEObject = New InternetExplorer IEObject.Visible = True IEObject.navigate url:="https://streeteasy.com/building/" & Cells(2, 4).Value ...

What is the most effective way to create a live preview using AngularJS and CSS?

Is there a way to dynamically change the background color of the body based on the hexadecimal value entered in a textfield, without using jQuery? I want the change to happen live as the user types. The current code works but it doesn't feel right. I ...

Ways to apply CSS to a changing div ID

I am looking to apply CSS to a dynamically generated div ID. var status = var status = item.down().next().innerHtml(); if(status == "test") { var c = 'item_'+i ; c.style.backgroundColor = 'rgb(255, 125, 115)'; //'ite ...

Expansive HTML body width (exceeding 100%)

I am currently in the process of designing a horizontal webpage and I am aiming to create a dynamic body that expands based on the content it contains. You can view the website I am working on here: While everything is functioning as intended, I am strug ...

What is the process for incorporating a scrolling feature into my HTML page?

I need to display the following content on my website: What code do I need to add in order to achieve this? I tried using the code snippet below but it did not work as expected: <td nowrap align="left" style="word-wrap: break-word"><pre style="w ...

Creating HTML elements dynamically in ASP.NET MVC on the server-sideWould you like a hand with

Is there a way to generate HTML components on the server side of ASP.NET MVC or in the controller? Currently, my approach involves generating an HTML table in a controller function which returns a string of HTML table code. Then, I make an AJAX call to th ...

Verify the entered information in the input field and showcase it in the display box after pressing the ENTER key

I need to display selected values of a tree structure in a show box on the other side of the page using code. However, I also need to include HTML input boxes in some lines of the tree structure so that users can input data. The challenge is getting the in ...

Having trouble with the gradient hover effect on the Tailwind Button in dark mode?

In my nextjs project with Tailwind CSS, I am encountering an issue while trying to apply specific properties to a button using globals.css. The problem lies with the dark:hover properties not working as expected. .coloredButton { @apply bg-gradient-to- ...

Challenges with conflicting CSS styling issues

I am facing a CSS issue on my website. I am trying to add captions for images on my site. I have experimented with the jquery-capty plugin () and some custom CSS styles for framed images (). While both solutions work in my regular layout, they do not funct ...

Utilizing jQuery for Summation Calculation

How can I use jQuery to calculate the total sum of prices for all products in a table with the column name "price"? Once calculated, how do I store this sum in a variable? <table id ="tab"> <th>Product</th><th>Price</th> ...

Removing data entry from MySQL database table using PDO

I'm facing an issue with deleting a row from my database. Despite trying various methods like PDO and MySQL, the data is not getting deleted, but it shows as if it has been deleted. Can someone please help me identify what might be wrong with my code? ...

Bootstrap 5's gutter-x class is causing alignment issues with divs

My div alignment is getting thrown off due to the default .row class. The margin_left and right call it includes add gutters that cause borders to overlap my div. Although I have disabled gutters in this particular element, it leads to problems elsewhere o ...

The hyperlink function is not operational in Gmail attachments

Using an anchor tag to navigate to a specific section within the same page works perfectly when the HTML file is on my local machine. However, when I attach the file in Gmail and open the attachment, it doesn't work. Why is this happening? How can I m ...

sort jquery alphabetical characters

I have created some html and jQuery code that allows me to hide elements based on class name when I click on specific buttons in the filter div. However, I am now looking to add functionality to sort the elements alphabetically by class name when I press ...

Position the table at the bottom of the page using CSS

I'm experiencing an issue where my HTML is not rendering correctly. I need to move the table with the ID "footer" to the bottom of the page. Here's the structure of the situation: <div id="container"> <table id="footer"></tabl ...

The function I created is having trouble connecting to the controller.js file

My JavaScript controller file function ServicesCtrl($scope) { console.log("Greetings from ServicesCtrl"); $scope.message = "Hello!"; } The main HTML file <html> <head> <title>The Complete Web Development Stack</ti ...

Creating an online store checkout system with JavaScript?

Recently, I have been experimenting with the PayPal HTML code to create my own shopping cart from scratch instead of using a pre-made system. My current approach involves storing the items added to the cart in an array, although ideally I would like to st ...

Attempting to adjust the spacing between my <h2> and <ul> tags to be more concise

I'm having trouble reducing the space between my h2 and ul elements. It seems that margins are not effective for ul elements and I can't seem to achieve the desired spacing. Is there a specific trick or technique that can be used to address this ...

`Enhance Image with a Fresh Hue`

Let me explain my dilemma: I'm dealing with a two-tone png image - one tone is black and the other is transparent. Right now, I'm relying on the background color attribute to dynamically change the color of the transparent section. However, I ...