盘点一些不常用却非常有用的css属性

有一个页面本来使用2组的Bootstrap的List Group + Card(里面嵌套List Group的子项目数量的表格),现在要增加1组变成3组,本来使用row和col-2类页面呈现还算布局合理,然增加1组后不能再使用row类(表格内容需要一定宽度显示)。只好考虑给容器盒子加上固定的宽度和display:flex;,这时新的问题出现了,3组card里面的内容高度是不同的,这时3组card高度都变成了最大高度。大片白色的空白呈现在浅灰色背景中。。。一切似乎是由于card的高度引起的,遂试着指定height属性,偶然看到了不知何时起height属性居然多了 max-content、min-content、fit-content 的取值。将height属性设置为以上3个值中任何一个值均可以解决问题。

写个demo想复现上面的问题,然同样的问题并未出现。只好好好学习下这3个取值的用法,再看看有什么新增的css属性

width:max-content

     <style>
      .text_box div {
        float: left;
        margin: 0 5px;
        border: 1px solid red;
      }
    </style>
    <div>
      <div class="text_box">
        <!-- 浏览器自动计算 -->
        <div style="width: auto">
          <table>
            <tr>
              <td>标题1</td>
              <td>标题2</td>
            </tr>
            <tr>
              <td>1111111111111111111111111111</td>
              <td>标题2</td>
            </tr>
          </table>
        </div>
        <!-- 扩展至内容最大宽度(防止文本换行) -->
        <div style="width: max-content">
          <table>
            <tr>
              <td>标题1</td>
              <td>标题2</td>
            </tr>
            <tr>
              <td>2111111111111111111111111111</td>
              <td>标题2</td>
            </tr>
          </table>
        </div>
        <!-- 收缩到内容最小宽度(显示最短不可断内容) -->
        <div style="width: min-content">
          <table>
            <tr>
              <td>标题1</td>
              <td>标题2</td>
            </tr>
            <tr>
              <td>3111111111111111111111111111</td>
              <td>标题2</td>
            </tr>
          </table>
        </div>
        <!-- 元素宽度自适应内容 -->
        <div style="width: fit-content">
          <table>
            <tr>
              <td>标题1</td>
              <td>标题2</td>
            </tr>
            <tr>
              <td>4111111111111111111111111111</td>
              <td>标题2</td>
            </tr>
          </table>
        </div>
      </div>
    </div>

配合flex实现了宽度随内容自动增加且能保持在同一行的效果

<div style="display: flex; width: max-content">
      <div style="width: 280px; height: 500px; border: 1px solid silver">1</div>
      <div style="width: 400px; height: 1500px; border: 1px solid silver">2</div>
      <div style="width: 280px; height: 500px; border: 1px solid silver">3</div>
      <div style="width: 400px; height: 900px; border: 1px solid silver">4</div>
      <div style="width: 280px; height: 500px; border: 1px solid silver">5</div>
      <div style="width: 400px; height: 500px; border: 1px solid silver">6</div>
</div>

 

