html - 如何使用 CSS 和 HTML 使导航栏居中

标签 html css navbar

我已经创建了一个导航栏,但我不知道如何将其置于网页中央。请告知,我已将链接附加到底部的 fiddle 。不知道还要写什么,因为堆栈溢出希望我在发布此内容之前提供更多详细信息。希望您能尽快回答我的问题!

HTML:

<!DOCTYPE html>
<html>

<head>
     <link rel="stylesheet" href="styles.css">
</head>

<body>

    <header>
        <navbar>
            <div class="topnav">
                <a class="active" href="#home">Home</a>
                <a href="#news">News</a>
                <a href="#contact">Contact</a>
                <a href="#about">About</a>
            </div>
        </navbar>
    </header>

    <div class="row">
        <p>Test text</p>
    </div>

</body>
</html>

CSS

.row {
    max-width: 1140px;
    background-color: green;
    margin: auto;
    width: 80%;
}

body {
    margin: 0;
}

/* Add a black background color to the top navigation */
.topnav {
    background-color: #333;
    overflow: hidden;
}



/* Style the links inside the navigation bar */
.topnav a {
    float: left;
    color: #f2f2f2;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
}

/* Change the color of links on hover */
.topnav a:hover {
    background-color: #ddd;
    color: black;
}

/* Add a color to the active/current link */
.topnav a.active {
    background-color: #4CAF50;
    color: white;
}

https://jsfiddle.net/delboy1881/725ncud3/1/

最佳答案

给你,

https://jsfiddle.net/90m2m0uh/

.topnav a {
    /* dont use this */
    /* float:left */
}

请注意“ float ”CSS

The float CSS property specifies that an element should be placed along the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the web page, though still remaining a part of the flow (in contrast to absolute positioning).

关于html - 如何使用 CSS 和 HTML 使导航栏居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48999759/

相关文章:

html - 带有静态 html 的 gh-pages 存在时无法访问文件

javascript - 图像作为单选按钮

html - 水平导航栏 : How to center text and set colours

html - 让一组按钮填满父 div 的整个宽度

javascript - 使用猫头鹰轮播在触摸设备上滑动时禁用垂直滚动

javascript - 带有 AND OR 条件的 jquery 多个选择器

html - div 行为上的颜色叠加

jquery - 数据表不滚动

java - 解析jsoup url android

html - 居中 Navbar Bootstrap 4