我正在尝试在 Sass 样式表中使用 calc()
函数,但遇到了一些问题。这是我的代码:
$body_padding: 50px
body
padding-top: $body_padding
height: calc(100% - $body_padding)
如果我使用文字 50px
而不是我的 body_padding
变量,我会得到我想要的。但是,当我切换到变量时,这是输出:
body {
padding-top: 50px;
height: calc(100% - $body_padding);
}
如何让 Sass 认识到它需要替换 calc
函数中的变量?
body
height: calc(100% - #{$body_padding})
对于这种情况,border-box 也足够了:
body
box-sizing: border-box
height: 100%
padding-top: $body_padding
要在 height 属性的 calc()
中使用 $variables
:
HTML:
<div></div>
SCSS:
$a: 4em;
div {
height: calc(#{$a} + 7px);
background: #e53b2c;
}
尽管它没有直接关系。但是我发现如果你没有正确放置空格,CALC 代码将不起作用。
所以这对我不起作用calc(#{$a}+7px)
但这有效calc(#{$a} + 7px)
我花了一些时间来解决这个问题。
calc
之后也不允许有额外的空格。 calc (#{$a} + 7px)
行不通。
我已经尝试过了,然后我解决了我的问题。它将按照给定的速率(基本大小/速率大小)自动计算所有媒体断点
$base-size: 16;
$rate-size-xl: 24;
// set default size for all cases;
:root {
--size: #{$base-size};
}
// if it's smaller then LG it will set size rate to 16/16;
// example: if size set to 14px, it will be 14px * 16 / 16 = 14px
@include media-breakpoint-down(lg) {
:root {
--size: #{$base-size};
}
}
// if it is bigger then XL it will set size rate to 24/16;
// example: if size set to 14px, it will be 14px * 24 / 16 = 21px
@include media-breakpoint-up(xl) {
:root {
--size: #{$rate-size-xl};
}
}
@function size($px) {
@return calc(#{$px} / $base-size * var(--size));
}
div {
font-size: size(14px);
width: size(150px);
}
这是一个使用 SASS/SCSS 和数学公式样式的非常简单的解决方案:
/* frame circle */
.container {
position: relative;
border-radius: 50%;
background-color: white;
overflow: hidden;
width: 400px;
height: 400px; }
/* circle sectors */
.menu-frame-sector {
position: absolute;
width: 50%;
height: 50%;
z-index: 10000;
transform-origin: 100% 100%;
}
$sector_count: 8;
$sector_width: 360deg / $sector_count;
.sec0 {
transform: rotate(0 * $sector_width) skew($sector_width);
background-color: red; }
.sec1 {
transform: rotate(1 * $sector_width) skew($sector_width);
background-color: blue; }
.sec2 {
transform: rotate(2 * $sector_width) skew($sector_width);
background-color: red; }
.sec3 {
transform: rotate(3 * $sector_width) skew($sector_width);
background-color: blue; }
.sec4 {
transform: rotate(4 * $sector_width) skew($sector_width);
background-color: red; }
.sec5 {
transform: rotate(5 * $sector_width) skew($sector_width);
background-color: blue; }
.sec6 {
transform: rotate(6 * $sector_width) skew($sector_width);
background-color: red; }
.sec7 {
transform: rotate(7 * $sector_width) skew($sector_width);
background-color: blue; }
最后,我强烈建议您了解 transform-origin
、rotate()
和 skew()
:
https://tympanus.net/codrops/2013/08/09/building-a-circular-navigation-with-css-transforms/
尝试这个:
@mixin heightBox($body_padding){
height: calc(100% - $body_padding);
}
body{
@include heightBox(100% - 25%);
box-sizing: border-box
padding:10px;
}
不定期副业成功案例分享
box-sizing: border-box;
当然是理想的选择。一般来说,盒子尺寸模型应该使calc(100% - var(--body_padding))
一起走 css 路线