Overriding NavLink in React Bootstrap with custom CSS doesn't work as expected

Recently diving into the world of React and Bootstrap, I decided to experiment with them by using react-bootstrap. It was mentioned that a simple way to override Bootstrap CSS is to apply your own CSS on top of it. So here's what I've been working on:

https://i.sstatic.net/ua0DD.png

First up, my App.js:

import React from 'react';
import Nav from 'react-bootstrap/Nav';
import Navbar from 'react-bootstrap/Navbar';
import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
import "bootstrap/dist/css/bootstrap.css";
import './App.css';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';


function App() {
  return (
    <Router>
      <Container fluid>
      <Navbar bg="dark" expand="lg" id="navbar-parent">
        <Navbar.Brand href="#home" id="navigation-bar">TURLS</Navbar.Brand>
        <Navbar.Toggle aria-controls="basic-navbar-nav"/>
        <Navbar.Collapse id="basic-navbar-nav">
          <Nav className="mr-auto" variant="pills">
            <Nav.Link href="#home" className="whte">Home</Nav.Link>
            <Nav.Link href="#api" className="whte">API</Nav.Link>
          </Nav>
        </Navbar.Collapse>
      </Navbar>

      </Container>
    </Router>
    );
}

export default App;

Next, we have the App.css:

#navigation-bar {
  border-style: solid;
  padding: 15px;
  border-radius: 50px;
  font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
  font-weight: 900;
  color: white;
  border-width: 5px;
  font-size: 1.5em;
}

#navigation-bar:hover {
  color: #3399ff;
}

.whte {
  color: white; 
}

While I successfully customized Nav.Brand, I encountered difficulty overriding styles for Nav.Link using my custom class .whte. My goal was to tweak the colors, fonts, pill sizes, and background colors, but I'm unsure how to proceed.

I resorted to using !important for text color changes, but I'm aware this isn't the best practice. Upon inspecting elements, I discovered that Nav.Links were inheriting their colors from certain Bootstrap classes despite my custom class .whte.

.navbar-light .navbar-nav .nav-link {
    color: rgba(0,0,0,.5);
}

It turns out the blue-colored nav-pills were associated with these classes:

.navbar-light .navbar-nav .active>.nav-link, .navbar-light .navbar-nav .nav-link.active, .navbar-light .navbar-nav .nav-link.show, .navbar-light .navbar-nav .show>.nav-link {
    color: rgba(0,0,0,.9);
}

and

.nav-pills .nav-link.active, .nav-pills .show>.nav-link {
    color: #fff;
    background-color: #007bff;
}

I'm puzzled about which classes to target for achieving my desired effects. While I attempted to add my own CSS implementations, they didn't yield the expected results.

So, what's the typical approach to customizing Bootstrap CSS? Should I override every associated CSS class for a Bootstrap element? Or should I assign an ID to each element and customize them individually?

If my question seems basic or unclear, I appreciate any guidance on the recommended methods for modifying or customizing Bootstrap CSS within React Bootstrap.

Thank you for your assistance.

Answer №1

Your logical reasoning is quite impressive. It is common knowledge that An inline CSS will be faster to load if the content size downloads quicker than your server's response time for an external CSS file request (taking into account DNS time, server latency, etc).

You can implement inline css here using style={{/*your styles */ }}.

You may want to refer to some helpful answers mentioned in this SO thread.

Answer №2

After conducting a thorough search, you have successfully found the right solution. It is true that using "important" on bootstrap elements is not recommended, but sometimes there is no other way to customize these elements without modifying their own CSS files. One workaround is to apply inline CSS like this "{{style : //like this}}" or create your own custom bootstrap classes. If you prefer working with libraries that offer different styles, material-ui is a great alternative. In summary, you have 4 options available:

1: Inline Css :

<Navbar.Brand href="#home" id="navigation-bar" style = {{color:'white'}}>TURLS</Navbar.Brand>

2: Custom Css :

 #navigation-bar {
       color : white !important;
    }

3: Modifying Bootstrap's own CSS files :

//Edit the downloaded Bootstrap files in nodemodules to make changes to navbar-related elements.

4: Using react-use classes alongside Bootstrap :

<Navbar.Brand className = "text-light" href="#home" id="navigation-bar">TURLS</Navbar.Brand>

Best of luck with your customization efforts! :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating consistently sized rows of Bootstrap columns - with either equal heights or equal spacing between each row

It would be great if bootstrap had a built-in feature where it automatically assigns the wrapper div of any item with a height based on the height of the largest div. In this example on JSFiddle, you can see that I have different heights for the video-ite ...

How to Transfer Information Between Two React Components

I recently started learning React and I'm not sure how to approach this task. Basically, I have a list of cars and when the user clicks on each car, it should display detailed information about that specific car in a full-page slide. This is my curr ...

