css - 如何根据另一个不相关元素定义一个元素的宽度?

标签 css sass width

我想让一个 div 与另一个 div 一样宽 80%,但它们既不是嵌套的,也不是同级的。

如何使用 SASS 做到这一点

最佳答案

您可以使用原始宽度变量并使用它来计算宽度。我使用 mixin 来展示宽度以像素和百分比为单位的演示。

HTML

<div class="parent">
  <div class="three">three</div>
  <div class="four">four</div>
</div>
<div class="one">one</div>
<div class="two">two</div>

SCSS

@mixin calc_width($obj) {
    width: calc(0.8 * #{$obj})  
}


$width_in_px: 300px;
$width_in_percentage: 50%;

.one{
  width: $width_in_px;
  height: 50px;
  background: red;
}
.two {
  @include calc_width($width_in_px); 
  height: 50px;
  background: green;
  }

.parent {
  width: $width_in_px;
  background: orange;
}
.three{
  width: $width_in_percentage;
  height: 50px;
  background: lime;
}
.four {
  @include calc_width($width_in_percentage);  
  height: 50px;
  background: black;
  }

Updated Codepen

关于css - 如何根据另一个不相关元素定义一个元素的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27653889/

相关文章:

iphone - 如何禁用水平拖动? (webkit-溢出-滚动 : touch;)

javascript - Angular Material - 禁用 mat-sidenav 过渡动画但保留 child 动画?

angular - 在ionic 3中动态改变背景颜色

css - 如何使用百分比宽度使一行 5 个 <divs> 响应?

css - 如何创建一个包装浏览器 View 区域的界面?

javascript - 使用 ng-class 附加 css 时 uib-rating 没有变色

html - 如何将侧边导航栏上的用户图标移到中央?以及如何为侧边导航栏创建边框?

reactjs - reactjs 应用程序中的全局 SASS 变量

android - 如何在 api 17 或更高版本中使 android seekbar 全宽?

html - 在 div 中水平和垂直居中 2 个选择控件