Home  >  Article  >  Web Front-end  >  css如何实现span在div中水平居中_html/css_WEB-ITnose

css如何实现span在div中水平居中_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:29:462322browse

css如何实现span在div中水平居中:
本章节介绍一下如何将一个span设置在div中水平居中,下面就通过代码实例介绍一下如何实现此效果。
很多朋友可能会认为对span元素施加margin:0px auto就可以将span元素水平居中,其实这是错误的,因为span是内联元素,使用此方式无法实现居中效果,代码实例如下:

<!DOCTYPE html>     <html>     <head>     <meta charset=" utf-8">     <meta name="author" content="http://www.softwhy.com/" />     <title>蚂蚁部落</title> <style type="text/css">.box{  width:200px;  height:100px;  background:green;}span{  background:red;  margin:0px auto;}</style></head><body><div class="box">  <span>蚂蚁部落</span></div></body></html>

以上代码并不能够实现span元素的居中效果,代码修改如下:

<!DOCTYPE html>     <html>     <head>     <meta charset=" utf-8">     <meta name="author" content="http://www.softwhy.com/" />     <title>蚂蚁部落</title> <style type="text/css">.box{  width:200px;  height:100px;  background:green;  text-align:center;}span{  background:red;}</style></head><body><div class="box">  <span>蚂蚁部落</span></div></body></html>

以上代码实现了我们的要求,只要使用text-align:center就可以实现我们的要求,因为span是内联元素。

更多内容可以参阅:http://www.softwhy.com/divcss/

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn