Skip to content

css

可劲学吧,学不完的。

基础

css属性值

initial

让css某个属性恢复到属性默认值

inherit

让css某个属性继承父元素

unset

让css某个属性不设置,并且继承父元素(如果可以继承的话)

revert

让css某个属性恢复到浏览器的默认样式

inset

inset 为简写属性,对应于 top、right、bottom 和 left 属性

布局

固定元素宽高比

方法一:aspect-ratio 存在兼容性

aspect-ratio 为盒子规定了首选纵横比,这个纵横比可以用于计算 auto 尺寸以及其他布局函数。

当前图片宽度:,当前图片高度:
astral

小提示

当同时设置 widthheight 时,aspect-ratio 会被忽略。

方法二:padding-top 完美适配

padding-top 是一个百分比值时,它基于包含块(父元素)的宽度来计算高度。

padding-top
查看完整代码
vue
<template>
  <div class="conatiner">
    <div class="content">
      <div class="inner">
        <div class="item">padding-top</div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.conatiner {
  width: 100%;
  height: 700px;
  background-color: #333333;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  // 外包装
  .content {
    width: 100%;
    margin: 0 auto;
    // 设置content高度为宽度的75%
    .inner {
      width: 100%;
      // 元素顶部内边距区域的高度 - <percentage> 相对于包含块的行内尺寸(水平语言中的宽度,由 writing-mode 定义)的百分比。
      padding-top: 75%;
      position: relative;
      // 真正展示的内容
      .item {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: #5bceaa;
        font-size: 5rem;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }
  }
}
</style>

适应内容(宽度/高度)

查看完整代码
vue
<template>
  <div class="container">
    <div class="nav">
      <h1>导航栏</h1>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 400px;
  background-color: #333333;
  display: flex;
  justify-content: center;
  align-items: center;
  .nav {
    width: 100%;
    height: 50px;
    background-color: rgba(175, 175, 175, 0.5);
    h1 {
      background-color: #37ad54;
      line-height: 50px;
      width: fit-content;
    }
  }
}
</style>
  • width: fit-content
  • height: fix-content

Flex 布局

Flexible Box 模型,通常被称为 flexbox,是一种一维的布局模型。它给 flexbox 的子元素之间提供了强大的空间分布和对齐能力。

flex-wrap:wrapflex-direction:rowjustify-content:centeralign-items:center
0
1
2
3
4
5
6
7
8
9

Released under the MIT License.