尝试学习“制作导航栏”的方法

huangapple go评论74阅读模式
英文:

Trying to learn the "making of navigation bar"

问题

我阅读了一些文章,但无法清楚理解它们。
我想让我的导航栏具备一些特点,

  1. 导航栏的背景颜色不应该在上面和两侧留有空隙
  2. 我希望元素位于中间,但彼此之间有一些间隔。
#nav_bar {
  background-color: aliceblue;
  padding: 14px 16px;
  width: 100%;
}

有哪些制作良好的导航栏的不同方法?

我已经尝试阅读来自许多网站的各种文章。

英文:

I read some articles but couldn't understand them clearly.
There are some things I want my navigation bar to have,

  1. The background color of the navigation bar shouldn't have a gap above and on the sides
    尝试学习“制作导航栏”的方法
  2. I want the elements to be in center but with a some gap between each other.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#nav_bar {
  background-color: aliceblue;
  padding: 14px 16px;
  width: 100%;
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;main.css&quot;&gt;
  &lt;title&gt;Product Landing Page&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;nav id=&quot;nav_bar&quot; class=&quot;nav_class&quot;&gt;
    &lt;a href=&quot;#welcome&quot;&gt;Home&lt;/a&gt;
    &lt;a href=&quot;#product-info&quot;&gt;Products&lt;/a&gt;
    &lt;a href=&quot;#product-pricing&quot;&gt;Pricings&lt;/a&gt;
    &lt;a href=&quot;#about&quot;&gt;About&lt;/a&gt;
    &lt;a href=&quot;#contact&quot;&gt;Contact Us&lt;/a&gt;
  &lt;/nav&gt;

  &lt;section class=&quot;welcome&quot;&gt;
    &lt;h1&gt;Hello&lt;/h1&gt;
  &lt;/section&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

What are different ways to make a good navigation bar?

I have tried to read various articles from many websitA

答案1

得分: 0

#nav_bar {
background-color: aliceblue;
padding: 14px 16px;
width: 100%;
display: flex;
justify-content: center;
column-gap: 30px;
}

i think this work

英文:
#nav_bar {
background-color: aliceblue;
padding: 14px 16px;
width: 100%;
display: flex;
justify-content: center;
column-gap: 30px;

}

i think this work

答案2

得分: 0

额外的空间来自body元素的默认边距,您可以将其重置为0:

body {
  margin: 0;
}

对于导航,您可以使用flex来居中以及在每个项目之间添加一些间距:

#nav_bar {
  background-color: aliceblue;
  padding: 14px 16px;
  width: 100%;
  display: flex;
  justify-content: space-evenly;
}

以下是应用上述调整的示例:

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
body {
  margin: 0;
}

#nav_bar {
  background-color: aliceblue;
  padding: 14px 16px;
  width: 100%;
  display: flex;
  justify-content: space-evenly;
}
<!-- language: lang-html -->
<nav id="nav_bar" class="nav_class">
  <a href="#welcome">Home</a>
  <a href="#product-info">Products</a>
  <a href="#product-pricing">Pricings</a>
  <a href="#about">About</a>
  <a href="#contact">Contact Us</a>
</nav>

<section class="welcome">
  <h1>Hello</h1>
</section>
<!-- end snippet -->
英文:

The extra space is from body element's default margins, you can reset it to 0:

body {
  margin: 0;
}

For the navigation, you can use flex to center it as well as giving some space between each item:

#nav_bar {
  background-color: aliceblue;
  padding: 14px 16px;
  width: 100%;
  display: flex;
  justify-content: space-evenly;
}

Here is an example with the above tweaks:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  margin: 0;
}

#nav_bar {
  background-color: aliceblue;
  padding: 14px 16px;
  width: 100%;
  display: flex;
  justify-content: space-evenly;
}

<!-- language: lang-html -->

&lt;nav id=&quot;nav_bar&quot; class=&quot;nav_class&quot;&gt;
  &lt;a href=&quot;#welcome&quot;&gt;Home&lt;/a&gt;
  &lt;a href=&quot;#product-info&quot;&gt;Products&lt;/a&gt;
  &lt;a href=&quot;#product-pricing&quot;&gt;Pricings&lt;/a&gt;
  &lt;a href=&quot;#about&quot;&gt;About&lt;/a&gt;
  &lt;a href=&quot;#contact&quot;&gt;Contact Us&lt;/a&gt;
&lt;/nav&gt;

&lt;section class=&quot;welcome&quot;&gt;
  &lt;h1&gt;Hello&lt;/h1&gt;
&lt;/section&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月9日 17:02:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定