在 CSS 中创建一个十字形状

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17359787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 23:21:05  来源:igfitidea点击:

Create a cross shape in CSS

cssshapescss-shapes

提问by user1937021

IS it possible, I know all the following shapes are possible in this link:

是否可能,我知道此链接中可能出现以下所有形状:

http://css-tricks.com/examples/ShapesOfCSS/

http://css-tricks.com/examples/ShapesOfCSS/

but cross must be possible too. When I say cross I mean like this:

但交叉也必须是可能的。当我说十字架时,我的意思是这样的:

enter image description here

在此处输入图片说明

回答by Fabrizio Calderan

You could achieve something like this with pseudoelements only:

你可以只用伪元素来实现这样的事情:

http://jsbin.com/upiyoc/1/edit

http://jsbin.com/upiyoc/1/edit

#cross {
   width: 100px;
   height: 100px;
   position: relative;
}

#cross:before, #cross:after {
  content: "";
  position: absolute;
  z-index: -1;
  background: #d00;
}

#cross:before {
  left: 50%;
  width: 30%;
  margin-left: -15%;
  height: 100%;
}

#cross:after {
  top: 50%;
  height: 30%;
  margin-top: -15%;
  width: 100%;
}

The size of the cross will proportionally scale, according to the widthand heightof the #crosselement

根据元素的widthheight,十字的大小将按比例缩放#cross



Update: another solution (using less code) could simply involve multiple linear-gradients (without pseudolements) e.g.

更新:另一个解决方案(使用更少的代码)可以简单地涉及多个线性梯度(没有伪元素)例如

http://codepen.io/anon/pen/zxwgPo

http://codepen.io/anon/pen/zxwgPo

#cross {
  width: 100px;
  height: 100px;

  background: linear-gradient(to bottom, transparent 35%, 
                                         #d00 35%, 
                                         #d00 65%,  
                                         transparent 65%),

              linear-gradient(to right, transparent 35%, 
                                         #d00 35%, 
                                         #d00 65%,  
                                         transparent 65%),
}

回答by Jerska

Of course it is. You just have to use two elements : See http://jsfiddle.net/92XTx/2/

当然是这样。你只需要使用两个元素:见http://jsfiddle.net/92XTx/2/

The enclosing div is relatively positioned so that both children can be absolutely positioned.

封闭的 div 被relative定位,以便两个孩子都可以绝对定位。

#cross {
    position: relative;
    width: 150px;
    height: 150px;
}

Here they are both absolutely positioned:

在这里,它们都是绝对定位的:

#cross div {
    position: absolute;
    background: red;
}

to make them superpose.

使它们重叠。

And then create your shapes:

然后创建你的形状:

.cross-vertical {
    left: 33%;
    width: 33%;
    height: 100%;
}

.cross-horizontal {
    top: 33%;   
    width: 100%;
    height: 33%;
}

回答by che-azeh

Because all the answers I see here look either lengthy or vendor-prefix-dependent,

因为我在这里看到的所有答案看起来都很冗长或依赖于供应商前缀,

#cross { 
  background: red; 
  height: 100px; 
  position: relative; 
  left: 50px;
  width: 20px; 
} 
#cross:after { 
  background: red; 
  content: ""; 
  height: 20px; 
  left: -40px; 
  position: absolute; 
  top: 40px; 
  width: 100px; 
}
<div id="cross"></div>

回答by Danield

This can be done with a regular '+' plus character together with a text-stroke

这可以通过常规的“+”加号字符和 text-stroke

DEMO(Webkit,Android only)

演示Webkit,仅限Android

div {
  font-size: 80px;
  -webkit-text-stroke: 20px red;
  display: inline-block;
  padding: 0 20px;
}
<div>+</div>

回答by Ajain Vivek

CSS Transform can be easily used to achieve plus shape

CSS Transform 可以轻松实现加号形状

.close {
  position: absolute;
  right: 10px;
  top: 6px;
  width: 20px;
  height: 20px;
  opacity: 0.3;
}
.cross:before, .cross:after {
  position: absolute;
  left: 15px;
  content: ' ';
  height: 21px;
  width: 2px;
  background-color: #333;
}
.cross:before {
  transform: rotate(0deg);
}
.cross:after {
  transform: rotate(-90deg);
}