Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: set border-color to parent's background-color

assume the following snippet.

.parent {
  width: 300px;
  height: 300px;
  background-color: grey;
  display: flex;
}

.child {
  width: 50%;
  height: 50%;
  background: white;
  margin: auto;
  border: 5px dotted grey;
}

.parent:hover {
  background-color: darkcyan;
}

.parent:hover>.child {
  border-color: darkcyan;
}
<div class="parent">
  <div class="child"></div>
</div>

Note that the child-div has a border which has the same color like the parent-div. When hovering parent-div, its background-color changes while the border-color (actually I'm using -webkit-text-stroke) of the child-div changes to the same color.

Instead of setting the child's border-color manually, I actually want the child to always use its parent background-color. This behaviour is easy to implement when both elements are using the same property by simply inheriting it. Is there any way to achive this behaviour using different properties?

Thanks

like image 382
user3479074 Avatar asked Jan 25 '17 11:01

user3479074


People also ask

How do you inherit a border color in CSS?

The syntax for the CSS border-color property (with 3 values) is: border-color: top right_left bottom; When three values are provided, the first value will apply to the top of the box. The second value will apply to the right and left sides of the box.

Is background color inherited in CSS?

Background properties do not inherit, but the parent element's background will shine through by default because of the initial 'transparent' value on 'background-color'. In terms of the box model, "background" refers to the background of the content and the padding area.

Can you style 4 different colors to a border in CSS?

You can use the border-image property to create a gradient border with 4 colors.

How do you make a two color border in CSS?

If you mean using two colours in the same border. Use e.g. border-right: 1px white solid; border-left: 1px black solid; border-top: 1px black solid; border-bottom: 1px white solid; there are special border-styles for this as well ( ridge , outset and inset ) but they tend to vary across browsers in my experience.


2 Answers

You can use border-color: transparent

.parent:hover > .child {
  border-color: transparent;
}

This will allow you to change background of parent only without overriding child's border-color each time.

Don't forget to set child's background-clip to padding-box so that background covers only the content area excluding border.

* {box-sizing: border-box;}
.parent {
  width: 300px;
  height: 300px;
  background-color: grey;
  display: flex;
}

.child {
  width: 50%;
  height: 50%;
  background: white;
  background-clip: padding-box;
  margin: auto;
  border: 5px solid grey;
}

.parent:hover {
  background-color: darkcyan;
}

.parent:hover>.child {
  border-color: transparent;
}
<div class="parent">
  <div class="child"></div>
</div>
like image 94
Mohammad Usman Avatar answered Sep 23 '22 11:09

Mohammad Usman


You can use CSS custom properties aka variables - they are inherited by children and can be used for any property.

https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties

Define them in parent, and then you can reuse them in parent and child class:

.parent {
  --parent-mainfill: grey;
  --parent-hoverfill: darkcyan;
 
  width: 300px;
  height: 300px;
  background-color: var(--parent-mainfill);
  display: flex;
}

.child {
  width: 50%;
  height: 50%;
  background: white;
  margin: auto;
  border: 5px dotted var(--parent-mainfill);
}

.parent:hover {
  background-color: var(--parent-hoverfill);
}

.parent:hover>.child {
  border-color: var(--parent-hoverfill);
}
<div class="parent">
  <div class="child"></div>
</div>
like image 35
tequilacat Avatar answered Sep 21 '22 11:09

tequilacat