img标签的object-fit、clip-path和filter属性

    <style>
      .img_box img {
        height: 200px;
        width: 200px;
        margin: 0 5px;
        border: 1px solid red;
      } 
    </style>
    <div>
      <h5>object-fit</h5>
      <div class="img_box">
        <!-- ‌contain‌ 等比缩放完整显示、保持图片比例、留空白边,适合产品图册/证件照等需完整展示的场景 -->
        <img src="../images/1.jpg" alt="" style="object-fit: contain" title="contail" />
        <!-- ‌cover‌ 等比填充整个容器、保持图片比例、裁剪边缘,适合封面图/头像等需要视觉饱满的场景 -->
        <img src="../images/1.jpg" alt="" style="object-fit: cover" title="cover" />
        <!-- ‌fill‌ 强制拉伸填充容器 、破坏图片比例、可能变形,适合需要精确匹配容器尺寸的图表类图片 -->
        <img src="../images/1.jpg" alt="" style="object-fit: fill" title="fill" />
        <!-- ‌none 保持原始尺寸、中心定位,适合超大尺寸图片的局部展示 -->
        <img src="../images/1.jpg" alt="" style="object-fit: none" title="none" />
        <!-- scale-down‌	智能选择较小尺寸、自动对比none和contain,不确定尺寸的文档类图片 -->
        <img src="../images/1.jpg" alt="" style="object-fit: scale-down" title="scale-down" />
      </div>

      <h5>clip-path</h5>
      <div class="img_box">
        <!-- 圆形裁剪(半径+圆心坐标) -->
        <img src="../images/1.jpg" alt="" style="clip-path: circle(50% at center)" />
        <!-- 椭圆裁剪(长/短轴+中心) -->
        <img src="../images/1.jpg" alt="" style="clip-path: ellipse(30% 50% at 50% 50%)" />
        <!-- 多边形裁剪(顶点坐标) -->
        <img src="../images/1.jpg" alt="" style="clip-path: polygon(10% 10%, 90% 10%, 90% 90%, 10% 90%)" />
        <!-- 矩形内切(边距+圆角) -->
        <img src="../images/1.jpg" alt="" style="clip-path: inset(10% 5% 15% 20% round 8px)" />
      </div>

      <h5>filter</h5>
      <div class="img_box">
        <img src="../images/1.jpg" alt="" />
        <!-- 模糊效果:元素边缘产生高斯模糊 -->
        <img src="../images/1.jpg" alt="" style="filter: blur(2px)" />
        <!-- 亮度调整:1为原始亮度,0为全黑,可超1 -->
        <img src="../images/1.jpg" alt="" style="filter: brightness(0.8)" />
        <!-- 对比度调整:1为原始对比度,0为全灰 -->
        <img src="../images/1.jpg" alt="" style="filter: contrast(150%)" />
        <!-- 投影效果:drop-shadow(offset-x offset-y blur-radius color) -->
        <img src="../images/1.jpg" alt="" style="filter: drop-shadow(4px 4px 6px rgba(0, 0, 0, 0.5))" />
        <!-- 灰度转换:0为原始,1或100%为完全灰度 -->
        <img src="../images/1.jpg" alt="" style="filter: grayscale(80%)" />
        <!-- 色相旋转 :hue-rotate(angle) -->
        <img src="../images/1.jpg" alt="" style="filter: hue-rotate(180deg)" />
        <!-- 反色效果:invert(percentage),0为原始,1或100%为完全反色 -->
        <img src="../images/1.jpg" alt="" style="filter: invert(0.3)" />
        <!-- 透明度调整 :0(完全透明)到1(不透明) -->
        <img src="../images/1.jpg" alt="" style="filter: opacity(0.6)" />
        <!-- 饱和度调整:1为原始,0为完全去色 -->
        <img src="../images/1.jpg" alt="" style="filter: saturate(2)" />
        <!-- 复古色调:0为原始,1或100%为完全深褐色 -->
        <img src="../images/1.jpg" alt="" style="filter: sepia(70%)" />
      </div>
    </div>

其它不常用属性

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      :root {
        color-scheme: dark light;
        accent-color: mediumvioletred;
      }

      div {
        margin: 10px;
      }

      .aspect-ratio-hd {
        display: inline-block;
        aspect-ratio: 16/9;
        height: 100px;
        border: 1px solid silver;
      }

      .aspect-ratio-square {
        display: inline-block;
        aspect-ratio: 1;
        height: 100px;
        border: 1px solid silver;
      }

      .image {
        object-fit: cover;
        aspect-ratio: 1;

        max-block-size: 100px;
      }

      a:not(.link) {
        text-underline-offset: 0.25em;
      }

      .outline-offset-p {
        display: inline-block;
        aspect-ratio: 1;
        height: 100px;
        outline: 2px dashed blue;
        outline-offset: var(--outline-offset, 2px);
        border: 1px solid silver;
      }

      .outline-offset-n {
        display: inline-block;
        aspect-ratio: 1;
        height: 100px;
        outline: 2px dashed blue;
        outline-offset: var(--outline-offset, -2px);
        border: 1px solid silver;
        margin-left: 10px;
      }
    </style>
  </head>
  <body>
    <h5>aspect-ratio</h5>
    <div>
      <span class="aspect-ratio-hd">16/9</span>
      <span class="aspect-ratio-square"> 1</span>
    </div>

    <h5>object-fit</h5>
    <div>
      <img class="image" src="../images/1.jpg" />
      <img class="image" src="../images/2.jpg" />
    </div>

    <h5>text-underline-offset</h5>
    <div>
      <a href="">链接下划线偏移</a>
      <a class="link" href="">链接</a>
    </div>

    <h5>outline-offset</h5>
    <div>
      <span class="outline-offset-p">有轮廓线</span>
      <span class="outline-offset-n">无轮廓线</span>
    </div>

    <h5>accent-color</h5>
    <div>
      <input type="radio" checked />是否同意 <input type="checkbox" checked />是否同意
      <input type="range" />
    </div>
  </body>
</html>

posted @ 2025-02-25 10:07  carol2014  阅读(4)  评论(0编辑  收藏  举报