"Exploring the World of Button Coloration

I'm struggling with customizing the colors of each of the 4 buttons that link to different tables using CSS. I want to assign a specific color to each button, like red for the first one and blue for the second.

Your assistance in this matter would be greatly appreciated. Thank you!

Link to demo: http://jsfiddle.net/LpLhP/8/

HTML:


        <a class="button" data-table="1" href="#">Slifer Level</a>
        
        <a class="button" data-table="2" href="#">Ra Level</a>
        
        <a class="button" data-table="3" href="#">Obelisk Level</a>
        
        <a class="button" data-table="4" href="#">Exodia Level</a>

        <table id="1">
            <thead>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Jill</td>
                    <td>Smith</td>
                    <td>50</td>
                </tr>
                ...
            </tbody>
        </table>
        ...
    

CSS:


        body{
            font-family: sans-serif;
            font-size: 16px;
            line-height: 1.5em;
            text-align: center;
            margin-top: 1.5rem;
        }
        
        a.button {
            background-color: #ed8c15;
            color: white;
            padding: 0.5rem;
            border-radius: 5px;
            text-decoration: none;
            // Add hover effect here
        }
        ...
    

JavaScript:


        (function () {
            var tables = $("table");
            tables.hide().first().show();
            
            $("a.button").on("click", function () {
                tables.hide();
                var tableTarget = $(this).data("table");
    
                $("table#" + tableTarget).show();
            })
        })();
    

Answer №1

Try implementing the nth:child() selector in your CSS like this:

a.button:nth-child(2){
     background-color: #4679BD;
}

For more information, you can visit this link: http://jsfiddle.net/LpLhP/17/

Answer №2

Add an extra custom class to each 'button class' as shown below:

<a class="button red" data-table="1" href="#">Slifer Level</a>

<a class="button blue" data-table="2" href="#">Ra Level</a>

<a class="button green" data-table="3" href="#">Obelisk Level</a>

<a class="button yellow" data-table="4" href="#">Exodia Level</a>

Next, provide the CSS styles for the newly defined classes

 .red { color: red; }
 .blue { color: blue; }
 .green { color: green; }
 .yellow { color: yellow; }

Answer №3

There are numerous methods to achieve this task, one simple approach is to create a new CSS class

<button class="btn red"></button>

.red {
    background-color: red;
}

button:nth-of-type(1) <--- Style for the First Button

Answer №4

Give This a Shot

a.button[data-table="1"]{background-color:red ;}
a.button[data-table="2"]{background-color: blue;}
a.button[data-table="3"]{background-color: green;}
a.button[data-table="4"]{background-color: black;}

TRY IT OUT

Answer №5

Have you considered using CSS in this case?

a.button:nth-child(1){
    background-color:green;
}
a.button:nth-child(2){
    background-color:yellow;
}

Answer №6

<a class="buttona" data-table="1" href="#">click here</a>

<a class="buttonb" data-table="2" href="#">click there</a>

<a class="buttonc" data-table="3" href="#">click everywhere</a>

<a class="buttond" data-table="4" href="#">click nowhere</a>


   .buttona{
    background-color: #ff5733;
    color: white;
    padding: 0.5rem;
    border-radius: 10px;
    text-decoration: underline;
    &:hover{
        background-color: darken(salmon, 15%);
        }
}


.buttonb{
    background-color: aquamarine;
    color: white;
    padding: 0.5rem;
    border-radius: 7px;
    text-decoration: none;
    &:hover{
        background-color: violet;
        }
}
  1. Apply Unique Styles to Each Button.
  2. Modify Background Colors and Hover Effects for Each Button.

Answer №7

    <style>
 #orange{
 background-color: orange;    
     }
 #purple{
 background-color: purple;   
 }
  #yellow{
 background-color: yellow;   
 }
 .btn{
 color: #fff;
 padding: .5rem;
 border-radius: 5px;
 text-decoration: none;
 }
     </style>   

        <a class="btn" id="orange" data-table="3" href="#">button3</a>
        <a class="btn" id="purple" data-table="3" href="#">button3</a>
        <a class="btn" id="yellow" data-table="3" href="#">button3</a>

Answer №8

Give this a try.

I hope you find this useful.

Assign specific identifiers to each of the buttons.

<a class="button" id="button5" data-table="1" href="#">button5</a>

<a class="button" id="button6" data-table="2" href="#">button6</a>

<a class="button" id="button7" data-table="3" href="#">button7</a>

<a class="button" id="button8" data-table="4" href="#">button8</a>

Live Example

Answer №9

A data table can be effective for targeting specific elements, but a more versatile approach would be to implement site-wide styling. Using nth of type is useful for lists, but may not have much reusability in this context. Additionally, adjacent selectors like a.red-button are not necessary.

It's recommended to establish a site-wide button style and then utilize block element modifier styling.

If you're interested, here is a fiddle link:

HTML

<a href="#" class="button">Button</a>

<button class="button">Button</button>

<input type="submit" class="button" value="button" />

CSS

.button { /* Button reset - base */
  display: inline-block;
  border: 0;
  outline: 0;
  margin: 0;
  padding: 0;
  border-radius: 0;
  background: gray;
  font-family: inherit;
  color: inherit;
  text-align: center;
  text-decoration: none;
  -webkit-appearence: none;
  -moz-appearence: none;
  appearence: none;
  cursor: pointer;
    
  font-size: 1.2em;
  padding: .5em .7em
}

/* Specific styles */
.light {
    background: red;
}

