【问题标题】:Remove unnessesary margin in mobile view删除移动视图中不必要的边距
【发布时间】:2019-10-11 19:35:35
【问题描述】:

我正在研究移动视图上的网站导航,一个纯 CSS 汉堡菜单,我的问题是标题(包括徽标和汉堡菜单)与我的导航下拉列表之间存在间隙。

/*
html5doctor.com Reset Stylesheet
v1.6.1
Last Updated: 2010-09-17
Author: Richard Clark - http://richclarkdesign.com
Twitter: @rich_clark
*/

html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var,
b, i,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure,
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
    margin:0;
    padding:0;
    border:0;
    outline:0;
    font-size:100%;
    vertical-align:baseline;
    background:transparent;
}

body {
    line-height:1;
}

article,aside,details,figcaption,figure,
footer,header,hgroup,menu,nav,section {
    display:block;
}

nav ul {
    list-style:none;
}

blockquote, q {
    quotes:none;
}

blockquote:before, blockquote:after,
q:before, q:after {
    content:'';
    content:none;
}

a {
    margin:0;
    padding:0;
    font-size:100%;
    vertical-align:baseline;
    background:transparent;
}

/* change colours to suit your needs */
ins {
    background-color:#ff9;
    color:#000;
    text-decoration:none;
}

/* change colours to suit your needs */
mark {
    background-color:#ff9;
    color:#000;
    font-style:italic;
    font-weight:bold;
}

del {
    text-decoration: line-through;
}

abbr[title], dfn[title] {
    border-bottom:1px dotted;
    cursor:help;
}

table {
    border-collapse:collapse;
    border-spacing:0;
}

/* change border colour to suit your needs */
hr {
    display:block;
    height:1px;
    border:0;  
    border-top:1px solid #cccccc;
    margin:1em 0;
    padding:0;
}

input, select {
    vertical-align:middle;
}

/*--------------MY CODE----------*/

/*--------------MY CODE----------*/

/*MOBILE VIEW*/
* {
    text-decoration: none;
    font-family: Catamaran, sans-serif;
}

header {
    background-color: #dbd3d34b;
    height: 100px;
    box-shadow: 1px 1px rgb(177, 175, 175);
    z-index: 1;
}

.logo {
    display: block;
    float: left;
    margin: 27px 0;
}

.logo img{
    width: 200px;
    height: 50px;
}

nav ul {
    overflow: hidden;
}
nav ul li a {
    display: block;
    background-color: #f2f2f2;
    font-family: Roboto, sans-serif;
    font-size: 16px;
    padding: 20px;
    font-weight: 400;
    color: #111;
    border-bottom: 1px solid rgb(8, 189, 53);
    position: relative;
    z-index: 1;
}
nav ul li a:hover {
    color: rgb(8, 189, 53);
}

/*-------HAMBURGER MENU-------------*/
nav ul {
   border: 1px solid red;
}
nav .menu {
    clear: both;
    max-height: 0;
    transition: max-height .2s ease-out;
}
nav .menu-icon {
    margin: 30px 0;
    padding: 20px 15px;
    position: relative;
    float: right;
}
nav .menu-icon .nav-icon {
    background-color: #111;
    display: block;
    height: 3px;
    width: 25px;
    position: relative;
    transition: background .2s ease-out;
}
nav .menu-icon .nav-icon:before {
    background-color: #111;
    content:"";
    height: 100%;
    width: 100%;
    position: absolute;
    transition: all .2s ease-out;
    top: 7px;
}
nav .menu-icon .nav-icon:after {
    background-color: #111;
    content:"";
    height: 100%;
    width: 100%;
    position: absolute;
    transition: all .2s ease-out;
    bottom: 7px;
}
.menu-btn {
    display: none;
}

nav .menu-btn:checked ~ .menu {
    max-height: 260px;  
}

nav .menu-btn:checked ~ .menu-icon .nav-icon {
  background-color:transparent;
}
nav .menu-btn:checked ~ .menu-icon .nav-icon:before {
  transform: rotate(-45deg);
  top:0;
}
nav .menu-btn:checked ~ .menu-icon .nav-icon:after {
    transform: rotate(45deg);
    top:0;
}
/*----END OF HAMBERGER MENU*/

/*---------DESKTOP DISPLAY*/
@media only screen and (min-width: 1000px) {
.menu-btn {
    display: none;
}
nav .menu {
    clear: none;
    max-height: none;
}
nav .menu-icon {
    display: none
}
.logo {
    display: block;
    float: left;
    margin: 4px 0;
}

.logo img{
    width: 380px;
    height: 86px;
}

nav {  
    float : right;
    width: 600px;
    height: 100px;
    margin-right: 80px;
}
nav ul {
    height: inherit;
    display: flex;
    flex-flow: row;
    justify-content: space-between;
    align-items: center; 
}

nav ul li a {
    font-size: 24px;
    font-weight: 600;
    color: #111;
    border-bottom: none;
}


.main {
    background-image: url("img/header.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center center;
    width: 100%;
    height: 400px;
    margin-top: 1px;

    display: flex;
    justify-content: center;
    align-items: center;
}

.main .header-text{
    width: 600px;
    text-align: center;
}

.main .header-text h1{
    font-size: 68px;
    color: #f2f2f2;
    text-shadow: 1px 1px #111, -0.5px -0.5px #111;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0 , maximum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type ="text/css" href="main-page.css">
    <title>Portfolio Michael</title>
</head>
<body>
    <header class="header">
        <div class="logo">
            <img src="img/logo.png" alt="Web Page Logo">
        </div>
        <nav>
            
        <input class="menu-btn" type="checkbox" id="menu-btn">
        <label class="menu-icon" for="menu-btn"><span class="nav-icon"></span></label> 
        
        <ul class="menu">
            <li><a href="#">Excercises</a></li>
            <li><a href="#">Projects</a></li>
            <li><a href="#">About</a></li>
        </ul>
        
        </nav>
    </header>

    <main>
        <section class="main">
            <div class="header-text">
                <h1>Hi! I'm Michael! And this is my Website</h1>
            </div>    
        </section>
    </main>
    
</body>
</html>
如果您运行此代码,您可以在移动视图中看到标题和我的主页之间有一个小间隙,我制作了一个红色边框以突出显示该间隙。

我尝试在“导航菜单”中添加负边距顶部和边距 0,但没有任何反应。

【问题讨论】:

标签: html css


【解决方案1】:

问题在于nav .menu

您可以通过在 CSS 中指定 0 的边距来解决此问题。

nav .menu {
  clear: both;
  max-height: 0;
  transition: max-height 0.2s ease-out;
  margin: 0;
}

【讨论】:

  • 我自己的截图是你想要的吗?如果是这样,那么事情的后端就没有发生正确的事情
【解决方案2】:

我不敢相信我解决了自己的问题,我只是想通过查看 Inspect 元素来分享我的标签,该标签具有一类菜单图标,我的边距在标题区域内重叠,导致额外空间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多