I'm trying to change the text of the button when you hover on it, I found a solution but for some reason it won't work.
What I want to happen is when I hover on it it will turn to green and change the text to Completed!.
Currently the button changes color when I hover on it but the text wont change.
Here's my css:
.button {
border: 0px solid #004B84;
background: red;
padding: 3px 21px;
-webkit-border-radius: 16px;
-moz-border-radius: 16px;
border-radius: 16px;
color: #ffffff;
font-size: 12px;
font-family: Questrial;
text-decoration: none;
vertical-align: middle;
letter-spacing: 2px;
}
.button:hover {
border: 0px solid #1292e1;
background: green;
color: white !important;
content: "Completed!" !important;
}
.button:active {
background: #1292e1;
color: white;
text-decoration: none !important;
}
<button class="button" type="submit" name="completed" value="">New Request</button>
And here's the html for the button:
<button class="button" type="submit" name="completed" value="">New Request</button>
You could do this using a psuedo element.
.button {
width: 150px; /* set a width so it doesnt change upon hover */
border: 0px solid #004B84;
background: red;
padding: 3px 21px;
-webkit-border-radius: 16px;
-moz-border-radius: 16px;
border-radius: 16px;
color: #ffffff;
font-size: 12px;
font-family: Questrial;
text-decoration: none;
vertical-align: middle;
letter-spacing: 2px;
}
.button:hover span {
display:none
}
.button:hover:before {
content:"Completed!";
}
.button:hover {
background-color: green;
}
<button class="button">
<span>New request</span>
</button>
CSS (without HTML):
You don't need to touch your HTML (manually adding <span> tags, manually
removing HTML element content, etc) for this.
Just set the button's text content font-size to 0px then add your own text and font-size to the button using :hover, ::after and ::after:hover.
Check and run the following Code Snippet for a practical example of using just CSS to edit your button's content:
.button {font-size: 0px;min-width: 100px;min-height: 22px;}
.button::after {font-size: 14px;}
.button::after {
content: "New Request";
}
.button:hover::after {
content: "Completed!";
}
.button:hover {
background-color: green;
border: 0px solid #1292e1;
color: white !important;
}
<button class="button" type="submit" name="completed" value="">New Request</button>
JavaScript:
Just use the textContent() property to change your text on hover (mouseover) to Completed and back to New Request when hovered out (mouseout).
Check and run the following Code Snippet for a practical example of the JavaScript approach above:
var btn = document.querySelector(".button");
btn.addEventListener("mouseover", function() {
this.textContent = "Completed!";
})
btn.addEventListener("mouseout", function() {
this.textContent = "New Request";
})
.button {
min-width: 100px;
min-height: 22px;
}
.button:hover {
background-color: green;
border: 0px solid #1292e1;
color: white !important;
}
<button class="button" type="submit" name="completed" value="">New Request</button>
#K. Rogers Try Code Hope This Will Work For You!
/* .button */
.button {
display: inline-block;
position: relative;
border: 0px solid #004B84;
background: red;
padding: 8px 25px;
-webkit-border-radius: 16px;
-moz-border-radius: 16px;
border-radius: 16px;
color: #ffffff;
font-size: 12px;
font-family: Questrial;
text-decoration: none;
vertical-align: middle;
letter-spacing: 2px;
overflow: hidden;
}
.button:hover { background: green; }
.button span {
transition: 0.6s;
transition-delay: 0.2s;
}
.button:before,
.button:after {
content: '';
position: absolute;
top: 0.67em;
left: 0;
width: 100%;
text-align: center;
opacity: 0;
transition: .4s,opacity .6s;
}
/* :before */
.button:before {
content: attr(data-hover);
transform: translate(-150%,0);
}
/* :after */
.button:after {
content: attr(data-active);
transform: translate(150%,0);
}
/* Span on :hover and :active */
.button:hover span,
.button:active span {
opacity: 0;
transform: scale(0.3);
}
/* :before pseudo-element on :hover
and :after pseudo-element on :active */
.button:hover:before,
.button:active:after {
opacity: 1;
transform: translate(0,0);
transition-delay: .4s;
}
.button:active:before {
transform: translate(-150%,0);
transition-delay: 0s;
}
<button class="button" type="button" data-hover="COMPLETED" data-active="I'M ACTIVE"><span>New Request </span></button>
Here is one more solution wit no change button html-code. Just using css style of pseudo-element ::after
To prevent a change of button width on hover you need to define width property.
.button {
border: 0px solid #004B84;
min-width: 150px;
background: red;
padding: 5px 21px;
-webkit-border-radius: 16px;
-moz-border-radius: 16px;
border-radius: 16px;
color: #ffffff;
font-size: 12px;
font-family: Questrial;
text-decoration: none;
vertical-align: middle;
letter-spacing: 2px;
}
.button:hover {
background: green;
}
.button::after {
content: "New request!";
}
.button:hover::after {
content: "Completed!";
}
.button:active {
background: #1292e1;
}
.button:active::after {
background: #1292e1;
}
<button class="button" type="submit" name="completed" value=""></button>
See:
https://www.w3schools.com/cssref/pr_gen_content.asp
The content property is used with the ::before and ::after
pseudo-elements, to insert generated content.
You will need Javascript or more.
e.g.
<button class="button" type="submit" name="completed" value="" onmouseover="document.getElementsByName('completed')[0].innerHTML ='Completed!';" onmouseleave="document.getElementsByName('completed')[0].innerHTML ='New Request';">New Request</button>
If you want to change the value of an element on a page, then you need to do it per code - try to do it with JavaScript
Edit:
Seems like there is a way:
How can I replace text with CSS?
Related
I know this questions has been asked a dozen times, but I can't seem to get my button to have a hover and pressed effect. I am trying to have the color of my button change when I hover over it. I would also like the button to have an active pressed effect when clicked. I feel like it has something to do with my css linkage, but I have tried a bunch of different combinations and still can't get it to work.
HTML
<a href="#">
<button>Click for Plans</button>
</a>
css
button {
background-color: #3AF8AB;
border-style: none;
color: white;
position: absolute;
border-radius: 10px;
font-size: 3.5vmin;
margin-top: 0px;
font-weight: bold;
padding: 7px;
margin-left: 73%;
margin-right: 1%;
cursor: pointer;
}
.button:hover {
background-color: blue;
transition: 0.7s;
}
.button:active {
background-color: blue;
box-shadow: 0 5px #666;
transform: translatey(4px);
}
button:hover and button:active in css are class and you have element. You can add the class button to your button or change the style to button ( and not .button )
button {
background-color: #3AF8AB;
border-style: none;
color: white;
position: absolute;
border-radius: 10px;
font-size: 3.5vmin;
margin-top: 0px;
font-weight: bold;
padding: 7px;
margin-left: 73%;
margin-right: 1%;
cursor: pointer;
}
button:hover {
background-color: blue;
transition: 0.7s;
}
button:active {
background-color: blue;
box-shadow: 0 5px #666;
transform: translatey(4px);
}
<button>Click for Plans</button>
In my project I have a list of 'result items' which can be favourited and then they appear on in a favourites column to the side of the page.
To allow keyboard users easy access, I have created a hidden cta which allows them to skip directly to their favourited item, without having to tab through all the result items first.
I only want this cta to be visible when it is tabbed to. My understanding is that :focus-visible would enable this. However, for some reason, it's not working for me. My HTML and CSS look like:
.sr-only-focusable {
position: absolute;
visibility: hidden;
left: 56%;
top: rem(65);
border-radius: 5px;
border: 1px solid #d7d7d7;
background-color: $white;
padding: 1rem 2rem;
z-index: 10;
}
.sr-only-focusable:focus-visible {
visibility: visible;
}
<div class="sr-only-focusable">
<button tabindex="0" #click="skipToShortlist">Skip To Shortlist</button>
</div>
Could anyone help me figure out where i'm going wrong?
Thanks in advance
The button element is what you're trying to tab to, not the div it's contained in. Rather than trying to get to the div, target the button instead. Also, use opacity because visibility doesn't seem to play nicely with tabindex. You can also apply your styling to the button as well.
.sr-only-focusable {
position: absolute;
left: 56%;
top: rem(65);
z-index: 10;
}
.myBtn {
display: inline-block;
font-family: Helvetica, Arial, sans-serif;
color: black;
margin: 0 auto;
font-size: 1.2vw;
font-weight: 700;
line-height: 1.25;
letter-spacing: 0.5px;
cursor: pointer;
text-decoration: none;
opacity: 0;
border-radius: 5px;
border: 1px solid #d7d7d7;
background-color: $white;
padding: 1rem 2rem;
}
.myBtn:focus-visible {
opacity: 1;
}
<div class="sr-only-focusable">
<button class="myBtn" tabindex="0" #click="skipToShortlist">Skip To Shortlist</button>
</div>
From my experience (mostly on chrome) elements with display:none or visibility:hidden cannot be focused by mouse or by keyboard.
Most implementations use something along the lines of this:
.wrapper {
background: black;
text-align: center;
width: 360px;
height: 100px;
padding: 40px;
box-sizing: border-box;
color: blanchedalmond;
}
.image-nav-button {
color: blanchedalmond;
text-decoration: none;
font-size: 20px;
line-height: 1em;
width:1px;
height:1px;
position:absolute;
clip: rect(1px,1px,1px,1px);
clip-path: inset(50%);
}
.next {
float: right;
}
.prev {
float: left;
}
.image-nav-button:focus {
outline: none;
/* Try uncommenting below, then clicking the buttons */
/* outline: 3px solid red; */
}
.image-nav-button:focus-visible {
outline: 3px solid blanchedalmond;
width:auto;
height:auto;
position:relative;
clip: none;
clip-path: none;
}
<div class="wrapper">
← Prev Image
Next Image →
</div>
I'm trying to create a search bar in vue. Right now the search bar opens from the left side, but I want it to open from the right side. Instead of background image, I want to use a font awesome icon as well. How do I achieve these two?
<template>
<span id="demo-2">
<input type="search" placeholder="Search">
</span>
</template>
<style scoped>
input {
outline: none;
}
input[type=search] {
-webkit-appearance: textfield;
-webkit-box-sizing: content-box;
font-family: inherit;
font-size: 100%;
}
input[type=search] {
background: #ededed url(https://static.tumblr.com/ftv85bp/MIXmud4tx/search-icon.png) no-repeat 9px center;
border: solid 1px #ccc;
padding: 9px 10px 9px 32px;
width: 55px;
-webkit-border-radius: 10em;
-moz-border-radius: 10em;
border-radius: 10em;
-webkit-transition: all .5s;
-moz-transition: all .5s;
transition: all .5s;
}
#demo-2 input[type=search] {
width: 15px;
padding-left: 10px;
color: transparent;
cursor: pointer;
}
#demo-2 input[type=search]:hover {
background-color: #fff;
}
#demo-2 input[type=search]:focus {
width: 130px;
padding-left: 32px;
color: #000;
background-color: #fff;
cursor: auto;
}
#demo-2 input:-moz-placeholder {
color: transparent;
}
#demo-2 input::-webkit-input-placeholder {
color: transparent;
}
</style>
I don't know the answer for the animation but if you want to add font awesome icon you can add its link to the main template file for the vuejs . just copy paste you link to the head section in there and use the <i>search icon</i> in the vue code.
Replacing background by a font-awesome icon
You could use :after or :before property in CSS :
https://developer.mozilla.org/en/docs/Web/CSS/::before
It will allow you to display content, linked to your input (like a font-awesome glyphicon for example) :
input {
position: relative;
}
input:after {
content: '\f002';
position: absolute;
right: 10px;
top: 5px;
}
In this case, the font-awesome icon is : \f002 :
https://fontawesome.com/icons/search?style=solid
Animate from the right
If you use :after property and the position is absolute to your input (with a right CSS property), it should open from the right.
I have two buttons on my website. When someone hovers over button A it should become a bit bigger while Button B becomes the same bit smaller. And vice versa for when someone hovers over button B. My question is, how do I change the size elements of a div class from within another div class.
This is the HTML5 code relating to the two button elements. For examples sake we will call the "Im hungry" button, Button A. And "Show me more" button B.
.btn:link,
.btn:visited {
display: inline-block;
padding: 10px 30px;
font-weight: 300;
text-decoration: none;
border-radius: 100px;
text-align: center;
color: white;
transition: background-color 0.5s;
transition: width 0.5s;
}
.btn-full:link,
.btn-full:visited {
background-color: #2ecc71;
border: 2px solid #2ecc71;
width: 14%;
}
.btn-ghost:link,
.btn-ghost:visited {
border: 2px solid #2ecc71;
width: 20%;
}
.btn:hover,
.btn:active {
background-color: #27ae60;
color: white;
}
.btn-full:hover,
.btn-full:active {
width: 16%;
}
.btn-ghost:hover,
.btn-ghost:active {
width: 22%;
}
<a class="btn btn-full" href="#">I’m hungry</a>
<a class="btn btn-ghost" href="#">Show me more</a>
So as another form of example. When I hover over button A it should grow from 14% to 16%, and Button B should simultaneously shrink from 22% to 20%.
You can achieve this effect with the flex-grow property. reference w3schools
.container {
width: 500px;
display: flex;
}
.btn {
background-color: lightgreen;
border: 2px solid green;
padding: 8px;
flex-grow: 1;
transition: all.5s;
}
.btn:hover {
flex-grow: 2;
}
<div class="container">
<a class="btn" href="#">I’m hungry</a>
<a class="btn" href="#">Show me more</a>
</div>
The following solution uses the fact that parent element receives :hover as well:
.buttons a {
float:left;
width: 5em;
height: 3em;
margin: 0 1em;
border-radius: 3em;
text-align: center;
line-height: 3;
color: #fff;
background: navy;
transition: .5s
}
.buttons:hover a {
width: 3em;
background: grey
}
.buttons:hover a:hover {
width: 7em;
background: forestgreen
}
<div class="buttons">
A
B
</div>
I'm trying to create a continue button that shows a right pointing arrow in a hover state. I also want the arrow to be centered.
I've tried adding .icon-arrow-right:before {content: "&rarr";}
body {
background: #00b894;
}
.btn {
font-size: 14px;
background: none;
padding: 25px 80px;
display: inline-block;
margin: 15px 30px;
position: relative;
border: 3px solid #fff;
color: #fff;
overflow: hidden;
transition: all 0.3s;
}
.btn:before {
position: absolute;
height: 100%;
font-size: 125%;
line-height: 3.5;
color: #fff;
transition: all 0.3s;
left: 130%;
top: 0;
}
.icon-arrow-right:before {
content: "#";
}
.btn:hover:before {
left: 80%;
}
<button class="btn icon-arrow-right">Continue</button>
I would like the arrow to be to the right of the button text in a hover state. I would also like the arrow to be centered with the button text.
Make your pseudo element into the triangle you desire:
.icon-arrow-right:before {
margin-top: 21px;
content: "";
width: 0;
height: 0;
border-top: 7px solid transparent;
border-left: 14px solid white;
border-bottom: 7px solid transparent;
}
This uses the awesome transparent border trick to make the element box appear as a triangle. Change the border widths to alter the size of the triangle, notice the border with color is twice as wide as the transparent sides. You can play with these values to nuance the triangle how you like.
I also changed how you are positioning the text in the button:
.btn {
padding: 0 80px; /* padding on sides only */
height: 64px; /* height of the button you want */
line-height: 58px; /* same as height minus the border-top and border-bottom */
border: 3px solid #fff;
}
By using line-height this way you can guarantee your text will be vertically centered in the button at any font-size.
Check out the full working example:
body {
background: #00b894;
}
.btn {
font-size: 14px;
background: none;
padding: 0 80px; /* padding on sides only */
height: 64px; /* height of the button you want */
line-height: 58px; /* same as height minus the border-top and border-bottom */
display: inline-block;
margin: 15px 30px;
position: relative;
border: 3px solid #fff;
color: #fff;
overflow: hidden;
transition: all 0.3s;
cursor: pointer;
}
.btn:before {
position: absolute;
height: 100%;
font-size: 125%;
line-height: 3.5;
color: #fff;
transition: all 0.3s;
left: 130%;
top: 0;
}
.icon-arrow-right:before {
margin-top: 21px;
content: "";
width: 0;
height: 0;
border-top: 7px solid transparent;
border-left: 14px solid white;
border-bottom: 7px solid transparent;
}
.btn:hover:before {
left: 80%;
}
<button class="btn icon-arrow-right">Continue</button>
I have vertically centered the arrow with top:50%;transform:translateY(-50%);. I removed the line-height and height CSS from the .btn pseudo element because they weren't needed. I have just used > for the arrow, but you can use something like fontawesome to get a nice icon.
body {
background: #00b894;
}
.btn {
font-size: 14px;
background: none;
padding: 25px 80px;
display: inline-block;
margin: 15px 30px;
position: relative;
border: 3px solid #fff;
color: #fff;
overflow: hidden;
transition: all 0.3s;
}
.btn:before {
position: absolute;
font-size: 125%;
color: #fff;
transition: all 0.3s;
left: 130%;
top:50%;
transform:translateY(-50%);
}
.icon-arrow-right:before {
content: ">";
}
.btn:hover:before {
left: 80%;
}
<button class="btn icon-arrow-right">Continue</button>
Just expanding on #BugsArePeopleToo 's answer, using borders ans transforms to keep the ">" shape the OP wanted.
body {
background: #00b894;
}
.btn {
font-size: 14px;
background: none;
padding: 0 80px; /* padding on sides only */
height: 64px; /* height of the button you want */
line-height: 58px; /* same as height minus the border-top and border-bottom */
display: inline-block;
margin: 15px 30px;
position: relative;
border: 3px solid #fff;
color: #fff;
overflow: hidden;
transition: all 0.3s;
cursor: pointer;
}
.btn:before {
position: absolute;
height: 100%;
font-size: 125%;
line-height: 3.5;
color: #fff;
transition: all 0.3s;
left: 130%;
top: 0;
}
.icon-arrow-right:before {
content: "";
width: 0.5em;
height: 0.5em;
position:absolute;
top:50%;
border-top:2px solid #fff;
border-right:2px solid #fff;
transform:translateY(-50%) rotate(45deg);
}
.btn:hover:before {
left: 80%;
}
<button class="btn icon-arrow-right">Continue</button>