Mixins 是 Sass (其实很多预处理语言都有) 中用来方便地复用代码的方法,你可以简单点理解成函数,返回的是一组 CSS 属性或代码。
@mixin border-radius($radius) { -webkit-border-radius: $radius; -moz-border-radius: $radius; -ms-border-radius: $radius; border-radius: $radius;}.box { @include border-radius(10px); }
上面的代码处理后,将得到下面 CSS 代码。
.box { -webkit-border-radius: 10px; -moz-border-radius: 10px; -ms-border-radius: 10px; border-radius: 10px;}
如果你对 Sass 不熟悉,可以先去 学习一番,让我们进入主题,介绍几个实用的 mixins。
Absolute Position
如果你的页面中经常用到 absolute 定位,你可以试试下面这个:
@mixin abs-pos ($top: auto, $right: auto, $bottom: auto, $left: auto) { top: $top; right: $right; bottom: $bottom; left: $left; position: absolute;}
使用
.abs { @include abs-pos(10px, 10px, 5px, 15px);}
产出
.abs { top: 10px; right: 10px; bottom: 5px; left: 15px; position: absolute;}
带有优雅降级地使用 rem 单位
熟悉 rem 单位的前端er是否遇到过不支持的浏览器? 你如何解决的呢?下面这个 mixin 我们加上 px 单位作 fallback:
@function calculateRem($size: 16) { $remSize: $size / 16px; @return $remSize * 1rem;}@mixin font-size($size) { font-size: $size; font-size: calculateRem($size);}
使用
p { @include font-size(14px)}
产出
p { font-size: 14px; font-size: 0.8rem;}
Clearfix
网上有很多 clearfix 的 hacks,下面这种比较常见,兼容到 IE6以上
@mixin clearfix() { &:before, &:after { content: ""; display: table; } &:after { clear: both; }}
使用
.container { @include clearfix();}
When-inside
个人比较中意的 mixin,常用在修改元素在不同状态下的属性。比如我们的H5页面会给当前页面加个 active类名,我们想让当前页面中的 foo对象执行动画。
/// Make a context based selector a little more friendly/// @author Hugo Giraudel/// @param {String} $context@mixin when-inside($context) { #{$context} & { @content; }}
使用
.foo { @include when-inside('.active') { animation: fadeIn 0.3s 1s forwards; }}
产出
.active .foo { animation: fadeIn 0.3s 1s forwards; }
方便的媒体查询
媒体查询再做响应式网站时候,非常方便。如果下面的代码满足不了你的需求的话,那么推荐你看之前的文章 《使用Sass之更高级的媒体查询》
$breakpoints: ( 'sm': 'only screen and (min-width: 480px)', 'md': 'only screen and ( min-width: 768px)', 'lg': 'only screen and ( min-width: 992px)') !default;@mixin respond-to($breakpoint) { $query: map-get($breakpoints, $breakpoint); @if not $query { @error 'No value found for `#{$breakpoint}`. Please make sure it is defined in `$breakpoints` map.'; } @media #{if(type-of($query) == 'string', unquote($query), inspect($query))} { @content; }}
用法
.foo { @include respond-to('sm') { padding-left: 20px; padding-right: 20px; }}
产出
@media only screen and (min-width: 480px) { .foo { padding-left: 20px; padding-right: 20px; } }
长阴影
这个mixin可能不常用,但是效果真的很酷~
@function makelongshadow($color) { $val: 0px 0px $color; @for $i from 1 through 200 { $val: #{$val}, #{$i}px #{$i}px #{$color}; } @return $val;}@mixin longshadow($color) { text-shadow: makelongshadow($color);}
使用
h1 { @include longshadow(darken($color, 5% ));}
效果
See the Pen Long Shadow Sass Mixinby Helkyle ( @HelKyle) on CodePen.

HTML不僅是網頁的骨架,更廣泛應用於多種領域:1.在網頁開發中,HTML定義頁面結構並結合CSS和JavaScript實現豐富界面。 2.在移動應用開發中,HTML5支持離線存儲和地理定位等功能。 3.在電子郵件和新聞通訊中,HTML提升郵件的格式和多媒體效果。 4.在遊戲開發中,HTML5的CanvasAPI用於創建2D和3D遊戲。

theroottaginanhtmldocumentis.servesasthetop-levellementThateNcapsulatesAllotherContent,確保properdocumentstrumentstrumentsureandbrowserparserparsing。

文章解釋說,HTML標籤是用於定義元素的語法標記,而元素是完整的單位,包括標籤和內容。他們一起工作以構建網頁。查拉克計數:159

本文討論了Lt; Head> &<身體> HTML中的標籤,它們對用戶體驗的影響以及SEO的影響。正確的結構增強了網站功能和搜索引擎優化。

本文討論了HTML標籤,和和關注其語義與表現用途及其對SEO和可訪問性的影響之間的差異。

文章討論了在HTML中指定字符,重點介紹了UTF-8。主要問題:確保正確顯示文本,防止亂七八糟的字符,並增強SEO和可訪問性。

本文討論了用於構建和造型Web內容的各種HTML格式標籤,強調了它們對文本外觀的影響以及語義標籤對可訪問性和SEO的重要性。

本文討論了HTML的“ ID”和“類”屬性之間的差異,重點是它們的獨特性,目的,CSS語法和特異性。它解釋了它們的使用如何影響網頁樣式和功能,並為


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。