Can CSS have inheritance like OOP?
For example I have this style
.myButton {
background-color:#ffec64;
border:1px solid #ffaa22;
}
Can I define parent for color attributes? Something like
myYellow: #ffec64
So that in every styles I will just use
.myButton {
background-color:myYellow;
border:1px solid #ffaa22;
}
So that changing yellow color will only be on myYellow attribute not for every background-color attributes.
Thanks in advance
This is not possible when using CSS alone.
You can do this by using a css preprocessor like LESS or SASS. These allow for variables, mixins, functions and many other techniques that allow you to make CSS that is more maintainable, themable and extendable.
Once you have written your LESS or SASS you then need to compile it to standard css (in the case of LESS this can be done client-side).
You may need to use CSS Pre Processors like LESS or SASS.
Example Using LESS variables
#myYellow: #ffec64;
.myButton {
background-color: #myYellow;
border: 1px solid #ffaa22;
}
or even you can use LESS mixin to inherit css class.
.myCommonButton {
background-color: #myYellow;
border: 1px solid #ffaa22;
}
.myButton {
.myCommonButton;
color: black;
}
Related
With the LESS preprocessor, you can nest CSS code inside other CSS code, like this:
.Element {
.AnotherElement {
background-color: #FFF;
}
.YetAnotherElement {
background-color: #000;
}
}
This would make the background of .Element .AnotherElement white, and it makes .Element .YetAnotherElement have a background color of black. It does it all without writing it out like:
.Element .AnotherElement {
background-color: #FFF;
}
.Element .YetAnotherElement {
background-color: #000;
}
Does the first example coincide with CSS syntax, or do I have to use the LESS preprocessor?
Nesting is a feature of LESS and SASS, not native to CSS.
This is one of the most common uses for CSS preprocessors, but they offer a lot more too.
No, css doesn't support this syntax, in your css example the "Element" and "AnotherElement" will to receive this properties, AnotherElement will not inherit properties of Element.
Have never done this before, so my understanding may be totally off. I understand that LESS optimizes and minifies your CSS. So... is it better to just use LESS or should you still minify your files some how?
Feel free to raise other differences that I don't know about.
Less is a preprocessor, it compiles into static CSS. Less enables you to use variables and mixins which help you to write DRY and reusable code.
Example:
CSS:
p {
color:red;
}
h1 {
color: red;
}
Less:
#color: red;
p {
color: #color;
}
h1 {
color: #color;
}
If you want to use yellow instead of red you only have to change #color: red; now.
The same for mixins, Less:
.default-style() {
color: #color;
border: 1px solid black;
}
p {
.default-style();
}
h1 {
.default-style();
}
If you do not need a border for your default style any more, you will only have to remove the border property from the .default-style() mixin.
Less does not minify or optimize your (comiled) CSS code.
Since Less 2 you can use plugins that postprocess your compiled CSS code. This plugins can minify / compress or optimize your code.
For instance use clean-css (https://github.com/less/less-plugin-clean-css) for compressing and CSScomb (https://github.com/bassjobsen/less-plugin-csscomb) for optimizing.
I've tried to find the answer, and can't seem to do so, which is leading me to believe that it isn't possible. With my minimal knowledge of how CSS works, I also don't think it would be possible, but I just want to ask before I start working around a problem that may or may not exist.
Basically what I'm trying to do is use a previously defined attribute in a new class in my CSS stylesheet. For instance, say I had a couple of classes that just held background or font colors, like this:
.black { background-color: #000000; color: #000000; }
.white { background-color: #FFFFFF; color: #FFFFFF; }
Now if I was defining a new class (or using any selector for that matter), would it be possible to use the value of an attribute from an already existing class? Here is what my idea would look like:
.newClass {
width: 100%;
height: 100%;
background-color: .black; /* this would just get the background-color attribute from the .black class definition */
}
background-color: .black; is basically just a placeholder for "get the background-color attribute from the .black class definition". Is that possible using purely CSS? I'm aware of a ton of alternatives with PHP/JS, but I'd like to know if CSS can tackle this by itself. Thanks guys.
SASS is a thing to go. Your code will be like
#mixin black-theme {
.black { background-color: #000000; color: #000000; }
}
.newClass {
width: 100%;
height: 100%;
#include black-theme;
}
SASS
PHP compiler for SASS PHPSASS
There are javascript based solutions too like LESS but I generally don't recommend them as if Javascript load slow then presentation becomes jerky.
No, this is not currently possible in CSS. CSS does not have variables or the ability to reference values from previous rules. You would have to look for a CSS preprocessing language that gets processed into plain CSS before going onto the web site.
If you're willing to go the preprocessed way, you can look at SASS or LESS.
Yea possible using SASS or LESS css
#bgcolor : black;
.newClass {
width: 100%;
height: 100%;
background-color:#bgcolor;
}
Is it possible to define the default text color codes in a stylesheet
For example: I use this color all over my site, I hate typing it. color:#27408B;
Instead what I want to do is define the default text blue to be color:#27408B;
so I can just type border:1px solid blue; and it will come out border:1px solid #27408B;
You can use preprocessors like:
LESS: http://lesscss.org/
or
Sass: http://sass-lang.com/
For example in LESS you can do this:
#color: #4D926F;
#header {
color: #color;
}
h2 {
color: #color;
}
What you're looking to do can be accomplished by using a CSS Pre-processor. LESS, Sass, and Stylus are some of the more notable ones out there. Pre-processors allow you to create variables, mixins, functions, and more to help streamline your work.
To create a variable, all you'll need to do is declare it in your stylesheet accordingly:
#blue = "#27408b"
In the example above, #blue is the name of the new variable, whereas the value between the quotations is the true value for #blue henceforth.
body {background: #blue;} /* your background will be the color #27408b */
a {color: #blue;} /* your link will be the color #27408b */
div {border: 1px solid #blue;} /* your 1px border will be the color #27408b */
I have defined some background colors that I'll be using on my site. So I can easily set the background color of different elements like:
.background_highlite{
background-color: rgb(231, 222, 207); /*Cream in my Coffee*/
}
.background_shadow{
background-color: rgb(201, 179, 156); /*Moose Mousse*/
}
Now, if I want all textarea elements on my page to have Moose Mousse color as their background I want to write another CSS rule that references back to .background_shadow, so I only have to change the rgb values in one place.
Something like:
textarea{
height:50px;
background-color: background_highlite /* want to feed forward to keep the rgb in one place */
}
Is this possible with CSS?
People have been frustrated by CSS's simplistic structure, and have created pre-processors to write CSS more conveniently. Look at Less, for example, or CleverCSS.
You can assign all the elements the same class, and then set the background color in the class's CSS:
<textarea class="background_shadow">blah</textarea>
Keep in mind that you can assign a number of classes to any element, so you can use one class just to control the background color, and then use other classes for your other needs:
<textarea class="background_shadow another_class something_else">...</textarea>
Not really. http://dorward.me.uk/www/css/inheritance/ lists your main options.
Sorry, no. CSS does not support variables, or chaining.
however, there is a javascript library that allows that. http://lesscss.org/
The best you can do would be
.hilight textbox {
background: black;
}
textbox {
color: pink;
}
.background_shadow {
background: grey;
}
Or, of course, you could add the .hilite class to your div.
You have two options to work with:
Native CSS, which is possible, but not good to maintain.
Preprocessor, like xCSS, which can create more cleaner code and provide variables.
For simple projects I assume, native CSS will be good. But in more complicated it`s best to use some sort of processors, like pals talked earlier.
In this method you can always use some human readable rule like:
.blabla {min-height: 20px}, which pre-processor by your own logic transform to CSS, that all of our target browsers can understand, like .blabla {min-height: 20px; height: auto !important; height: 20px;} etc.
Also what I realy like in preprocessors is that you can right code, as here:
.specialClass extends .basicClass {} // see more at extends
.selector {
a {
display: block;
}
strong {
color: blue;
}
} // see more at children
or what you needed is vars {
$path = ../img/tmpl1/png;
$color1 = #FF00FF;
$border = border-top: 1px solid $color1;
} // see more at vars