【问题标题】:Why has the CSS margin no effect?为什么 CSS 边距没有效果?
【发布时间】:2018-02-24 09:22:32
【问题描述】:

瞄准:段落和“链接按钮”之间的垂直间隙。

我似乎可以将我的链接按钮的边距更改为任何数字,但它似乎没有任何效果。 (我可以通过在我的“段落”样式中添加一个底部边距来解决这个问题,但它仍然困扰着我,我不知道为什么我不明白为什么下面的代码不起作用。)

有人知道我在这里缺少什么吗?

 <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">  
    <style>
    .link {
        padding: 15px 30px;
        border: solid 2px black;
        margin-top: 600px; <!-- Why does this have no effect?-->
     }
     </style>
</head>


<body>
     <p>
​This is a paragraph text.
    </p>
    <a class="link" href="google.com">Google</a>
</body>

【问题讨论】:

  • an &lt;a&gt; 元素是一个 inline 元素,这不受顶部边距的影响,这就是将 display 更改为 inline-block 的原因。
  • 非常感谢!

标签: css margin margins


【解决方案1】:

试试这个

CSS:

.link {
    padding: 15px 30px;
    border: solid 2px black;
    margin-top: 600px; 
    display:inline-block;
 }
【解决方案2】:

请在.linkcss 中添加display:inline-block

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">  
    <style>
    .link {
        padding: 15px 30px;
        border: solid 2px black;
        margin-top: 600px;
        display:inline-block;
     }
     </style>
</head>


<body>
     <p>
​This is a paragraph text.
    </p>
    <a class="link" href="google.com">Google</a>
</body>

【讨论】:

    猜你喜欢
    • 2013-02-19
    • 2021-04-17
    • 2013-01-18
    • 2010-11-25
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    相关资源
    最近更新 更多