.dark {
    background: purple;
}

.selector {
    background: color;
}

.big-button {
    font-size: 1.6em;
}


You can now apply these styles to your data table if desired, especially since you are using SASS. Extend these "mini-themes" to buttons or divs to easily reuse common color combinations.

.button[data-table="1"] {
    @extend .light;
}

Alternatively,

a[data-table="1"] {
    @extend .dark;
    @extend .big-button;
}

/* Consider making them mixins and including them as needed */

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

Populating a Listview in jqueryMobile with dynamic elements

I am struggling with my listview. Whenever I try to add or remove items from the list, the jquery mobile styling does not get applied to the new content that is added. <ul data-role="listview" id="contributionList"> <li id="l1"><a>5. ...

Issue with setState function within setInterval

My current challenge involves updating the value of stateValue with the value of i within a setInterval. However, I am encountering an issue where only the value of i changes and does not update the stateValue as intended within the setInterval. updateV ...

Toggling a div overlay through various user interactions such as clicking, pressing a button, or hitting the escape

I've created a simple div overlay that appears when a user clicks inside an input box: HTML <html> <body> <input id="search" type="text" placeholder="Search..."> </body> </html> CSS #overlay { position: fixed; ...

Implementing restriction measures for deterring harmful conduct within ExpressJS

Recently, I was alerted to some potential vulnerabilities in the application I'm currently developing, particularly in relation to the JavaScript code on the front-end. These flaws could allow a user to exploit the system by clicking multiple buttons ...

Showing content based on the current date using Javascript

Although I may not be proficient in Javascript, I was able to gather examples and piece together the following code: var date = new Date().getDate(); var greeting; if (date < 24) { greeting = "Nej det är:"; } else { ...

Function execution in React component is not happening

Trying to master React and next.js with Firebase as the database has been an interesting journey. I recently encountered an issue where a function in my component is not being called. Upon trying to debug using console.logs(), it appears that the function ...

Tips for choosing a specific set of list items using HTML

In my application, I have a list that is divided into sections like "currently viewing as," "recently viewed," and "staff." All elements in the list share the same classes, making it difficult to identify a specific list element in a particular section, su ...

"Concealing tooltip boxes in Vue: A step-by-step guide

I am trying to achieve a functionality where a tooltip box appears on each incorrect or empty input field when the 'Save' button is pressed. I have been able to display the tooltip boxes upon clicking the 'Save' button, but the issue ar ...

What is the best way to have several automatic slideshows on a single webpage?

Currently, I am exploring the W3 HTML/CSS tutorial on incorporating multiple slideshows into a single page. While the tutorial provides examples for manual navigation of multiple slideshows or a single automatic slideshow, it lacks guidance on setting up m ...

Enable the use of multiple decimal separators in a ReactJS application

I am currently developing a small project that involves using a POST request, which requires multiple fields with 2 float values. My backend system expects the decimal separator to be a ".", but my target audience is primarily French and they ar ...

The orderBy function is not functioning properly when used in conjunction with ng

I attempted to replicate the functionality demonstrated here, which involves organizing an array of objects in the view using the filter orderBy. While my code is functioning properly, when I apply the orderBy filter, nothing appears in the view. You can ...

How can I retrieve the checked values of checkboxes and store them in an array using React?

https://i.sstatic.net/jTRRU.png This toggle button is dynamically generated based on data, and I need to save the checked values in an array. Code {this.state.customersmsselect.map((e, key) => { if(e.isactive == "ON"){ return ( &l ...

Positioning Images at the Top of the Screen in React Native

I have two images that I would like to display side by side at the top of the screen. <View style={styles.container}> <View style={styles.topWrapper}> <Image source={TOP_START_GRAPHIC} style={styles.topStartImage} /> ...

Customize Your Google Maps with Checkboxes for Style Options

Trying to make dynamic changes in the style of a Google Maps using checkboxes. Some checkboxes control colors of features, while others control visibility. Having trouble figuring out how to combine different features of styles since they are not strings ...

The system does not detect the form

I'm having trouble with my form not being recognized. I've gone over everything multiple times but still can't find the mistake. The PHP code is supposed to take the information from the form and display it, but I keep getting an error that ...

Unusual Occurrence: Unexpected Gap at the Beginning of HTML

There seems to be an issue with white space appearing unexpectedly at the top and right side of the file in a website I am working on. Despite adding margin: 0; padding: 0; to both <body> and <html>, the problem persists. After inspecting the ...

Angularjs directives failing to compute accurately~

I'm working with an AngularJS directive where the HTML template is compiled within the linker function. var htmlTemplate = '<img ng-src="~/img/logos/{{account.Logo}}" width="45" height="35" />' var linker = function (scope, element) ...

CSS briefly displays overridden styles before applying the final styling

Presently, I have implemented a third-party library directive for a UI toggle button. I made some adjustments to the background color and positioning of the toggle button to align with my business requirements. Instead of the default light green for true a ...

Converting the Google logo to grayscale for the Google Translate button

Is there a simple way for a non-coder like myself to change the colors of the Google Logo in the Google Translate Button to grey scale instead of its usual bright shades? Below is the embed code I am using: <div id="google_translate_element"></di ...

I'm confused about why the PHP contact form is displaying an error message instead of sending the message

Hello there, I'm having trouble understanding why I'm receiving a message about not having permission to use a script from another URL when I fill out this form. The expected behavior would be for the form to thank me and then proceed to send a ...