CSS link button animation on hover pushes content [duplicate] - html

This question already has an answer here:
CSS Divs Jumping when Border Added
(1 answer)
Closed 7 months ago.
I have a link button in a div:
<div class="text-box">
<h1 class="main-title">
<span class="main-title--primary">outdoors</span>
<span class="main-title--secondary">is where life happens</span>
</h1>
discover
</div>
And css property for it:
.main-cta-btn:link,
.main-cta-btn:visited {
display: inline-block;
background: white;
color: #28b485;
border: none;
border-radius: 50px;
padding: 10px 40px;
font-weight: bold;
font-size: 20px;
text-transform: uppercase;
animation: moveBottom 2s ease-in;
text-decoration: none;
}
When I try add hover affect to it with this code:
.main-cta-btn:hover,
.main-cta-btn:active {
background: transparent;
color: white;
border: 2px solid white;
cursor: pointer;
}
The link push up other div elements in the text-box div. How I can fix this push effect and I must say I have absolute positioning on main-title for centering it in the page.
main-title css property:
.text-box {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}

It's this border: 2px solid white; added on hover who's causing the issue, by increasing the width and hight of the button. The trick is to have that border from the beginning but with a transparent colour, and just change the colour on hover.
.main-cta-btn:link,
.main-cta-btn:visited {
display: inline-block;
background: white;
color: #28b485;
border: none;
border-radius: 50px;
padding: 10px 40px;
font-weight: bold;
font-size: 20px;
text-transform: uppercase;
animation: moveBottom 2s ease-in;
text-decoration: none;
/* Line I added */
border: 2px solid transparent;
}
.main-cta-btn:hover,
.main-cta-btn:active {
background: transparent;
color: red;
/* Line I added */
border-color: red;
cursor: pointer;
}
.text-box {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
<div class="text-box">
<h1 class="main-title">
<span class="main-title--primary">outdoors</span>
<span class="main-title--secondary">is where life happens</span>
</h1>
discover
</div>

Related

CSS animation on the container instead of on the link

I have a CSS border animation on an HTML link. The animation sets on mouse hover and it automatically adjusts to the with of the link. Once the mouse is hover, border animations appear in pairs, left-right + top-down. When the animation is over the borders will form a square around the link.
It works fine until the link gets a line break and instead of one line I have a link with two lines. In that case the animation will form around the top line in the link and ignore the line break.
I have been trying and trying and I cannot figure out a way to make the animation go around the whole link instead of only around the first meaning. Can anyone help me out?
Code Pen: https://codepen.io/jo-o-figueiredo/pen/KKZOjWM
Thanks in advance!
div.caixa {
margin: 4em auto;
padding: 4em;
box-sizing: border-box;
text-align: center;
}
.sparkle {
max-width: 10em;
color: #5a4d1a;
margin: auto auto;
font-size: 25px;
line-height: 40px;
text-decoration: underline;
text-decoration-color: #b1d6b1;
text-underline-offset: 0.5em;
text-decoration-thickness: 3px;
font-weight: 500;
}
.sparkle:hover {
cursor: pointer;
text-decoration: none;
color: #1a1a1a;
}
.u-hover--sparkle {
box-sizing: border-box;
position: relative;
padding: 0.75em;
}
.u-hover--sparkle::before,
.u-hover--sparkle::after {
content: "";
box-sizing: border-box;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
transform-origin: center;
transition: transform .20s;
}
.u-hover--sparkle::before {
border-top: 1px solid #1a1a1a;
border-bottom: 1px solid #1a1a1a;
transform: scale3d(0, 1, 1);
}
.u-hover--sparkle::after {
border-left: 1px solid #1a1a1a;
border-right: 1px solid #1a1a1a;
transform: scale3d(1, 0, 1);
}
.u-hover--sparkle:hover::before,
.u-hover--sparkle:hover::after {
transform: scale3d(1, 1, 1);
transition: transform .35s;
}
<div class="wpb_text_column wpb_content_element caixa">
<div class="wpb_wrapper">
<p>
<a class="sparkle u-hover--sparkle" href="#paket" rel="noopener">Sällskap -
Sammankomster</a>
</p>
</div>
</div>
Set the .sparkle to display block so it covers the whole thing.
Also define how your text should break, because if it's a long word, it has no choice to go out of bound by default.
.sparkle {
display: block;
word-break: break-all;
/* rest of your code */
}
div.caixa {
margin: 4em auto;
padding: 4em;
box-sizing: border-box;
text-align: center;
}
.sparkle {
max-width: 10em;
color: #5a4d1a;
margin: auto auto;
font-size: 25px;
line-height: 40px;
text-decoration: underline;
text-decoration-color: #b1d6b1;
text-underline-offset: 0.5em;
text-decoration-thickness: 3px;
font-weight: 500;
display: block;
word-break: break-word;
}
.sparkle:hover {
cursor: pointer;
text-decoration: none;
color: #1a1a1a;
}
.u-hover--sparkle {
box-sizing: border-box;
position: relative;
padding: 0.75em;
}
.u-hover--sparkle::before,
.u-hover--sparkle::after {
content: "";
box-sizing: border-box;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
transform-origin: center;
transition: transform .20s;
}
.u-hover--sparkle::before {
border-top: 1px solid #1a1a1a;
border-bottom: 1px solid #1a1a1a;
transform: scale3d(0, 1, 1);
}
.u-hover--sparkle::after {
border-left: 1px solid #1a1a1a;
border-right: 1px solid #1a1a1a;
transform: scale3d(1, 0, 1);
}
.u-hover--sparkle:hover::before,
.u-hover--sparkle:hover::after {
transform: scale3d(1, 1, 1);
transition: transform .35s;
}
<div class="wpb_text_column wpb_content_element caixa">
<a class="sparkle u-hover--sparkle" href="#paket" rel="noopener">Sällskap -
Sammankomster</a>
</div>
I think there are multiple things to point out:
Easy Fix: Add display: block; to the .sparkle class and increase max-width to enable the text to stand in one line.
Alternatively you could also apply the animation styles to the divs surrounding the Link
Optional: I think you could also remove the <p> element surrounding the link as it is unnecessarily making your markup more complex.

How to make ::after content have the same width an height as the previous?

I want to make a small button hover animation. the animation is like that:
a blue div which have the same width an height as the button comes from the bottom left of the button to cover it. The problem is that they have not the same height and width.
Here's the HTML and the css :
[![button{
box-sizing: border-box;
background-color: transparent;
border: none;
display: inline-block;
}
.button-wrapper {
position: relative;
overflow: hidden;
padding: 15px 30px;
font-family: 'Montserrat', sans-serif;
font-weight: bold;
font-size: 14px;
line-height: 20px;
border: solid 2px #2A5A8B;
border-radius: 6px;
text-decoration: none;
color: #2A5A8B;
cursor : pointer;
transition: all .5s;
z-index: 1;
}
.button-wrapper::after{
width: 100%;
height: 100%;
margin: auto;
position: absolute;
bottom: -100%;
right: -100%;
background-color: #2A5A8B;
transition: all .25s;
z-index: -1;
content: '';
}
.button-wrapper:hover{
color: white;
}
.button-wrapper:hover::after{
transform: translateX(-100%) translateY(-100%);
}
<button>
<div className='button-wrapper'>
{Login}
</div>
</button>
PS: it's working properly on firefox.

How to keep components auto resizable while creating templates in React JS?

So I am trying to put a background image and then center the text to it, but when I change the screen size the text misaligns and moves to the top.
Here's my code:
import React from "react";
import UniversalValues from "../constants.js";
export default function Body() {
return (
<div>
<div className="Container">
<img
className="ResizeImage"
src="https://images.unsplash.com/photo-1533139143976-30918502365b?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max"
alt="Background not loading! X_X"
/>
<div className="TextField">
<h1 className="BGH1">WE DO IT TOGETHER</h1>
<h1 className="BGH1">SO LET'S CREATE</h1>
</div>
</div>
</div>
);
}
Here is my css file:
.App {
font-family: sans-serif;
text-align: center;
}
.BGH1 {
letter-spacing: 0.06em;
font-family: "Cinzel", serif;
font-size: 5rem;
position: relative;
}
.BrandPoint {
font-size: 3rem;
font-family: "Cinzel", serif;
padding-left: 3rem;
}
.Container {
position: relative;
}
.dropbtn {
background: transparent;
color: white;
padding-bottom: 10px;
font-size: 1.2rem;
font-weight: bold;
border: none;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #33393f;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
/* Links inside the dropdown */
.dropdown-content a {
color: white;
padding: 12px 16px;
text-decoration: none;
display: block;
}
/* Change color of dropdown links on hover */
.dropdown-content a:hover {
transition: transform 0.5s ease;
transform: scale(0.9);
color: #3eccb5;
border: 1px solid;
border-radius: 0.2rem;
border-color: #e0dede;
}
/* Show the dropdown menu on hover */
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
transition: transform 0.5s ease;
transform: scale(0.9);
color: #3eccb5;
border: 1px solid;
border-radius: 0.2rem;
border-color: #e0dede;
}
.HeaderElem {
color: white;
padding-bottom: 10px;
font-size: 1.2rem;
}
.HeaderElem:hover {
transition: transform 0.5s ease;
transform: scale(0.9);
color: #3eccb5;
border: 1px solid;
border-radius: 0.2rem;
border-color: #e0dede;
}
.HeaderSeperator {
padding-left: 2rem;
}
.HeadUp {
position: fixed;
top: 0;
left: 0;
}
.ResizeImage {
height: auto;
width: 100%;
margin: 0;
padding: 0;
opacity: 0.99;
}
.TextField {
position: absolute;
bottom: 40%;
left: 35%;
color: white;
padding-left: 20px;
padding-right: 20px;
}
What I am getting vs What I wanted:
I want to pack this whole thing together and so it could just effectively change its shape according to the screen size. I have used Bootstrap but I am new to React and designing my entire website on codesandbox.io. Do let me know the best way to do so.
Thanks in advance.
I think your problem is purely HTML/CSS and not so much React. Thanks for flexbox centering content got a lot easier today.
Change your HTML like this:
<div>
<div class="Container">
<div class="imageContainer"></div>
<div class="TextField">
<h1 class="BGH1">WE DO IT TOGETHER</h1>
<h1 class="BGH1">SO LET'S CREATE</h1>
</div>
</div>
</div>
And your CSS like this:
.Container {
display: flex;
justify-content: center;
align-items: center;
background-color:blue;
position: relative;
color: white;
text-align: center;
height: 300px;
}
.imageContainer {
position: absolute;
width: 100%;
height: 100%;
opacity: 0.2;
background-image: url("https://images.unsplash.com/photo-1533139143976-30918502365b?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max");
background-position: center center;
}
Here is a fiddle test it: https://jsfiddle.net/gtqna9c1/7/
How it works
Instead of trying to center changing text by absolute positioning it is a lot easier to create conditions where the text is always centered, no matter what. The background image is now a simple background of a div that is covering your whole box.
The fiddle is simplified and coloured for clarity. Feel free to add paddings etc. as you wish.

Top and bottom: different transition

I have an oval, position defined by absolute and "top: -2px". If I add a border to it when hovered, the result is the lower part of the following image, which is still aligned to the center. However if I define the position by "bottom: ??px", the result becomes the upper part, in which the oval is raised up.
Can I achieve the lower part result when I define the position by "bottom"?
CSS for oval:
#oval
{
position: absolute;
top: -2px;
left: 50%;
transform: translate(-50%, -50%);
padding: 5px 15px;
cursor: pointer;
border: none;
border-radius: 20px;
text-align: center;
color: #fff;
font-size: 15;
-webkit-transition-duration: 0.5s;
transition-duration: 0.5s;
}
#oval:hover
{
border: solid //a new color ;
background-color: //a new color ;
color: #000;
}
Its "relative" parent:
.parent
{
position: relative;
width: 600px;
padding: 30px 10px 10px 10px;
border-style: solid;
border-radius: 20px;
margin: auto;
text-align: center;
font-family: "Arial";
font-size: 14;
}
HTML
<div class="parent">
<input type="button" id="oval" value="<?php echo $value; ?>"
onclick='window.open("<?php echo $link; ?>")'>
....
JSF demo
When changing border colours on hover, it's often a good idea to start off with a transparent border and modify that on hover, rather than adding and removing a border each time. This will save you a lot of headaches such as jumping positions, changing box etc.
#oval
{
position: absolute;
top: -2px;
left: 50%;
transform: translate(-50%, -50%);
padding: 5px 15px;
cursor: pointer;
/* OVER HERE */
border: 2px solid transparent;
border-radius: 20px;
text-align: center;
color: #fff;
font-size: 15;
-webkit-transition-duration: 0.5s;
transition-duration: 0.5s;
}
#oval:hover
{
border-color: black;
background-color: red;
color: black ;
}
Found the solution already.
transform: translate(50%, 50%); right:50% when defining by bottom
transform: translate(-50%, -50%); left:50% when defining by top

Element top margin doesn't seem to change

I have some text, and beneath it a button. No matter what I change the top margin to, the button still sticks to the bottom of the text.
I am trying to leave a small gap between the text and the button.
Here is a Jsfiddle: http://jsfiddle.net/24bk2t78/
#import url(http://fonts.googleapis.com/css?family=Nunito:300);
.homethree a { text-decoration: none; }
sup { font-size: 36px; font-weight: 100; line-height: 55px; }
.button
{
margin-top:30%!important;
text-transform: uppercase;
letter-spacing: 2px;
text-align: center;
color: #0C5;
font-size: 24px;
font-family: "Nunito", sans-serif;
font-weight: 300;
position:relative;
padding: 20px 0;
width: 220px;
height:30px;
background: #0D6;
border: 1px solid #0D6;
color: #FFF;
overflow: hidden;
transition: all 0.5s;
}
.button:hover, .button:active
{
text-decoration: none;
color: #0C5;
border-color: #0C5;
background: #FFF;
}
.button span
{
padding-right: 0;
transition: padding-right 0.5s;
}
.button span:after
{
content: ' ';
right: -18px;
opacity: 0;
width: 10px;
height: 10px;
background: rgba(0, 0, 0, 0);
border: 3px solid #FFF;
border-top: none;
border-right: none;
transition: opacity 0.5s, top 0.5s, right 0.5s;
transform: rotate(-45deg);
}
.button:hover span, .button:active span
{
padding-right: 30px;
}
.button:hover span:after, .button:active span:after
{
transition: opacity 0.5s, top 0.5s, right 0.5s;
opacity: 1;
border-color: #0C5;
right: 0;
top: 50%;
}
<div class="row text-center homethree">
<div class="col-md-4">
<h4 class="service-heading">A Range of Classes</h4>
<p class="text-muted">WE TEACH CHILDRENS CLASSES, FAMILY GROUPS & ADULTS.</p>
<span>page 1</span>
</div>
<div class="col-md-4">
<h4 class="service-heading">Passionate Instructors</h4>
<p class="text-muted">ALL OUR CLASSES ARE TAUGHT BY PASSIONATE, MOTIVATIONAL & INSPIRING INSTRUCTORS.</p>
<span>page 2</span>
</div>
<div class="col-md-4">
<h4 class="service-heading">Friendly Team</h4>
<p class="text-muted">NEW MEMBERS ARE ALWAYS WELCOME. TWO FREE LESSONS FOR ALL.</p>
<span>page 3</span>
</div>
</div>
The problem is that <a> tags are inline elements. Changing them to an inline-block or block style element will make your margins properly apply.
.button {
display: inline-block;
}
More info: The Difference Between “Block” and “Inline”
On a sidenote, why not use <button> tags instead of <a>? They would be more appropriate.
try this demo
Fiddle
.button{
float:left;
margin-top:0;
}
.col-md-4
{
float:left;
width:100%
}