百科狗-知识改变命运!

width - 宽度 - css 尺寸

梵高2年前 (2023-11-21)阅读数 28#技术干货
文章标签宽度

width

width 属性用于设置元素的宽度。默认设置内容区域的宽度。但如果box-sizing属性被设置为border-box,就转而设置边框区域的宽度。

width - 宽度 - css 尺寸

示例

/*  values */
width: 300px;
width: 25em;

/*  value */
width: 75%;

/* Keyword values */
width: max-content;
width: min-content;
width: fit-content(20em);
width: auto;

/* Global values */
width: inherit;
width: initial;
width: unset;

浏览器支持

浏览器都支持width

语法

width:auto| | |min-content|max-content|fit-content()

width 属性也指定为:

  • 下面关键字值之一:min-content,max-content,fit-content,auto。
  • 一个长度值或者百分比值。

取值:

  • auto:浏览器将会为指定的元素计算并选择一个宽度。
  • :使用绝对值定义宽度。参见
  • :使用外层元素的容纳区块宽度(the containing block's width)的百分比定义宽度。参见
  • min-content:元素内容固有的(intrinsic)合适宽度。还处于实验阶段
  • max-content:元素内容固有的最小宽度还处于实验阶段
  • fit-content()还处于实验阶段.取以下两种值中的较大值:
    • 固有的最小宽度
    • 固有首选宽度(max-content)和可用宽度(available)两者中的较小值
    • 可表示为:min(max-content, max(min-content,))。
默认值auto
适用于除未替换的内联元素、表行和行组之外的所有元素
继承性
动画性
计算值指定值

实例

.px_length {
  width: 200px;
  background-color: red;
  color: white;
  border: 1px solid black;
}

.em_length {
  width: 20em;
  background-color: white;
  color: red;
  border: 1px solid black;
}

.percent {
  width: 20%;
  background-color: silver;
  border: 1px solid red;
}
p.maxgreen {
  background: lightgreen;
  width: intrinsic;           /* Safari/WebKit 使用了非标准的名称 */
  width: -moz-max-content;    /* Firefox/Gecko */
  width: -webkit-max-content; /* Chrome */
}

p.minblue {
  background: lightblue;
  width: -moz-min-content;    /* Firefox */
  width: -webkit-min-content; /* Chrome */
}

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)