What is the process for customizing link and hover colors in Bootstrap 4?

I am struggling to change the link and hover color in a specific section of my design, especially since I am new to Bootstrap. Can someone guide me on how to achieve this in Bootstrap 4? Below is a simple code snippet I have attempted so far:

<div class="card" style="max-width: 320px;">
     <div class="card-header text-center"><h3>Navigation</h3></div>
          <div class="card-block">
               <ul class="nav navbar-nav" style="font-size: 1.50em;">
                   <li><a href="#" class="nav-link">Home</a></li>
                    <li><a href="#" class="nav-link">Documents</a></li>
                    <li><a href="#" class="nav-link">Videos</a></li>
                    <li><a href="#" class="nav-link">Gallery</a></li>
                    <li><a href="#" class="nav-link">Download</a></li>
                </ul>
           </div>                   
</div>

Answer №1

Bootstrap 4 now uses Sass (SCSS) for its CSS code compilation instead of Less. It comes with a grunt build chain for easier customization. The recommended way to customize Bootstrap is by using the default build chain.

  1. Start by downloading and unzipping the source code.
  2. Go to the bootstrap (bootstrap-4-dev) folder.
  3. Run npm install in your console.
  4. Execute grunt dist to recompile the CSS code.

To change colors, you can now edit either scss/bootstrap.scss or

scss/_variables.scss</code. When editing <code>scss/bootstrap.scss
, ensure to redefine the variables at the beginning of the file.

You can adjust the colors of .nav-link and nav-link:hover by changing the default color values for the a selectors in scss/bootstrap.scss:

$link-color:                 #f00; //red
$link-hover-color:           #0f0; //green

// Import core variables and mixins
@import "variables";
@import "mixins";
....

Keep in mind that the above changes will affect all links. To modify colors for specific elements like .nav .nav-link or .card .nav .nav-link, you'll need to compile CSS code with higher specificity. Avoid using !important declarations.

Note that Bootstrap is currently in an alpha state, so it's not recommended for production use. The variables to define colors for .nav-link may be introduced in future updates. Check https://github.com/twbs/bootstrap/issues/18630 for updates.

To customize the colors of all .nav .nav-links, add the following SCSS code at the end of scss/bootstrap.scss:

....
// Utility classes
@import "utilities";
.nav-link {
 color: #f00; //red

  @include hover-focus {
    color: #0f0; //green
  }  
}  

For adjusting the colors of .nav-links within .cards, create CSS code with higher specificity like this:

....
// Utility classes
@import "utilities";
.card .nav-link {
 color: #f00; //red

  @include hover-focus {
    color: #0f0; //green
  }  
}  

You can also add your custom CSS code at the end of the compiled bootstrap.css file to suit your requirements. Use higher specificity as needed.

To change colors for all links:

