As an HTML element's pseudo classes ("pseudo states") cannot be triggered via JavaScript, I want to emulate pseudo-class CSS styling by creating identical CSS rules with regular classes, then use JavaScript to toggle these emulated pseudo classes:
document.getElementById('action-button').addEventListener('click', () => {
const underTest = document.getElementById('under-test');
underTest.classList.contains('hover') ?
underTest.classList.remove('hover') :
underTest.classList.add('hover');
});
button:hover {
background-color: pink;
}
button.hover {
background-color: pink;
}
<button id="action-button">
Regular button<br />
Hover for seeing :hover pseudo class styling<br />
Click for toggling emulated .hover class on "under test" button
</button>
<br />
<br />
<button id="under-test" class="hover">under test: Pseudo-class hover button</button>
This approach works for my purposes. Now I'm looking for a solution which generates such emulated CSS rules automatically from my SASS / .scss stylesheets:
For example, given this input input.scss:
span {
a:focus {
color: red;
}
}
I'd like this output output.css:
span a:focus {
color: red;
}
span a.focus {
color: red;
}
or
span a:focus,
span a.focus {
color: red;
}
Is there a SASS configuration or some Webpack plugin which automatically derives additional "emulated CSS rules" from all pseudo-class rules?
Related
I am trying to style element plus' el-radio-button in a el-radio-group to have different colours. It is not available by the el-radio-button properties, so I'm trying to target the rendered html elements. When I inspect my webpage, something like the following is shown:
<label class="el-radio-button" role="radio" aria-checked="false" aria-disabled="false" tabindex="-1" data-v-bf51d4b2="" style="color: red;">
<input class="el-radio-button__original-radio" type="radio" name="" tabindex="-1" value="Karthus">
<span class="el-radio-button__inner">Karthus</span>
</label>
The styles are applied on label.el-radio-button and span.el-radio-button__inner but I can't seem to target them using the following styles in my sfc:
<style lang="scss" scoped>
.el-radio-button {
color: red;
padding: 20em;
.el-radio-button__inner {
color: blue;
&:hover {
color: red;
}
}
}
</style>
None of these are applying. I can't really tell whether it's because of specificity, or just overrides, etc. !important on the color properties also don't seem to apply. Is there a way to properly target the rendered html elements in the sfc style?
Use more specific css selectors and it should work
<style>
.el-radio-group .el-radio-button {
color: red;
padding: 20em;
}
.el-radio-group .el-radio-button__inner {
color: blue;
}
.el-radio-group .el-radio-button__inner:hover {
color: red;
}
</style>
example
I am using a framework (vuetify) which by default inserts the following css rule:
[type="button"] {
color: inherit;
}
The problem is that this is always inserted at last and I cannot control that. So if I am using the html <button type="button" class="button">Test</button> with the style .button { color: red; }, the css rule is not used because it gets overriden by the other rule. This means that for all button classes I either have to use another selector like button.button or I have to use !important. Is there another way to globally disable the property color: inherit so that I can still use a class like .button without using a more restrictive selector?
You can get around it by not putting type="button" on your buttons, it has that (button) behaviour by "default".
.button {
color: deepskyblue;
}
.container {
color: deeppink;
}
[type="button"] {
color: inherit;
}
<div class="container">
<button class="button" type="button">What color will I have?</button>
<button class="button">What color will I have?</button>
</div>
.button a:link {
color: blue;
}
.button a:visited {
color: purple;
}
.button a:hover {
color: red;
}
<div class="btn-group">
<button>Cambridgeshire</button>
<button>Cornwall</button>
<button>Cumbria</button>
<button>Derbyshire</button>
<button>Devon</button>
</div>
The links do not change color when selected. I have also tried:
btn-group a:link {color:blue} etc. but again that wording doesn't work.
What wording should I use?
If by "selected" you mean that they have focus, you can add a selector for :focus.
Also note that your buttons are children of the links, not vice versa, as your CSS rules imply, so you have to turn that around; plus button is a tag, not a class, therefore it shouldn't have a preceding dot in a CSS selector.
a:link button {
color: blue;
}
a:visited button {
color: purple;
}
a:hover button {
color: red;
}
a:focus button {
color: green;
}
<div class="btn-group">
<button>Cambridgeshire</button>
<button>Cornwall</button>
<button>Cumbria</button>
<button>Derbyshire</button>
<button>Devon</button>
</div>
.button is targeting an element with a class of "button", to target an element itself you would just use button. It probably makes more sense to target .btn-group instead of applying styles to all button's. It's also good to be specific, by adding that this only applies to button elements within .btn-group that are direct children of a tags.
You can set all the buttons within an a tag (:link) to be blue, and then set all buttons within an a tag that have been visited to be purple, and then set all buttons to be red on hover.
.btn-group a:link > button {
color: blue;
}
.btn-group a:visited > button {
color: purple;
}
.btn-group a > button:hover {
color: red;
}
<div class="btn-group">
<button>Cambridgeshire</button>
<button>Cornwall</button>
<button>Cumbria</button>
<button>Derbyshire</button>
<button>Devon</button>
</div>
Your CSS is wrong. You are trying to style an element with the class button and you are targeting an a element inside of that element. You don't need the button elements at all.
You should target .btn-group a. That basically means an a element inside of your .btn-group div.
.btn-group a:link {
color: blue;
}
.btn-group a:visited {
color: purple;
}
.btn-group a:hover {
color: red;
}
<div class="btn-group">
Cambridgeshire
Cornwall
Cumbria
Derbyshire
Devon
</div>
Notes:
:link - Styles the link in its normal state.
:visited - Styles the link once it has been visited by a user
:hover - Styles the link as the user hovers.
:active - this is a temporary state as the user is actively engaging, e.g. holding down the mouse button on click.
.btn-group a:link > button {
color: blue;
}
.btn-group a:visited > button {
color: purple;
}
.btn-group a:hover > button{
color: red;
}
<div class="btn-group">
<button>Cambridgeshire</button>
<button>Cornwall</button>
<button>Cumbria</button>
<button>Derbyshire</button>
<button>Devon</button>
</div>
I know I don't need the buttons, but that's how I want the links to appear and not merely with the name.
It doesn't appear to make any difference whether I use
<div class="btn-group">
<button>Cambridgeshire</button>
<button>Cornwall</button>
<button>Cumbria</button>
or
<div class="btn-group">
<button>Cambridgeshire</button>
<button>Cornwall</button>
<button>Cumbria</button>
I have changed the CSS to:
.btnn-group a:link { color: blue;
}
.btn-group a:visited { color: purple;
}
.btn-goup a:hover { color: red;
}
.btn-group a:active { color: red;
}
I tried to change the background-color in b-modal -> in .modal-header using bootstrap-vue. But the vue doesn't see my styles and I don know why :/
here is the code. I follow by answer in link
HTML:
b-modal(id="card-1" title="CARTÃO DE CRÉDITO" :modal-class="myclass" header-text-variant="light")
VUE
export default {
data: {
myclass: ['myclass']
},
}
CSS
.myclass > .modal-dialog > .modal-content > .modal-header {
background-color: #da2228 !important;
color: white;
}
But I still doesn't see the results. Modal header is white. Any ideas why?
You're probably using a scoped style tag in your .vue file.
If you are, then you need to use a deep-selector to properly target the subcomponent.
The selectors are /deep/, >>> or ::v-deep.
In the below example i use /deep/, but the others should work for you as well.
You can also use the header-class prop on b-modal to directly add the class to the header if you wish.
<template>
<b-modal header-class="my-class">
<template #modal-header>Header</template>
Hello
</b-modal>
<b-modal modal-class="my-second-class">
<template #modal-header>Header</template>
Hello
</b-modal>
</template>
<style scoped>
/deep/ .my-class {
background: black;
color: white;
}
/deep/ .my-second-class > .modal-dialog > .modal-content > .modal-header {
background: black;
color: white;
}
</style>
You can use content-class="your-class" in the vue bootstrap modal.
<b-modal id="myModal" content-class="your-class" title="BootstrapVue">
Body Content
</b-modal>
Or else it is difficult to style the modal cuz the normal class="" does not apply to the code.
I am reading through this guide.
Go to section that says "Selector Sequences", it is near 3/4 of the web page, there is this codes:
#fake-links .link {
#extend a;
}
a {
color: blue;
&:hover {
text-decoration: underline;
}
}
In the guide it says that SCSS codes will transpile into this CSS codes:
a, #fake-links .link {
color: blue; }
a:hover, #fake-links .link:hover {
text-decoration: underline; }
My question: Why the #fake-links in third line does not become #fake-links:hover?
This is because it is extending .link when it's a child of #fake-links, not extending #fake-links itself.
In this scenario you would have something like:
<div id='#fake-links'>
<a href='#' class='link'>...</a>
</div>
Where as your a tag with the link class is what's styled.
If you want the a styles to apply to #fake-links you would omit .link from the selector in your SCSS:
#fake-links {
#extend a;
}
As an example:
https://www.sassmeister.com/gist/2df1a3ca4d889201fe3a86e324643c04