- CSS 教程
 - CSS - 主页
 - CSS - 简介
 - CSS - 语法
 - CSS - 包含
 - CSS - 测量单位
 - CSS - 颜色
 - CSS - 背景
 - CSS - 字体
 - CSS - 文本
 - CSS - 图像
 - CSS - 链接
 - CSS - 表格
 - CSS - 边框
 - CSS - 边距
 - CSS - 列表
 - CSS - 填充
 - CSS - 光标
 - CSS - 轮廓
 - CSS - 尺寸
 - CSS - 滚动条
 - CSS - 内联块
 - CSS - 下拉菜单
 - CSS - 可见性
 - CSS - 溢出
 - CSS-Clearfix
 - CSS-浮动
 - CSS - 箭头
 - CSS - 调整大小
 - CSS - 引号
 - CSS - 顺序
 - CSS - 位置
 - CSS - 连字符
 - CSS - 悬停
 - CSS - 显示
 - CSS - 焦点
 - CSS - 缩放
 - CSS - 翻译
 - CSS - 高度
 - CSS - 宽度
 - CSS - 不透明度
 - CSS - Z 索引
 - CSS - 底部
 - CSS - 导航栏
 - CSS - 叠加
 - CSS - 表单
 - CSS - 对齐
 - CSS - 图标
 - CSS - 图标
 - CSS - 图片库
 - CSS - 注释
 - CSS - 加载器
 - CSS - 属性选择器
 - CSS - 组合器
 - CSS-根
 - CSS - 盒子模型
 - CSS - 计数器
 - CSS - 剪辑
 - CSS - 书写模式
 - CSS - Unicode-bidi
 
- CSS 高级
 - CSS-弹性盒
 - CSS - 可见性
 - CSS - 定位
 - CSS - 层
 - CSS - 伪类
 - CSS - 伪元素
 - CSS - @规则
 - CSS - 文本效果
 - CSS - 媒体类型
 - CSS - 分页媒体
 - CSS-听觉媒体
 - CSS - 打印
 - CSS - 布局
 - CSS - 验证
 - CSS - 图像精灵
 - CSS - 重要
 
- CSS3 教程
 - CSS3 - 教程
 - CSS3 - 圆角
 - CSS3 - 边框图像
 - CSS3 - 多背景
 - CSS3 - 颜色
 - CSS3 - 渐变
 - CSS3 - 阴影
 - CSS3 - 文本
 - CSS3 - 网页字体
 - CSS3 - 二维变换
 - CSS3 - 3D 变换
 - CSS3 - 动画
 - CSS3 - 多列
 - CSS3 - 用户界面
 - CSS3 - 盒子尺寸
 
- CSS 响应式
 - CSS - 响应式网页设计
 
- CSS 资源
 - CSS - 有用的资源
 - CSS - 讨论
 
CSS-宽度
CSS 宽度属性
width属性设置元素内容区域的宽度。如果box-sizing设置为border-box,则属性width设置边框区域的宽度。
width属性指定的值保持在min-width和max-width属性定义的值范围内。
请参阅图像以了解元素的宽度。
句法
CSS 允许以多种方式设置元素的宽度。让我们检查所有可能的可用语法来设置元素的宽度。
长度值
width: 120px; width: 10em; width: 100vh;
百分比值
width: 75%; width: 50%;
关键词值
width: auto; width: max-content; width: min-content; width: fit-content(20em);
全球价值
width: inherit; width: initial; width: revert; width: revert-layer; width: unset;
不接受像 width: -200px 这样的负值。
适用于
除不可替换的内联元素、表行和行组之外的所有 HTML 元素。
DOM语法
object.style.width = "100px";
CSS 宽度 - 演示
尝试为 CSS宽度属性选择不同的值,并在右侧框中查看结果。
CSS 宽度 - 长度值
CSS 允许将元素的宽度设置为特定的长度值,例如像素 (px)、厘米 (cm)、英寸 (in) 等。下面是以长度单位向div元素添加宽度的示例:
<html>
<head>
<style>
   div {
      border: 1px solid black;
      margin-bottom: 5px;
      padding:10px;
   }
   div.a {
      width: 100px;
      background-color: rgb(230, 230, 203);
   }
   div.b {
      width: 5em;
      background-color: rgb(230, 230, 203);
   }
</style>
</head>
<body>
   <div class="a">This div element has a width of 100px.</div>
   <div class="b">This div element has a width of 5em.</div>
</body>
</html>
CSS 宽度 - 百分比值
CSS 允许将元素的高度设置为包含元素的宽度的百分比。以下是向div元素添加宽度(以百分比值表示)的示例:
<html>
<head>
<style>
   .parent{
      border: 1px solid black;
      width: 400px;
      background-color: rgb(230, 230, 203);
   }
   .child{
      border: 1px solid black;
      width: 50%;
      background-color: rgb(76 175 80);
   }
</style>
</head>
<body>
   <div class="parent">
       <div class="child">
        <p>This div element has a 50% width of the parent.</p>
       </div>
   </div>
</body>
</html>
CSS 宽度 - 自动值
这里浏览器会根据内容自动计算宽度。它是元素的默认宽度。以下是向div元素添加宽度为auto的示例:
<html>
<head>
<style>
   div {
      border: 1px solid black;
      margin-bottom: 5px;
   }
   div.auto {
      width: auto;
      background-color: yellow;
   }
</style>
</head>
<body>
   <div class="auto">This div element has a width set as auto.</div>
</body>
</html>
CSS 宽度 - 最大内容/最小内容
这是宽度等于max-content和min-content的示例。最大内容定义了固有的首选宽度,而最小内容定义了固有的最小宽度。
<html>
<head>
<style>
   div {
      border: 1px solid black;
      margin-bottom: 5px;
   }
   div.c {
      width: max-content;
      background-color: bisque;
   }
   div.d {
      width: min-content;
      background-color: darkseagreen;
   }
</style>
</head>
<body>
   <div class="c">This div element has a width as max-content.</div>
   <div class="d">This div element has a width of min-content.</div>
</body>
</html>
CSS 宽度 - 适合内容
以下是为列表宽度设置的fit-content值的示例:
<html>
<head>
<style>
   ul {
      background-color: beige;
      width: fit-content;
      padding: 1.5em;
      border: 2px solid black;
   }
   li {
      display: inline-flex;
      background-color: orange;
      border: 2px solid black;
      padding: 0.5em;
   }
</style>
<body>
   <ul>
      <li>Item1</li>
      <li>Item2</li>
      <li>Item3</li>
      <li>Item4</li>
   </ul>
</body>
</html>
CSS 宽度 - 相关属性
以下是width相关 CSS 属性列表: