在styled-components div内绘制一个十字架?

huangapple go评论75阅读模式
英文:

Drawing a cross inside a styled-components div?

问题

我尝试在我样式化的 div 内部画一个十字。我正在使用 styled-components。

到目前为止,我的代码如下:

export const StyledCell = styled.div
width: auto;
background: rgba(${props => props.color}, 0.1);
border: ${props => (props.type === 0 ? '0px solid' : '4px solid')};
border-bottom-color: rgba(${props => props.color}, 0.5);
border-right-color: rgba(${props => props.color}, 1);
border-top-color: rgba(${props => props.color}, 1);
border-left-color: rgba(${props => props.color}, 0.7);
position: relative;


我尝试添加了以下内容:

:after{
content:"";
position:absolute;
border-top:1px solid red;
width:40px;
transform: rotate(45deg);
transform-origin: 0% 0%;
}


这对一个对角线来说效果相当不错,但我不太确定如何处理另一个对角线,因为我的最初想法是改变角度,但似乎得到了一个奇怪位置的对角线。
英文:

Im trying to draw a cross inside a div i styled. I'm using styled-components.

So far my code is as follows:

export const StyledCell = styled.div`
    width: auto;
    background: rgba(${props => props.color}, 0.1);
    border: ${props => (props.type === 0 ? '0px solid' : '4px solid')};
    border-bottom-color: rgba(${props => props.color}, 0.5);
    border-right-color: rgba(${props => props.color}, 1);
    border-top-color: rgba(${props => props.color}, 1);
    border-left-color: rgba(${props => props.color}, 0.7);
    position: relative;
`

i have tried adding:

:after{
  content:"";
  position:absolute;
  border-top:1px solid red;
  width:40px;
  transform: rotate(45deg);
  transform-origin: 0% 0%;
}

to it which works pretty well for one diagonal, but i'm not quite sure how to do the other one as my initial thought was to just change the angle but that seemed to come up with a weirdly placed diagonal

答案1

得分: 0

以下是您要翻译的内容:

另一种选择是使用 `linear-gradient()` 来生成这些线条;因为它支持多个渐变,我们可以这样做:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

    div {
      margin: 2rem;
      width: 20rem;
      height: 20rem;
      position: relative;

      /* 画一个渐变,它是:
         从透明到比中点少1像素,
         然后红色持续几个像素,
         然后再次变为透明
      */

      /* 首先从左下角到右上角 ... */
      background: linear-gradient(to top right, 
        transparent calc(50% - 1px), 
        red calc(50% - 1px) calc(50% + 1px), 
        transparent calc(50% + 1px)),
        
        /* ... 然后从左上角到右下角 */
        linear-gradient(to bottom right, 
          transparent calc(50% - 1px), 
          red calc(50% - 1px) calc(50% + 1px), 
          transparent calc(50% + 1px));     
    }


<!-- language: lang-html -->

    <div></div>

<!-- end snippet -->
英文:

An alternative option is to use linear-gradient() to produce the lines; as this supports multiple gradients, we can do this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

div {
  margin: 2rem;
  width: 20rem;
  height: 20rem;
  position: relative;

  /* draw a gradient which is:
     transparent up to 1px less than the midpoint,
     then red for a couple of pixels,
     then transparent again
  */

  /* first from the bottom left corner to the top right ... */
  background: linear-gradient(to top right, 
    transparent calc(50% - 1px), 
    red calc(50% - 1px) calc(50% + 1px), 
    transparent calc(50% + 1px)),
    
    /* ... then from the top left corner to the bottom right */
    linear-gradient(to bottom right, 
      transparent calc(50% - 1px), 
      red calc(50% - 1px) calc(50% + 1px), 
      transparent calc(50% + 1px));     
}

<!-- language: lang-html -->

&lt;div&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月13日 21:02:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定