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
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.
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.
You can use the border-image property to create a gradient border with 4 colors.
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.
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
topadding-box
so that background covers only the content area excludingborder
.
* {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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With