在经历了传统 CSS 後,发现了一些 CSS 的缺点
.card {}
,假设是写在纯 css 档中,使用 import 'app.css' 的方式引用,在其他 component 的 html 同样具有 card 的class name,将会吃到这个相同的属性,并不能针对不同 component 有独特的 style 样式,会污染其他 component。使用 css 方式
//example.js
useEffect(()=>{
document.querySelect('.customer-container').style.setProperty('--current-height',
'200px')
},[])
<div classname="customer-container"></div>
//example.css
{
.customer-container{
height:var(--current-height);
}
}
//这边一个 height 就要埋一个变数,试想假设有 width 或其他变数,也会造成埋过多的 var 属性
使用 style-component 写法
<Wrapper width={width} height={height} />
</Wrapper>
const Wrapper = styled.div`
width: ${p => p.width};
height: ${p => p.height};
`;
// 这样的写法就乾净很多,而且可以带很多变数当成 props 到 Wrapper
CSS in JS 则不用考虑过多的命名
// 使用传统 css
// BEM 命名, 为了区别 nav 区块需要命名 nav__item, 命名状态需要 --active
<div class="nav">
<li class="nav__item nav__item--active"><a href="#about">About</a></li>
<li class="nav__item"><a href="#product">Product</a></li>
</div>
//使用 emotion
<div
css={css`
......
`}
>
<li
css={css`
....
`}
>
<a href="#about">About</a>
</li>
<li
css={css`
....
`}
>
<a href="#product">Product</a>
</li>
</div>
//在每个 element 都有属於自己的 css 样式,就不用过度命名
这时候我们就需要依靠 CSS in JS 来解决这些缺点,我们下一篇将介绍 emotion css 的使用。
https://blog.logrocket.com/5-things-you-can-do-css-in-js/
https://www.joshwcomeau.com/css/styled-components/
https://dev.to/rleija_/5-reasons-to-go-with-css-in-js-for-your-next-application-43m
https://blog.logrocket.com/5-things-you-can-do-css-in-js/
<<: [Day 16] 以 Programmatic 取代 Annotation 的方式撰写 OpenAPI 文件
>>: Swift纯Code之旅 Day11. 「TableView(3) - 实作Delegate & DataSource」
Alias Records 一种 record 类型, 让你将流量路由到 AWS resource...
第一部分(建立档案以及设定资料库) composer create-project laravel/...
运算子 今天这篇主要会介绍一些运算子, 不一定都是数学上的运算, 也有判断True或False的, ...
你的 WhatsApp Business 帐号有试过被 WhatsApp 封锁吗?相信有不少企业都试...
在这边我会一起讲解这一 part 里面的组件,由於 App Bar 的部分之前已先讲解这边就不再提及...