gpt4 book ai didi

html - 在 vue 组件中使用样式的最佳实践是什么?

转载 作者:行者123 更新时间:2023-12-03 06:36:12 26 4
gpt4 key购买 nike

我使用 vue cli 创建一个元素,但我的样式有“错误”
我只是测试一个有 3 行 20vh 50vh 30vh 的组件

<template>
<div class="gridContact">
<div class="one">1</div>
<div class="two">2</div>
<div class="three">3</div>
</div>
</template>
<style scoped>
.gridContact {display: grid; grid-template-areas: 'one' 'two' 'three'; box-sizing: border-box;}
.one {grid-area: one; background: rebeccapurple; box-sizing: border-box; height: 20vh; }
.two {grid-area: two; background: cadetblue; box-sizing: border-box; height:50vh;}
.three {grid-area: three; background: coral; box-sizing: border-box; height: 30vh; }
</style>
还有我的 app.vue
*{padding: 0; margin: 0; box-sizing: border-box;}
我得到了这种风格(需要的风格)
enter image description here
但是,如果我刷新页面,我得到了这个破坏样式的空白,然后如果再次重新加载它看起来不错,然后如果我刷新我得到相同的空白,为什么?
enter image description here
在开发人员工具上检查此行为时,我看到注入(inject)了此属性,当然该边距不是由我插入的,我认为这是在 vue 中造成的行为,也许是作用域属性?但是为什么呢?干什么用的?如何解决?
enter image description here

最佳答案

你不知道 body 的margin-bottom正在申请,就像在下面的例子中我故意添加了 margin-bottom body 和应用你的风格。

.gridContact {display: grid; grid-template-areas: 'one' 'two' 'three'; box-sizing: border-box;}
.one {grid-area: one; background: rebeccapurple; box-sizing: border-box; height: 20vh; }
.two {grid-area: two; background: cadetblue; box-sizing: border-box; height:50vh;}
.three {grid-area: three; background: coral; box-sizing: border-box; height: 30vh; }

body{
margin-bottom:50px;
}

*{padding: 0px; margin: 0px; box-sizing: border-box;}
<body>
<div class="gridContact">
<div class="one">1</div>
<div class="two">2</div>
<div class="three">3</div>
</div>
</body>

现在我包括 !important强行应用我的 CSS 样式并且它起作用了。

.gridContact {display: grid; grid-template-areas: 'one' 'two' 'three'; box-sizing: border-box;}
.one {grid-area: one; background: rebeccapurple; box-sizing: border-box; height: 20vh; }
.two {grid-area: two; background: cadetblue; box-sizing: border-box; height:50vh;}
.three {grid-area: three; background: coral; box-sizing: border-box; height: 30vh; }

body{
margin-bottom:50px;
}

*{padding: 0px !important; margin: 0px !important; box-sizing: border-box;}
<body>
<div class="gridContact">
<div class="one">1</div>
<div class="two">2</div>
<div class="three">3</div>
</div>
</body>

关于html - 在 vue 组件中使用样式的最佳实践是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62505232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com