CSS – 如何使导航栏链接的活动背景颜色宽于文本

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

CSS - How to make nav bar link active background color wider than the text

问题

I'm working on a navbar for CSS and I've gotten it working the way I want however the active state shows the background color that has the same width as the text itself. I feel like this is basic but I've been searching around for a couple of hours and have not found any answers to this. Ideally, I'd like the background color to have a little bit more width than the text itself. When I use the width property in css, it seems like it hardcodes the width and pays no attention to the width of the text.

I'm using react and bootstrap.

It currently looks like the image below. As you can see the background color is the same width as the navlink's text "about".

CSS – 如何使导航栏链接的活动背景颜色宽于文本

how can I make it have a little bit of breathing room like in the image below? In this example, there is space between the ends of the text and the background color.

Any and all help/direction is appreciated.

CSS – 如何使导航栏链接的活动背景颜色宽于文本

Here is my js for the nav item:

import React from "react";
import "./NaviBar3.css";

function NaviBar3() {
  return (
    <div className="NaviBarDiv sticky-top">
      <nav className="d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom">
        <a
          href="/"
          className="d-flex align-items-center mb-3 mx-5 me-md-auto text-light text-decoration-none"
          data-to-scrollspy-id="home"
        >
          <span className="fs-2">CM</span>
        </a>

        <a
          href="/"
          className="nav-link fs-5"
          data-to-scrollspy-id="about"
        >
          About
        </a>
        <a
          href="#projects"
          className="nav-link fs-5"
          data-to-scrollspy-id="projects"
        >
          Projects
        </a>
      </nav>
    </div>
  );
}

export default NaviBar3;

Here is my css code:

