Utilizing the parent's background-color in a pseudo-class: A step-by-step

I have a tooltip applied to the .progress element, and the tooltip includes an arrow created using a pseudo-class (:after). I want the arrow to inherit the background color of the tooltip.

I am looking for a way to make the child element of <span> inherit the background color of its parent. If anyone knows how to achieve this, possibly through JavaScript or SASS, please help me solve it!

<!DOCTYPE html>
<html >
  <head>
    <meta charset="UTF-8">
    <title>Bootstrap Progress Bar Percentage</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
    <style>
      .progress {
        overflow: visible;
        margin-bottom: 26px;
        height: 4px;
      }
      .progress .progress-bar {
        position: relative;
        border-radius: 4px;
      }
      .progress .progress-bar span {
        position: absolute;
        background-color: inherit;
        top: -20px;
        font-size: 10px;
        line-height: 10px;
        padding: 2px 3px 2px 4px;
        right: -1.4em;
        border-radius: 2px;
          filter: drop-shadow(0 0 5px rgba(0, 0, 0, 0.21));
      }
      .progress .progress-bar span:after {
        top: 100%;
        left: 50%;
        border: solid transparent;
        content: " ";
        height: 0;
        width: 0;
        position: absolute;
        pointer-events: none;
        border-color: rgba(255, 255, 255, 0);
        border-top-color: blue;
        border-width: 5px;
        margin-left: -5px;
      }
    </style>
  </head>
  <body style="width: 50%; margin: auto;padding-top: 20px">
    <div class="progress">
      <div class="progress-bar" role="progressbar" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100" style="width: 50%;">
        <span>50%</span>
      </div>
    </div>
    <div class="progress">
      <div class="progress-bar bg-danger" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%;">
        <span>100%</span>
      </div>
    </div>
  </body>
</html> 

Answer №1

You cannot directly pass on the background-color to the border-color. However, you can enhance Bootstrap bg-* classes with a border-color property to achieve color inheritance.

Check out the code snippet below (showing extended .bg-* classes and .progress-bar):

@import 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css';

