scss 流体方面:用于创建固有比率的Sass mixin

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scss 流体方面:用于创建固有比率的Sass mixin相关的知识,希望对你有一定的参考价值。

# Fluid Aspect

**fluid-aspect** is a Sass mixin for creating [intrinsic ratios](http://alistapart.com/article/creating-intrinsic-ratios-for-video) in CSS. Intrinsic ratios allow elements to fill the width of their containing block and resize on the fly while maintaining their aspect ratio.

```scss
@include fluid-aspect($ratio, [$target]);
```

- **$ratio**: An aspect ratio represented as two numbers separated by a space. *Defaults to 1:1*
- **$target**: A selector targeting the element to be made fluid. *Defaults to "> :first-child"*

## Usage

Create a fluid aspect ratio container by including the mixin and its first child will by made fluid.

```scss
.my-container {
	@include fluid-aspect(16 9);
}
```

Rendered as CSS:

```css
.my-container {
	padding-bottom: 56.25%;
	position: relative;
}

.my-container > :first-child {
	left: 0;
	height: 100%;
	position: absolute;
	top: 0;
	width: 100%;
}
```

Create a fluid container that specifies the descendant that will become fluid.

```scss
.my-container {
	@include fluid-aspect(4 3, iframe);
}
```

Rendered as CSS:

```css
.my-container {
	padding-bottom: 75%;
	position: relative;
}

.my-container iframe {
	left: 0;
	height: 100%;
	position: absolute;
	top: 0;
	width: 100%;
}
```

Remember that advanced selectors must be wrapped in a string. Within a string, you can still reference parent selectors using the ampersand (&) character.

```scss
.my-container {
	@include fluid-aspect(5 3, "&--fluid");
}
```

Rendered as CSS:

```css
.my-container {
	padding-bottom: 60%;
	position: relative;
}

.my-container--fluid {
	left: 0;
	height: 100%;
	position: absolute;
	top: 0;
	width: 100%;
}
```
@mixin fluid-aspect($ratio: 1 1, $selector: "> :first-child") {
	$selector: unquote($selector);

	padding-bottom: percentage(nth($ratio, 2) / nth($ratio, 1));
	position: relative;

	#{$selector} {
		left: 0;
		height: 100%;
		position: absolute;
		top: 0;
		width: 100%;
	}
}

以上是关于scss 流体方面:用于创建固有比率的Sass mixin的主要内容,如果未能解决你的问题,请参考以下文章

用于小型和大型(流体/响应)列的 Zurb Foundation 4 SCSS 混合

用于 css3 关键帧动画的 SASS(不是 SCSS)语法

scss Sass用于创建顶部和底部边距类

如何使用 Hugo 设置 SCSS

scss #sass我的一些用于SASS / Compass的goto mixins

SCSS/SASS 文件不适用于电子 nuxt