有没有办法使这个弹性盒(以及其中的项目)居中显示?

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

Is there some way to make this flex box (and the items inside) centered?

问题

试着检查一下 li 元素是否有默认的内外边距,可能会导致额外的间距。

英文:

I am attempting to make the nav bar. For some reason, there is some padding (as noted by chrome's inspect (or so I think?)) But I can't seem to find it in the css file, any tips? I may be doing the flex boxes wrong, but maybe something else.

screen shot of the website (hosted locally)

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

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

.header-nav {
  background-color: #E1D7C6;
  display: inline-flex;
  justify-content: space-between;
  align-items: center;
  text-decoration: none;
  font-size: 20px;
}

nav {
  font-size: 15px;
  display: flex;
  justify-content: center;
}

li {
  display: block;
  background-color: #D5BDAF;
  text-decoration: none;
}

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

&lt;nav&gt;
  &lt;ul class=&quot;header-nav&quot;&gt;
    &lt;li&gt;&lt;a href=&quot;about.html&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;what-we-do.html&quot;&gt;What We Do&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;get-involved.html&quot;&gt;Get Involved&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;donate.html&quot;&gt;Donate&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;get-help.html&quot;&gt;Get Help&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/nav&gt;

<!-- end snippet -->

I tried to remove the other padding, spaces, and other text to see if something was causing it there. Any help/advice is appreciated! Thanks for the help!

答案1

得分: 0

你可以将 padding 设置为 0,以去掉 .header-nav 的右边 40px 的填充。

还应该使用 space-around,而不是 between,在导航栏的开头和结尾创建一些空间,并在 flex 项之间添加一个间隙(我使用了 10px)。

.header-nav {
  width: 500px;
  background-color: #E1D7C6;
  display: flex;
  padding: 0;
  gap: 10px;
  justify-content: space-around;
  align-items: center;
  text-decoration: none;
  font-size: 20px;
}

nav {
  font-size: 15px;
  display: flex;
  justify-content: center;
}

li {
  display: block;
  background-color: #D5BDAF;
  text-decoration: none;
}
<nav>
  <ul class="header-nav">
    <li><a href="about.html">About</a></li>
    <li><a href="what-we-do.html">What We Do</a></li>
    <li><a href="get-involved.html">Get Involved</a></li>
    <li><a href="donate.html">Donate</a></li>
    <li><a href="get-help.html">Get Help</a></li>
  </ul>
</nav>
英文:

You can set your padding to 0, to get rid the 40px right-padding of .header-nav.

You should also use space-around, not between to create a little space in the start and the end of the navbar, and add a gap(i used 10px) betweet the flex items

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

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

.header-nav {
  width: 500px;
  background-color: #E1D7C6;
  display: flex;
  padding: 0;
  gap: 10px;
  justify-content: space-around;
  align-items: center;
  text-decoration: none;
  font-size: 20px;
}

nav {
  font-size: 15px;
  display: flex;
  justify-content: center;
}

li {
  display: block;
  background-color: #D5BDAF;
  text-decoration: none;
}

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

&lt;nav&gt;
  &lt;ul class=&quot;header-nav&quot;&gt;
    &lt;li&gt;&lt;a href=&quot;about.html&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;what-we-do.html&quot;&gt;What We Do&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;get-involved.html&quot;&gt;Get Involved&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;donate.html&quot;&gt;Donate&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;get-help.html&quot;&gt;Get Help&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/nav&gt;

<!-- end snippet -->

Hope that helps

huangapple
  • 本文由 发表于 2023年4月17日 21:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035892.html
匿名

发表评论

匿名网友

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

确定