英文:
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".
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.
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".
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.
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;
}
答案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;
它现在看起来像这样:
英文:
@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 "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-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 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;
答案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="d-flex active-scroll-spy p-4 align-items-center mb-3 mx-5 me-md-auto text-light text-decoration-none"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论