Can you suggest a more effective method for changing the color of buttons?

While the title provides some context, allow me to elaborate further. I've been working on a website for quite some time now. Each section of the website features 3 buttons, each highlighted in a unique color. My question is, is there a more streamlined approach to achieve the following:

/* Button 1 */
#button1 {
  display: inline; 
  margin-left: 26px; 
  color: orangered; 
  border: 3px solid orangered; 
  box-sizing: border-box; 
  padding: 7px 30px 7px 30px; 
  border-radius: 30px; 
  -webkit-border-radius: 30px; 
  -moz-border-radius: 30px;
}
#button1:hover {
  background-color: orangered; 
  color: white;
}

/* Button 2 */
#button2 {
  display: inline; 
  margin-left: 26px; 
  color: dodgerblue; 
  border: 3px solid dodgerblue; 
  box-sizing: border-box; 
  padding: 7px 30px 7px 30px; 
  border-radius: 30px; 
  -webkit-border-radius: 30px; 
  -moz-border-radius: 30px;
}
#button2:hover {
  background-color: dodgerblue; 
  color: white;
}

/* Button 3 */
#button3 {
  display: inline; 
  margin-left: 26px; 
  color: #DD3157; 
  border: 3px solid #DD3157; 
  box-sizing: border-box; 
  padding: 7px 30px 7px 30px; 
  border-radius: 30px; 
  -webkit-border-radius: 30px; 
  -moz-border-radius: 30px;
}
#button3:hover {
  background-color: #DD3157; color: white;
}
<a href="#" id="button1">Read more</a>
<a href="#" id="button2">Read more</a>
<a href="#" id="button3">Read more</a>

There must be a simpler solution for this.

Answer №1

Create a single general definition with CSS variables to customize multiple buttons effortlessly:

.button {
  display: inline-block; 
  margin-left: 26px; 
  color: var(--c,black); 
  border: 3px solid var(--c,black); 
  box-sizing: border-box; 
  padding: 7px 30px 7px 30px; 
  border-radius: 30px; 
}
.button:hover {
  background-color: var(--c,black); 
  color: white;
}
<a href="#" class="button" style="--c:red">Read more</a>
<a href="#" class="button" style="--c:blue">Read more</a>
<a href="#" class="button" style="--c:darkblue">Read more</a>
<a href="#" class="button" style="--c:yellow">Read more</a>
<a href="#" class="button" style="--c:green">Read more</a>
<a href="#" class="button" >Read more</a>

Answer №2

Group all similar button properties under a single CSS class:

/* Common Button Properties */
.button {
  display: inline;
  margin-left: 26px; 
  border: 3px solid;
  padding: 7px 30px 7px 30px; 
  border-radius: 30px;
}

.button:hover {
  border-color: transparent;
  color: white;
}

/* Styling for Button 1 */
.button1 { 
  color: orangered; 
}
.button1:hover {
  background: orangered; 
}

/* Styling for Button 2 */
.button2 {
  color: dodgerblue; 
}
.button2:hover {
  background: dodgerblue;
}

/* Styling for Button 3 */
.button3 {
  color: #DD3157; 
}
.button3:hover {
  background: #DD3157;
}
<a href="#" class="button button1">Read more</a>
<a href="#" class="button button2">Read more</a>
<a href="#" class="button button3">Read more</a>

Answer №3

.orange {
  color: orangered;
  border-color: orangered;
}

.orange:hover {
  background-color: orangered;
}

.blue {
  color: dodgerblue;
  border-color: dodgerblue;
}

.blue:hover {
  background-color: dodgerblue;
}

.red {
  color: #DD3157;
  border-color: #DD3157;
}

.red:hover {
  background-color: #DD3157;
}

a {
  display: inline;
  margin-left: 26px;
  border: 3px solid;
  box-sizing: border-box;
  padding: 7px 30px 7px 30px;
  border-radius: 30px;
  -webkit-border-radius: 30px;
  -moz-border-radius: 30px;
}

a:hover {
  color: white;
}
<a href="#" class="orange">Discover more</a>
<a href="#" class="blue">Explore further</a>
<a href="#" class="red">Learn more</a>

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

Why is the value returned by getBoundingClientRect 190 when the y attribute is set to 200 in SVG?

When placing textBox1 at a y-position of 200, getBoundingClientRect is returning a value of 190. What could be causing this discrepancy? For more details and code snippets, please refer to the following link: https://codepen.io/anon/pen/REKayR?editors=101 ...

Is there an issue with my Regex pattern for extracting link elements when scraping content?

I'm currently facing an issue with my VB.NET scraper. The scraper is designed to extract all links from an HTML string that I have already retrieved, and upon inspection, the links are present. It appears that the problem lies within my regex expressi ...

What is the best way to achieve a never-ending vertically scrolling image using CSS?

