2

I found 2 issues on this CSS code -

  1. The whole button is not clickable.
  2. The text of the button always a different color than what I set. For Example, I set text color as white but still it is in a different color.

.visitbutton {
  line-height: 1.6;
  font-style: normal;
  font-size: 20px;
  text-align: center;
  color: white;
  background-color: #34495E;
  text-decoration: none;
  font-weight: bold;
  font-family: Arial, sans-serif;
  border: solid 0 #e3edf4;
  border-bottom: 2px solid #AEB6BF;
  border-radius: 4px;
  padding: 16px 40px;
}
<div class="visitbutton">
  <a href="https://google.com" target="_blank">Visit Now</a>
</div>
Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
Mertz
  • 33
  • 4

6 Answers6

2

The reason it's not working is that <a> tag is not a button, it defines a hyperlink. Using a button would be a better alternative to this.

To have the font colour white you have to target that element or its selector.

Here's the correction to your code.

.visitbutton {
  line-height: 1.6;
  font-style: normal;
  font-size: 20px;
  text-align: center;
  color: white;
  background-color: #34495E;
  text-decoration: none;
  font-weight: bold;
  font-family: Arial, sans-serif;
  border: solid 0 #e3edf4;
  border-bottom: 2px solid #AEB6BF;
  border-radius: 4px;
  padding: 16px 40px;
}
<div>
  <a href="https://google.com" class="visitbutton" target="_blank">Visit Now</a>
</div>
Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
Bhavesh Lohana
  • 376
  • 3
  • 5
1

1: Put the button div inside the a-tag (<a ...><div>Visit now</div></a>).

2: The reason it's a different color is that anchor tags (<a>) have implicit styling. Simply ovveride it manually to fix it, but you have to override it in the a-tag or lower. So it might be fixed together with the clickability!

Sebastian
  • 1,321
  • 9
  • 21
  • for the information, we should not put a block element inside an inline element, see https://stackoverflow.com/questions/1714121/block-level-elements-inside-inline-elements – mohcine nazrhan Jul 26 '20 at 19:44
1

The reason the whole button does not act as a link is because it is a div. If you want it to be a link, and set the colors of the link then you need to style it directly. Right now you are just styling the div around it. Here is an example that uses some JavaScript: The second button may not open, as your browser considers it a pop-up, use a link instead.

button {
  line-height: 1.6;
  font-style: normal;
  font-size: 20px;
  text-align: center;
  color: white;
  background-color: #34495E;
  text-decoration: none;
  font-weight: bold;
  font-family: Arial,sans-serif;
  border: solid 0 #e3edf4;
  border-bottom: 2px solid #AEB6BF;
  border-radius: 4px;
  padding: 16px 40px;
}
<button onclick="window.location.href='https://example.com'">Changes the current page</button>
<br />
<button onclick="window.open('https://example.com')">Opens in a new tab</button>
luek baja
  • 1,475
  • 8
  • 20
1

In you case, the whole area is not clickable because by default only the text inside a is a clickable link. You need to make changes to css of a as follows for desirable results-

.visitbutton {
  line-height: 1.6;
  font-style: normal;
  font-size: 20px;
  text-align: center;
  color: white;
  background-color: #34495e;
  text-decoration: none;
  font-weight: bold;
  font-family: Arial, sans-serif;
  border: solid 0 #e3edf4;
  border-bottom: 2px solid #aeb6bf;
  border-radius: 4px;
}

.visitbutton a {
  display: block;
  text-decoration: none;
  color: white;
  padding: 16px 40px;
}
<div class="visitbutton">
  <a href="https://google.com" target="_blank">Visit Now</a>
</div>

I have also moved the padding from .visitbutton to .visitbutton a so that the whole area is clickable link.


Also, you could use a button instead of using div which would probably a better option here -

.visitbutton {
  line-height: 1.6;
  font-size: 20px;
  color: white;
  background-color: #34495e;
  font-weight: bold;
  font-family: Arial, sans-serif;
  border: solid 0 #e3edf4;
  border-bottom: 2px solid #aeb6bf;
  border-radius: 4px;
  width: 100%;
}

a {
  text-decoration: none;
  color: white;
  padding: 16px 40px;
}
<button class="visitbutton">
  <a href="https://google.com" target="_blank">Visit Now</a>
</button>
Abhishek Bhagate
  • 5,583
  • 3
  • 15
  • 32
1

block elements by default take up 100% of the space

.visitbutton,
a {
  line-height: 1.6;
  font-style: normal;
  font-size: 20px;
  text-align: center;
  color: white;
  background-color: #34495E;
  text-decoration: none;
  font-weight: bold;
  font-family: Arial, sans-serif;
  border: solid 0 #e3edf4;
  border-bottom: 2px solid #AEB6BF;
  border-radius: 4px;
  padding: 16px 40px;  
  display: block;
}
<div class="visitbutton">
  <a href="https://stackoverflow.com">Visit Now</a>
</div>
DCR
  • 14,737
  • 12
  • 52
  • 115
0

See, there are some things to be noted, if you want a button, use button tag and the different color is because it is a link to fix it, you can use this code:

.visitbutton {
  line-height: 1.6;
  font-style: normal;
  font-size: 20px;
  text-align: center;
  background-color: #34495E;
  text-decoration: none;
  font-weight: bold;
  font-family: Arial,sans-serif;
  border: solid 0 #e3edf4;
  border-bottom: 2px solid #AEB6BF;
  border-radius: 4px;
  padding: 16px 40px;
}

.visitbutton a{
  text-decoration: none;
  color: white;
}
<button class="visitbutton">
  <a href="https://google.com" target="_blank">Visit Now</a>
</button>
Praneet Dixit
  • 1,393
  • 9
  • 25