【问题标题】:Why aren't my grid items aligned to the center?为什么我的网格项目没有与中心对齐?
【发布时间】:2021-09-23 16:21:07
【问题描述】:

我创建了这个导航栏并使用网格系统对其进行了对齐。但是导航网格的内容由于某种原因溢出到导航栏之外,并在它们应该在的位置下方和右侧。我删除了我能找到的任何边距和填充,但无济于事。导航栏的位置是固定的,内部内容不是固定的,但考虑到它们是同一网格中的项目,这应该不是问题。下面是代码:

:root{
    --main-color:#ffc40c;
    --secondary-color:lightyellow;
    --main-red:#d9001d;
    --secondary-red:#ff033e;
    --dark-color:#444;
    --light-color:#fafafa
}
body{
    font-family:"Source Code Pro",monospace;
    background-color:var(--light-color);
    color:var(--dark-color);
    margin-top:50px;
    margin-left:0;
    margin-right:0;
    margin-bottom:0;
    text-shadow:1px 1px 5px var(--light-color)
}
form{
    width:100vw;
    padding:1em
}
a{
    text-decoration:none
}
input{
    border:1px solid var(--main-color);
    margin:.75em;
    border-radius:.75em;
    padding:.75em 1em
}
.navbar{
    background-color:var(--main-color);
    position:fixed;
    display:grid;
    align-items:center;
    grid-template-areas:"searchbar close-button";
    top:0;
    left:0;
    height:30px;
    width:100%
}
.searchbar{
    background-color:var(--secondary-color);
    opacity:50%;
    width:calc(100vw - 80px);
    border-radius:1em;
    padding:1em
}
.close-button{
    display:grid;
    place-items:center;
    height:30px;
    width:30px;
    background-color:var(--light-color);
    color:var(--main-red);
    border:2px solid var(--main-red);
    border-radius:50%;
    font-weight:700
}
.close-button:hover{
    background-color:var(--main-red);
    color:var(--light-color);
    border:2px solid var(--light-color);
    transition-duration:.4s
}
.close-button:active{
    rotate:360deg;
    transition-duration:.5s
}
<form class="navbar">
      <input
        class="searchbar"
        type="search"
        id="name"
        name="searchbar"
        placeholder="Search here..."
      />
      <a href="#!" class="close-button">
        X
      </a>
    </form>

我如何把它带到顶部?

【问题讨论】:

    标签: html css navbar css-grid


    【解决方案1】:

    在导航栏类中添加margin: auto;,并从搜索栏类中删除填充

    :root{
        --main-color:#ffc40c;
        --secondary-color:lightyellow;
        --main-red:#d9001d;
        --secondary-red:#ff033e;
        --dark-color:#444;
        --light-color:#fafafa
    }
    body{
        font-family:"Source Code Pro",monospace;
        background-color:var(--light-color);
        color:var(--dark-color);
        margin-top:50px;
        margin-left:0;
        margin-right:0;
        margin-bottom:0;
        text-shadow:1px 1px 5px var(--light-color)
    }
    form{
        width:100vw;
        padding:1em;
    }
    a{
        text-decoration:none
    }
    input{
        border:1px solid var(--main-color);
        margin:.75em;
            padding:.75em 1em;
    
        border-radius:.75em;
        align-items: center;
    }
    .navbar{
        background-color:var(--main-color);
        position:fixed;
        display:grid;
        align-items:center;
        grid-gap: auto;
        grid-template-areas:"searchbar close-button";
        top:0;
        left:0;
        height:30px;
        width:100%
    }
    .searchbar{
        background-color:var(--secondary-color);
        opacity:50%;
        width:calc(100vw - 80px);
        border-radius:1em;
        margin: 0;
    }
    .close-button{
        display:grid;
        align-items:center;
        justify-content: center;
        height:30px;
        width:30px;
        background-color:var(--light-color);
        color:var(--main-red);
        border:2px solid var(--main-red);
        border-radius:50%;
        font-weight:700;
    }
    .close-button:hover{
        background-color:var(--main-red);
        color:var(--light-color);
        border:2px solid var(--light-color);
        transition-duration:.4s
    }
    .close-button:active{
        rotate:360deg;
        transition-duration:.5s
    }
    <form class="navbar">
          <input
            class="searchbar"
            type="search"
            id="name"
            name="searchbar"
            placeholder="Search here..."
          />
          <a href="#!" class="close-button">
            X
          </a>
        </form>

    【讨论】:

    • 我照你说的做了,并像在你的代码中一样在搜索栏中输入了零边距,它起作用了。
    • 但如果您在浏览器中使用检查功能,内容仍会稍微偏右
    • 全部内容是?
    • 您可以在关闭按钮上应用边距,请参阅我的更新
    • 只使用grid-gap 来分隔项目而不是margin
    猜你喜欢
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 2022-11-19
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    相关资源
    最近更新 更多