css - 为什么 3 x 33% 宽度的列不适合 100% 的容器

标签 css

这个问题已经困扰我一段时间了。请有人解决我这个代码谜题,而不将列宽从完全等于 100% 更改。

<style>
/* slider style rules */
  * {
     padding: 0; margin: 0;  
    }
  .slider ul {
     list-style: none; 
   }
 .slider ul li {
    display: inline-block;
    width: 33%; 
        /* ^= set this to 32% and watch the horizontal adjustment
        of the items if your   confused   */
    }
   </style>

   <div class="slider">
      <h2>Why do 3 x 33% width columns not fit in a 100% container?</h2>
        <span>Shouldn't there be an <em>EXTRA</em>1%? if each column is 33% x 3 columns = 99%    why with every single elements padding and margin reset do the elements not fit in the page</span>
    <ul>
        <li>Item</li>
        <li>Item</li>
        <li>Item</li>
   </ul>
 </div>

Heres the fiddle...

最佳答案

这是一个工作片段:

/* slider style rules */

* {
  padding: 0;
  margin: 0;
}
.slider ul {
  list-style: none;
}
.slider ul li {
  display: inline-block;
  border: 1px solid red;  /*demo purposes */
  box-sizing: border-box; /*demo purposes */
  width: 33%;  /*<= set this to 32% and watch the horizontal adjustment of the items if your confused */
}
<div class="slider">
  <h2>Why do 3 x 33% width columns not fit in a 100% container?</h2>
  <span>Shouldnt there be an <em>EXTRA</em> 1%? if each column is 33%x3coluns = 99% why with every single elements padding and margin reset do these els not fit in the page</span>
  <ul>
    <li>Item</li><li>Item</li><li>Item</li>
  </ul>
</div>

问题是 li 具有 display:inline-block ,这会产生间隙,因此您的方法是删除 li 之间不必要的间隙在你的 HMTL 中(我在这个 fiddle 上使用的解决方案)

在此处查看其他解决方案:

http://css-tricks.com/fighting-the-space-between-inline-block-elements/

关于css - 为什么 3 x 33% 宽度的列不适合 100% 的容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22572802/

相关文章:

css - asp.net 5 mvc 6 中的地铁 ui css

javascript - 在每个条形中间添加文本标签 - 堆积条形图和鼠标悬停事件

css - 使用 CSS 格式化表格

php - 如何使我的表格单元格自动适应 Bootstrap 3 中的内容?

html - h2 在移动设备上强加导航栏崩溃

css - 浏览器告诉 "failed to compile"

PHP/HTML/CSS : Styling an array of data

html - 下拉式菜单;悬停 css 不适用于兄弟成员,但适用于后代成员

html - Vue.js 2 - 从 body 标签中删除初始边距

css - 小宽度的破损按钮