我正在使用 styled-components 并想要定位到 Text
的第一个子元素,但是无法实现。
const Text = styled.p`
font-size: 12px;
&:first-child {
margin-bottom: 20px;
}
`;
... component
return(
<div>
<p>I am just regular text</p>
<p>Me too</p>
<Text>Hello Joe</Text> // this should have the margin bottom
<Text>Goodbye</Text >
</div>
)
p
标签内有一个span
,这会导致它不起作用吗?很抱歉之前没有包含这个信息。另外,当我使用你的代码时,所有内容都是蓝色的,所以首个子元素选择器似乎不起作用。 - peter flanaganp
标签。我已经更新了我的解决方案。希望能有所帮助 :) - Arun AK