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;
}
4
Answers
@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:
It now looks like this:
try use to "padding" in css
You can add padding on your active link:
or even use bootstraps padding in classname like this:
Just add padding to
.active-scroll-spy
class