I have the following BEM setup:
.mytable {
font-size: 16px;
margin: 30px 0;
&--standard {
border: 1px solid red;
&__row {
border: 1px solid blue;
}
}
What I am trying to do is apply the row styles only to the modified table class.
This outputs the following
.mytable--standard__row {
border: 1px solid blue;
}
Which is obviously not what I am trying to achieve.
Is there a neat/standard way to solve this problem?
You can add another ampersand after the modifier to get the desired output:
.mytable {
font-size: 16px;
margin: 30px 0;
&--standard {
border: 1px solid red;
}
&--standard & { //<--
&__row{
border: 1px solid blue;
}
&__some-other-element{}
}
}
We use:
.mytable {
font-size: 16px;
margin: 30px 0;
&--standard {
border: 1px solid red;
}
&--standard &__row {
border: 1px solid blue;
}
}
Its not ideal (a && grandparent selector would be nice), but its the best we could come up with so far
Related
i have a error that when i touch my touch screen then the buttons will have border.
I added "border-radius" in qss file.But this don't help.
-video video https://www.youtube.com/watch?v=h3e5ArF6GIk
remove yellow
screen capture
how can i remove it?
Button QSS
QPushButton{
background-color:transparent;
border: 3px solid black;
border-radius: 30px;
font: bold italic;
font-size: 20pt;
}
QPushButton#btn_checkout{
text-align: right;
}
QPushButton:checked,
QPushButton:pressed {
border-color: 3px blue;
background-color: #d0d67c;
color: #0000FF;
}
QPushButton:hover {
border: 2px solid blue;
}
QPushButton:disabled{
color: gray;
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
i've been confused for finding the best way of naming multiple modifiers,
i know the basic rules of this methodology and i read the official BEM quick-start documentation ( bem.info/methodology/quick-start )
i have problem with naming modifiers when i have some modifiers in a block which one of these has some effects on another modifier ( in other word, a modifier in addition to modifying the main block, also is modifying another modifier of that block )
instead of explaining the problem i use a simple example for this situation, so please just tell me which one of this following examples is the correct and the best option:
Solution 1)
SCSS:
.btn {
font-size: 14px;
&--primary {
background: blue;
}
&--secondary {
background: black;
}
&--outline {
background: transparent;
}
&--outline#{&}--primary {
border: 1px solid blue;
}
&--outline#{&}--secondary {
border: 1px solid black;
}
}
Compiled CSS:
.btn {
font-size: 14px;
}
.btn--primary {
background: blue;
}
.btn--secondary {
background: black;
}
.btn--outline {
background: transparent;
}
.btn--outline.btn--primary {
border: 1px solid blue;
}
.btn--outline.btn--secondary {
border: 1px solid black;
}
Sample of usage in HTML:
<a href="#" class="btn btn--primary btn--outline>...</a>
Solution 2)
SCSS:
.btn {
font-size: 14px;
&--primary {
background: blue;
}
&--secondary {
background: black;
}
&--outline {
background: transparent;
}
&--outline#{&} {
&--primary {
border: 1px solid blue;
}
&--secondary {
border: 1px solid black;
}
}
}
Compiled CSS is same as solution 1
Sample of usage in HTML is also same as solution 1
Actually i just did a nesting in modifiers for prevent repeating the "outline",
Solution 3)
SCSS:
.btn {
font-size: 14px;
&--primary {
background: blue;
}
&--secondary {
background: black;
}
&--outline {
background: transparent;
&--primary {
border: 1px solid blue;
}
&--secondary {
border: 1px solid black;
}
}
}
Compiled CSS:
.btn {
font-size: 14px;
}
.btn--primary {
background: blue;
}
.btn--secondary {
background: black;
}
.btn--outline {
background: transparent;
}
.btn--outline--primary {
border: 1px solid blue;
}
.btn--outline--secondary {
border: 1px solid black;
}
Sample of usage in HTML:
...
Solution 4)
SCSS:
.btn {
font-size: 14px;
&--primary {
background: blue;
}
&--secondary {
background: black;
}
&--outline {
&--primary {
background: transparent;
border: 1px solid blue;
}
&--secondary {
background: transparent;
border: 1px solid black;
}
}
}
Compiled CSS:
.btn {
font-size: 14px;
}
.btn--primary {
background: blue;
}
.btn--secondary {
background: black;
}
.btn--outline--primary {
background: transparent;
border: 1px solid blue;
}
.btn--outline--secondary {
background: transparent;
border: 1px solid black;
}
Sample of usage in HTML:
...
As you see this solution is easier for usage in HTML, but we are duplicating same CSS property in each primary and secondary modifier ( in this case we are duplicating background: transparent; in other modifiers ), you may think it doesn't matter, but in real cases we may have lots of property which is duplicating in many places so definitely this solution will have important problems for the development of this component in the future
Solution 5)
SCSS:
.btn {
font-size: 14px;
&--primary {
background: blue;
&--outline {
background: transparent;
border: 1px solid blue;
}
}
&--secondary {
background: black;
&--outline {
background: transparent;
border: 1px solid black;
}
}
}
Compiled CSS:
.btn {
font-size: 14px;
}
.btn--primary {
background: blue;
}
.btn--primary--outline {
background: transparent;
border: 1px solid blue;
}
.btn--secondary {
background: black;
}
.btn--secondary--outline {
background: transparent;
border: 1px solid black;
}
Sample of usage in HTML:
...
We also have duplicated properties in this solution ( like the solution 4 )
So it doesn't have a good development structure and it will bother programmer in a large component
i just want to know which solution is the best choice, or if you have another solution for this problem please write base on this button example.
also i mention this point again:
I wrote that example in a very little scale of a real component, so any duplicating of properties or classes name may doesn't seem to care about in this example, but in the more complex cases in the real project it will be a serious problem for development of a component.
Chaining attribute selectors may seem a good fit for your special case.
let's say we have all the buttons containing a .btn at least.
This way you could both keep the logic for each modification case in a the dedicated css rule and also have the duplications to their minimum.
let's say you have a
<button class="btn--primary--outline"></button>
then you could probably write them css rules as follows:
[class^="btn"] {
font-size: 14px;
}
[class^="btn"][class*="--primary"] {
background: blue;
border-color: blue;
}
[class^="btn"][class*="--outline"] {
border: 1px solid;
}
In SASS's class selecter, I want to select parent's sibling.
.bw-textarea {
height: 150px;
padding: 10px;
overflow-y: auto;
background: white;
text-align: left;
width: 100%;
border: 1px solid #eeeeee;
font-size: 12px !important;
color: #666666;
// textarea:disabled & {
// background: #efefef;
// }
}
Compiled above sass code,
.bw-textarea textarea:disabled {
background: #efefef;
}
But I want to show result like this css code...
How to select like this in sass?
textarea.bw-textarea:disabled {
background: #efefef;
}
You gotta use #root in this case. It's pretty simple
This link will give a clear idea about this selector
.bw-textarea {
#at-root textarea#{&}:disabled{
background: cyan;
}
}
This will compile to
textarea.bw-textarea:disabled {
background: cyan;
}
See this PEN
This is what your looking for:
.bw-textarea {
height: 150px;
padding: 10px;
overflow-y: auto;
background: white;
text-align: left;
width: 100%;
border: 1px solid #eeeeee;
font-size: 12px !important;
color: #666666;
&:disabled {
background: #efefef;
}
}
Check out this SO answer for a lil more idea on the nested pseudo selectors
Sass parent selector and hover?
And of course check the sass docs
https://sass-lang.com/documentation/file.SASS_REFERENCE.html
This question already has an answer here:
Append the parent selector to the end with Sass
(1 answer)
Closed 6 years ago.
This is what I want:
From something like this (this code doesn't work, it throws an error):
.ia-loading {
select.& {
border: 2px solid blue;
}
input.& {
border: 2px solid orange;
}
}
Get this:
select.ia-loading { border: 2px solid blue; }
input.ia-loading { border: 2px solid orange; }
I have tried several combinations, none of which have worked:
This one:
.ia-loading {
select & {
border: 2px solid blue;
}
input & {
border: 2px solid orange;
}
}
Produces this css, which is not what I want:
select .ia-loading { border: 2px solid blue; }
input .ia-loading { border: 2px solid orange; }
This one:
.ia-loading {
select& {
border: 2px solid blue;
}
input& {
border: 2px solid orange;
}
}
throws this error:
"&" may only be used at the beginning of a compound selector.
Many thanks in advance.
You could make small mixin for this like so:
#mixin test($elem) {
#{$elem}.test {
#content;
}
}
And if you really want to use it inside your class you can use #at-root directive since Sass 3.3 (else you just put it outside your main class)
.test {
margin: 20px;
#at-root {
#include test(select) {
border: 1px solid red;
}
#include test(input) {
border: 1px solid blue;
}
}
}
DEMO
What is the reason you are using '&' behind input and select?
Its only used when you want to have a specific "action" like hover, active etc.
Example
.ia-loading
&:hover
color: #FFF
Also in Sass you can't use brackets, that is used in scss
I also don't think you can do it the way you want it. It should be done like this:
.ia-loading
border: 2px solid
select
.ia-loading
border-color: blue
input
.ia-loading
border-color: orange
Is it be possible to use some sort of #directive creation syntax, similar to creating #mixins? Secondly, is it possible to create a SASS-only pseudo class?
I'd like to declare my own SASS directive,although I'd prefer not to have to force my teammates to install an extra ruby gem to use it so I'd want to store it in a scss partial. I do understand that they are orders of levels in complexity, so perhaps it just isn't possible.
In addition to perhaps creating a new scss-only pseudo class (such as :parent, :clicked, :unchecked, etc) I'd be interested in a custom-made directive that assists with using checkboxes to direct css animations ("css checkbox hack"). Here is my scss pseudocode to generalize what I'm trying to do:
// this code monitors when a checkbox (#someinput) is checked,
// then applies style to div #target div. Basically an 'onClick' event in scss.
body {
#wrapper {
#targetdiv {
#spotcheck(#someinput) { #
color: red; border: 2px solid blue; # <-- move this ...
} #
color: blue; border: 0;
#someinput {
width: 20px; height: 20px;
}
}
}
}
// ... the above scss should be converted to this pre-compiled state, also scss
body {
#someinput:checked ~ #targetdiv { #
color: red; border: 2px solid blue; # <-- ..to here. it needs to be
} # above the #targetdiv
#wrapper {
#targetdiv {
color: blue; border: 0;
#someinput {
width: 20px; height: 20px;
}
}
}
}
Make your selectors only as specific as they absolutely need to be and no more. A mixin would only be more verbose with no real benefit.
#targetdiv {
color: blue; border: 0;
#someinput:checked ~ & {
color: red; border: 2px solid blue;
}
}
#someinput {
width: 20px; height: 20px;
}
Output:
#targetdiv {
color: blue;
border: 0;
}
#someinput:checked ~ #targetdiv {
color: red;
border: 2px solid blue;
}
#someinput {
width: 20px;
height: 20px;
}
Alternately, this would give the same result with the ability to overqualify as much as you want:
#targetdiv {
color: blue; border: 0;
}
#someinput {
width: 20px; height: 20px;
~ #targetdiv {
color: red; border: 2px solid blue;
}
}