Top and bottom: different transition - html

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

Related

Why isn't my button translating correctly in my div?

I'm new to HTML/CSS and attempting to replicate this button effect in a project of mine where the buttons are children of a div. However, whenever I do the translateY, my buttons move to the side instead. Here is the HTML and CSS; button-2 is the problematic one:
.container {
position: relative width: 600px;
height: 600px;
}
#button-1 {
position: absolute;
padding: 15px 25px;
font-size: 24px;
cursor: pointer;
text-align: center;
text-decoration: none;
outline: none;
color: #fff;
background-color: #4CAF50;
border: ridge;
border-radius: 15px;
box-shadow: 0 9px #999;
top: 50%;
left: 25%;
}
#button-1:hover {
background-color: #3e8e41
}
#button-1:active {
background-color: #3e8e41;
box-shadow: 0 5px #666;
transform: translateY(10px);
}
#button-2 {
position: absolute;
width: 100px;
padding: 5px 5px;
border-style: solid;
border-width: 2px;
border-radius: 10px;
border-color: darkslategray;
background-color: antiquewhite;
box-shadow: 0px 9px gray;
cursor: pointer;
top: 62%;
left: 50%;
/* if i remove this line, translation works as expected
but then the button is no longer centered horizontally
consequently, keeping the line breaks the affect
but the button can be centered horizontally
*/
transform: translate(-50%, 0);
}
#button-2:hover {
background-color: cornsilk;
}
#button-2:active {
background-color: cornsilk;
box-shadow: 0px 3px gray;
transform: translateY(5px);
}
<h2>Animated Button - "Pressed Effect"</h2>
<div class=container>
<button id="button-1">Btn1</button>
<button id="button-2">Btn2</button>
</div>
The problem seems to stem from transform: translate(-50%, 0);, which is what I have to do to center button-2 horizontally in the container div. I can remove that line and the effect works, but then button-2 is no longer centered horizontally. Why is it that transform: translate(-50%, 0); seemingly causes the subsequent transform: translateY(5px); not to work for button-2? Is there anyway I can horizontally (but not vertically) center button-2 while also being able to achieve the desired effect?
The problem is that when you set the translate property on the second button becoming active, you overwrite the original transform which is moving the button by half its width in the negative X direction.
It is important to keep this translation as well as the new one so make the translation in the active state set both the X and Y values that you want.
.container {
position: relative width: 600px;
height: 600px;
}
#button-1 {
position: absolute;
padding: 15px 25px;
font-size: 24px;
cursor: pointer;
text-align: center;
text-decoration: none;
outline: none;
color: #fff;
background-color: #4CAF50;
border: ridge;
border-radius: 15px;
box-shadow: 0 9px #999;
top: 50%;
left: 25%;
}
#button-1:hover {
background-color: #3e8e41
}
#button-1:active {
background-color: #3e8e41;
box-shadow: 0 5px #666;
transform: translateY(10px);
}
#button-2 {
position: absolute;
width: 100px;
padding: 5px 5px;
border-style: solid;
border-width: 2px;
border-radius: 10px;
border-color: darkslategray;
background-color: antiquewhite;
box-shadow: 0px 9px gray;
cursor: pointer;
top: 62%;
left: 50%;
/* if i remove this line, translation works as expected
but then the button is no longer centered horizontally
consequently, keeping the line breaks the affect
but the button can be centered horizontally
*/
transform: translate(-50%, 0);
}
#button-2:hover {
background-color: cornsilk;
}
#button-2:active {
background-color: cornsilk;
box-shadow: 0px 3px gray;
transform: translate(-50px, 5px);
}
<h2>Animated Button - "Pressed Effect"</h2>
<div class=container>
<button id="button-1">Btn1</button>
<button id="button-2">Btn2</button>
</div>
Note: as others have suggested you may like to look into using flex (or grid) to position your buttons rather than having to mess around with positioning using absolute and translations. The translate for Y could then remain as in the original and you wouldn't be translating in the X direction at all.
you have gotten your X and Y axis mixed up.
you translated via translate(-50%, 0) which moves it by an X of -50% and a Y of nothing, you can fix this by changing it to translate(0, -50%) which will move it down a Y of -50%, and an X of 0.
test the snippet:
.container {
position: relative width: 600px;
height: 600px;
}
#button-1 {
position: absolute;
padding: 15px 25px;
font-size: 24px;
cursor: pointer;
text-align: center;
text-decoration: none;
outline: none;
color: #fff;
background-color: #4CAF50;
border: ridge;
border-radius: 15px;
box-shadow: 0 9px #999;
top: 50%;
left: 25%;
}
#button-1:hover {
background-color: #3e8e41
}
#button-1:active {
background-color: #3e8e41;
box-shadow: 0 5px #666;
transform: translateY(10px);
}
#button-2 {
position: absolute;
width: 100px;
padding: 5px 5px;
border-style: solid;
border-width: 2px;
border-radius: 10px;
border-color: darkslategray;
background-color: antiquewhite;
box-shadow: 0px 9px gray;
cursor: pointer;
top: 62%;
left: 50%;
/* if i remove this line, translation works as expected
but then the button is no longer centered horizontally
consequently, keeping the line breaks the affect
but the button can be centered horizontally
*/
transform: translate(0, -50%);
}
#button-2:hover {
background-color: cornsilk;
}
#button-2:active {
background-color: cornsilk;
box-shadow: 0px 3px gray;
transform: translateY(5px);
}
<h2>Animated Button - "Pressed Effect"</h2>
<div class=container>
<button id="button-1">Btn1</button>
<button id="button-2">Btn2</button>
</div>

