CSS Variables - How to concatenate a minus symbol with CSS Variable in a calc()

ぐ巨炮叔叔 提交于 2019-11-28 01:58:47

Yes you can do it. Simply multiply by -1:

:root {
  --margin-l: 50px;
}

body {
  padding: 0 100px;
  border:1px solid;
}

.box-1 {
  background: red;
  height: 100px;
  width: 200px;
  margin-left: calc(-1 * var(--margin-l));
}

.box-2 {
  background: green;
  height: 100px;
  width: 200px;
  margin-left: calc(-1 * calc(-1 * var(--margin-l))); /* You can also nest */
}
<div class="box-1">
</div>
<div class="box-2">
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!