reactjs - 使用特异性级别为 1 的 styled-components 定位子元素
问题描述
场景是我有一个组件,即<BrandLogo />
使用底层<a>
元素进行样式设置。该组件还具有一个img
子元素。styled-components
有没有一种方法可以在具有特异性级别 1的帮助下为子元素创建样式。
我知道我可以直接使用styled-components
.
方法一:
import React from 'react';
import styled from 'styled-components';
const BrandLogo = styled.a`
color: #1a1c1c;
display:inline-block;
vertical-align:top;
img {
display:block;
}
`;
const App = () => {
return (<div style={{backgroundColor: '#e6e6e6'}}>
<BrandLogo href="/">
<img src="https://dcstatic.com/images/brandcrowd/logos/brandcrowd-logo-5d59400c52.svg" alt="BrandCrowd"/>
</BrandLogo>
</div>)
};
export default App;
将生成以下标记:
<div style="background-color: rgb(230, 230, 230);">
<a class="sc-bdVaJa gdDbnn" href="/"><img alt="BrandCrowd" src="https://dcstatic.com/images/brandcrowd/logos/brandcrowd-logo-5d59400c52.svg"></a>
</div>
但是img
元素的特异性是2级。
.gdDbnn img {
display: block;
}
方法二:
将元素创建img
为样式组件,但现在它被视为组件。是的,这种方法将创建具有 1 级特异性的样式。
但是是否有任何其他方法可以对具有特异性级别 1 的子组件进行样式设置。
解决方案
推荐阅读
- javascript - Angularjs 在函数后执行(承诺?)
- c# - 下一步是什么,我有代码,但我不知道下一步该怎么做?
- bash - bash:如何计算不同列的平均值?
- swift - 按下两次标签栏滚动到 UIViewController 的顶部
- google-cloud-firestore - Firebase 安全规则来限制除一条以外的所有路径的访问?
- python - 如何通过嵌套的for循环?
- templates - 构造函数中无法解析的两阶段模板化别名?
- android - onsavedinstancestate 似乎不支持双数组列表我有什么选择?
- python - 使用 Python Flask 和 jQuery 删除 Sqlite 数据库中的行
- python-3.x - 从集合列表中查找元素数量最少的集合