html - 如何使用 CSS 使按钮居中?

标签 html css button

我正在尝试向我的简单网页添加一个按钮。按钮本身看起来不错,但我不太确定如何放置它。我希望它居中并位于正文下方。它目前位于左侧。知道怎么做吗?

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

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="spiekermann.css">
<title></title>

<style>

    #logo {
        width: 100%;
        text-align: center;
        padding: 10em 0 0.2em 0;
        font-family: lato;
    }

    #sub {
        color: #fff;
        font-family: lato;
        text-align: center;
        word-spacing: 5em;
    }

    .button {
        background-color: #3b3d45;
        border: 6px solid #fff080;
        display: inline-block;
        cursor: pointer;
        color: #ffffff;
        font-family: Arial;
        font-size: 12px;
        padding: 15px 20px;
        text-decoration: none;
        margin: auto;
        text-align: center;
    }

    .button:hover {
        background-color: #707488;
    }
</style>
</head>

<body>
<div class id="logo">
    <h1>ERIK SPIEKERMANN</h1>
</div>
<div class id="sub">
    <p>Designer Typographer Entrepreneur </p>
</div>

<a href="#" class="button">Learn More</a>
</body>

</html>

如有任何帮助,我们将不胜感激。谢谢!

最佳答案

您不一定需要容器。尝试以下操作:

.button {
    background-color: #3b3d45;
    border: 6px solid #fff080;
    display: block;
    cursor: pointer;
    color: #ffffff;
    font-family: Arial;
    font-size: 12px;
    padding: 15px 20px;
    text-decoration: none;
    margin: auto;
    text-align: center;
    width: 62px;
}

自动边距不适用于行内 block 元素。

关于html - 如何使用 CSS 使按钮居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36731757/

相关文章:

html - 容器 div 在 Internet Explorer 11 中无法正确调整大小

css - 如何限制div的宽度?

html - 所有字体上升/下降的上方/下方的空间是否相同?

javascript - 上传图片后如何移动按钮?

javascript - 按钮类型 ="button"在 JavaScript 中意味着什么

javascript - 如何关注具有相同类的 img

jquery - 带有 jquery 和 css 的字体调整器

java - 在 Android 应用程序中单击按钮时没有任何反应

javascript - JavaScript 中的幻灯片无法正常工作

javascript - 如何创建将导航到 jquery 移动页面之外的页面的链接/按钮