a {color: #f00;}
a:hover {color: #0f0;}

HTML:

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="bootstrap-4-dev/dist/css/bootstrap.css">
<style>
  a {color: #f00;}
  a:hover {color: #0f0;}
</style> 

For higher specificity:

.nav-link {color: #f00;}
.nav-link:hover {color: #0f0;}

Or specifically for .card .nav-link:

.card .nav-link {color: #f00;}
.card .nav-link:hover {color: #0f0;}    

Answer №2

To customize the appearance of bootstrap classes, create a custom CSS file such as mystyle.css and include it right after the bootstrap in the head section:

<!-- Bootstrap CSS -->
<link href="https://fonts.googleapis.com/css?family=Lato:400,700&display=swap"  rel="stylesheet">     
<link rel="stylesheet"  href="path/mystyle.css" type="text/css"/>

In the mystyle.css file, you can modify the styles as needed:

.navbar-light .navbar-nav .nav-link {
color: whitesmoke;
}
.navbar-light .navbar-nav .nav-link:hover {
color: whitesmoke;
}

Answer №3

One possible method to customize Bootstrap styles is by:

1. Implementing your own CSS modifications

Bootstrap's styles can be easily overridden by creating your own .css file with specific styles. For example, you can customize the navbar links by adding the following code to your CSS:

.navbar ul li a {
color:#fff;}
.navbar ul li a:hover {
color:#000;}

By doing this, you can achieve the desired look and feel for your website.

2. Analyzing and modifying Bootstrap's default styles

While it is possible to directly modify Bootstrap's CSS, it is not recommended unless absolutely necessary. It is more beneficial to create custom styles that overwrite Bootstrap's defaults for a more organized and maintainable codebase.

Answer №4

If you want to change just the color of a link or the < a > tag on hover, you can do so by adding a custom class like hover_black:

My links are normally white but turn black when hovered over

//CSS

.text_white          { color: white;  } 
a.hover_black:hover  { color: black !important;  } 

// HTML
<a class="text_white hover_black"> Link </a>

Answer №5

I am looking to make some specific changes to the link and hover color in my design.

I have a solution that can address all 3 changes using BS4 with sass in one go. If you have access to the .scss file in your custom project and can compile it, here is the recommended approach...

For this particular scenario, you can create a custom mixin like this:

@mixin my-variant($bgcolorOff, $borcolorOff, $bgcolorOn, $borcolorOn, $bgcolorAct, $borcolorAct, $txtcolorOff, $txtcolorOn, $txtsize, $txtalign){
    @include button-variant($bgcolorOff, $borcolorOff, $bgcolorOn, $borcolorOn, $bgcolorAct, $borcolorAct);
    color:#333; /*set a fallback color*/
    font-weight:normal; /*customize other attributes like text size, alignment, etc.*/
    text-transform:none; /*customize other attributes like text size, alignment, etc.*/
    text-decoration:none; /*customize other attributes like text size, alignment, etc.*/
    text-align:$txtalign; /*reference parameter values*/
}


Assuming you already have assigned hex colors to sass variables like this:

$m-color-red: #da291c;
$m-color-blue: #004c97;
..etc..

If so, you can call your mixin from any specific location in your sass file. Make sure to properly define your colors to create your custom variant.

  .m-variant {
      @include my-variant($m-color-white, $m-color-grey-light, $m-color-off-white, $m-color-grey-light, $m-color-blue, $m-color-grey-light, $m-color-grey-light, $m-color-grey-light, 1.200em, 'left');
   }


When creating buttons or anchor links, you can include the following in your element structures:

<a class="btn btn-sm m-3 m-variant ">my custom button</a>


By following these 4 simple steps, you can take complete control over your link and hover colors. Feel free to reuse or create as many variants as needed.

I hope this solution works for you.

Answer №6

Copy and paste the following into your stylesheet:

.nav-link {color:#FFFFFF;}
.nav-link:hover {color:#F00F00; text-decoration:none;}

Before using the !important tags, check for any conflicts. It's recommended to search for relevant classes and parent divs to avoid conflicts or rename the class if needed.

Answer №7

As a beginner, I discovered that Bootstrap 4 offers a handy class known as "nav-link" which can be used in the link element tag. While working on a dark, primary colored navbar, I noticed that the links were blending in with the background. Luckily, using this class solved the issue for me.

<ul class="navbar-nav">
    <li class="nav-item">
        <a href="/Results/Create" class="nav-link">Create New Request</a>
    </li>
</ul>

Answer №8

These CSS styles are perfect for my needs

.nav-pills .nav-link {
    color: #fff;
    cursor: default;
    background-color: #868686;
}

.nav-pills .nav-link.active {
    color: #000;
    cursor: default;
    background-color: #afafaf; 
    font-weight: bold;
}

Answer №9

For those seeking to maintain the hover color identical to the non-hover state, simply utilize the following variable:

$matching-hover-darken-percentage: 0;

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

Guide to positioning the layout inflater on the right side

I am facing an issue with the layout inflater where the inflated layout appears in the center of the page instead of the intended right corner placement. I have attempted to modify the gravity to right, but it did not resolve the problem. Below is a snipp ...

HTML tends to disregard the dimensions specified in the JavaScript file

I'm currently working on replicating an Etch-a-Sketch style drawing board where hovering over a single pixel with the mouse changes its color. However, I've hit a roadblock when it comes to drawing the board. The flexbox container doesn't se ...

How do I get rid of the navigation bar on my website?

After implementing Azure login in ASP.NET Core, I noticed that when logging out of the project, it redirects to the sign-in page where a navbar is displayed. How can I remove this navbar? <div class='page-topbar @ViewData["pagetopbar_class&quo ...

Can you please explain the purpose of this function?

I came across this code snippet on a website and I'm curious about its function and purpose. While I'm familiar with PHP, HTML, CSS, and JavaScript, I haven't had the chance to learn JQUERY and AJAX yet. Specifically, I'm interested in ...

Having trouble showing the image stored in the database on the webpage

I'm having trouble displaying my full image on my webpage from the database. Here is my code snippet: $con= mysqli_connect("localhost", "root", "", "project"); if(!$con) { die('not connected'); } ...

Embed a video within an image while ensuring it remains responsive on all devices

I have a picture that resembles something like this one My goal is to embed a video into it while maintaining its aspect ratio when the screen size is reduced. The responsive design should only affect the width since the image will be displayed in portrai ...

Troubleshooting techniques for resolving CSS issues with the video.js package in ReactJS

When using video.js in react to build a video player, I encountered an issue with the npm package not providing its inbuilt CSS. How can I add the inbuilt CSS of video.js? Instead of the control bar, I am getting something different than what is shown in t ...

How can Bootstrap's Collapse be activated without relying on buttons or anchor tags?

Is there a way to activate Bootstrap's Collapse feature when a user selects a Radio button, like this: <input type="radio" name="radios" value="More" data-toggle="collapse" href="#collapseExample"> <div class="collapse" id="collapseExample" ...

Steps for showing the text entered into the input box as soon as it is typed:

I am attempting to create a feature where text is displayed as soon as it is typed into an input box. Currently, my JavaScript function is not working at all. I simply want the function to display text when it is typed into or erased in the text boxes. & ...

The color of my Navbar remains stationary despite implementing Javascript for scroll effects

I am having trouble with the Javascript portion of my code, which I copied from a tutorial. The Navbar is not changing color when scrolled, and I'm not sure if the issue lies with the Javascript, the CSS, or if I need to add 'scrolled' to th ...

Looking to add elements to a specific div dynamically using jQuery? Let's explore how to insert comments seamlessly

I would like to implement a comment system that adds entered comments to a specific div. Here's the code I have so far: <ul class="comments"> <li> <a class="commenter_name" href="/">Dushyanth Lion</a> ...

In the Opera browser, the closing script tags seem to mysteriously vanish

I recently built a website using bootstrap and implemented the JQuery load function to merge separate HTML files into one for better organization. The site is currently live on github pages. However, I've encountered an issue specifically with Opera ...

"Emphasizing the Html.ActionLink menu for improved user navigation and

Currently, I am facing an issue with my menu. I want to clear previously visited links while keeping the current one styled as a:visited in CSS. Although I have attempted to achieve this, unfortunately, the code is not functioning properly. Here is what I ...

What is the best way to customize the look of the v-calendar component in Vue.js?

I've recently started a project that involves Vue.js, and I needed to create a datepicker for it. After some research, I opted to utilize the v-calendar package. The implementation of the component went smoothly and functioned as expected right out o ...

What could be causing my linear background to behave in this unusual manner?

Just when I thought styling my background with a simple line of code would be easy, I encountered an unexpected issue. Instead of a smooth linear-gradient from the top left to the bottom right, my background is showing rows of red and blue in between each ...

How to easily make a table in the center of the page using

I have a collection of 20 icons that I would like to arrange in a centered horizontal line of images. However, I am looking for a responsive solution and cannot use an HTML table. It needs to be mobile-friendly and wrap properly for small screens. If I wer ...

Transparent Box for Images - Overlay Effect

Is it possible to add text on top of an image with a background, such as the example shown below? <div id="slider" class="nivoSlider"> <img src="images/slide1.jpg"/> </div> ...

Text located incorrectly beside image within container box

Below is the HTML code that defines a box containing title text and an image. <div id="about"> <div id="title"> <h3><b>About</b></h3></div> <div id="text"><p>Text</p></div> <div id="img ...

Is it possible to align the last item in a MUI stack to the bottom?

https://i.stack.imgur.com/IsutM.png Currently, I am working on a modal component using MUI's Grid. My goal is to position the Button at the bottom of the modal. const styles = { modalBox: { position: 'absolute', top: &ap ...

Is it possible to set a unique identifier in Vue.js that persists even after the page is reloaded?

Currently, I'm working on a small project diving into Vue js - a Habittracker. Unfortunately, there is a bug in my code. When the page is reloaded and new habits are added, the function that should change the background doesn't work as expected. ...