I have 3 CSS classes defined:
table-header-shading
table-content-shading
table-lines
Normally I would use them like so:
<table class="table-header-shading table-content-shading table-lines">
However is it possible to defined a composite Class like "ReportTable" which is comprised of these 3 classes such that I can write the following:
<table class="ReportTable">
I suspect this is a basic CSS questions, but alluding me at present.
Many thanks in advance.
EDIT
Felix suggested something along the lines of
.ReportTable table-header-shading {}
.ReportTable table-content-shading {}
.ReportTable table-lines {}
To call
<table class="ReportTable">
However I am unsure whether my understanding is correct ??
EDIT2
I am sorted on this now. Thanks to all
It is not possible with "clean" CSS. If you will use LESS, you can do:
.ReportTable {
.table-header-shading;
.table-content-shading;
.table-lines;
}
Related
I got introduced to BEM by a recommendation to another question I asked here on SO, and have found in so valuable. After a year of using it, I often find myself having deep nested levels of composition. e.g.
.abc__main
.abc__main__card
.abc__main__card__item
.abc__main__card__item__heading
.abc__main__card__item__icon
.abc__main__card__item__text
.abc__main__card__item__text__em
.abc__main__card__item__text__em--green
Im pretty sure the point of BEM is to break away from composition, but I seem to lock myself into it. I have read the BEM website, but feel I must be missing something.
Is there some sort of guideline to help know how one should compose an item?
E.g. should I break it apart like this (With the help of #Chris Pickford suggestion):
.abc__main
.abc__card
.abc__item
.abc__item__heading
.abc__item__icon
.abc__item__text
.abc__item__text__em
.abc__item__text__em--green
EDIT: Thanks to Chris's answer I just had a realisation thats its not called BEEM, or BEEEM, but simply BEM, there should only ever be one nested element inside the block.
You should completely flatten your block structure to use BEM convention.
It might look something like this:
.abc {}
.abc__main {}
.card {}
.card__item {}
.card__heading {}
.card__icon {}
.card__text {}
.card__text--em {}
.card__text--green {}
It's in the FAQ here: http://getbem.com/faq/#css-nested-elements
I needed to create a function for some big color work on my Bootstrap variables. Unfortunately LESS doesn't allow you to create functions that can be called like theirs (ex. #myvar: darken(#color, 20%);).
The option provided on the doc site is to use a mixin that returns a variable. This worked well for me when I used it where the variable was declared as the property value, but I need to run my new mixin on many variables in the Bootstrap variable.less file. If I call the mixin multiple times there, it always returns the first color.
Part that works:
.mixin(#color) {
#var: #color;
}
.caller-1 {
.mixin(blue);
color:#var;
}
.caller-2 {
.mixin(red);
color:#var;
}
CSS
.caller-1 {
color:blue;
}
.caller-2 {
color:red;
}
What does not work:
.mixin(blue);
#color-1: #var; // My value is now blue
.mixin(red);
#color-2: #var; // My value is also blue
I thought I could get around this by building a unique variable in the mixin, but I can't find anyway to build one.
.mixin(#color; #num)
#var+#{num}: #color;
}
.mixin(blue; 1);
#color-1: #var1;
.mixin(red; 2);
#color-2: #var2;
Any idea on how to create a variable name in a mixin or other ideas on how to make one work like the LESS functions?
You can't define variables dynamically in LESS right now, but you can dynamically define selectors (as you probably knew). I will just give an example of that and leave it to you to apply it to the color/variables issue.
.towerMaker (#index) when (#index > 0) {
.block-#{index} {
z-index: #{index};
}
.towerMaker(#index - 1);
}
.towerMaker (7);
Variables are actually constants, and their scope is based only on context (where they appear in the block doesn't matter). It's only different when you call it within a selector block because of context. When you call the mixin at top-level, you define #var once for that level and it won't be overriden.
If you have to use variables, I suggest you try to find a solution taking advantage of the context. For example, you might be able to try something with mixin guards & when(condition) {...} (it's actually even simpler, as #seven-phases-max commented below). This is a way to run a mixin outside the context of a selector but still inside a context (updated example):
& {
.mixin(red);
.test1 { color: #var; }
}
& {
.mixin(blue);
.test1 { color: #var; }
}
You actually can define functions that will be called using Less by your Less runtime, but they can't be defined using Less. This is possible if you run your processor using Node.js, for example. But it's quite a hack and not trivial since you have to write them in JavaScript and wrap values in undocumented less.js types.
You can also call core JavaScript enclosing it within backticks (this is also undocumented). It's good for small blocks of code and for core functions:
length: unit(`Math.log(#{value})`, px);
If you run your Less processor from a Node.js app you can call your own functions that way.
I am trying to write a nested selector that selects a certain tag that has a certain attribute, for example
<li foo="bar">
To select this, li[foo="bar"] would work, but I want to nest it under [foo="bar"] using the scss & notation because I have other things with the [foo="bar"] attribute (e.g., <div foo="bar" class="baz">), and I want to group them together. When I try:
[foo = "bar"]{
&li{
...
}
&.baz{
...
}
}
It returns an error that says li may only be used at the beginning of a compound selector, and if I try:
[foo = "bar"]{
li&{
...
}
&.baz{
...
}
}
then it says & may only be used at the beginning of a compound selector. How can I do this correctly?
The right syntax nowadays would be li#{&}.
Last I heard, this is actually an upcoming feature in SASS 3.3 or 3.4.
See this thread for a question similar to yours and this thread for the proposed solution to be included (which, at the time of writing, seems to be &__element).
The issue here isn't the use of [] and & together - it's the use of a plain element in the selector. Your example with .baz should work as expected.
Like this
#ShuttleGrey: #606369;
I need same output color value from more variables [I am looking for a single line solution]
#themeOne:, #themeTwo:, #themeThree: #ShuttleGrey;
I know my code is an error , Does anyone know to fix this situation ?
Thanks
Assuming that you want the same properties to apply in many places you can do something like this
.aProperty{
color:#606369
}
Then you can do something like this to add the same properties to other elements
.anotherProperty{
background:#000; //just like that
.aProperty;
}
This way .anotherProperty will inherit the properties of aProperty.
This way you can even add other properties and use them at many places.
.aProperty{
color:#606369
}
Then you can do something like this to add the same properties to other elements
.anotherProperty{
background:#000; //just like that
.aProperty;
}
Since no answer is accepted I am trying my luck:)
I have been searching in Google etc., but I couldnt find what I was looking for (I hope I didnt overlook something).. So I thought my best bet is to ask you guys :)
I am playing around with LESS-JS for the first time and I really like it. However I have a little problem now.
I am using the #arguments variable like this:
.basicBorder(#width:1px, #type:solid, #color:#black){
border:#arguments;
}
Which works as expected. Now when I want the border to be red, I am adding this to the element in my css:
.basicBorder(1px, solid, #red);
Which also works as expected. However I would like to avoid writing 1px, solid,, since these are my default values already, but when I try this:
.basicBorder(#red);
Or this:
.basicBorder(,,#red);
It doesnt work.
So I was wondering if any1 knows how I could "skip" the first 2 variables so that I can just input the color in case I dont want the border-width and type to be changed.
I hope you get what I am trying to say!
Regards!
You actually can name later parameters and skip the first ones. The syntax for your question is:
.basicBorder(#color:#red);
You can also use normal ordered arguments at the beginning and pluck out named arguments from the rest of the parameters:
.basicBorder(2px, #color:#red);
This sets #width to 2px, #type to the default, and #color to #red. Really nice if you have more seldom used arguments.
The parametric mixins in LESS works sorta like javascript functions, you can't skip the first parameters. So if you want to only change the color, you could rewrite the mixin like this:
.basicBorder(#color:#black, #width:1px, #type:solid){
border:#width #type #color;
}
Then you'd be able to call it like this:
.basicBorder(#red);
.basicBorder(#red, 2px, dotted);
edit
Using your original mixin, you could also create these
.basicBorderType(#type) {
.basicBorder(1px, #type, #black);
}
.basicBorderColor(#color) {
.basicBorder(1px, solid, #color);
}
Now you could overwrite any of the styles:
.basicBorderType(dotted); //1px dotted black;
.basicBorderColor(#red); //1px solid red;
.basicBorder(2px); //2px solid black;
A bit of a hack, but it's the only thing I can think of to help you out...