【问题标题】:CSS from a Vue Single File Component overrides some CSS from buefy/bulma and some not来自 Vue 单文件组件的 CSS 覆盖了一些来自 buefy/bulma 的 CSS,而另一些则没有
【发布时间】:2021-01-13 08:39:01
【问题描述】:

我希望有人能给我一个提示,如何调查这个问题。

在 Vue 项目中,buefy 包含在 main.js 中,如下所示:

import Vue from 'vue' 
import Buefy from 'buefy'
import './style.scss'
Vue.use(Buefy)

import Client from './client.vue' 

let vm = new Vue({
    el: '#app',
    render: h => h(Client) 
})

,其中style.css 导入 buefy/bulma 样式:

@import "~bulma/sass/utilities/_all";
[...]
@import "~bulma";
@import "~buefy/src/scss/buefy";

Buefy/Bulma css 应该被单文件 vue 组件中定义的样式覆盖,例如:

<style lang="scss">
.logLine{
    margin: 0px;
    padding: 1em; 
}
</style>

奇怪的观察是,虽然 padding 覆盖了 bulma 样式(如预期的那样),但 margin 被 bulma 样式覆盖:

screenshot from firefox css inspector

它表明,虽然来自 bulma/sass/base/minireset.sass 的样式被来自单个文件组件的 .logLine 样式定义覆盖。但是.logLine 样式定义会被bulma/sass/elements/content.sass 覆盖。

我希望来自 .logLine 的样式覆盖所有 Buefy/Bulma 样式。

【问题讨论】:

    标签: css vue.js bulma


    【解决方案1】:

    通常情况下,在提出问题时,细节会引起您的注意,从而引导您找到答案:

    bulma 的样式覆盖了我在单个文件组件中的样式,它使用了 css 伪类 :last-child

    .content p:not(:last-child), [...] {
        margin-bottom: 1em;
    }
    

    我认为这个:last-child 让firefox 授予这种样式规则优先级。

    我通过将.content .logLine:not(:last-child) 添加到单个文件组件中的选择器来“解决”了这个问题。

    .content .logLine:not(:last-child), 
    .logLine, {
        margin: 0px;
    }
    

    现在它不再被覆盖。

    【讨论】:

    • 请注意,我必须使用.content .logLine:not(:last-child)。只是.logLine:not(:last-child) 不会有任何效果。有CSS高手能解释一下这里的优先顺序吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 2017-09-24
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 2018-12-01
    • 2018-01-27
    相关资源
    最近更新 更多