保证金自动无效

问题描述 投票:-1回答:3

以下是代码段中的html和css:

    * {
        margin: 0;
        padding: 0;
    }
    
    html, body {
        height: 100%;
        width: 100%;
    }
    
    header {
        height: 100px;
        background-color: #35424a;
        border-bottom: #e8491d 5px solid;
    }
    
    h1 {
        float: left;
        margin-top: auto;
        margin-bottom: auto;
        color: white;
    }
    
    .acme {
        color: #e8491d;
    }
    
    nav {
        float: right;
        margin-top: auto;
        margin-bottom: auto;
    }
    
    li {
        display: inline;
        font-size: 150%;
        padding: 0px 20px;
    }
<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link rel="stylesheet" href="style.css">
        <title>Document</title>
    </head>
    <body>
       
        <header>
            <h1><span class="acme">Acme </span>Web Design</h1>
    
            <nav>
                <ul>
                    <li>HOME</li>
                    <li>ABOUT</li>
                    <li>SERVICES</li>
                </ul>
            </nav>
        </header>
        
    </body>
    </html>

问题是h1和导航我将顶部和底部边距设置为自动并且标题高度为100px(边框为105px),即使页边距自动仍然不起作用我试图添加

display: block;

但仍然没有发生任何事情

html css html5 css3
3个回答
3
投票

您只需将header CSS更新为:

header {
  display: flex;
  justify-content: space-between;
  width: 100%;  
  align-items: center;
  height: 100px;
  background-color: #35424a;
  border-bottom: #e8491d 5px solid;
}

这解决了问题


0
投票

- >请将以下代码附加到附件中。

* {
        margin: 0;
        padding: 0;
    }
    
    html, body {
        height: 100%;
        width: 100%;
    }
    
    header {
        height: 100px;
        background-color: #35424a;
        border-bottom: #e8491d 5px solid;
    }
    
    h1 {
        float: left;
        margin-top: auto;
        margin-bottom: auto;
        color: white;
    }
    
    .acme {
        color: #e8491d;
    }
    
    nav {
        float: right;
        margin-top: auto;
        margin-bottom: auto;
    }
    
    li {
        display: inline;
        font-size: 150%;
        padding: 0px 20px;
    }
    .display-table {
        display: table;
        height: 100%;
        width: 100%;
    }
    .vertical-content {
        display: table-cell;
        vertical-align: middle;
        height: 100%;
        width: 100%;
    }
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <title>Stack</title>
        <link href="css/style.css" rel="stylesheet">
    </head>
    <body> 
        <header>
            <div class="display-table">
                <div class="vertical-content">
                    <h1><span class="acme">Acme </span>Web Design</h1>
                    <nav>
                        <ul>
                            <li>HOME</li>
                            <li>ABOUT</li>
                            <li>SERVICES</li>
                        </ul>
                    </nav>
                </div>
             </div>
        </header>
    </body>
</html>

-1
投票

HTML不适用于margin-top或margin-bottom,你需要创建一个flex展示,请检查:https://css-tricks.com/snippets/css/a-guide-to-flexbox/

根据你的需要,你需要这样做:

display: flex;
align-items: center;

如果您需要flex的其他内容,请检查链接

© www.soinside.com 2019 - 2024. All rights reserved.