2012-02-24 60 views

回答

3

我想你只需要在你的CSS添加text-align: center

.example { 
    background: black; 
    height: 250px; 
    width: 500px; 
    text-align: center 
} 
.example:after { 
    content: "+"; 
    color: white; 
    font-size: 100px; 
} 

如果你也想垂直對齊方式:

.example { 
    background: black; 
    height: 250px; 
    width: 500px; 
    text-align: center; 
    display: table-cell; 
    vertical-align: middle; 
} 
.example:after { 
    content: "+"; 
    color: white; 
    font-size: 100px; 
} 
+0

你是否也想要垂直對齊? – 2012-02-24 03:14:16

+0

這不會將text-align:center應用於.example內容嗎? – raina77ow 2012-02-24 03:16:55

+0

是的,它會,但在jsfiddle – 2012-02-24 03:19:56

2

另一種方式做到這一點:

.example { ... position: relative; } 
.example:after { ... 
    position: absolute; 
    top: 50%; margin-top: -{50% of the element's height}; 
    left: 50%; margin-left: -{50% of the element's width}; 
}