span margin is not working

来源:互联网 发布:同济大学软件研究生 编辑:程序博客网 时间:2024/04/30 19:18

Unlike div, p 1 which are Block Level elements which can take up margin on all sides,span cannot as it’s an Inline element which takes up margins horizontally only.

From the specification :

Margin properties specify the width of the margin area of a box. The ‘margin’ shorthand property sets the margin for all four sides while the other margin properties only set their respective side. These properties apply to all elements, but vertical margins will not have any effect on non-replaced inline elements.
Demo 1 (Vertical margin not applied as span is an inline element)

Solution? Make your span element, display: inline-block; or display: block;.

Demo 2

Would suggest you to use display: inline-block; as it will be inline as well as block. Making it block only will result your element to render on another line, as block level elements take 100% of horizontal space on the page, unless they are made inline-block or they are floated to left or right.

0 0
原创粉丝点击