目标子元素样式化组件

28
我试图通过将其作为ImgGrid的子元素来定位ColorBox元素的第二个子元素(nth-child(2))并改变它的背景颜色,但似乎一直无法使其生效。我已经尝试了多种不同的元素组合,但是仍然没有看到任何效果。
如果我不尝试定位nth-child元素,则可以更改背景颜色。
const ImgGrid = styled.div`
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  ${ColorBox} {
    background: #ff00ff;
  }
`

我该如何定位 ColorBox 元素的第 nth-child(2) 个子元素?

import React from 'react'
import styled from 'styled-components'
// import Img from '../../atoms/Img'
import { array, bool } from 'prop-types'

const Wrap = styled.div`
  padding: 20px 0;
`

const BoxWrap = styled.div`
  position: relative;
  border: 1px solid #000;
  height: auto;
  padding: 20px;
`

const Title = styled.div`
  position: absolute;
  display: flex;
  align-items: center;
  top: -20px;
  left: 50%;
  -webkit-transform: translateX(-50%);
  transform: translateX(-50%);
  height: 40px;
  background: #f6f6f6;
  padding: 10px;
`

const BoxInner = styled.div`
  width: 100%;
  height: 100%;
`

const ColorBox = styled.div`
  height: 60px;
  width: 100%;
  background: #FFD700;
`

const ImgCell = styled.div`
  flex: 0 0 25%;
  padding: 5px;
`

const ImgGrid = styled.div`
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  &:nth-child(2) ${ColorBox} {
    background: #ff00ff;
  }
`

const ImgCellInner = styled.div`
  position: relative;
`

// const ImgText = styled.div`
//   position: absolute;
//   bottom: 0;
//   left: 0;
//   padding: 5px;
//   width: 100%;
//   background: rgba(0,0,0,0.7);
//   color: #fff;
// `


const ImgWrap = styled.div`
  width: 100%;
`

const ColorWrap = styled.div`

`

const Filter = ({ filterData, color }) => (
  <Wrap>
    <BoxWrap>
      <Title>
        For Women
      </Title>
      <BoxInner>
        <ImgGrid>
          {filterData.map(filter =>
            <ImgCell>
              <ImgCellInner>
                <ImgWrap>
                  {color &&
                  <ColorWrap>
                    <ColorBox />
                    {filter.text}
                  </ColorWrap>
                  }
                </ImgWrap>
              </ImgCellInner>
            </ImgCell>,
          )}
        </ImgGrid>
      </BoxInner>
    </BoxWrap>
  </Wrap>
)

/* eslint-disable*/
Filter.propTypes = {
  filterData: array,
  color: bool,
}

Filter.defaultProps = {
  filterData: array,
  color: bool,
}

export default Filter

将索引类型的prop传递给ColorBox组件,然后在styled-component中访问这个prop,这样做是否可行? - Sergii Shvager
如果那能运行,当然可以。 - tom harrison
3个回答

45

我可以假设你的代码没有发挥作用,因为&:nth-child(2)表示你选择的是ImgGrid本身作为第二个子元素。尝试将ImgCell指定为nth-child(2):

const ImgGrid = styled.div`
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  ${ImgCell}:nth-child(2) ${ColorBox} {
    background: #ff00ff;
  }
`

我不确定作用域,所以你可能需要在之前使用 &

const ImgGrid = styled.div`
      display: flex;
      flex-wrap: wrap;
      flex-direction: row;
      & ${ImgCell}:nth-child(2) ${ColorBox} {
        background: #ff00ff;
      }
    `

1
我如何在代码中的一系列<tr>中应用相同的内容?我尝试使用const CustomTbody = styled.tbodytr:nth-child(odd){className: 'odd'} - Tarun
@Tarun,我不太确定你的代码在做什么,因为 { } 内应该是 CSS,而 className: 'odd' 不是。 - Sergii Shvager
抱歉,我的错。我想用伪元素添加类。我混淆了SASS类名和styled components。据我从行业专家了解到,应该使用styled components来应用主题。 - Tarun

9

如果你在寻找如何选中所有子元素的问题上遇到困难,那么这里就是解决方法;

    const ParentDiv = styled.div`
       >*  {
           background: white;
           padding: 5px;
           };
            /* ...etc */
           
           `;

2
我曾经遇到过同样的问题。在父元素中使用:focus-within对我有用,也许能解决你的问题。

例如:

export const Parent = styled.div`
  display: flex;
  align-items: center;
  border: 1px solid #000;

  &:focus-within {
    border: 1px solid #2ADCC6;
  }
`;

export const Child = styled.input`
  flex: 1;
  border: none;
`;

文档:https://developer.mozilla.org/zh-CN/docs/Web/CSS/:focus-within


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接