2015-10-02 118 views
0

如何在CSS中創建交叉?我只需要一個十字的高度爲1像素寬度和20像素。我試過了這個問題的代碼「Create a cross shape in CSS」。但它讓我瘋狂,只有1條線。如何在CSS中創建交叉?

+0

使用Unicode字符。請參閱https://en.wikipedia.org/wiki/X_mark。 –

+0

人們需要停止這樣做。 CSS樣式的HTML元素。這不是一個繪圖程序。 – Rob

回答

0

此代碼應爲你做它:

<div class="cross"></div> 

<style> 
    .cross { 
     height: 20px; 
     width: 20px; 
     position: relative; 
     //-webkit-transform: rotate(45deg); // this will make the cross an X 
     //-moz-transform: rotate(45deg); // this will make the cross an X 
     //transform: rotate(45deg); // this will make the cross an X 
    } 
    .cross:after, .cross:before { 
     position: absolute; 
     content: ""; 
     background: red; 
    } 
    .cross:after { 
     top: 50%; 
     left: 0; 
     right: 0; 
     height: 1px; 
    } 
    .cross:before { 
     left: 50%; 
     top: 0; 
     bottom: 0; 
     width: 1px; 
    } 
<style> 
+0

爲了未來的用戶,請詳細說明如何解決提問者問題。歡迎來到棧溢出推薦閱讀[回覆] –

0

我個人用一個小coffeescrpit和CSS與這使其更好。

HTML

<span class="cross"></span> 

CSS3

@import "compass/css3"; 

body {background: #111;} 

$cross-width: 15px; 
$cross-thick: 1px; 

.cross{ 
    transform: rotateZ(45deg); 
} 

.cross, 
.cross:before, 
.cross:after { 
    position: absolute; 
    top: 50%; 
    left: 50%; 
    width: $cross_thick; 
    height: $cross_width; 
    background: #fafafa; 
} 

.cross:before, 
.cross:after { 
    content: ''; 
    display: inline-block; 
    position: absolute; 
    top: ($cross_width - $cross_thick)/ 2; 
    left: -($cross_width - $cross_thick)/ 2; 
    width: ($cross_width - $cross_thick)/ 2; 
    height: $cross_thick; 
} 

.cross:after { 
    left: $cross-thick; 
} 
+0

CoffeeScript與此有關嗎? –

+0

對不起,我寫錯了。 –

0

吉姆擊的溶液取得靈感,我修改它是更普遍的,並允許自定義厚度

CSS

.cross { 
    height: 30px; 
    width: 30px; 
    position: relative; 
    //-webkit-transform: rotate(45deg); // this will make the cross an X 
    //-moz-transform: rotate(45deg); // this will make the cross an X 
    //transform: rotate(45deg); // this will make the cross an X 
} 
.cross:after, .cross:before { 
    position: absolute; 
    content: ""; 
    background: red; 
} 
.cross:after { 
    top: 50%; 
    left: 0; 
    right: 0; 
    height: 10px; 
    margin-top:-5px; //half of heigth 
} 
.cross:before { 
    left: 50%; 
    top: 0; 
    bottom: 0; 
    width: 10px; 
    margin-left:-5px; //half of width 
} 

的關鍵是要設置兩個:before:after僞類的寬度和高度,然後應用它們的負餘量(適當的方向)等於一半的厚度

如果您使用的預處理器一樣都不能少,這樣可以優化:

LESS

@cross-size:30px; 
@cross-thickness:10px; 
@cross-color:red; 

.cross { 
    height:@cross-size; 
    width:@cross-size; 
    position: relative; 
    //-webkit-transform: rotate(45deg); // this will make the cross an X 
    //-moz-transform: rotate(45deg); // this will make the cross an X 
    //transform: rotate(45deg); // this will make the cross an X 
} 
.cross:after, .cross:before { 
    position: absolute; 
    content: ""; 
    background:@cross-color; 
} 
.cross:after { 
    top: 50%; 
    left: 0; 
    right: 0; 
    height:@cross-thickness; 
    margin-top:-(@cross-thickness/2); //half of heigth 
} 
.cross:before { 
    left: 50%; 
    top: 0; 
    bottom: 0; 
    width:@cross-thickness; 
    margin-left:-(@cross-thickness/2); //half of width 
} 
0

.cross:after { 
 
    content: "\271d" 
 
}
<span class="cross"></span>

瞧你有交叉,如果你想要不同然後檢查UNICODE表