Intelligent method for replacing hover and focus styles

I have customized the bootstrap pagination to remove background and border, but I find myself duplicating a lot of code to make it work.

.pagination > li > a, 
.pagination > li > a:hover, 
.pagination > li > a:focus, 
.pagination > li > span,
.pagination > li > span:hover,
.pagination > li > span:focus {
    background: none;
    border: none;
    color: #a8a9ab
}
    .pagination > li > a:hover,
    .pagination > li > a:focus,
    .pagination > li.active > a, 
    .pagination > li.active > a:hover,
    .pagination > li.active > a:focus,
    .pagination > li.active > span,
    .pagination > li.active > span:hover,
    .pagination > li.active > span:focus {
        background: none;
        border: none;
        color: #000
    }

Is there another way to achieve this without using !important or adding extra classes to the markup?

This is what I end up with when using !important:

.pagination > li > a,
.pagination > li > span {
    background: none !important;
    border: none !important;
    color: #a8a9ab !important;
}
    .pagination > li > a:hover,
    .pagination > li > a:focus,
    .pagination > li > span:hover,
    .pagination > li > span:focus,
    .pagination > li.active > a,
    .pagination > li.active > span {
        color: #000 !important;
    }

Here's a simple example: http://codepen.io/anon/pen/FpCEA

Answer №1

UPDATE : Upon further review, it appears that avoiding additional classes may not be feasible in order to streamline your CSS code without resorting to using !important. Alternatively, you could consider utilizing LESS/SASS as viable alternatives.


To simplify your styling for the .pagination class, you can introduce a new class:

<ul class="pagination custom">
    <li><a href="#">1</a></li>
    <li><a href="#">2</a></li>
    <li class="active"><a href="#" >3</a></li>
    <li><a href="#">4</a></li>
    <li><a href="#">5</a></li>
</ul>

This approach eliminates the need for using !important:

.pagination.custom > li > a, 
.pagination.custom > li > span {
    background: none;
    border: none;
    color: #a8a9ab
}
.pagination.custom > li > a:hover, 
.pagination.custom > li > a:focus, 
.pagination.custom > li > span:hover,
.pagination.custom > li > span:focus,
.pagination.custom > li.active > a,
.pagination.custom > li.active > span {
    background: none;
    border: none;
    color: #000
}

Check out the updated Codepen version here

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

Is it possible to shift the icon over to the right using CSS?

I'm currently working on a website project with Bootstrap and I'm facing an issue with the desktop icon placement. I've tried using float, but I can't seem to get it right for better visibility. Here's an image of how I want it to ...

The functionality of the Bootstrap carousel may be compromised when initialized through JavaScript

Why isn't the Bootstrap carousel working in the example below? It starts working when I paste it into .content <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script sr ...

What is the reason the BODY tag does not supersede the properties of the HTML tag?

After applying this css to my html file: html { background-color: red; } body { background-color: yellow; } The page's background actually turns out to be red instead of yellow. Since the body comes after html in the code, it should override t ...

Guide: Using jQueryUI's explode effect to animate an HTML element explosion

I'm having trouble getting the jQueryUI explode effect to work properly. I've tested it out on this jsfiddle, but the explosion effect doesn't seem to happen as expected - the element just disappears with no explosion animation. $('h1, ...

Discovering the tiniest value in every row within an HTML table with the help of PHP

I am working with a table that looks like this: 6xx 8xx 9xx 11xx 12xx 1 0.01 0.002 0.004 0.001 0.025 2 0.025 0.125 0.002 0.01 0.011 I need to highlight the smallest value in each row by making that cel ...

Tips for creating an array:

Currently, I am working on a project that involves using an array of objects. While I have a good understanding of what an array is and how to create an object, I am struggling with incorporating my student object into the array. In this project, each st ...

By simply clicking a button in a React component, I aim to alter the font style of the text

function makeTextBold() { const boldText = document.querySelector('.form-control'); boldText.style.fontWeight = 'bold'; setText(boldText); } When I click the button, it redirects me to a blank page in the browser. ...

Modify the appearance of a nested div using CSS hover on the main JSX container

Within the material-ui table component, I am dynamically mapping rows and columns. The className is set to clickableRow. The TableRow also includes a hover options div on the right side: const generateTableHoverOptions = () => { if (selected) { ...

The ion-item is refusing to be centered on the page

I'm having trouble centering an ion-item and it seems slightly off-center. The standard methods like text-align: center and adjusting padding in ion-content don't seem to be working for me. Can someone please assist? https://i.stack.imgur.com/QZ ...

Is it possible to only set style.marginLeft one time?

My navigation menu is positioned off-screen to the right, and I am able to hide it by adjusting the style.marginLeft property. However, when I try to reveal it by setting the property again, nothing happens. function navroll(state) { if(state == true) ...

What is the best way to ensure that all the divs within a grid maintain equal size even as the grid layout changes?

I have a grid of divs with dimensions of 960x960 pixels, each block is usually 56px x 56px in size. I want to adjust the size of the divs based on the changing number of rows and columns in the grid. Below is the jQuery code that I am using to dynamicall ...

Having trouble applying the alternate style sheet to handheld devices

For a single page, I have implemented two different style sheets. <link rel="stylesheet" href="css/lsharecomplete_mob.css" media="handheld" type="text/css" /> <link rel="stylesheet" href="css/lsharecomplete_dt.css" type="text/css" media="Screen" ...

Having trouble with Angular 5 build due to issues with adding the bootstrap css file?

I have developed an application using angular 5, integrated ng-bootstrap, and now trying to link the bootstrap css file from node_modules: ../node_modules/bootstrap/dist/css/bootstrap.min.css. However, upon running the ng build command, I encountered the f ...

Experience unique website functionalities across Windows/Android and Apple ecosystems

Apologies for any language errors in advance. I'm facing an issue where my website appears differently on Safari or Chrome on iOS/MacOS compared to Windows 10 or Android devices. If you observe the List of Receiving Countries in the images provided: ...

Maintain the visibility of the section while it is being hovered over

I have successfully created a slidedown function that utilizes data in links to execute actions on specific sections. The script works well, however, there is an issue when trying to access the content within the section. Whenever I hover over the link, th ...

javascript the unseen element becomes visible upon page loading

my website has the following HTML snippet: function getURLParameters() { var parameters = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) { parameters[key] = value; }); return param ...

calc() not supported on IE11

Why is IE11 unable to calculate this? The console shows it as invalid (highlighted in red) h6 { ... font-size: calc(calc((( (20 / 16) / 16 - 1 / 16) * (100vw - 20rem) / (992 / 16 - 20) + 1 / 16 * 1rem) * 16)); ... } However, the following code works ...

.addClass successfully adding classes to certain elements while encountering issues with others

Struggling with using the .addClass function on certain <div> tags. It seems to work fine on some, but not at all on others. I'm thinking that specific attributes within the class might be affecting it, such as position:relative; versus absolute ...

Animation doesn't apply to child components, but is successful when applied to the parent component

I am working on animating the width of a child component's div. Here is my simple code: export const OuterComponent = () => { const sidebarCtx = useContext(SidebarContext); const style = "w-[100px] h-[60px] transit ...

Load images in advance for hover effects on active selectors within the specified CSS file paths

Currently, I have opted not to utilize css sprites. While I acknowledge that it is the most efficient solution, time constraints prevent me from implementing it at this moment. I am seeking a method to preload all images for hover and active selectors. ...