2

For example, CSS properites with multiple inputs:

Is something like this possible?

.button:active {
  background-color: rgba(inherit, inherit, inherit, 0.5);
}

Is the only way to do this with a scripted-css like LESS?

penu
  • 968
  • 1
  • 9
  • 22
  • `.button:active { opacity: 0.5; }` doesn't do what you want? – Paul Abbott Feb 11 '17 at 00:06
  • That is utterly invalid. Not because you can't inherit parent properties, but more because there is no such property as `rgba` on `html` elements. Your current understanding of how `CSS` works seems distorted. – tao Feb 11 '17 at 00:07
  • No, opacity:0.5 will modify borders and image contained – penu Feb 11 '17 at 00:07
  • @AndreiGheorghiu oops, forgot to add background-color – penu Feb 11 '17 at 00:08
  • Not possible in CSS. However, it's totally possible in `less` or `sass`. See [this answer](http://stackoverflow.com/questions/42164133/css-animated-icon-resize/42164606#42164606) I provided a few hours ago, on a similar question. You're looking for [`transparentize()`](http://sass-lang.com/documentation/Sass/Script/Functions.html#transparentize-instance_method) function in `sass`. I don't know enough `less` to tell you how to do it there, but Google can help, I'm sure. :) If you only want the `CSS`, you can use `sass` in `jsfiddle` or `codepen` and get the rendered `CSS` from page source. – tao Feb 11 '17 at 00:11
  • ah ok, shame that vanilla css isn't more extensible. thanks for the help! – penu Feb 11 '17 at 00:14

2 Answers2

2

You can achieve an answer to the example given by tackling the problem slightly differently. By putting the background colour on a pseudo element with a z-index of -1 so that it sits behind the content (which is given a z-index of 1), and then changing just the opacity... you've achieved what you wanted above without any LESS.

See this fiddle: https://jsfiddle.net/3uopd059/

.button {
  padding:1em;
  position:relative;
  float:left;
  color:#000;
  z-index:1;
}
.button::after {
  content:"";
  position:absolute;
  top:0;
  left:0;
  width:100%;
  height:100%;
  background-color: grey;
  z-index:-1;
}
.button:active::after {
  opacity:0.5;
}

Of course, if you wanted to change each of the colours independently then you'd need to use another method.

WizPip
  • 378
  • 1
  • 3
  • 11
  • 1
    gmta. same idea struck me shortly after dismissing it with the *"not doable"* label. props! (and welcome to SO). – tao Feb 11 '17 at 01:02
  • Definitely! Had to do something similar recently so it was already in my head. Been a lurker for years, time to give back :) – WizPip Feb 11 '17 at 01:11
1

After giving it some thought, here's a workaround that would achieve what you want without anything else but what you gracefully called vanilla CSS. Passing opacity (along with background color) to a pseudo element, absolutely positioned:

@import url('https://fonts.googleapis.com/css?family=Shadows+Into+Light');
button {
  font-family: 'Shadows Into Light', cursive;
  position: relative;
  font-size: 2rem;
  color: white;
  margin: 1rem;
  min-width: 15rem;
  background-color: transparent;
  -webkit-appearance: none;
  -moz-appearance: none;
  border: none;
  cursor: pointer;
}
button>span {
  position: relative;
  z-index: 1;
}
button::before {
  content: '';
  display: block;
  position: absolute;
  top: 0; left:0;
  width: 100%;
  height: 100%;
  opacity: 1;
  transition: opacity .3s cubic-bezier(.4,0,.2,1);
  z-index: 0;
  background-color: #bada55;
  border-radius: .4rem;
}
button[orange]:before {
  background-color: #f50;
}
button:hover::before {
  opacity:.65;                              /* <-- the juice is here */
}

/* don't mind the below, i'm just playing */

body {
  margin: 0; padding: 0;
}
center-me-please {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: transparent url(http://lorempixel.com/g/1200/800/fashion) no-repeat 50% 50% /cover;
}
<center-me-please>
  
  <button><span>#bada55</span></button>
  <button orange><span>not so #bada55</span></button>
tao
  • 82,996
  • 16
  • 114
  • 150
  • very interesting!! thanks! Time to scrap my JS option :) – penu Feb 11 '17 at 01:38
  • @penu Lost quite a bit of time building it as I had some trouble understanding a `z-index` problem I didn't know existed till now and still can't figure out. I'm in the process of writing a question on it and I'll link it here when I'm done, if you're curious, though it's not directly related. Always happy to help, when I find a good question – tao Feb 11 '17 at 01:44
  • Yes, please do, I'm interested myself – penu Feb 11 '17 at 02:01
  • @penu [Here it is](http://stackoverflow.com/questions/42171688/why-is-text-node-rendered-below-its-parents-before-by-default). – tao Feb 11 '17 at 03:01