英文:
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: "";
    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 -->
 <div class="carousel-item"></div>
<!-- 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
你的代码是正确的,但在你的代码中有一些缺少的东西。你没有关闭大括号}。你应该添加left或right的position(例如: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: "";
    height: 500px;/* Or 100% */
    left: 0; /* It'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: "";
    height: 500px;/* Or 100% */
    left: 0; /* It'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: "";
height: 500px;/* Or 100% */
left: 0; /* It'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: "";
height: 500px;/* Or 100% */
left: 0; /* It'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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。




评论