CSS中的百分比宽度不起作用

11

我使用了一种在CSS中使用视口度量来定义宽度属性的方法,CSS代码:

#content {
    position: absolute;
    background-color: white;
    width: 100vw;
    top: 115px;
    bottom: 30px;
    display: table;
    z-index: 0;
}
当我把宽度从width:100vw改为width:100%时,我的代码就不起作用了。我想要改成百分比的测量方式,因为我发现在Android < 4.4上视口将无法正常工作。
其他相关的HTML和CSS代码:
HTML
  <div id="mainslide"> 
    <section id="aanbod"> 
      <div id="content" align="center">
        <table class="tablestyleorchideeaanbod">
          <tr>
            <td class="titel" width="85%">Orchidee</td>
            <td class="beschrijving" width="15%" rowspan="1" align="right">21 aug</td>
          </tr>
          <tr>
            <td class="soort">Cherry red</td>
            <td width="15%" rowspan="2" align="right"><svg height="30" width="30">
              <circle cx="15" cy="15" r="12" fill="#ff4641" />
              </svg></td>
          </tr>
          <tr>
            <td class="beschrijving"><span class="width">Aantal: 100 stuks</span>Grootte: 3 Liter</td>
          </tr>
        </table>
      </div>
    </section>

    <section>
    ..............
    </section>

    <section>
    ..............
    </section>
  </div>

CSS

#mainslide {
    position: absolute;
    left: 100%;
    z-index: 1;
}
#aanbod {
    position: absolute;
    left: 0px;
}
.tablestyleorchideeaanbod {
    width: 100%;
    padding: 12px;
    padding-left: 8%;
    padding-right: 8%;
    border-bottom-width: 1px;
    border-top-width: 0px;
    border-left-width: 0px;
    border-right-width: 0px;
    border-style: solid;
    border-color: #8cca49;
}

“left: 100%;” 是正确的吗?为什么不用“right: 0px;”呢? - Thomas Rbt
“left:100%;” 超出了你的屏幕范围,请使用 “right:0;” 代替。 - Nick
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Kevin Ammerlaan
1个回答

7

将位置设置为相对而不是绝对,然后width:100%就可以实现。

#mainslide {
    position: relative;
    left:100%
    z-index: 1;
}
#aanbod {
    position: relative;
    left: 0px;
}
.tablestyleorchideeaanbod {
    width: 100%;
    padding: 12px;
    padding-left: 8%;
    padding-right: 8%;
    border-bottom-width: 1px;
    border-top-width: 0px;
    border-left-width: 0px;
    border-right-width: 0px;
    border-style: solid;
    border-color: #8cca49;
}
#content {
    position: absolute;
    background-color: white;
    width: 100%;
    top: 115px;
    bottom: 30px;
    display: table;
    z-index: 0;
}

工作示例:http://jsfiddle.net/duvx5qLp/

如果您需要其他内容,请告诉我 :)


到目前为止,你是我的英雄!你能解释一下为什么这会有所不同吗? - Kevin Ammerlaan
@KevinAmmerlaan 很高兴我能帮到你,我会尽力简要说明绝对定位元素的区别。具有绝对定位的元素是相对于具有相对或绝对定位的第一个父元素放置的。这完全取决于包含绝对定位项的项目。 - m0bi5
@KevinAmmerlaan 如果我有帮助到您,请将此标记为答案 :),您会帮了个忙。 - m0bi5
非常感谢,我现在也修复了许多其他的错误,并在几分钟内改变了我的网站! - Kevin Ammerlaan
1
@KevinAmmerlaan 蹲一个 bug,它们就都跑了 xD - m0bi5
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接