I want to apply custom styles to all elements of a modal. First I try to override all the styles that may come "inherited" for the "modal" class, for example, in case I use Bootstrap.
So I have the following rule:
.modal * {
all: unset;
}
Now I would give each element of the modal its own styles. Problem: I also load my own styles into other sheets, for example for buttons, so the above rule overrides them. I wish I could apply them without increasing specificity or adding !important to all these styles I'm going to load. For example I would use this HTML for a button (which is under the modal class):
...Sign inAnd the CSS for the buttons:
.button-group .btn {
border-color: grey;
padding: 1rem;
}
.button-group .btn-primary {
background: #2196F3;
border-color: blue;
}
I have tried, without success, to change the startup rule using :not() :
.modal:not(.button-group) * {
all: unset;
}
.modal *:not(.button-group) {
all: unset;
}
.modal * :not(.button-group) {
all: unset;
}
I don't know if it can be done or if there is another way.
edited
I am making a library to use in multiple cases. Initially to add to projects designed with Bootstrap without having to make many changes when "decoupling" Bootstrap , but they would coexist for a while. It may be used with other libraries and with/without Bootstrap , it's just visually based on Bootstrap .
This is the actual code , the HTML is in the "examples" folder.
As you have stated, the styles
.button-group .btn
above should be applied to the ones of.modal *
since the first ones have more specificity. If the border color is not being applied to you, it is because by makingall: unset;
all the elements have a parent,.modal
you are removing theborder-with
and the button from the buttonborder-style
, which are necessary for the border to be displayed.Not having a minimum and verifiable example, I have made a quick example:
Comment
Making use of
all: unset;
seems quite drastic to me (since this rule also loads the styles that browsers give by default to certain elements and that are almost a standard: how to treat them<p>
as a block element). I would rethink the problem you have to try to find another solution.I don't see why it is necessary to do it that way, that is, the style sheets are read in cascade , unless the specificity dictates otherwise.
If I have a modal with these styles that bootstrap loads
But then I override that behavior and with a second sheet that I call after the framework call I write my own:
These should be taken by the browser and be the ones applied to your modal
On the other hand, I don't see much point in using this rule either:
.modal *
trying to select all the elements, because if the main container of your modal is the one with the class,.modal
then invoking that class should be enough to modify the styles of both that container as well as subsequent elements.So I consider that if you have: