What is the best way to utilize the parent-selector :has() in order to choose a child element of a parent, making sure there are no other elements between the parent and child?

In my quest to enhance auto-color contrast correction, I am utilizing Tailwind and experimenting with CSS rules that will automatically adjust colors when the contrast is insufficient.

Consider the following example:

.bg-white .text-white {
  color: black;
}

It's a simple solution. By changing white text on a white background to black text on a white background, we eliminate concerns about meeting WCAG Accessibility guidelines for those who use .text-white.

However, a challenge arises when white text becomes illegible amid other background colors, as shown in this HTML snippet:

<div class="bg-white">
   <div class="bg-black">
      <span class="text-white">I was legible until you tried to be smart.</span>
   </div>
</div>

There are numerous solutions to this issue, but I'm intrigued by the possibility of using the :has() selector to address it.

My proposed approach looks like this:

.bg-white:not(:has(class*="bg-")) .text-white {
  color: black;
}

The idea is to target any text-white within a bg-white element that doesn't contain any other classes similar to "bg-", indicating changes in background color.

Ultimately, I aim to designate only the most immediate color-changing class as the background color.

Answer №1

Good effort, but you're missing the crucial []

.bg-white:not(:has([class*="bg-"])) .text-white {
  color: red;
}
<div class="bg-white">
  <div class="bg-black">
    <span class="text-white">I was legible until you tried to be smart</span>
  </div>
</div>

<div class="bg-white">
    <span class="text-white">I was legible until you tried to be smart.</span>
</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

adjusting array based on screen size fluctuations

Imagine having two arrays of images named landscape_images[] and portrait_images[]. One is for the landscape view and the other for the portrait view. When the screen width is in landscape mode, the wide resolution images will be displayed on the body. C ...

The arrangement of Bootstrap columns does not appear to be in alignment with one another

While utilizing Bootstrap 4, I am encountering an issue with the Footer section. My intention is to create three columns of equal width using the col-md-6 class as outlined in the documentation. Here is the code snippet: <footer class="footer"> &l ...

Eliminating projectiles from disorganized list

Query If you are interested in accessing the html and css for the current website I am developing, you can locate it at /Query I am currently facing an issue with removing the bullets displayed in the top horizontal menu (Home...Contact). Despite follow ...

Video posters can now feature a sleek black border to enhance the

How can I add a black border to a video poster? I've tried to work it out on my own, but I'm having trouble getting it to work. Thank you for any assistance... echo '<video id="video" width="300px" height="200px&q ...

Creating spacious gaps between list items

I have been working on creating a scrollable horizontal list using iscroll. Although the list functions properly, I am encountering an issue with spacing between each li item. Despite trying various solutions, I have been unable to remove the space betwe ...

Tips for centering a horizontal unordered list within a division element

I can't seem to figure out why the text-align center isn't working. I've followed other tutorials and applied the textalign: center to the div, then added display: inline to the ul. Below is a sample of my HTML code from the browser as I am ...

Tips for preventing extra whitespaces and line breaks from being added to the render tree in Blazor applications

Consider the code snippet below <div> @for (int i = 0; i < 10; i++) { <span class="@classes[i]">@i</span> } </div> The desired output is (with each character styled differently) 01234567890 Howev ...

Potential Issue with CSS General Sibling Selector

It seems like I have encountered a potential bug with the General Sibling Selector. When I use p ~ div, the selector does not work as expected. However, when I replace p with the specific class name text_paragraph, it works fine. You can view my code and f ...

What is the process for altering the color of an upload button once the maximum number of file uploads has been surpassed?

Within my HTML code, I have included a line to restrict the number of file uploads to a maximum of 3. <input *ngIf="uploader.queue.length < 3" #uploadButton ng2FileSelect (change)="fileUpload()" [uploader]="uploader&qu ...

Issue with fadeIn effect not functioning properly on the second child element

Requesting assistance with troubleshooting my code. The intended functionality is to animate the img tags using fadeIn and fadeOut, but currently only the first image fades out without the second image fading in. I suspect there may be an issue in my CSS t ...

The persistent bar that refuses to abide by z-index rules in the Google Chrome browser

While working on a website, I decided to add a navigation bar in ribbon style. To ensure it sticks to the top of the viewport when the user scrolls, I used the jQuery Sticky Plugin. Everything was running smoothly in Firefox, but unfortunately Chrome star ...

Bootstrap - the ability to resize elements dynamically

I'm currently working on a layout using Bootstrap and I could use some guidance to achieve the desired outcome. Essentially, what I am trying to accomplish is having resizable elements on the screen. When there's only one element, it should take ...

Updating the color of the hamburger menu based on the scroll position using an event listener

Is there a way to change the color of my burger bar lines using event listeners? I'm trying to target the spans that make up the burger menu lines by assigning them a class "span". Here's what I have: var distFromTop = document.querySelector(".o ...

The navigation bar extends beyond the page's width

Currently, I am working on a website that was started by a former colleague. One issue I have noticed is that when the browser window's resolution reaches 768px or lower, the navigation transforms into a drop-down menu that appears wider than the page ...

button click event blocked due to unforeseen circumstances

Recently, I've been attempting to change the CSS properties of a div by triggering a click event. However, no matter what I do, it doesn't seem to be working and it's starting to frustrate me. Can anyone shed some light on why this might be ...

Choose the option for overseeing safaris

Hello there! I need some help with Safari. Can you please guide me on how to disable the arrows? https://i.stack.imgur.com/1gzat.png ...

The output from the xpath query is: [object Text]. Ideally, it should be a valid element

I am currently experiencing an issue with the text "Access my account" at the end of the page that has been causing errors <p class="breadcrumb"> <a href="http://www.examplewebsite/comm/index-eng.html" title="Titleblock">Home</a>& ...

Transitions fail to appear correctly when the page first loads

I need to fill a gauge meter based on a variable sent to the html file. To achieve this, I have initially set the value to zero: transform:rotate(.0turn); transition: all 1.3s ease-in-out; However, when the HTML is first loaded or refreshed (with F5 ...

What steps can I take to address this particular issue with Javascript/CSS?

I need to figure out how to get the smaller div box to display properly within the larger div. Every time I try to drag and drop it, the result is not what I expect: What could be causing the border of the small box to not show correctly in the larger bo ...

Tips for incorporating a CSS class or id into Html.PagedListPager

Currently utilizing a PagedListPager: @Html.PagedListPager((IPagedList)Model, page => Url.Action("InboxWithNoReplyListPaging", new { page = page }), PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions() { HttpMethod = "G ...