I want to incorporate a vertically scrolling image similar to the one on Adam Whitcroft's amazing landing page. I have tried examining his source code, but I am unable to grasp how he achieved it. The concept is to have the "I'm a scientist" text ...

What is the best way to select an element that is currently visible but hidden underneath another element?

I have developed a circular graphic using primarily HTML and CSS, with some JavaScript and JQuery functionalities incorporated for text curving and planned interactions in the future. However, I've encountered an issue where clicking on the upper rig ...

Every time I make a change to the code, Laravel's css/app.css file mysteriously vanishes from the mix-manifest.json

Whenever I make a change in my code, the line css/app.css in mix-manifest.json disappears. The only solution is to execute npm run watch or npm run dev, but the problem reoccurs each time I modify the code. Here is how my mix-manifest.json looks when eve ...

What is preventing me from modifying the input file name in PHP?

After executing the code below, an error message pops up: "Notice: Undefined index: uploadFile in C:\xampp\htdocs\ImageTest\processImage.php on line 17" However, when I switch every occurrence of 'uploadFile' with 'fi ...

Display an image from a Google image search when hovering over a specified data attribute

My goal is to add a code snippet that will assign a class of "hoverme" to a table cell. When a user hovers over that cell, an ajax query will be triggered using the data attribute containing information stored there to perform a Google search. Below is a ...

Interior CSS border

Check out my progress so far: jsfiddle.net/warpoluido/JkDhN/175/ I'm attempting to adjust the height of the green section to be slightly higher than the blue (or similar color) one, as shown in the image. The design needs to remain responsive. < ...

"Can you explain the method by which reveal.js adjusts the size of

I have been exploring the resizing behavior of elements in reveal.js and trying to understand how they dynamically adjust. If you resize the page height, you can observe that the elements shrink up to a certain extent along with the page. However, when in ...

Adjusting the width of the container <div> will automatically resize the inner <div> to match

I have a main container element. .wrapper{ border:1px solid blue; position:relative; top:20%; left:30%; height: 300px; width: 350px; } When I adjust the width of the parent container, the child box does not reposition itself accor ...

Create paper "cut" borders using HTML canvas or pseudo-elements

As a designer with a penchant for pain, I am striving to achieve an "art nouveau" aesthetic on paper for my NextJS project with MUI as the main design solution. Despite the abundance of CSS in the background, I am faced with the challenge of creating inner ...

Can someone help me troubleshoot why my multi-step form is not functioning properly?

I've been working on a multi-phase form, but it's not behaving as expected. I've tried different code variations, but for some reason, it's not functioning properly. After spending two days on it, I'm starting to feel a bit frustra ...

Positioning a flex item to the right by using float

I am in need of assistance with my HTML layout. <div class="container"> <div class="sidebar" style="float:right"> Ignore sidebar? </div> <div> main content </div> </div> The container is set to have a flex disp ...

Retrieve the file name of the webpage from the URL bar

Is there a way to retrieve the page name from the address bar using jquery or javascript instead of PHP? I am working on an HTML website and would prefer not to use PHP for this specific task. For example, if the address is www.mywebsite.com/hello.htm, ho ...

Applying style changes to the height on scroll event in Javascript for Chrome, Firefox, and Internet Explorer

I am working on triggering an event when scrolling to a specific height. I have code that successfully adds a style in Chrome, but it is not functioning properly in IE. Can anyone provide assistance? myID = document.getElementById("subnav"); var mySc ...

The CSS code for ::before is not functioning properly

Trying to enhance the style in Ionic2, I have a message box: https://i.sstatic.net/PCRtA.png I am attempting to integrate this image into it: https://i.sstatic.net/PSQID.png To create a seamless look with the message box. Facing an issue where the ima ...

The CSS isn't loading properly once the file is uploaded to the server using FileZilla or cPanel

After uploading the file.css file to the server using both FileZilla and cPanel, I noticed that when I visit the website, the CSS is not being applied properly. Specifically, I changed padding-left: 10px; However, upon viewing the Page source, it appears ...

Ways to remove border when image is not present

I've been attempting to use JavaScript to hide the border, but it doesn't seem to be working. Is it possible to hide the border in HTML using Java? ......................... Check out my page here Here is a snippet of my HTML: <!-- Single P ...

Align two DIVs in the correct layout: One as a sidebar that remains fixed, and the other as an expanding element

I am currently working on a code snippet to build a basic website featuring two main sections: a sidebar and the main content: html, body { height: 100%; margin: 0; } .container { display: flex; flex-direction: row; height: 100%; } .sidebar { ...

The Bootstrap glyphicon content breaks when using ASP.NET MVC minification

When working with asp.net, I noticed that minification tends to disrupt the display of bootstrap UTF symbols. For example, here is what it looks like in the original file: .glyphicon-edit:before { content: "\e065"; } And here is how it appears in ...