[Sass] LEVEL 7: RESPONSIVE -- Ex
NESTED MEDIA QUERIES
Our stylesheet contains a responsive breakpoint with some alterations to .factory
. Let's clean up the media query a bit by nesting it inside .factory
.
.factory { width: 100%; } @media (min-width: 960px) { .factory { width: percentage(600px / 960px); } }
Answer:
.factory { width: 100%; @media (min-width: 960px) { width: percentage(600px / 960px); } }
RESPOND-TO I
Media query handling has now been moved to a respond-to
mixin, which has a fixed breakpoint for when desktop
is passed as an argument. Use @else if
to add a second condition that checks for an argument of tablet
. tablet
should output a media query with a min-width
of 768px
containing the @content
block.
@mixin respond-to($media) { @if $media == desktop { @media (min-width: 960px) { @content; } } } .factory { width: 100%; @include respond-to(desktop) { width: percentage(600px / 960px); } @include respond-to(tablet) { width: 50%; } }
Answer:
@mixin respond-to($media) { @if $media == desktop { @media (min-width: 960px) { @content; } }@else if $media == tablet{ @media (min-width: 768px){ @content; } } } .factory { width: 100%; @include respond-to(desktop) { width: percentage(600px / 960px); } @include respond-to(tablet) { width: 50%; } }
RESPOND-TO II
Let's make our respond-to
more flexible by allowing it to accept query values rather than fixed breakpoints. Rewrite the mixin to output a media query with a min-width
of the $query
argument, which contains the @content
block.
@mixin respond-to($query) { } .factory { width: 100%; @include respond-to(960px) { width: percentage(600px / 960px); } @include respond-to(768px) { width: 50%; } }
Answer:
@mixin respond-to($query) { @media (min-width: $query){ @content; } } .factory { width: 100%; @include respond-to(960px) { width: percentage(600px / 960px); } @include respond-to(768px) { width: 50%; } }
RESPOND-TO III
We should further expand our mixin to allow for queries beyond min-width
. Add $type
as the first argument to respond-to
, which will be used in place of min-width
when passed in.
@mixin respond-to($query) { @media (min-width: $query) { @content; } } .factory { width: 100%; @include respond-to(min-width, 960px) { width: percentage(600px / 960px); } @include respond-to(max-width, 768px) { width: 50%; } }
Answer:
@mixin respond-to($type, $query) { @media ($type: $query) { @content; } } .factory { width: 100%; @include respond-to(min-width, 960px) { width: percentage(600px / 960px); } @include respond-to(max-width, 768px) { width: 50%; } }
RESPOND-TO IV
We've noticed that our most common call to respond-to
passes in min-width
for $type
and960px
for $query
: create default values out of each so that our simplified @include
works correctly.
@mixin respond-to($type, $query) { @media ($type: $query) { @content; } } .factory { width: 100%; @include respond-to { width: percentage(600px / 960px); } @include respond-to(max-width, 768px) { width: 50%; } }
Answer:
@mixin respond-to($type: min-width, $query: 960px) { @media ($type: $query) { @content; } } .factory { width: 100%; @include respond-to { width: percentage(600px / 960px); } @include respond-to(max-width, 768px) { width: 50%; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具