Create a cross shape in CSS

非 Y 不嫁゛ 提交于 2019-12-17 12:44:22

问题


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

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

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


回答1:


You could achieve something like this with pseudoelements only:

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 width and height of the #cross element


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

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%),
}



回答2:


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

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

#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%;
}



回答3:


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>



回答4:


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

DEMO (Webkit,Android only)

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



回答5:


CSS Transform can be easily used to achieve plus shape

.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);
}


来源:https://stackoverflow.com/questions/17359787/create-a-cross-shape-in-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!