CSS link button animation on hover pushes content [duplicate]

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>

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.

CSS showing different render behavior on different zoom level

I created this Tooltip element using HTML and CSS. But after a while I realized that the pointer of my tooltip is getting cut. At first I thought it was some overflow issue but it wasn't. Then I tried to change the zoom level of my chrome and I got this
ZOOM LEVEL <= 250%
As you can see the pointer of the tooltip (highlighted in red) is getting cut.
ZOOM LEVEL => 250%
Now, as you can see the same tooltip on different zoom level worked or rendered just fine.
Anyone can tell me what is the actual problem behind all this or this is a browser issue??
CODE FOR TOOLTIP
<div class="tooltip">
<div class="tooltip-pointer" data-pointer-direction="bottom"></div>
<div class="tooltip-content">Hello, World!</div>
</div>
.tooltip {
position: absolute;
color: white;
max-width: 196px;
font-weight: 600;
user-select: none;
border-radius: 4px;
font-size: 0.875rem;
pointer-events: none;
word-wrap: break-word;
transform-origin: 50% 0;
letter-spacing: 0.03125rem;
background-color: #18191c;
will-change: opacity, transform;
box-shadow: 0 5.18px 10.36px -3.89px rgba(0, 0, 0, 0.25);
.tooltip-pointer {
width: 0;
height: 0;
position: absolute;
pointer-events: none;
border: 5px solid transparent;
border-top-color: #18191c;
&[data-pointer-direction='bottom'] {
top: 100%;
left: 50%;
margin-left: -5px;
}
}
}
UPDATE
I haven't found any solution though instead what I did I change the pointer of tooltips from HTML to SVG after doing this the tooltip arrows or pointers is not getting cut on different zoom level or any zoom level. If anyone want the code DM me.
Why don't you try creating the down arrow in a different way? Something like this: https://jsfiddle.net/2jwhnrm3/
HTML
<div class="tooltip-pointer"></div>
<div class="tooltip">
<div class="tooltip-content">Hello, World!</div>
</div>
CSS
.tooltip {
position: absolute;
color: white;
max-width: 196px;
font-weight: 600;
user-select: none;
border-radius: 4px;
font-size: 0.875rem;
pointer-events: none;
word-wrap: break-word;
transform-origin: 50% 0;
letter-spacing: 0.03125rem;
background-color: #18191c;
will-change: opacity, transform;
box-shadow: 0 5.18px 10.36px -3.89px rgba(0, 0, 0, 0.25);
}
.tooltip-pointer {
border: solid red;
background: red;
border-width: 0 3px 3px 0;
display: inline-block;
position: absolute;
padding: 5px;
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
top: 17px;
left: 50px;
margin-left: -5px;
}

How do I make a post heading decoration

I am trying to recreate the decorative line under the heading. Is there anyway that I can do this using just HTML and CSS? Does anyone have any experience in doing something similar?
I thought about doing something like this:
<p>———•———</p>
unfortunately the outcome is not as expected.
I would use a bottom border for the line and then a pseudo element for the dot.
The dot uses use border-radius to make the element spherical. Then we use background-color to make it white and set the border color to the same color as the background, allowing us to mask out parts of the bottom border.
body {
background-color: salmon;
}
h2 {
margin: 1rem 0 0.5rem;
padding-bottom: 0.5rem;
font: 2rem/1.25 Arial, sans-serif;
color: white;
border-bottom: 1px solid white;
text-align: center;
position: relative;
}
h2::after {
content: '';
position: absolute;
z-index: 5;
transform: translateX( -50% );
bottom: -13px; /* border thickness + half height */
left: 50%;
width: 6px;
height: 6px;
background-color: white;
border: 10px solid salmon;
border-radius: 50%;
}
<h2>Portfolio</h2>
One caveat with this approach is that you will need sufficient space between the dot and the text above, otherwise, the border thickness of the dot that is used to mask out the bottom border will overlay/mask your text. See example below:
body {
background-color: salmon;
}
h2 {
margin: 1rem 0 0.5rem;
padding-bottom: 0.5rem;
font: 2rem/1.25 Arial, sans-serif;
color: white;
border-bottom: 1px solid white;
text-align: center;
position: relative;
}
h2::after {
content: '';
position: absolute;
z-index: 5;
transform: translateX( -50% );
bottom: -23px; /* border thickness + half height */
left: 50%;
width: 6px;
height: 6px;
background-color: white;
border: 20px solid gold;
border-radius: 50%;
}
<h2>Portfolio</h2>
You could also do something similar but make the content of the pseudo element a bullet •.