【问题标题】:CSS "calc" function doesn't work as expected in Internet Explorer 11CSS“计算”功能在 Internet Explorer 11 中无法按预期工作
【发布时间】:2019-10-04 11:18:41
【问题描述】:

我的网页需要在现代浏览器(例如 Chrome)和旧版浏览器(例如 IE11)上运行。

大部分都可以,但是当我尝试使用calc (left: calc(50% - 40px);) 将按钮放在父容器div 的中间时,它在IE11 中不起作用,而是放在父容器之外。

这是我的 CSS 代码:

.buttonContainer {
  position: fixed;
  width: 336px;
  height: 62px;
  background-color: #fff;
  display: inline-block;
  text-align: center;
  vertical-align: middle;
  margin-bottom: 10px;
  box-shadow: 0 0 2px 0 #d2d2d2;
}

.button {
  position: fixed;
  left: calc(50% - 40px);
  .color {
    background-color: #ff0033;
    color: #ffffff;
    display: inline-block;
    height: 26px;
    width: 64px;
    margin-top: 10%;
    padding: 8px 16px;
    font-size: 14px;
    cursor: pointer;
    text-align: center;
    vertical-align: middle;
    line-height: 28px;
  }
}

以上内容适用于现代浏览器,其中.button 将位于buttonContainer 的中间,但在 IE11 中它将位于其外部。

【问题讨论】:

标签: html css internet-explorer sass cross-browser


【解决方案1】:

IE 使用 calc 可能有点困难。一种解决方案是将left 设置为50%,然后使用转换来更正按钮的宽度,如下所示:

.button {
    left: 50%;
    -moz-transform: translateX(-50%);
    -webkit-transform: translateX(-50%);
    -o-transform: translateX(-50%);
    -ms-transform: translateX(-50%);
    transform: translateX(-50%);  // -50% of the width of the button
}

要记住的另一件事是,位置固定将相对于浏览器窗口定位元素,而不是相对于它的包含元素(除非包含元素是浏览器窗口:)。

【讨论】:

  • 在 IE11 中对此进行了测试,它确实有效,但前提是您将 transform 属性放在 left 属性之上..?这太疯狂了!
  • 用供应商前缀更新了我的答案,请再试一次
  • 当然!另外,请忽略我的最新编辑,我在您更新答案时将其发布。
  • 它成功了,-ms-transform 似乎成功了,这很奇怪,因为 IE11 不(不应该)需要供应商前缀。
猜你喜欢
  • 2011-08-01
  • 1970-01-01
  • 2020-02-26
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
  • 2012-10-28
  • 2016-11-12
相关资源
最近更新 更多