在 CSS 中制作加号

新手上路,请多包涵

我有下面的 CSS 代码,它给出了 + 符号,但与设计不匹配,基本上它需要很薄。见片段和 codpen

 .plus {
  position:relative;
  border: 1px dotted white;
  width: 3px;
  height: 3px;
  background-color: black;
  box-sizing: border-box;
  transform: scale(11);
}
 <div class="plus"></div>

应该看起来像下面的符号: CSS 中的加号

任何其他样式对我来说也很好,但应该看起来像快照。

原文由 Sharath 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 523
2 个回答

我们可以使用如下的一个渐变来实现这一点:

 .plus {
  --b: 4px; /* the thickness */
  width: 40px; /* the size */
  aspect-ratio: 1;
  border: 10px solid #000; /* the outer space */
  background:
    conic-gradient(from 90deg at var(--b) var(--b),#000 90deg,#fff 0)
    calc(100% + var(--b)/2) calc(100% + var(--b)/2)/
    calc(50%  + var(--b))   calc(50%  + var(--b));
  display: inline-block;
}

.alt {
  border: none;
  margin: 10px;
  background:
    conic-gradient(from 90deg at var(--b) var(--b),#fff 90deg,#000 0)
    calc(100% + var(--b)/2) calc(100% + var(--b)/2)/
    calc(50%  + var(--b))   calc(50%  + var(--b));
}
.radius {
  border-radius: 50%;
}
 <div class="plus">
</div>

<div class="plus alt">
</div>

<div class="plus radius">
</div>

旧答案

使用如下多个背景:

 .plus {
  display:inline-block;
  width:50px;
  height:50px;

  background:
    linear-gradient(#fff 0 0),
    linear-gradient(#fff 0 0),
    #000;
  background-position:center;
  background-size: 50% 2px,2px 50%; /*thickness = 2px, length = 50% (25px)*/
  background-repeat:no-repeat;
}

.alt {
  background:
    linear-gradient(#000 0 0),
    linear-gradient(#000 0 0);
  background-position:center;
  background-size: 50% 2px,2px 50%; /*thickness = 2px, length = 50% (25px)*/
  background-repeat:no-repeat;
}
.radius {
  border-radius:50%;
}
 <div class="plus">
</div>

<div class="plus alt">
</div>

<div class="plus radius">
</div>

加号 CSS

这是透明的:

 .plus {
  width:50px;
  height:50px;
  display:inline-block;

  background:
    linear-gradient(#000 0 0) top left,
    linear-gradient(#000 0 0) top right,
    linear-gradient(#000 0 0) bottom left,
    linear-gradient(#000 0 0) bottom right;
  background-size: calc(50% - 1px) calc(50% - 1px); /*thickness = 2px (2*1px) */
  background-repeat:no-repeat;
  border:10px solid #000; /*length = 30px (50px - 2x10px) */
  box-sizing:border-box;
}

.radius {
  border-radius:50%;
}

body {
 background:pink;
}
 <div class="plus">
</div>

<div class="plus radius">
</div>

加号与 CSS

我们可以添加 CSS 变量来轻松控制整体形状:

 .plus {
  --t:2px;   /* Thickness */
  --l:40px;  /* size of the symbol */
  --s:10px;  /* space around the symbol */
  --c1:#fff; /* Plus color*/
  --c2:#000; /* background color*/

  display:inline-block;
  width:var(--l);
  height:var(--l);
  padding:var(--s);
  box-sizing:border-box; /*Remove this if you don't want space to be included in the size*/

  background:
    linear-gradient(var(--c1) 0 0) content-box,
    linear-gradient(var(--c1) 0 0) content-box,
    var(--c2);
  background-position:center;
  background-size: 100% var(--t),var(--t) 100%;
  background-repeat:no-repeat;
}

.radius {
  border-radius:50%;
}
 <div class="plus"></div>
<div class="plus" style="--l:35px;--t:3px;--c2:green"></div>
<div class="plus" style="--l:50px;--t:1px;--s:5px;--c1:red;"></div>
<div class="plus" style="--l:35px;--t:5px;--s:0px;--c1:blue;--c2:orange;"></div>

<br>
<div class="plus radius"></div>
<div class="plus radius" style="--l:35px;--t:3px;--c2:green"></div>
<div class="plus radius" style="--l:50px;--t:1px;--s:5px;--c1:red;"></div>
<div class="plus radius" style="--l:35px;--t:5px;--s:0px;--c1:blue;--c2:orange;"></div>

用 CSS 加号

原文由 Temani Afif 发布,翻译遵循 CC BY-SA 4.0 许可协议

我建议使用伪元素之前和之后来实现这一点。

基本上我只将 div 用作黑色背景,将前元素用作垂直线,将后元素用作水平线。

 .plus {
  position: relative;
  width:20px;
  height:20px;
  background:#000;
}

.plus:before,
.plus:after {
  content: "";
  position:absolute;
  background:#fff;
}

/* the vertical line */
.plus:before {
  left:50%;
  top:4px; /* this defines how much black "border" there should be */
  bottom:4px;
  width:2px;
  transform:translateX(-50%);
}

/* the horizontal line */
.plus:after {
  top:50%;
  left:4px;
  right:4px;
  height:2px;
  transform:translateY(-50%);
}

这是一个完整的例子: https ://codepen.io/Fitzi/pen/zbMBVw

原文由 Fitzi 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