【问题标题】:Can I add additional background attribute to subclass?我可以为子类添加额外的背景属性吗?
【发布时间】:2018-03-12 07:52:28
【问题描述】:

我有一个 CSS 文件,其代码如下所示...

.button{
    width: 700px;
    height: 100px;
    text-align: center;
    display: inline-block;
    font-size: 50px;
    font-family: Gauge;
    color: white;
    border: rgba(255,255,255,0.3) 11px solid;
    box-shadow: 0 5px 10px white;
    text-shadow: 0 0 10px #000;
    margin: 15px 15px 15px 15px;
    transition: 0.4s
}

button.oneshot{
    background: linear-gradient(rgba(0,0,0,1),rgba(0,0,0,0.4),rgba(0,0,0,1)) no-repeat center center fixed,
                url("oneshot.png") center 60%;

button.lisatp{
    background: linear-gradient(rgba(0,0,0,1),rgba(0,0,0,0.4),rgba(0,0,0,1)) no-repeat center center fixed,
                url("lisa the painful.jpg") 45% 60%;
}
...
...

如您所见,子类 oneshotlisatp 中有一行重复:

linear-gradient(rgba(0,0,0,1),rgba(0,0,0,0.4),rgba(0,0,0,1)) no-repeat center center fixed

但是,由于每个子类也有一个图像作为背景,所以我找不到将重复的行放在.button 中的方法。

是否有可能以某种方式进一步简化这一点,或者这是否尽可能简单?

【问题讨论】:

标签: html css background linear-gradients


【解决方案1】:

使用 CSS 变量来简化这一点,然后您将能够轻松地单独更改每个元素的所有元素的渐变:

.button{
    width: 700px;
    height: 100px;
    text-align: center;
    display: inline-block;
    font-size: 50px;
    font-family: Gauge;
    color: white;
    border: rgba(255,255,255,0.3) 11px solid;
    box-shadow: 0 5px 10px white;
    text-shadow: 0 0 10px #000;
    margin: 15px 15px 15px 15px;
    transition: 0.4s;
    --grad:linear-gradient(rgba(0,0,0,1),rgba(0,0,0,0.4),rgba(0,0,0,1)) no-repeat center center fixed;
    background:var(--grad);
}

.button.oneshot{
    background:var(--grad),url("https://lorempixel.com/400/200/") center 60%;
}

.button.lisatp{
    background: var(--grad),url("https://lorempixel.com/300/200/") 45% 60%;
}

.button.new-grad{
    --grad:linear-gradient(rgba(0,190,0,1),rgba(0,190,0,0.4),rgba(0,180,0,1)) no-repeat center center fixed;
    background: var(--grad),url("https://lorempixel.com/300/200/") 45% 60%;
}
<span class="button"></span>
<span class="button oneshot"></span>
<span class="button lisatp"></span>
<span class="button new-grad"></span>

【讨论】:

  • CSS 变量将是这里的最佳用途。但是,并非所有浏览器都支持它们,因此请在使用前检查您的目标。
  • @GhostPengy only IE 无法支持caniuse.com/#search=variables .. 所以对我来说,每个浏览器都支持它,因为我不介意 IE ;)
  • CSS 变量仅从 IE15 开始支持,其他几个浏览器也不支持。快速估计,您将无法支持大约 6% 的用户群。我只是放弃。 w3schools.com/browsers/browsers_explorer.asp
  • @GhostPengy 是的,我知道这一点,因为它是 IE,我不介意 :) .. 所以我们应该强迫 6% 的人使用新的更强大的 borwser 并离开老死的不再- 支持的浏览器,如 IE ;) 你可能不同意,但我认为是这样的 :)
  • 是和不是。它归结为硬件。例如 Opera mini 和三星,浏览器是硬件锁定的。这意味着如果没有新设备,您将无法真正升级它。我们应该强迫那个用户吗?
猜你喜欢
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-07
  • 2013-01-15
  • 1970-01-01
  • 2015-03-08
  • 1970-01-01
相关资源
最近更新 更多