Express.js automatically sets timeouts for requests to static files

I've been facing this issue for several weeks now - after launching, Express functions properly for a few hours and then suddenly stops serving static files. The situation is as follows: GET / 304 153ms GET /js/bootstrap.min.js 200 120000ms GET /img/ ...

Positioning the bottom of a two-column layout using CSS

I am working on a 2 column layout that should have a height of 100% of the window size. The left side will contain an image taking up 90% of the window size positioned at the bottom of the wrapper. On the right side, there will be text occupying the top 50 ...

Toggle the visibility of dropdown list items in different ways: Add or Remove, or Show or

Currently, I am working on a project that involves 3 drop down lists for security questions. I have implemented javascript functionality that triggers an alert when a selection is made in any of the drop down boxes. My challenge now is figuring out how t ...

Tips for implementing validation in JavaScript

I'm brand new to learning Javascript. Recently, I created a template for a login page and it's working perfectly fine. However, I am struggling with setting up validation and navigation. My goal is to redirect the user to another page if their us ...

Creating a single Vuetify expansion panel: A step-by-step guide

Is there a way to modify the Vuetify expansion panel so that only one panel can be open at a time? Currently, all panels can be closed which is causing issues. I would like the last opened panel to remain open. I also want to prevent closing the currently ...

Error: Couldn't locate Next.js - TypeScript module

I encountered an error with the image, but I am unsure of the reason behind it. Additionally, the directory is included in the second image. https://i.sstatic.net/knUzH.png import Link from 'next/link'; import { useState } from 'react' ...

Tips for using jQuery to load JSON data from a backing bean within a customized JSF component

What is the most efficient way in JSF 2.2 to load JSON data from a backing bean into a JavaScript program that runs in a browser? Currently, I am transitioning a messy, iframed visjs network graph to JSF 2.2 - Primefaces 6.1. All our custom UI components ...

React Component: Issue with toggle functionality in child component not meeting expectations

I'm currently working on a checkbox user interface, where I need to pass a toggle function down to a list component. The issue I'm facing is that while I can check the checkbox successfully, I'm unable to uncheck it for some reason. Below i ...

Assigning a class to every alternate item

Currently, I am in the process of compiling a list of products. To enhance the layout, I would like to assign a specific class to every other element. This class could be :before, :after, or simply .My-clas <div class="product-single"></div> ...

Displaying data within SVG elements with React.js inputs

For a test, I need to display values from one column of inputs into an SVG in another column. While I'm comfortable working with the input column, I'm unsure about how to show these values in the SVG. If anyone has any tips or helpful links on ...

Is there a glitch preventing the connection between HTML and CSS in Notepad++ even though the link is correct?

I have encountered a puzzling situation that has left me scratching my head in confusion. Despite being a rookie, I've had experience with linking CSS files before and can't understand why it's not working this time around. This issue is per ...

What is the functionality of Mongoose for handling multiple updates?

Array; arr=[ { id: [ '5e6e9b0668fcbc7bce2097ac', '5e6e9b0e68fcbc7bce2097af' ], color: [ 'a', 'b' ] } ] Models; const varyant = Models.varyant function; Promise.all( arr.map((item)=>{ return var ...

What is the best way to incorporate both the left and right menus with the main content

My goal is to incorporate a left and right menu alongside a main feed in the center. However, I'm encountering an issue where the left and right menus are scrolling with the scroll bar. Ideally, I would like the main feed to scroll with the bar while ...

Can someone assist me in properly spacing out my divs for three distinct media queries?

Here is the link to the CodePen containing the code: https://codepen.io/gregelious/pen/zYmLGex The CodePen features a restaurant menu with 3 categories (divs) displayed as separate boxes. (For detailed instructions, visit: https://github.com/jhu-ep-cours ...

Encountering a syntax error when attempting to generate a div dynamically with jQuery

I am in the process of creating a view application form using Bootstrap after submitting a form. Initially, I created it utilizing two 'div' elements. Now, I am exploring how to dynamically generate a div upon clicking a button: <i> Sectio ...

What is the reason for not being able to locate the controller of the necessary directive within AngularJS?

A couple of angularjs directives were written, with one being nested inside the other. Here are the scripts for the directives: module.directive('foo', [ '$log', function($log) { return { restrict: 'E', r ...

I am unable to utilize folder paths in CSS within my React application

Having trouble setting a background image in CSS within my React app. When styling and setting the image from a React component, it works just fine. Can anyone provide assistance? This code snippet will work: const style={ width:'300px', ...

Vue-Routes is experiencing issues due to a template within one of the routes referencing the same ID

I encountered an issue while developing a Vue application with Vue-routes. One of the routes contains a function designed to modify the background colors of two divs based on the values entered in the respective input fields. However, I am facing two probl ...