Imagine I have this:
.class1 {
color: white;
}
.class2 {
background-color: black;
}
I can use both classes and use both properties the color and the background-color like this:
<p class="class1 class2">text</p>
But now imagine this:
.bottom {
box-shadow: 0 1em red;
}
.top {
box-shadow: 0 -1em red;
}
I want to use both box-shadows without a need to a new class like .bottom-top or .top-bottom. If I use a new class, other people wouldn't know what order the words needed to be unless I created the two classes that do the same thing. Imagine I want a .left and .right classes too, I would need to have at least (2^n)-1 classes (n being the initial number of classes).
I first thought I could use the box-shadow: inherit, <shadow>; so it uses the box-shadow it already has plus the new one but it does not look like it works.
Is it even possible to do this in CSS?
Thanks in advance!
Yep, pretty straightforward. You can't have 2 different box shadow rules as the last one will overrule the first one because of the cascade. You can use .class1.class2 and then override both with an additional box shadow rule see below
/* this is just to make stuff visible */
body {
background-color:skyblue;
}
div {
margin-bottom:3rem;
color:red;
}
/*end*/
/*This is the example*/
.class1 {
color: white;
box-shadow: 0 1em 5px red;
}
.class2 {
background-color: black;
box-shadow: 0 -1em 5px green;
}
/*apply both box shadows when both classes are present */
.class1.class2 {
box-shadow: 0 -1em 5px green, 0 1em 5px red;
}
<div class="class1">Class1</div>
<div class="class2">Class2</div>
<div class="class1 class2">Class12</div>
Related
I have a project made with react and I want to optimize the css.
I have this code:
.class-1 {
margin-top: 15px;
}
.class-2 {
margin-bottom: 15px;
}
Is there a possible way to optimize like this during the build?
.class-3 {
margin: 15px 0 15px 0;
}
You can add both classes to your HTML element or combine the margins into one class like this:
.class1 {
margin-top: 15px;
background-color: skyblue;
}
.class2 {
margin-bottom: 15px;
}
.class3 {
margin: 15px 0;
background-color: red;
}
<div class='class1 class2'>My element with 2 classes</div>
<div class='class3'>My element with 1 class</div>
It all depends if you want to keep the classes separate for other areas of your code, or if you will never need those margins in separate classes.
I was curious about currentColor and how it behaves when it is inherited and/or used in other properties.
Another aspect is omitting a color value in the border-property for example which should default to the text-color.
.outer {
color: #f90;
border: 5px solid;
box-shadow: 0 0 15px;
text-shadow: 2px 2px 3px;
}
<div class="outer">
Outer Div
</div>
Nothing fancy in the above Snippet.
The shadows and the border is the same Color as the Text.
Now lets inherit the color:
.outer {
color: #f90;
border: 5px solid;
box-shadow: 0 0 15px;
text-shadow: 2px 2px 3px;
}
.inner {
color: lime;
display: inline-block;
border: inherit;
box-shadow: inherit;
}
<div class="outer">
Outer Div
<div class="inner">
Inner Div no CurrentColor
</div>
</div>
Resutls:
In IE11 & Chrome 43 only the Text-Color is lime.
In Firefox 38 on the other hand the shadows are green too. (Note not the border)
When actively setting everything to currentColor the Browsers are showing the same result by displaying only the text in lime and everything else in orange. (As you can see in the final snippet at the bottom)
/**
* playing with currentColor
*/
body {background: darkgray;} /* friendly wink */
.outer {
width: 85%;
color: #f90;
border: 5px solid;
box-shadow: 0 0 15px;
text-shadow: 2px 2px 3px;
padding: 15px; margin: 15px;
}
.outer.currentColor {
border: 5px solid;
box-shadow: 0 0 15px currentColor;
text-shadow: 2px 2px 3px currentColor;
}
.inner {
color: lime;
display: inline-block;
border: inherit;
box-shadow: inherit;
}
.inner.resetting {
border-color: currentColor;
/* text-shadow-color: currentColor; /* does not exist */
/* box-shadow-color: currentColor; /* does not exist */
}
<div class="outer">
Outer Div
<div class="inner">
Inner Div no CurrentColor
</div>
</div>
<div class="outer currentColor">
Outer Div
<div class="inner">
Inner Div with CurrentColor
</div>
<div class="inner resetting">
Inner Div with CurrentColor
</div>
</div>
Questions:
Why is there a difference with the border in Firefox when omitting currentColor
Why does inherit not use the color value on the same element?
Is there a way to use the same properties and switching the color? (for border-color there is as you can see in the example by resetting it)
Here is also a dabblet link if you want to play around with it:
http://dabblet.com/gist/587ea745c7cda7a906ee
So, a few things here:
The CSS Working Group agreed to change the meaning of currentColor between CSS Color level 3 and CSS Color level 4. In level 3, it is resolved at computed value time and the computed value is inherited; in level 4, the keyword currentColor is inherited as a computed value and it is resolved at used value time.
There were a number of reasons to make this change, though I can't find the minutes, and I've forgotten all the details. (I could find minutes at https://lists.w3.org/Archives/Public/www-style/2014Feb/0052.html discussing the change after the fact.) It makes things worse for transitions/animations, but better in a number of other cases. It slightly increases implementation complexity, but improves performance (at least in Gecko).
I think most implementations have not yet had a chance to update to the new rules in Level 4. Gecko certainly has not, though it's on my list of things to do (but not at the top).
Firefox has, for a long time (since well before currentColor existed) implemented a special internal value as the initial value of border-*-color and outline-color. (We also do the same for text-decoration-color, but haven't done so since 1998/1999.) This works like the level 4 currentColor does, so once we switch our implementation we can unify the two things, but we couldn't switch our implementation with the level 3 currentColor, since it would have been a significant performance and memory hit given that it was the initial value of the property. (Really, unifying our implementation means doing the same work that we've done for those properties for every other property that takes a color value.)
text-shadow and box-shadow, when the color is omitted, have explicitly specified the behavior for when the color is omitted as being equivalent to the way level 4 defines currentColor, even before currentColor worked that way: see the definition of box-shadow (the definition of text-shadow just points to box-shadow).
Why is there a difference with the border in Firefox when omitting currentColor
CSS's specifications for inheriting on text-shadow say it should inherit the .inner currentColor if it itself is set to inherit, but box-shadow is unspecified and looks like browsers are inconsistent on the implementation. Possible bug.
Why does inherit not use the color value on the same element?
It appears to inherit the computed value and not the inputted one. Example:
.outer {
color:red;
box-shadow: 2px 2px 2px; /* color omitted */
}
.inner {
box-shadow: inherit;
/* translates to:
box-shadow: 2px 2px 2px red; */
}
Like I said, it's faulty implementation.
Is there a way to use the same properties and switching the color? (for border-color there is as you can see in the example by resetting it)
How about explicitly duplicating instead of inheriting? This would give you the best result without resulting to SASS/LESS, imo.
.outer {
color: #f90;
}
.outer, .inner {
border: 5px solid;
box-shadow: 0 0 15px;
text-shadow: 2px 2px 3px;
}
.inner {
color: lime;
display: inline-block;
}
<div class="outer">
Outer Div
<div class="inner">
Inner Div no CurrentColor
</div>
</div>
I like it to separate different concerns of a class in css.
Example:
// Layout
.myElement {
width: 100px;
height: 100px;
margin: 10px;
padding: 5px;
}
// Chrome
.myElement {
background: red;
border: 2px solid green;
box-shadow: inset 0 0 10px black;
}
// Content
.myElement {
color: white;
font-size: 120%;
}
// Interaction
.myElement:hover {
background: black;
border: 10px dotted red;
}
Pretty fine so far. Personally I find that approach readable, nice to maintain and nice for development.
But is there any mechanism/tool available which would merge all rules of a class into one single class declaration before deployment automatically?
Desired result:
.myElement {
width: 100px;
height: 100px;
margin: 10px;
padding: 5px;
background: red;
border: 2px solid green;
box-shadow: inset 0 0 10px black;
color: white;
font-size: 120%;
}
.myElement:hover {
background: black;
border: 10px dotted red;
}
EDIT:
After reading the comments I felt my "artificial" example wasn't comprehensive enough. Below is a real world example written in the Stylus syntax but the results are the same as described above. I left the original example from above in place.
The next example exists as a single file which describes the appereance of a certain area and its contents which appears in various places on a site. It's like a microcosm for that area. As a consequence you can imagine there are more of these microcosms.
// Layout
.events
padding 2em
.eventItem
margin 0 0 4em
.eventCal
float left
padding 2.5em 0 0
.eventArticle
margin 0 0 0 6em
.eventHead
margin 0 0 1em
time
margin 0 2em 0 0
// Chrome
.eventCal
background url("../../assets/img/icon-events.png") no-repeat
.eventHead
border-bottom .5em solid $chimney
// Content
.eventItem
list-style-type none
a
color $beige
text-decoration none
h2
font-family $elsie
.eventCal
color $beige
font-family $elsie
font-weight bold
font-size 150%
.eventHead
font-size 75%
.eventDesc
font-size 90%
// Interaction
.eventItem
a:hover
color $chimney
text-decoration underline
My computer is sorta nagging me to reboot after a few straight days of coding , but basically I do stuff like that fairly regularly in my libraries. Basically what you can do to quickly mimmick this functionality is to grab one of the zillion coding examples for enumerating the CSSrules from the sheets.
Then in the enumeration function simply take the selectorText property and drop it into document.querySelector/querySelectorAll which will give you the elements matching the selectorText toss these , the selectorText and related cssText into a little {}/[] or whatever suits your fancy.
After the enumeration finishes do a document.createElement('style') and use the insertRule() to glue the styles together as you please.
Then simply grab the finished product from style.textContent and you have your merged stylesheet.
Being as the commonly available code for enumeration does the actual hard part , this is a nice lazy evenings coding task for most folks and it will do the job EXACTLY the way YOU want.
What I want is like this code:
class1:hover {
background-color: #f9f9f9;
box-shadow: 1px 1px 8px #E0DADF;
class2 {
opacity:0.5;
}
}
Is this even possible or there is any other way to make it like this?
.class1:hover {
background-color: #f9f9f9;
box-shadow: 1px 1px 8px #E0DADF;
}
.class1:hover .class2 {
opacity: 0.5;
}
Or use a CSS preprocessor.
You'll need to use SASS or LESS.
In case of SASS, or SCSS, yeah this is possible. But if you want to nest the CSS Classes, you can do this way:
class1:hover{
background-color:#f9f9f9;
box-shadow: 1px 1px 8px #E0DADF;
}
class1:hover class2{
opacity:0.5;
}
Just to add something.
What you can also do, is make the element have multiple classes. For example,
<div class="class1 class2"></div>
<div class="class2"></div>
The first div element would also have the css styles of the second.
I am new to pseudo-elements that are prefixed with a double colon. I came across a blog article discussing styling of scrollbars using some webkit only css. Can the pseudo-element CSS be applied to individual elements?
/* This works by applying style to all scroll bars in window */
::-webkit-scrollbar {
width: 12px;
}
/* This does not apply the scrollbar to anything */
div ::-webkit-scrollbar {
width: 12px;
}
In this fiddle, I would like to make the div's scrollbar customized, but the main window's scrollbar stay at the default.
http://jsfiddle.net/mrtsherman/4xMUB/1/
Your idea was correct. However, the notation div ::-webkit-scrollbar with a space after div is actually the same as div *::-webkit-scrollbar; this selector means "scrollbar of any element inside <div>". Use div::-webkit-scrollbar.
See demo at http://jsfiddle.net/4xMUB/2/
I want to use a class selector for using a custom scrollbar.
Somehow .foo::-webkit doesn't work, but I figured out that div.foo::-webkit does work! Those ##$$*## pseudo-things....
See http://jsfiddle.net/QcqBM/16/
You can also apply these rules by id of the element. Let's say scroll bar of a div has to be styled which has an id "myDivId". Then you can do following. This way you can use different styles for scroll bars of different elements.
#myDivId::-webkit-scrollbar {
width: 12px;
}
#myDivId::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
border-radius: 10px;
}
#myDivId::-webkit-scrollbar-thumb {
border-radius: 10px;
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5);
}
http://jsfiddle.net/QcqBM/516/
You can have a scss file and apply the style to a class there
style.scss
.myscrollbar {
::-webkit-scrollbar {
width: 13px;
height: 13px;
}
::-webkit-scrollbar-thumb {
background: linear-gradient(13deg, #f9d4ff 14%, #c7ceff 64%);
border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {
background: linear-gradient(13deg, #c7ceff 14%, #f9d4ff 64%);
}
::-webkit-scrollbar-track {
background: #ffffff;
border-radius: 10px;
box-shadow: inset 7px 10px 12px #f0f0f0;
}
}
home.html
<div class="myscrollbar">
put your contents here
</div>
I used the scrollbar generator here: https://w3generator.com/scrollbar