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>
Related
I have this scss class:
.dropdown-list {
display: block;
position: absolute;
background-color: white;
background-size: 100%;
list-style: none;
border: 1px solid var(--color-grey-light-2);
border-radius: 5px;
overflow: hidden;
li {
padding: 1rem;
&:not(:last-child) {
border-bottom: 1px solid var(--color-grey-light-2);
}
&:hover {
background-color: var(--color-grey-light-2);
}
}
a {
text-decoration: none;
color: currentColor;
}
}
However, the background color is not covering the entire div:
As you can see, there is a little border on the top and on the left of my div.
For completeness, i'm adding the html:
<ul class="dropdown-list" style="margin-top: 0.6rem; cursor: pointer">
<li>
Add Ingrediets to Shopping List
</li>
[...]
The "dropdown-list" class is linked on the "ul" tag and not on the "div" tag. You must add some css for the "div" tag or move the class "dropdown-list" depending on your need.
If it is not fixing your issue you should share the part with the "div" tag.
Give a background color in inline css
<div style="background-color: red;"></div>
this would work
The first image was captured in Python online course. The second image was captured in my own website. I want to mimic this website for practicing html/css skill. What I want to know is how to create the expected button. Thank you for your reply.
/*Here is my CSS code, which is unfinished.*/
.Matter a:hover{
background-color: rgb(230,230,230);
}
<!-- Here is my HTML code -->
<div class='Matter'>
<h1>Welcome to the Capstone</h1>
<p><a href='#'><b>Video:</b> Introduction: Welcome to the Class</a></p>
<p><a href='#'><b>Reading:</b> Capstone Overview</a></p>
<p><a href='#'><b>Reading:</b> Help Us Learn More About You!</a></p>
</div>
Let's handle your problem step by step:
Styling the link: We are going to add some padding, text decoration and color. We are also using display inline-block for our link take full width.
.Matter a {
padding: 8px;
display: inline-block;
width: 100%;
text-decoration: none;
color: black;
}
Adding hover style:
.Matter a:hover{
background-color: rgb(230,230,230);
}
Adding checkmark: We need to use CSS Pseudo element ::before to add the check mark in front of the link. To add the green circle around the check mark we are using some margin, padding, border radius, background, color and font size. We are also using position to adjust the check mark circle position.
.Matter a::before {
content: '🗸';
margin-right: 8px;
padding: 2px 4px;
background: green;
border-radius: 50%;
color: white;
font-size: 12px;
position: relative;
top: -2px;
}
Here's the complete solution. You can play with it as you wish.
.Matter a {
padding: 8px;
display: inline-block;
width: 100%;
text-decoration: none;
color: black;
}
.Matter a:hover{
background-color: rgb(230,230,230);
}
.Matter a::before {
content: '🗸';
margin-right: 8px;
padding: 2px 4px;
background: green;
border-radius: 50%;
color: white;
font-size: 12px;
position: relative;
top: -2px;
}
<div class='Matter'>
<h1>Welcome to the Capstone</h1>
<p><a href='#'><b>Video:</b> Introduction: Welcome to the Class</a></p>
<p><a href='#'><b>Reading:</b> Capstone Overview</a></p>
<p><a href='#'><b>Reading:</b> Help Us Learn More About You!</a></p>
</div>
So I am creating a code compiler with two buttons:
RUN and DOWNLOAD. But instead of using <button> tags, I use <div> tags so that I can have a more variety of CSS styles for my buttons. But the problem is, when I try to make the second button by copying and pasting the same code from the RUN button, the DOWNLOAD button is underneath the RUN button when I needed them on the same line.
So, I added the display: inline; style but that just messed up everything and the styles for the div didn't work. The only styles that worked were the styles for the text in the div buttons and that centered the text to the middle of the page.
Here is the code:
.dbutton,
.button {
background-color: red;
margin-right: 300px;
box-shadow: 0px 0px 5px gray;
font-size: 20px;
color: blue;
text-align: center;
width: 100px;
height: 50px;
}
.btn-text {
margin-top: 10px;
padding-top: 12px;
}
.dbtn-text {
margin-top: 10px;
padding-top: 12px;
}
.dbutton:hover,
.button:hover {
background-color: white;
-webkit-user-select: none;
}
.dbutton:active,
.button:active {
background-color: #00ad08;
}
div {
display: inline;
}
<div class="button">
<p class="btn-text">
Run >>
</p>
</div>
<div class="dbutton">
<p class="dbtn-text">
Download ↓
</p>
</div>
If you want the full code, here is the link.
JSFIDDLE
The two divs are not styling and the text in the buttons are placed in the middle of the page. I need the two divs to be beside each other and the for the style to work. Please help me!
.btn-bar {
position:relative
/* ... */
}
.btn {
float:left;
background:red;
width:7em;
padding:11px 3px;
margin:3px;
text-align:center;
text-transform:uppercase;
box-shadow:0 0 3px 0 #000;
}
<div class="btn-bar">
<div class="btn">run</div>
<div class="btn">compile</div>
</div>
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/
Below is my problem, these are the same link and the orange background is a hover effect set in css. As you can see when the window is compressed the text of the link moves onto the next lines to fit the screen. But the background effect of each line obscures the second. I can set the display to be block, but that would stretch the background to 100% of the window, which isn't what I want when the page is not narrow.
Thanks in advance
EDIT: CODE
<div class="PageSection">
<a class="LinkButton" href="">This Is My Link, There Are Many Like It But This One Is Mine</a>
</div>
.PageSection {
width: 100%;
margin: 50px 0px 50px 0px;
text-align: center;
font-size: 30px;
}
input[type="button"],
input[type="submit"],
.LinkButton {
padding: 5px 10px 5px 10px;
cursor: pointer;
color: inherit;
font-size: inherit;
text-decoration: none;
line-height: 100%;
border: none;
background-color: transparent;
}
input[type="button"]:hover,
input[type="submit"]:hover,
.LinkButton:hover {
background-color: #FF5A19;
}
EDIT:
I know I could set the line-height css property, but that gives the link ugly spacing, and I am also aiming for a square block of background colour, just not to the full width of the page.
assuming your padding is for creating that extra block effect.
try
.PageSection {
width: 100%;
margin: 50px 0px 50px 0px;
text-align: center;
font-size: 30px;
}
input[type="button"],
input[type="submit"],
.LinkButton {
/*padding: 5px 0 5px 0;*/
cursor: pointer;
color: inherit;
font-size: inherit;
text-decoration: none;
line-height: 100%;
border: none;
background-color: transparent;
background-clip: padding-box;
}
input[type="button"]:hover,
input[type="submit"]:hover,
.LinkButton:hover {
background-color: #FF5A19;
box-shadow: 10px 0 0 0px #FF5A19,-10px 0 0 0px #FF5A19;
}
all you need on the link is background-clip: padding-box; on the link and some box-shadow to cover the extra bit where padding can't reach due to inline element behaviour.
Edit: JSFiddle
I think i have managed to do what you wanted, do you want it multi-lined even on full width or when page width is smaller
I have used multiple spans tags to controls the lines
<div class="PageSection">
<a class="LinkButton" href=""><span>This Is My Link, </span> <span> There Are Many Like </span> <span> It But This One Is Mine </span></a>
</div>
here is the fiddle on what i have achieved
I HAVE UPDATED THE FIDDLE to your desired outcome
See the FIDDLE here