為什麼padding和margin在span中不好用,paddingmarginspan

來源:互聯網
上載者:User

為什麼padding和margin在span中不好用,paddingmarginspan

為什麼padding和margin在span中不好用:
可能標題的問題並不被所有人認同,有的可能覺得padding和margin是好用的,而有的卻認為是不好用的。下面通過執行個體分別介紹一下兩個屬性在span中到底好不好用。
一.padding屬性:

<!DOCTYPE html><html><head><meta charset=" utf-8"><meta name="author" content="http://www.51texiao.cn/" /><title>CSS教程</title><style type="text/css">span{  border:1px solid red;}.first{  padding:20px;}.second{  padding-left:20px;  padding-right:20px;}</style></head><body>  <span class="first">螞蟻部落</span>   <span class="second">螞蟻部落</span></body></html>

從以上代碼可以看出第一個span設定的padding並沒有完全正確的得到渲染,而是上邊框無法顯示,可能在某些瀏覽器中,下邊框也不能顯示。第二個span設定的左右內邊距都是正常的。由此可以得出,預設狀態下,span的左右內邊距是好用的,上下內邊距可能產生問題。

二.margin屬性:

<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.51texiao.cn/" /><title>CSS教程</title><style type="text/css">div{  width:200px;  height:100px;  border:1px solid blue;}span{  border:1px solid red;}.first{  margin:20px;}.second{  margin-left:20px;  margin-right:20px;}</style></head><body>  <div><span class="first">螞蟻部落</span></div>  <div><span class="second">螞蟻部落</span></div></body></html>

其實margin和padding是一樣的,也是左右外邊距是有效,上下外邊距可能出現問題。解決這個問題的方法很簡單,就是讓span變為區塊層級元素即可。

原文地址是:http://www.51texiao.cn/div_cssjiaocheng/2015/0519/1889.html

最原始地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=4697

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.