如何在 HTML/CSS 中正确定位图标和导航栏?

问题描述 投票:0回答:1

我正在尝试复制 YouTube 教程中的网站设计。这是目标布局:

...这是我的样子:

主要问题是图标和导航栏的定位。浏览器栏和我的元素之间似乎缺少空间。谁能帮我实现正确的布局?

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: 'poppins', sans-serif;
}

.topbar {
  position: fixed;
  background: #fff;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.08);
  width: 100%;
  height: 60px;
  padding: 0 20px;
  display: grid;
  grid-template-columns: 2fr 10fr 0.4fr 1fr;
  align-items: center;
  z-index: 1;
}

.logo h2 {
  color: #df7f27;
}

.search {
  position: relative;
  width: 60%;
  justify-self: center;
}

.search input {
  width: 100%;
  height: 40px;
  padding: 0 40px;
  font-size: 16px;
  outline: none;
  border: none;
  border-radius: 10px;
  background: #f5f5f5;
}

.search i {
  position: absolute;
  right: 15px;
  top: 15px;
  cursor: pointer;
}

.user {
  position: relative;
  width: 50px;
  height: 50px;
}

.user img {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  object-fit: cover;
}
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="styles.css">
  <script defer src="https://use.fontawesome.com/releases/v5.15.4/js/all.js" integrity="sha384-rOA1PnstxnOBLzCLMcre8ybwbTmemjzdNlILg8O7z1lUkLXozs4DHonlDtnE7fpc" crossorigin="anonymous"></script>
  <title>pomodone.app</title>
</head>

<body>
  <div class="container">
    <div class="topbar">
      <div class="logo">
        <h2>Pomodone</h2>
      </div>
      <div class="search">
        <input type="text" id="search" placeholder="search here">
        <label for="search"><i class="fas fa-search"></i></label>
      </div>
      <i class="fas fa-bell"></i>
      <div class="user">
        <img src="img/user.jpg" alt="">
      </div>
    </div>
    <div class="sidebar"></div>
    <div class="main"></div>
  </div>
</body>

html css layout navigationbar
1个回答
1
投票

@Iomipac,您需要设置标签的位置。 请添加此样式。希望对你有帮助~。

.search >label {
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
        }

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: 'poppins', sans-serif;
}

.topbar {
  position: fixed;
  background: #fff;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.08);
  width: 100%;
  height: 60px;
  padding: 0 20px;
  display: grid;
  grid-template-columns: 2fr 10fr 0.4fr 1fr;
  align-items: center;
  z-index: 1;
}

.logo h2 {
  color: #df7f27;
}

.search {
  position: relative;
  width: 60%;
  justify-self: center;
}

.search input {
  width: 100%;
  height: 40px;
  padding: 0 40px;
  font-size: 16px;
  outline: none;
  border: none;
  border-radius: 10px;
  background: #f5f5f5;
}

.search>label {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
}

.search i {
  position: absolute;
  right: 15px;
  top: 15px;
  cursor: pointer;
}

.user {
  position: relative;
  width: 50px;
  height: 50px;
}

.user img {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  object-fit: cover;
}
<div class="container">
  <div class="topbar">
    <div class="logo">
      <h2>Pomodone</h2>
    </div>
    <div class="search">
      <input type="text" id="search" placeholder="search here">
      <label for="search">icon<i class="fas fa-search"></i></label>
    </div>
    <i class="fas fa-bell"></i>
    <div class="user">
      <img src="img/user.jpg" alt="">
    </div>
  </div>
  <div class="sidebar"></div>
  <div class="main"></div>
</div>

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