Margin-bottom for <a> link elements

坚强是说给别人听的谎言 提交于 2020-05-07 18:02:49

问题


I have a problem with margin-top/bottom on <a> elements - it doesn't seem to work.

This is the HTML code:

<div class="pages-link">
     <a href="#">1</a>
     <a href="#">2</a>
     <a href="#">3</a>
     ....
</div>

This is the CSS code:

.pages-link {
     margin:2em 0;
     word-spacing:.25em;
}

.pages-link a {
     background:#d7d7d7;
     border:1px solid #ccc;
     -moz-border-radius:3px;
     -webkit-border-radius:3px;
     -khtml-border-radius:3px;
     border-radius:3px;
     color:#333;
     padding:.3em .5em;
     text-decoration:none;
}

This is how the final result looks like. The problem is obvious, I want 5 or 10px of margin-bottom for the <a> elements, but the property doesn't get applied.

enter image description here

What am I missing?


回答1:


You need to add display: inline-block; to your anchor selector. Anchors are by definition inline elements and do not accept width, height, margin etc until they are defined as block level or inline-block elements.




回答2:


I think you're better of doing display:block; and float:left; because display:inline-block; is not supported in all browsers.



来源:https://stackoverflow.com/questions/11969427/margin-bottom-for-a-link-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!