css - 有没有办法让 CSS calc() 永远不会是负值?

标签 css css-calc

我有以下代码,它非常适合将我的内容居中,直到 vh 小于 650px。我想知道是否有办法只使用 CSS calc 使这个值永远不会低于 0

.my-margin-top {
    margin-top: calc(50vh - 325px);
}

最佳答案

假设没有办法限制 calc() 计算的值,当 max-height 最多 650px 时,您可以使用媒体查询>

.my-margin-top {
    margin-top: calc(50vh - 325px);
}

@media all and (max-height: 650px) {
   .my-margin-top {
       margin-top: 0;
   }
}

或者您也可以通过将现有规则包装到 min-height 媒体查询中来还原逻辑

@media all and (min-height: 650px) {
   .my-margin-top {
       margin-top: calc(50vh - 325px);
   }
}

关于css - 有没有办法让 CSS calc() 永远不会是负值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35605852/

相关文章:

css - 计算器不工作 ie 11

css - Less 中的 Calc 问题

html - 为什么四个 child 不在父装修中?

javascript - 动态添加时 Span 元素溢出

css - 将 px 更改为 rem 后字体大小没有响应

css - 对表格使用 calc()

css - CSS 中的 "cast"单位有什么办法吗?

html - 将文本路径定位到包含圆 SVG 的中心

javascript - 交换 3 个 div 并使选择的 div 移至中心

css - 在CSS中将子类绑定(bind)到父类