.NaviBarDiv {
  background-color: #282c34;
}
.active-scroll-spy {
  height: 4ex;
  background-image: linear-gradient(135deg, #f34079 40%, #fc894d);
  border: none;
  border-radius: 5px;
  font-weight: bold;
  color: white;
  cursor: pointer;
}
.nav-link {
  margin-left: 1em;
  margin-right: 1em;
  display: flex;
  justify-content: center;
  align-items: center;
}
英文:

I'm working on a navbar for CSS and I've gotten it working the way I want however the active state shows the background color that has the same width as the text itself. I feel like this is basic but I've been searching around for a couple of hours and have not found any answers to this. Ideally, I'd like the background color to have a little bit more width than the text itself. When I use the width property in css, it seems like it hardcodes the width and pays no attention to the width of the text.

I'm using react and bootstrap.

It currently looks like the image below. As you can see the background color is the same width as the navlink's text "about".

CSS – 如何使导航栏链接的活动背景颜色宽于文本

how can I make it have a little bit of breathing room like in the image below? In this example, there is space between the ends of the text and the background color.

Any and all help/direction is appreciated.

CSS – 如何使导航栏链接的活动背景颜色宽于文本

Here is my js for the nav item:

import React from &quot;react&quot;;
import &quot;./NaviBar3.css&quot;;

function NaviBar3() {
  return (
    &lt;div className=&quot;NaviBarDiv sticky-top&quot;&gt;
      &lt;nav className=&quot;d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom&quot;&gt;
        &lt;a
          href=&quot;/&quot;
          className=&quot;d-flex align-items-center mb-3 mx-5 me-md-auto text-light text-decoration-none&quot;
          data-to-scrollspy-id=&quot;home&quot;
        &gt;
          &lt;span className=&quot;fs-2&quot;&gt;CM&lt;/span&gt;
        &lt;/a&gt;

        &lt;a
          href=&quot;/&quot;
          className=&quot;nav-link fs-5&quot;
          data-to-scrollspy-id=&quot;about&quot;
        &gt;
          About
        &lt;/a&gt;
        &lt;a
          href=&quot;#projects&quot;
          className=&quot;nav-link fs-5&quot;
          data-to-scrollspy-id=&quot;projects&quot;
        &gt;
          Projects
        &lt;/a&gt;
      &lt;/nav&gt;
    &lt;/div&gt;
  );
}

export default NaviBar3;

Here is my css code:

.NaviBarDiv {

  background-color: #282c34;
}
.active-scroll-spy {
  height: 4ex;
  background-image: linear-gradient(135deg, #f34079 40%, #fc894d);
  border: none;
  border-radius: 5px;
  font-weight: bold;
  color: white;
  cursor: pointer;
}
.nav-link {
  margin-left: 1em;
  margin-right: 1em;
  display: flex;
  justify-content: center;
  align-items: center;
}

答案1

得分: 1

尝试在CSS中使用 "padding":

.nav-link {
  padding-left: 1em;
  padding-right: 1em;
  display: flex;
  justify-content: center;
  align-items: center;
}
英文:

try use to "padding" in css

.nav-link {
  padding-left: 1em;
  padding-right: 1em;
  display: flex;
  justify-content: center;
  align-items: center;
}

答案2

得分: 0

只需将.active-scroll-spy类添加填充:

.active-scroll-spy {
  height: 4ex;
  background-image: linear-gradient(135deg, #f34079 40%, #fc894d);
  border: none;
  border-radius: 5px;
  font-weight: bold;
  color: white;
  cursor: pointer;
  padding: 0.5em 1em; /* 在左侧和右侧添加填充 */
}
英文:

Just add padding to .active-scroll-spy class

.active-scroll-spy {
  height: 4ex;
  background-image: linear-gradient(135deg, #f34079 40%, #fc894d);
  border: none;
  border-radius: 5px;
  font-weight: bold;
  color: white;
  cursor: pointer;
  padding: 0.5em 1em; /* Adding padding to left and right sides */
}

答案3

得分: 0

以下是代码部分的翻译:

@Mohammad Hasibul Hasan的回答启发了我。虽然他的回答没有奏效,但我开始尝试使用Bootstrap的填充,这个方法有效:

import React from "react";
import "./NaviBar3.css";

function NaviBar3() {
  return (
    <div className="NaviBarDiv sticky-top">
      {/* <nav className="d-flex flex-wrap justify-content-center border-bottom"> */}
      <nav className="d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom">
        <a
          href="/"
          className="d-flex align-items-ldent mb-3 mx-5 me-md-auto text-light text-decoration-none"
          data-to-scrollspy-id="home"
        >
          <span className="fs-2">CM</span>
        </a>

        <a
          href="/"
          className="nav-link fs-5 px-3 pt-1"
          data-to-scrollspy-id="about"
        >
          About
        </a>
        <a
          href="#projects"
          className="nav-link fs-5 px-3 pt-1"
          data-to-scrollspy-id="projects"
        >
          Projects
        </a>
      </nav>
    </div>
  );
}

export default NaviBar3;

它现在看起来像这样:

CSS – 如何使导航栏链接的活动背景颜色宽于文本

英文:

@Mohammad Hasibul Hasan's answer led me to this. Although his answer didn't work, I started messing around with the padding with bootstrap and this worked:

import React from &quot;react&quot;;
import &quot;./NaviBar3.css&quot;;

function NaviBar3() {
  return (
    &lt;div className=&quot;NaviBarDiv sticky-top&quot;&gt;
      {/* &lt;nav className=&quot;d-flex flex-wrap justify-content-center border-bottom&quot;&gt; */}
      &lt;nav className=&quot;d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom&quot;&gt;
        &lt;a
          href=&quot;/&quot;
          className=&quot;d-flex align-items-center mb-3 mx-5 me-md-auto text-light text-decoration-none&quot;
          data-to-scrollspy-id=&quot;home&quot;
        &gt;
          &lt;span className=&quot;fs-2&quot;&gt;CM&lt;/span&gt;
        &lt;/a&gt;

        &lt;a
          href=&quot;/&quot;
          className=&quot;nav-link fs-5 px-3 pt-1&quot;
          data-to-scrollspy-id=&quot;about&quot;
        &gt;
          About
        &lt;/a&gt;
        &lt;a
          href=&quot;#projects&quot;
          className=&quot;nav-link fs-5 px-3 pt-1&quot;
          data-to-scrollspy-id=&quot;projects&quot;
        &gt;
          Projects
        &lt;/a&gt;
      &lt;/nav&gt;
    &lt;/div&gt;
  );
}

export default NaviBar3;

It now looks like this:
CSS – 如何使导航栏链接的活动背景颜色宽于文本

答案4

得分: -1

你可以在你的活动链接上添加内边距:

.active-scroll-spy {
  height: 4ex;
  background-image: linear-gradient(135deg, #f34079 40%, #fc894d);
  border: none;
  border-radius: 5px;
  font-weight: bold;
  color: white;
  cursor: pointer;
  padding: 1em;
}

或者甚至在类名中使用Bootstrap的内边距,像这样:

className="d-flex active-scroll-spy p-4 align-items-center mb-3 mx-5 me-md-auto text-light text-decoration-none"
英文:

You can add padding on your active link:

.active-scroll-spy {
  height: 4ex;
  background-image: linear-gradient(135deg, #f34079 40%, #fc894d);
  border: none;
  border-radius: 5px;
  font-weight: bold;
  color: white;
  cursor: pointer;
  padding: 1em;
}

or even use bootstraps padding in classname like this:

className=&quot;d-flex active-scroll-spy p-4 align-items-center mb-3 mx-5 me-md-auto text-light text-decoration-none&quot;

huangapple
  • 本文由 发表于 2023年2月27日 13:25:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577013.html
匿名

发表评论

匿名网友

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

确定