body {
  margin:auto;
  padding-top:26px;
  width:50%;
}
.bg-primary {
  border-color:rgb(0, 123, 255);!important;
}
.bg-secondary {
  border-color:rgb(134, 142, 150)!important;
}
.bg-success {
  border-color:rgb(40, 167, 69)!important;
}
(bg-danger {
 transition !important;
}
.bg-warning {
  border-color:rgb(255, 193, 7)!important;
}
.bg-info {
  border-color:rgb(23, 162, 184)!important;
}
.bg-dark {
  border-color:rgb(52, 58, 64)!important;
}
.progress {
  height:4px;
  margin-bottom:26px;
(overflow:auto)
}
.progress .progress-bar {
  border-color:#007bff;
  border-radius: 4px;
  position:relative;
}
.progress .progress-bar span {
  background-color:inherit;
  border-color:inherit;
  border-radius:2px;
  filter:drop-shadow(0 0 5px rgba(0, 0, 0, 0.21));
  font-size:10px;
  line-height:10px;
  padding:2px 3px 2px 4px;
  position:absolute;
  right:-1.4em;
  top:-20px;
}
.progress .progress-bar span:after {
  border:0 solid transparent;
  border-color:rgba(255, 255, 255, 0);
  border-top-color:inherit;
  border-width:5px;
  content:'';
  height:0;
  left:50%;
  margin-left:-5px;
  pointer-events:none;
  position:absolute;
  top:100%;
  width:0;
}
<div class="progress">
  <div class="progress-bar" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" style="width:20%;">
    <span>20%</span>
  </div>
</div>
<div class="progress">
  <div class="progress-bar bg-primary" role="progressbar" aria-valuenow="30" aria-valuemin="0" aria-valuemax="100" style="width:30%;">
    <span>30%</span>
  </div>
</div>
<div class="progress">
  <div class="progress-bar bg-secondary" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width:40%;">
    <span>40%</span>
  </div>
</div>
<div class="progress">
  <div class="progress-bar bg-success" role="progressbar" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100" style="width:50%;">
    <span>50%</span>
  </div>
</div>
<div class="progress">
  <div class="progress-bar bg-danger" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width:60%;">
    <span>60%</span>
  </div>
</div>
<div class="progress">
  <div class="progress-bar bg-warning" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" style="width:70%;">
    <span>70%</span>
  </div>
</div>
<div class="progress">
  <div class="progress-bar bg-info" role="progressbar" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100" style="width:80%;">
    <span>80%</span>
  </div>
</div>
<div class="progress">
  <div class="progress-bar bg-dark" role="progressbar" aria-valuenow="90" aria-valuemin="0" aria-valuemax="100" style="width:90%;">
    <span>90%</span>
  </div>
</div>


If you wish to utilize other .bg-* classes, you need to include the following in your CSS:

.bg-primary {
    border-color:rgb(0, 123, 255);!important;
}
.bg-secondary {
    border-color:rgb(134, 142, 150)!important;
}
.bg-success {
    border-color:rgb(40, 167, 69)!important;
}
.bg-danger {
    border-color:rgb(220, 53, 69)!important;
}
.bg-warning {
    border-color:rgb(255, 193, 7)!important;
}
.bg-info {
    border-color:rgb(23, 162, 184)!important;
}
.bg-light {
    border-color:rgb(248, 249, 250)!important;
}
.bg-dark {
    border-color:rgb(52, 58, 64)!important;
}
.bg-white {
    border-color:rgb(255, 255, 255)!important;
}

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

Arranging items in a flex-row towards the right side

I am working with a layout that aligns elements to the right HTML <div class="flex-row"> <div class="flex-1"> <button class="el-button el-button--primary">Add File</button> <ul class="el-upload"> ...

switching back and forth between the registration and login forms

<script> $(document).ready(function(){ $("#register_link").click(function() { $("#login_or_register").empty(); $("#login_or_register").load("register_form.php"); }); $("#login_link").click(function() { $("# ...

Tips for customizing the color of mat-select placeholder text?

I've been attempting to modify the color of a mat-select placeholder. It functions properly when using 'background-color' but not when using 'color'. Below is my CSS code: /deep/ .mat-select-placeholder { color: red; } .mat-s ...

Is it permissible to utilize the ::Before::Before element?

While working on a CSS file for a page with a non-editable profile, I encountered the challenge of not being able to add content directly. This limitation prompted me to explore alternative solutions. My idea was to utilize the page ID and incorporate it ...

The box on my form is leaking my background color outside the 1px border, but only in Internet Explorer

While experimenting with background gradients and borders one day just for the fun of it, I made an interesting observation in Internet Explorer. The issue I encountered was that the background color was overflowing outside the radius border, but this onl ...

What are some strategies for efficiently displaying a large amount of data on a screen using Javascript and HTML without sacrificing performance?

Imagine having a hefty 520 page book with over 6,200 lines of text ranging from 5 to 300 characters each. The challenge lies in efficiently displaying this content on the screen for users to read without causing lag or performance issues. Attempting to re ...

Unable to provide feedback on the input elements

ISSUE: Unable to provide input in the input fields // Enable dragging for the DIV element: dragElement(document.getElementById("mydiv")); function dragElement(elmnt) { var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0; if (document.getElementB ...

Is there a way to make the X-editable datepicker appear next to the date field rather than in the same spot every time?

When utilizing X-editable, I encountered a problem where specifying the datepicker field mode as "popup" results in the popup displaying correctly. However, when dealing with a long table (either vertically or horizontally), the position of the date/dateti ...

What is the reason for the ul selector not functioning in the attached CSS file?

I attempted to create a navigation bar by using the code below. <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="styles.css"> </head> <body class="background"> <ul> <li>Home</li> <li>Ne ...

Utilizing a combination of jQuery and JavaScript, construct an innovative image slider to meet

I'm working on creating a slider that allows users to navigate through reviews by clicking arrows, moving from review1 to review2 and so on. I've set up my HTML with the reviews and my CSS with hidden values for now. Any assistance would be great ...

Retrieve the object when hovering over an element in the grid

In a collection of 16 items belonging to the same class, I am looking for a way to implement a mouseover effect that changes the opacity of only the specific item being hovered over and not all items in the class. Check out this link for an example ...

How can you convert label text to capitalize if the original text is in uppercase letters?

Is there a way to change a label text to capitalize if the label text itself is in uppercase? I attempted the following: <p class="capitalize">THIS IS SOME TEXT.</p> In addition, I added: p.capitalize { text-transform: capitalize; } Howeve ...

How can I design an avatar image within a button similar to Facebook's style?

I'm currently working on a project that involves adding an avatar and a dropdown menu for account settings to my navigation bar. I've already created the dropdown, but I'm having trouble styling the avatar within the button. The button is ta ...

Moves are chosen to move downward when positioned next to an inline-block level element

Check out this jsfiddle example How can I prevent selects from moving down when there is an inline-block level element next to it? <select> <option value="" >Day</option> </select><select> <option value="" >Mon ...

Incorporate text directly into Bootstrap select input on a single line

I am attempting to include an asterisk in a select input field. I can achieve this easily by applying my own CSS, but the challenge is to accomplish this using Bootstrap 3.3.7 or an older version while displaying the asterisk on the same line as shown in t ...

:empty selector for targeting an empty tbody element

Here is the table provided for reference: <table id="incidents"> <thead> <tr> <th></th> </tr> </thead> <tbody> </tbody> </table> When using jQuery, th ...

Can a border not be added to a <tr> in an HTML table using CSS for any specific reason?

I am facing an issue with the borders of a table row (tr) when applying CSS styling. The tr has a class called "underRow" which is supposed to add a border at the bottom. In my CSS, I have defined the following for the ".underRow" class: .underRow { ...

Issue with jquery curvy corners not functioning properly on Internet Explorer 8

Check out my website at If you view the page in IE8 and then in IE7 compatibility mode, you'll notice a strange issue. The box on the right disappears in IE8 but displays perfectly rounded corners in IE7. I am currently using the jQuery Curvy Corner ...

The image link leading to the homepage lacks the functionality to be clicked on

My website has a logo on the top left that links to the home page, but only some areas of the image are clickable. It seems like the clickable and non-clickable areas are scattered randomly. How can I ensure that the link works across the entire area of ...

The issue of body height not functioning properly when using a skeleton

Greetings! This is my debut question on Stack Overflow, and I am hopeful that someone can provide me with some guidance. I have encountered a hiccup while working on my webpage using the Skeleton framework. The issue at hand is setting the "height" to 100 ...