height: 100vh; background: url('@/assets/images/background.jpg') no-repeat; background-size: cover; position: fixed; 各有什么用
时间: 2024-03-31 15:36:21 浏览: 48
这是一个CSS样式代码片段,它的作用如下:
1. `height: 100vh;` 表示元素的高度等于视口高度。vh是视口高度的单位,1vh等于视口高度的1%。
2. `background: url('@/assets/images/background.jpg') no-repeat;` 表示元素的背景图片为`@/assets/images/background.jpg`,并设置不重复平铺。
3. `background-size: cover;` 表示元素的背景图片尽可能地覆盖元素,保持图片宽高比不变。
4. `position: fixed;` 表示元素的定位方式为固定定位,即相对于浏览器窗口固定位置,不随滚动条滚动。
相关问题
<template> <div class="form_container"> </div> </template> <style scoped> .form_container { position: relative; width: 100%; height: 100vh; background: url(../../assets/bg2.png) no-repeat; background-size: cover; } </style>
这是一个 Vue.js 的模板代码,包含一个名为 form_container 的 div 元素,以及一个 scoped 的 style 标签。在 style 标签中,.form_container 类被定义为一个相对定位的元素,并设置了宽度、高度、背景图片和背景大小。 scoped 属性表示该样式只适用于当前组件,不会影响全局样式。
优化下面代码.bg { width: 100%; height: 100vh; background-image: url('../../assets/img/info-bg.png'); background-size: 100% 100%; background-repeat: no-repeat; position: relative; font-family: AlibabaPuHuiTiR; .goBack { position: absolute; top: 34px; right: 65px; cursor: pointer; color: #ffffff; width: 181px; padding: 15px 10px; background: rgba(24, 31, 30, 0.52); border: 1px solid #4a524e; border-radius: 5px; font-size: 18px; font-family: AlibabaPuHuiTiR; z-index: 111; display: flex; flex-direction: row; justify-content: space-between; align-items: center; } .home-left { position: absolute; top: 18%; left: 40px; width: 41%; height: 76%; font-size: 24px; color: #ffffff; } .unit { font-size: 24px; color: #636363; } .home-left-title { font-size: 24px; color: #ffffff; line-height: 36px; } .home-right { position: absolute; top: 18%; right: 88px; width: 46%; height: 78%; } .model { display: flex; justify-content: center; align-items: center; height: 90%; } #threeContained { width: 100%; height: 100%; } .model-qk-img { width: 82%; height: 90%; background-image: url('../../assets/img/howo.png'); background-size: 100% 100%; background-repeat: no-repeat; } .model-zk-img { width: 56%; height: 90%; background-image: url('../../assets/img/heavyT.png'); background-size: 100% 100%; background-repeat: no-repeat; } .model-gj-img { width: 82%; height: 90%; background-image: url('../../assets/img/transit.png'); background-size: 100% 100%; background-repeat: no-repeat; } .car-online { margin-bottom: 50px; } } .day-data { display: flex; flex-direction: row; justify-content: space-between; align-items: center; height: 29%; margin-left: 30px; } .day-val { width: 40%; } .prefix { display: inline-block; width: 6px; height: 14px; background: #ffffff; margin-right: 20px; } .zh-title { margin-left: 30px; padding-top: 30px; font-size: 30px; font-weight: 700; text-align: left; color: #ffffff; line-height: 32px; letter-spacing: 0.3px; font-family: AlibabaPuHuiTiB; } .en-title { margin-left: 30px; font-size: 14px; font-weight: 400; text-align: left; color: #ffffff; line-height: 32px; letter-spacing: -0.91px; font-family: AlibabaPuHuiTiR; }
Here are some suggestions to optimize the code:
1. Use shorthand properties whenever possible. For example, instead of writing `background-size: 100% 100%;`, you can write `background-size: cover;`.
2. Consolidate similar styles into classes, instead of repeating them for every element. For example, you can create a class for the font family and apply it to all elements that use that font.
3. Remove unnecessary styles that are not being used or overwritten by other styles.
4. Use more specific selectors to target elements, instead of relying on the order of elements in the HTML. This will make the code more robust and easier to maintain.
5. Consider using a CSS preprocessor like Sass or Less, which can help you write cleaner and more organized code.
Here's an example of how the code could be optimized:
```
.bg {
width: 100%;
height: 100vh;
background: url('../../assets/img/info-bg.png') no-repeat center center / cover;
position: relative;
font-family: AlibabaPuHuiTiR;
}
.goBack {
position: absolute;
top: 34px;
right: 65px;
cursor: pointer;
color: #fff;
width: 181px;
padding: 15px 10px;
background: rgba(24, 31, 30, 0.52);
border: 1px solid #4a524e;
border-radius: 5px;
font-size: 18px;
font-family: AlibabaPuHuiTiR;
z-index: 111;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.home-left {
position: absolute;
top: 18%;
left: 40px;
width: 41%;
height: 76%;
font-size: 24px;
color: #fff;
}
.unit {
font-size: 24px;
color: #636363;
}
.home-left-title {
font-size: 24px;
color: #fff;
line-height: 36px;
}
.home-right {
position: absolute;
top: 18%;
right: 88px;
width: 46%;
height: 78%;
}
.model {
display: flex;
justify-content: center;
align-items: center;
height: 90%;
}
#threeContained {
width: 100%;
height: 100%;
}
.model-qk-img {
width: 82%;
height: 90%;
background: url('../../assets/img/howo.png') no-repeat center center / cover;
}
.model-zk-img {
width: 56%;
height: 90%;
background: url('../../assets/img/heavyT.png') no-repeat center center / cover;
}
.model-gj-img {
width: 82%;
height: 90%;
background: url('../../assets/img/transit.png') no-repeat center center / cover;
}
.car-online {
margin-bottom: 50px;
}
.day-data {
display: flex;
justify-content: space-between;
align-items: center;
height: 29%;
margin-left: 30px;
}
.day-val {
width: 40%;
}
.prefix {
display: inline-block;
width: 6px;
height: 14px;
background: #fff;
margin-right: 20px;
}
.zh-title {
margin-left: 30px;
padding-top: 30px;
font-size: 30px;
font-weight: 700;
text-align: left;
color: #fff;
line-height: 32px;
letter-spacing: 0.3px;
font-family: AlibabaPuHuiTiB;
}
.en-title {
margin-left: 30px;
font-size: 14px;
font-weight: 400;
text-align: left;
color: #fff;
line-height: 32px;
letter-spacing: -0.91px;
font-family: AlibabaPuHuiTiR;
}
.font-alibaba {
font-family: AlibabaPuHuiTiR;
}
.font-alibaba-bold {
font-family: AlibabaPuHuiTiB;
}
```
阅读全文