【问题标题】:Set property to current width of element with CSS? [duplicate]使用CSS将属性设置为元素的当前宽度? [复制]
【发布时间】:2017-12-23 17:40:34
【问题描述】:

我正在寻找这样的语法:

.centered{
    left: calc(50% - current-width/2)
}

基本上,一种引用目标元素的current-width 的方法,这样我就可以将一个灵活宽度的项目居中。这存在吗?

【问题讨论】:

标签: css


【解决方案1】:

一个答案是向左移动然后翻译它:

.centered {
    left: 50%;
    transform: translate(-50%);
}

但更好的方法是在父元素上使用 Flexbox:

.centeredParent {
    display: flex;
    justify-content: center;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-04
    • 2016-03-17
    • 2013-07-22
    • 2020-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多