在CSS中创建十字形状

use*_*021 14 css shapes css-shapes

是否有可能,我知道在这个链接中可以使用以下所有形状:

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

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

在此输入图像描述

fca*_*ran 40

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

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%;
}
Run Code Online (Sandbox Code Playgroud)

十字的大小将成比例地缩放,根据widthheight所述的#cross元件


更新:另一种解决方案(使用较少的代码)可能只涉及多个线性梯度(没有伪问题),例如

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%),
}
Run Code Online (Sandbox Code Playgroud)


Jer*_*ska 6

当然如此.您只需使用两个元素:请参阅http://jsfiddle.net/92XTx/2/

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

#cross {
    position: relative;
    width: 150px;
    height: 150px;
}
Run Code Online (Sandbox Code Playgroud)

在这里他们都绝对定位:

#cross div {
    position: absolute;
    background: red;
}
Run Code Online (Sandbox Code Playgroud)

使它们叠加.

然后创建你的形状:

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

.cross-horizontal {
    top: 33%;   
    width: 100%;
    height: 33%;
}
Run Code Online (Sandbox Code Playgroud)


che*_*zeh 5

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

#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; 
}
Run Code Online (Sandbox Code Playgroud)
<div id="cross"></div>
Run Code Online (Sandbox Code Playgroud)