为什么背景颜色只在开发者工具(切换设备工具栏)中起作用?

huangapple go评论140阅读模式
英文:

Why does background color only works in developer tools (toggle device toolbar)

问题

我正在尝试为我的主题中的走马灯顶部创建一个线性渐变背景。这是CSS代码:

.carousel-item::before {
    content: "";
    position: absolute;
    top: 0;
    height: 500px;
    width: 100%;
    background: linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background: -webkit-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background: -moz-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background: -ms-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background: -o-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
}

该样式在Firefox和Edge上运行良好,但在Chrome和Opera上不起作用,如您所见。

有一件奇怪的事情是:当我在开发者工具中使用"切换设备工具栏"时,它可以工作!

问题出在哪里?

英文:

I'm trying to make a linear gradient background for the top of a carousel in my theme.
This is the CSS code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

 .carousel-item::before {
    content: &quot;&quot;;
    position: absolute;
    top: 0;
    height: 500px;
    width: 100%;
    background: linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background: -webkit-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background: -moz-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background: -ms-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background: -o-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
  }

<!-- language: lang-html -->

 &lt;div class=&quot;carousel-item&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

The style works well on firefox and edge. but doesn't work on chrome and opera, as you see

为什么背景颜色只在开发者工具(切换设备工具栏)中起作用?

And there is one strange thing: when I use "toggle device toolbar" in developer tools, it works!

为什么背景颜色只在开发者工具(切换设备工具栏)中起作用?

Where is the problem?!

答案1

得分: 2

你的代码是正确的,但在你的代码中有一些缺少的东西。你没有关闭大括号}。你应该添加leftrightposition(例如:left: 0;right: 0;)。那段代码应该像这样:

.carousel-item{
    position: relative;
}
/* CSS3 syntax */
.carousel-item::before{
    background-color: transparent;
    background-image: linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background-image: -webkit-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background-image: -moz-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background-image: -ms-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background-image: -o-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    content: &quot;&quot;;
    height: 500px;/* Or 100% */
    left: 0; /* It&#39;s missing */
    position: absolute;
    top: 0;
    width: 100%;
}

/* 如果你在CSS3语法中遇到问题,你可以使用CSS2语法来支持更旧的浏览器和所有最新的浏览器 */
.carousel-item:before{
    background-color: transparent;
    background-image: linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background-image: -webkit-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background-image: -moz-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background-image: -ms-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    background-image: -o-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
    content: &quot;&quot;;
    height: 500px;/* Or 100% */
    left: 0; /* It&#39;s missing */
    position: absolute;
    top: 0;
    width: 100%;
}

如果你的CSS没有正确更新请尝试在浏览器中进行硬刷新或清除浏览器缓存
英文:

Your code is correct but few thing missing in your code. You haven't close Curly brackets }. You should add left or right position (i.e.: left : 0; or right: 0;). That code should look like this.

.carousel-item{
position: relative;
}
/* CSS3 syntax */
.carousel-item::before{
background-color: transparent;
background-image: linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
background-image: -webkit-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
background-image: -moz-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
background-image: -ms-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
background-image: -o-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
content: &quot;&quot;;
height: 500px;/* Or 100% */
left: 0; /* It&#39;s missing */
position: absolute;
top: 0;
width: 100%;
}
/* If you face any issue in CSS3 syntax you can use CSS2 syntax
to support more older browser and all latest browser */
.carousel-item:before{
background-color: transparent;
background-image: linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
background-image: -webkit-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
background-image: -moz-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
background-image: -ms-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
background-image: -o-linear-gradient(to bottom, black 0%, rgba(145, 232, 66, 0) 100%);
content: &quot;&quot;;
height: 500px;/* Or 100% */
left: 0; /* It&#39;s missing */
position: absolute;
top: 0;
width: 100%;
}

>If your CSS isn't update properly do your browser hard refresh or clean your browser cache.

huangapple
  • 本文由 发表于 2020年1月3日 15:30:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定