首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSS "width: auto“在<div>上不起作用

CSS "width: auto“在<div>上不起作用
EN

Stack Overflow用户
提问于 2016-07-08 01:17:49
回答 1查看 18K关注 0票数 13

我希望.test_info的宽度是自动的,不同于#test的宽度。

https://jsfiddle.net/ef6ukobf/

代码语言:javascript
复制
#test {
  background: #26A65B;
  height: 30px;
  width: auto;
  margin-top: -8px;
  margin-left: -8px;
  margin-right: -8px;
}
.test_info {
  background: #00ff00;
  border-radius: 5px;
  height: 20px;
  width: auto;
  margin-top: -2px;
  margin-left: 20px;
}
代码语言:javascript
复制
<div id="test">
  <div class="test_info">50</div>
</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-08 01:22:07

<div>元素是块级别的,默认情况下它使用容器的整个可用宽度。

如果你希望它是自动宽度(取决于里面的内容),你可以这样做:

代码语言:javascript
复制
.test_info {
  display: inline-block; /*or table*/
}

代码语言:javascript
复制
#test {
  background: #26A65B;
}
.test_info {
  background: #00ff00;
  display: inline-block;
}
代码语言:javascript
复制
<div id="test">
  <div class="test_info">50</div>
</div>

票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38251555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档