How to change the text color of a div on hover - html

I am trying to code a button that changes color when you hover over it/click on it. However, I ran into an issue. There is a space between the text and the edges of the div section, and if you hover over the button, it turns black but the text does not turn white. I put color:white;. I am not sure as to why this does not fix the problem.
Here is my code:
p {
margin: 0px;
}
.button {
width: 66px;
height: 20px;
border: 2px black solid;
border-radius: 4px;
padding: 5px;
}
.button:hover {
background-color: black;
color: white;
}
a {
color: black;
text-decoration: none;
}
a:hover {
color: white;
}
<div class="button">
<p> Click Me! </p>
</div>

just change your a:hover to .button:hover a
everything will look great. :>
p {
margin: 0px;
}
.button {
width: 66px;
height: 20px;
border: 2px black solid;
border-radius: 4px;
padding: 5px;
}
.button:hover {
background-color: black;
color: white;
}
a {
color: black;
text-decoration: none;
}
.button:hover a{
color: white;
}
<div class="button">
<p> Click Me! </p>
</div>

Ok so heres the deal. You made it too complex. If you had problem with the spaces, its because < a > tag is diplayed inline by default and it makes gap between it's container sometimes.
Here's your code, cleaned and working : https://jsfiddle.net/m6dphvm1/
<a class="button" href="https://www.google.com"> Click Me! </a>
a.button {
border: 2px black solid;
border-radius: 4px;
padding: 5px;
color: black;
text-decoration: none;
display: inline-block;
}
a.button:hover {
background-color: black;
color: white;
}

The problem with your CSS is that your anchor(link) color is black, when you hover on the button you are changing the background of button to black, i.e both anchor color and background are black. due to that text is not being visible.
Change either background-color of button or anchor color to a differnt color and that should work. For example I'm changing the color of anchor to blue.
.button:hover {
background-color: black;
color: white;
}
a {
color: blue;
text-decoration: none;
}

