width:100% 与 width:auto 的区别

width:100% 与 width:auto 的区别

一、当两者的子元素没有 border 或 padding 或 margin 的时候

先看一下示例代码和效果图

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
     
        color: white;
        font-size: 20px;
      }
      .out {
     
        width: 300px;
        border: 2px solid #ff00c3;
        padding: 10px;
      }
      .inner1 {
     
        width: auto;
        height: 100px;
        background-color: rgb(68, 0, 255);
      }
      .inner2 {
     
        width: 100%;
        height: 100px;
        background-color: rgb(68, 0, 255);
      }
    </style>
  </head>
  <body style="background-color: black;">
    <div class="out">
      <div class="inner1">auto</div>
      <hr>
      <div class="inner2">100%</div>
    </div>
  </body>
</html>

效果如下
在这里插入图片描述
当两者都没有 padding 或 margin 或 border 时 效果都是一样的,都能填满父元素的content 部分

二、当两者的子元素有 border 或 padding 或 margin 的时候

示例代码

<!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>
      * {
     
        color: white;
        font-size: 20px;
      }
      .out {
     
        width: 300px;
        border: 2px solid #ff00c3;
        padding: 10p
      }
      .inner3 {
     
        width: auto;
        height: 100px;
        background-color: rgb(68, 0, 255);
        border: 10px solid yellow;
        margin: 10px;
        padding: 10px;
      }
      .inner4 {
     
        width: 100%;
        height: 100px;
        background-color: rgb(68, 0, 255);
        border: 10px solid yellow;
        margin: 10px;
        padding: 10px;
      }
    </style>
  </head>
  <body style="background-color: black;">
    <div class="out">
      <div class="inner3">auto, border + padding + margin</div>
      <hr>
      <div class="inner4">100%, border + padding + margin</div>
    </div>
  </body>
</html>

效果图如下:
在这里插入图片描述
当两者有 padding 或 margin 或 border 时, auto 宽度的子元素 content 不再和父元素的content 对等,而是

父元素content =  子元素 content + padding + border + margin

而子元素的 宽度为 100% 时,子元素和父元素的content 始终对等

综上

  • width:100% : 子元素的 content 撑满父元素的content,如果子元素还有 padding、border等属性,或者是在父元素上设置了边距和填充,都有可能会造成子元素区域溢出显示;

  • width:auto : 是子元素的 content+padding+border+margin 等撑满父元素的 content 区域。

相关推荐

  1. width:100%width:auto区别

    2024-01-27 21:56:03       33 阅读
  2. Rust-vec!Vec::with_capacity初始化数组区别

    2024-01-27 21:56:03       69 阅读
  3. Kotlin中let、apply、also、with、run使用区别

    2024-01-27 21:56:03       26 阅读
  4. 日耗100100W投手思维区别

    2024-01-27 21:56:03       23 阅读
  5. =====区别

    2024-01-27 21:56:03       34 阅读
  6. & && 区别

    2024-01-27 21:56:03       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-27 21:56:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-27 21:56:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-27 21:56:03       82 阅读
  4. Python语言-面向对象

    2024-01-27 21:56:03       91 阅读

热门阅读

  1. 基于单链表实现通讯录项目

    2024-01-27 21:56:03       62 阅读
  2. GO——context

    2024-01-27 21:56:03       58 阅读
  3. RAG(检索增强生成)在LLM(大型语言模型)中的应用

    2024-01-27 21:56:03       34 阅读
  4. 字符串函数strncpy、strncat、strncmp的功能(3)

    2024-01-27 21:56:03       64 阅读
  5. Android中C++层fstream用法详解

    2024-01-27 21:56:03       47 阅读
  6. CSS--样式穿透

    2024-01-27 21:56:03       65 阅读
  7. Mysql 索引优化

    2024-01-27 21:56:03       62 阅读
  8. K8S、keepalived、haproxy 高可用集群实战

    2024-01-27 21:56:03       47 阅读
  9. CentOS 7 上使用 wget 安装 Nginx 并设置开机自启

    2024-01-27 21:56:03       53 阅读
  10. Dubbo 3.x:探索阿里巴巴的开源RPC框架新技术

    2024-01-27 21:56:03       57 阅读
  11. C#学习笔记_StringBuilder+程序效率测试

    2024-01-27 21:56:03       57 阅读