a is an inline element, meaning it's designed to be nested in plain text (or what otherwise could be). It's only occupying the immediate space around the text.
However, a tags are also totally allowed to wrap around elements according to the HTML5 spec (not that anyone would stop you otherwise, it's just convention). So if you want the a tag to occupy the entire space just wrap it around the element.
Better yet, only use the a tag. The rest is basically redundant:
<a class="button" href="https://www.google.com">
Click Me!
</a>
.button {
width: 66px;
height: 20px;
border-radius: 4px;
padding: 5px;
color: black;
border: 2px black solid;
}
.button:hover {
background-color: black;
color: white;
}
https://jsfiddle.net/hyp4a9ya/

Related

How to have a clickable button

I have a text link as "Click Me To Get There" that I transformed into a button via CSS.
This is what I used successfully:
.product-buttons-container {
background-color: #ff6347;
padding: 11px 24px;
line-height: 17px;
border-radius: 35px;
cursor: pointer;
text-align: center;
}
.product-buttons-container:hover {
background-color: #E5423A;
}
Click Me To Get There
The problem with that is that only the text still has the "a" tag with the destination URL. The area of the button is just look-and-feel and, although on mouse-hover the cursor change as pointer there is no link in the button padding.
What am I missing in order to get a fully clickable button?
The <a> tag has display: inline as default. You can make it display: inline-block or display: block to adjust the size of the element. As user Terry already pointed out, you can use cursor: pointer to style the cursor (it won't be necessary though if you use an <a> tag.)
You can either:
Style the <a> itself like a button
.button-link {
padding: 10px;
border-radius: 10px;
background: silver;
text-decoration: none;
color: black;
}
I can be a button, too
Wrap the <button> with a link element
.button-link {
background-color: silver;
}
button {
border: none;
padding: 10px;
border-radius: 10px;
cursor: pointer;
}
<button>I am clickable</button>
Use <button> with JS and onclick (not recommended)
document.body.onload = function() {
let buttons = document.querySelectorAll('[data-link]');
for (let i = 0; i < buttons.length; i++) {
let button = buttons[i];
button.onclick = function() {
window.location.href = button.dataset.link;
}
}
}();
.button {
padding: 10px;
border: none;
border-radius: 10px;
background-color: silver;
cursor: pointer;
}
<button class="button" data-link="https://www.google.com">I am a link, too</button>
You don't even have to use <a> or <button> you could link whatever element you like. You can always add a link with the onclick attribute in javascript.
Here is a link in a bigger context:
.button {
display: inline-block;
margin: 0 20px;
width: 100px;
padding: 10px;
border: 2px solid #333;
border-radius: 10px;
background-color: silver;
color: #000;
text-decoration: none;
}
.button:hover {
background-color: #eaeaea;
}
<p>This represents some text on a website la-di-da
<a class="button" href="#">this whole box shall be linked and act like a button</a> and after the link it just goes on ...
</p>
<p>Some other paragraph</p>

CSS: How to ignore default hyperlink colors for button class

This seems simple enough, but I believe I'm overthinking this so much I'm not able to get this to work.
I have my default hyperlink styles:
a {
text-decoration: none;
color: blue;
}
a:hover {
color: red;
}
a:visited {
color: purple;
}
And I also have some styling for a button:
.button {
padding: 0.4em;
border: 1px solid #535353;
color: #535353;
}
Lastly, I have some global classes that are added to elements to customize the font color:
.color-green {
color: green;
}
.color-yellow {
color: yellow;
}
So my problem is dealing with buttons which are hyperlinks, like:
<a class="button" href="#">A button</a>
I would like any link with class button to be color: #535353; in it's default state, on hover, and visited. Buttons should always be that color... unless...
Unless the button has a color class, such as:
<a class="button color-green" href="#">A green button</a>
If a button has a color class, the color should change to whichever color class is specified... this includes all states (default, hover, visited).
The problem I am having is just with the :hover and :visited states of hyperlink buttons. For example, if a button has a color class, the color always changes to red when hovered.
How can I update this so that if a button has a color class, it will be that color in all states?
Keep in mind the color classes are used on all sorts of elements (div, span, p, etc) and also the button classes are used on links, submit buttons, div, etc.
Also please note that a hyperlink my have class color-yellow, but I would still want a hyperlink to inherit the default hyperlink hover color. I only want anything with class button to ignore the default hyperlink hover and visited colors.
a {
text-decoration: none;
color: blue;
}
a:hover {
color: red;
}
a:visited {
color: purple;
}
.button {
width: auto;
padding: 0.4em;
border: 1px solid #535353;
color: #535353;
}
.color-green {
color: green;
}
.color-yellow {
color: yellow;
}
Normal hyperlink
<a class="color-yellow" href="#">Yellow normal, red hovered</a>
<a class="button" href="#">Default button</a>
<a class="button color-green" href="#">Green color button</a>
You can use :not() in combination with the class attribute wildcard selector to exclude .color-* from CSS rules.
a {
text-decoration: none;
color: blue;
}
a:not([class*="color-"]):visited {
color: purple;
}
a:not([class*="color-"]):hover {
color: red;
}
.button {
width: auto;
padding: 0.4em;
border: 1px solid #535353;
color: #535353;
}
.color-green {
color: green;
}
.color-yellow {
color: yellow;
}
Normal hyperlink
<a class="button" href="#">Default button</a>
<a class="button color-green" href="#">Green color button</a>
One solution is to use :not selector
a {
text-decoration: none;
color: blue;
}
a:not(.button):hover {
color: red;
}
a:not(.button):visited {
color: purple;
}
This is a perfect use-case for !important. No need to fiddle with additional markup/rules/selectors.
a {
text-decoration: none;
color: blue;
}
a:hover {
color: red;
}
.button {
padding: 0.4em;
border: 1px solid #535353;
color: #535353 !important;
}
.color-green {
color: green !important;
}
.color-yellow {
color: yellow !important;
}
This link has no class. <br> <br>
This link has class "button". <br><br>
This link has classes "button" and "color-green".
Try specifying color class with anchor tag : a.color-green
See below code
a {
text-decoration: none;
color: blue;
}
a:hover {
color: red;
}
a:visited {
color: purple;
}
.button {
width: auto;
padding: 0.4em;
border: 1px solid #535353;
color: #535353;
}
.color-green,
a.color-green {
color: green;
}
.color-yellow,
a.color-yellow {
color: yellow;
}
Normal hyperlink
<a class="button" href="#">Default button</a>
<a class="button color-green" href="#">Green color button</a>
One thing to point out is that styling these links actually has an order to them. See here. "Your hover MUST go after the visited".
As for the button colors, another thing to point out is that using .color-green is not referencing the class you are listing in the link, you write button color-green as the class which is not the same thing.
You can set the link style per class. Such as a.button:hover so in your case you would have quite a few different ones setup, but what you have at the moment will default to that red for all links.

CSS working with Chrome but not IE

I have a list of CSS to format my link button but it appears only working in Chrome but not IE, any ideas, the hover and everything works just not the link itself
thanks in advance
CSS
.button {
background-color: #4CAF50;
border-radius: 50%;
width: 170px;
height: 170px;
color: white;
padding: 4px 8px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
-webkit-transition-duration: 0.4s; /* Safari */
transition-duration: 0.4s;
cursor: pointer;
}
.button1 {
position: absolute;
margin-top: 0px;
margin-left: 400px;
background-color: white;
color: white;
border: 4px solid #83b739;
}
.button1:hover {
background-color: #83b739;
color: white;
}
HTML
<button class="button button1">link</button>
It's probably not even a CSS issue, but rather an issue with nesting interactive elements like that.
Don't put a link inside a button. That's just bizarre. Use just the <a> element and style that.
I'm not exactly sure what would have caused your problem, however is is most likely due to a css/html nesting problem, where multiple css styles interact with the nested elements differently on different browsers? It is better to simply remove the button element in the html and just style the <a> tag to look like a button. By doing this the code is less complicated, you should have fewer problems with styles and nested elements, and this is how most make link buttons anyway. Here is an example of how I made a link button in a recent project, some of the stylings are missing (custom fonts, etc) but it shows that you don't need the button tag, it works better without it, and how to make a button with just the <a> tag.
.btn:link,
.btn:visited {
display: inline-block;
padding: 10px 30px;
font-weight: 300;
text-decoration: none;
color: white;
border-radius: 200px;
border: 3px solid #1A75BB;
margin: 20px 20px 0px 0px;
transition: background-color 0.2s, border-color 0.2s, color 0.2s;
}
.btn:hover,
.btn:active {
background-color: #14598e;
border-color: #14598e;
}
.btn-full:link,
.btn-full:visited {
background-color: #1A75BB;
margin-right: 20px;
}
.btn-full:hover,
.btn-full:active {
background-color: #14598e;
}
.btn-ghost:link,
.btn-ghost:visited {
color: black;
border-color: #14598e;
}
.btn-ghost:hover,
.btn-ghost:active {
color:white;
}
Why use AnyMath?
What problems can AnyMath solve?
It’s not just about IE. Such link-inside-button does not work in Firefox too.
If you really (think twice) need this to be a button instead of just a link, remove the explicit link from your button and wrap the button in a simple form:
<form action="http://example.com/">
<button class="button button1" type="submit">link</button>
</form>
But based on your code, button element is unneeded, and you should just use a link instead:
<a href="http://example.com/" class="button button1">link</button>

Styling HTML buttons in Wordpress

In my HTML I have the following:
Knowledge Base
<br />
In my CSS I have the following classes:
.button1 {
display: block;
height: 25px;
width: 130px;
background: #ffffff;
border: 2px solid #000000;
Color: #000000;
text-align: center;
margin-left: auto;
margin-right:auto;
padding-top:10px;
font-weight: 700;
}
The link color is blue, I want to make the link black with white background. I want to change this on hover by making the background black and the link color white.
I can't make it by using the following CSS class:
.button1 a:hover {
background: #393939;
Color: #ffffff;
}
Do I have to change my HTML to be:
<button class="button1" id="save">Sample button</button>
Please advice. I appreciate your support
Is that something like that http://codepen.io/anon/pen/vNMLar ?
a.button1:hover {
background: #000;
Color: #fff;
transition: all 0.2s ease-in-out;
}
it should be
a.button1:hover {
background: #393939;
color: #ffffff;
}
.button1 is already your link. What you did is tell to look for a link inside .button1
EDIT: also the name for the attributes is lower case

How to change background colour of entire div on hover?

­I'm making a website and want to have a link inside a div that changes colour when hovered over, however I've come across a problem. Pre hover, I want a border around the div the same colour as the text in the link inside. The background colour should be white. Then, upon hover, I want the background colour to change to the colour of the text and border, and the text to become white. Because I have padding inbetween the link and div border, it doesn't quite work as intended. Here is the source html/css:
HTML:
<div id="home">
HOME
</div>
CSS:
#home {
border: 4px solid #00a651;
font-size: 30px;
padding: 10px 20px 10px 20px;
margin: 20px 100px 20px 100px;
display: inline-block;
}
#home a {
background: #ffffff;
color: #00a651;
text-align: center;
}
#home a:hover {
background: #00a651;
color: #ffffff;
}
When anywhere within the div is hovered over other than the link, nothing happens, and when you hover over the link the padding remains white. What do I need to do to make it so the colour change happens when anywhere on the div is hovered over, and the whole divs colour changes? Thanks, Brandon :)
#home {
font-size: 30px;
margin: 20px 100px 20px 100px;
display: inline-block;
}
#home a {background: #ffffff;
border: 4px solid #00a651;
padding: 10px 20px 10px 20px;
color: #00a651;
text-align: center;
}
#home a:hover {background: #00a651;
color: #ffffff;
}
<div id="home">
HOME
</div>
You need to add the hover event to the div and the anchor:
#home {
border: 4px solid #00a651;
font-size: 30px;
padding: 10px 20px 10px 20px;
margin: 20px 100px 20px 100px;
display: inline-block;
}
#home a {
background: #ffffff;
color: #00a651;
text-align: center;
}
#home:hover {
background: #00a651;
color: #ffffff;
}
#home:hover a {
background: #00a651;
color: #ffffff;
}
<div id="home">
HOME
</div>
I recommend using the jQuery library, if you know how to use it:
Link your jQuery download to your main html file or use a CDN.
In your JavaScript file add a hover event on your link and use the css effect to to change the color of the div once you hover over the link. Then add another css effect so the link changes color as the div does. Like this:
$(document).ready(function(){
$('a').hover(function(){
$('#home').css("background-color","#00a651");
$('a').css("color", "chooseacolor");
});
});
I am not entirely sure what you are asking of, so this is the best answer I can give!
I don't think using :hover on DIV elements is a good idea. As I know, this practice is not fully supported on all browsers of all versions. Why not use some JS?
But, anyway, if you need all area (including padding) in the DIV become clickable - you should rather make it "display:block" in CSS.
$(document).ready(function() {
$('#home').hover(function() {
$(this).css('background-color', '#00a651').find('a').css({
backgroundColor: '#00a651',
color: '#FFFFFF'});
}, function() {
$(this).css('background-color', '#FFFFFF').find('a').css({
backgroundColor: '#ffffff',
color: '#00a651'});
});
});
#home {
border: 4px solid #00a651;
font-size: 30px;
padding: 10px 20px 10px 20px;
margin: 20px 100px 20px 100px;
display: inline-block;
}
#home a {background: #ffffff;
color: #00a651;
text-align: center;
}
/* There is no need to use :hover anymore if using a script */
<!-- this goes to the HEAD section if not arleady there -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<div id="home">
HOME
</div>