CSS position absolute elements not aligning properly with their sibling elements - css

I'm building out a custom Tooltip component in React that allows for rendering both to the right of the element as well as below it. I've decided to handle the functionality by having the Tooltip accept children which it then renders, and when those children are hovered the tip itself, positioned absolutely, will become visible through CSS classes. The effect works exactly how I want when the elements are stacked in a column and the tooltip is set to render to the right, but when the elements are laid out in a row with the tip rendering on the bottom, the tip itself becomes misaligned from the child its rendering -- the length of the tooltip text seems to offset it to the right.
Here's a codesandbox displaying the problem. You'll see that in the first state, when the elements are in a column, the tip is aligned perfectly. Click the button to toggle them into a row format and hover again to see that they are now misaligned:
https://codesandbox.io/s/busted-tooltip-obf0h1?file=/src/Tooltip.scss
How can I get the tips to align centered with their child element when I've chosen to have them render below it?
// Tooltip.jsx
import React, { useState } from 'react';
import './Tooltip.scss';
const Tooltip = ({ children, label, orientation = 'right' }) => {
const [isHovered, setIsHovered] = useState(false);
return (
<div className={`Tooltip__Container ${orientation}`}>
<div
onMouseOver={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
{children}
</div>
<div className={`Tooltip__Inner ${orientation} ${isHovered ? 'Visible' : ''}`}>
{orientation === 'right' && <div className={`Tooltip__PointLeft ${isHovered ? 'Visible' : ''}`} />}
{orientation === 'bottom' && <div className={`Tooltip__PointUp ${isHovered ? 'Visible' : ''}`} />}
<div className={`Tooltip ${orientation} ${isHovered ? 'Visible' : ''}`}>
{label}
</div>
</div>
</div>
);
};
export default Tooltip;
/* Tooltip.scss */
#import 'src/styles/colors.scss', 'src/styles/typography.scss', 'src/styles/breakpoints.scss';
.Tooltip__Container {
display: flex;
position: relative;
&.right {
flex-direction: row;
}
&.bottom {
flex-direction: column;
}
}
.Tooltip__Inner {
display: flex;
z-index: -1;
position: absolute;
transition: all .25s ease;
opacity: 0;
left: 0;
top: 0;
&.right {
flex-direction: row;
}
&.bottom {
flex-direction: column;
}
}
.Tooltip__Inner.right.Visible {
opacity: 1;
left: 3.5rem;
}
.Tooltip__Inner.bottom.Visible {
opacity: 1;
top: 3.5rem;
}
.Tooltip__PointLeft.Visible {
opacity: 1;
width: 0;
height: 0;
border-top: 10px solid transparent;
border-bottom: 10px solid transparent;
border-right: 10px solid $white-transparent;
align-self: center;
justify-self: center;
}
.Tooltip__PointUp.Visible {
opacity: 1;
width: 0;
height: 0;
width: 0;
height: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid $white-transparent;
align-self: center;
justify-self: center;
}
.Tooltip {
background-color: $white-transparent;
padding: .75rem;
border-radius: 3px;
#include MainFont;
color: white;
}

For quick solution, you can update the bottom class inside .Tooltip__Inner
with this:
&.bottom {
flex-direction: column;
left: 50%;
-ms-transform: translate(-50%, 0);
transform: translate(-50%, 0);
}

Related

Negative margin value hides element

Im trying to make a carousel with arrows. for the arrows to be in the right position and look pretty, im using a negative margin (margin: -Npx).
This causes the arrow to be hidden inside the carousel.
ive trying applying z-index: 9999.
overflow-x of carousel cannot be visible (per design)
const CarouselWrapper = styled.div`
display: flex;
flex-wrap: nowrap;
overflow-x: hidden;
position: relative;
scroll-snap-type: x mandatory;
&::-webkit-scrollbar {
display: none;
}
& > * {
scroll-snap-align: start;
}
`;
const ArrowsContainer = styled.div`
display: flex;
align-items: center;
position: absolute;
width: 100%;
justify-content: space-between;
top: 50%;
transform: translateY(-50%);
pointer-events: none;
`;
const ArrowWrapper = styled.div`
background: white;
border-radius: 50%;
display: flex;
opacity: 0.7;
padding: ${rem(8)};
pointer-events: auto;
box-shadow: 0px 10px 6.2px -4.95px rgba(0, 0, 0, 0.06),
0px 0px 10.2px 0.55px rgba(0, 0, 0, 0.02);
visibility: ${({ show }) => (show ? 'visible' : 'hidden')};
&:first-child {
margin-left: ${rem(-20)};
}
`;
const CarouselContainer = styled.div`
position: relative;
`;
<CarouselContainer>
<CarouselWrapper ref={ref}>{children}</CarouselWrapper>
{showArrows && (
<ArrowsContainer>
<ArrowWrapper
show={scrollPosition !== 0}
onClick={() => scrollToNext(ref.current, -1)}
>
<ChevronLeft style={{ width: 24, height: 24 }} />
</ArrowWrapper>
<ArrowWrapper
show={scrollPosition !== maxScrollValue}
onClick={() => scrollToNext(ref.current)}
>
<ChevronRight style={{ width: 24, height: 24 }} />
</ArrowWrapper>
</ArrowsContainer>
)}
</CarouselContainer>
Seems the
`&:first-child {
margin-left: ${rem(-20)};
}`
has an effect on both the left and the right arrows as you are targeting in both cases the <ChevronLeft /> and the <ChevronRight /> with the &:first-child selector. You can remove this from the ArrowWrapper styled component and add the margin in the style tag inside the <ChevronLeft /> and the <ChevronRight />

Sidebar slides in and out on page refresh

I created a sidebar (hidden by default) in a web store that contains a list of items you like. When clicked on a button the likes-list will show sliding in from the right smoothly. However on page refresh/reload the sidebar slides in and out rapidly. I'm using css animation to do the trick.
It works fine but somehow on page reload the animation is triggered without clicking the button. How to prevent this from happening?
App is built in React (create-react-app).
React component
import React from 'react';
import { useGlobalContext } from '../Context'
import './likes.css';
const Likes = ({ showLikes, setShowLikes }) => {
let { liked, setLiked, paintings, setPaintings } = useGlobalContext()
//Remove item from likes list
const removeItemFromLikeslist = (id) => {
liked = liked.filter(item => {
return item.id !== id;
});
//Change likes-heart to green
paintings = paintings.map(el => {
if (el.id === id) {
el.like = false
}
return el
})
//remove sidebar overlay
if (liked.length === 0) {
setShowLikes(false)
}
setPaintings(paintings)
setLiked(liked)
localStorage.setItem("PAINTINGS", JSON.stringify(paintings))
localStorage.setItem('LIKES', JSON.stringify(liked))
}
return (
<div className={showLikes ? "likesContainer show" : "likesContainer"} >
<div className="likesHeader">You like these paintings</div>
{liked.map(item => {
const { id, name, imgSrc } = item;
return (
<div className="like-item" onClick={() => removeItemFromLikeslist(id)} key={id}>
<div className="like-info">
<img src={imgSrc} style={{ width: "100px" }} alt={name} className="picInCart" />
</div>
<div className="like-name">
<h5>{name}</h5>
</div>
</div>
)
})}
</div>
)
}
export default Likes
CSS file
.likesContainer {
position: fixed;
top: 83px;
right: -328px;
width: 320px;
background-color: rgb(70, 70, 70);
height:auto;
display: flex;
align-items: center;
justify-content: flex-start;
flex-direction: column;
z-index: 2;
box-shadow: 0px 4px 10px black;
animation: animateHide .5s linear forwards;
}
#keyframes animateHide {
from {
right: 0;
}
to {
right: -328px;
}
}
.likesContainer.show {
right: 0;
animation: animateShow .5s linear forwards;
}
#keyframes animateShow {
from {
right: -328px;
}
to {
right: 0;
}
}
Problem solved, changed from animation to transition like so:
.likesContainer {
position: fixed;
top: 83px;
right: -328px;
width: 320px;
background-color: rgb(70, 70, 70);
height:auto;
display: flex;
align-items: center;
justify-content: flex-start;
flex-direction: column;
z-index: 2;
box-shadow: 0px 4px 10px black;
transition: .5s ease-in-out;
}
.likesContainer.show {
right: 0;
}

How to fix CSS backdrop-filter blur disappearing when using transition-transform or duration? [Chrome only]

Edit: This bug has been fixed by Chromium some time ago
Simple Codepen: https://codepen.io/themanfromearth1/pen/WNRoyyW
With Slick Slider: https://codepen.io/maxbeat/pen/abNBrex
The error happens when you combine backdrop-filter blur with either transition-duration or transform in the parent.
Parent Div:
.slider {
transition-duration: 300ms; /** One of those two is enough **/
transition: transform 0.3s; /** But with either the bug occurs **/
transform: translate3d(-100px, 0, 0);
}
Child Div:
.slider__item {
background: rgba(206, 206, 206, 0.15);
backdrop-filter: blur(89px);
}
When you click the button, the CSS blur disappears for a second and then the slide is blurred again.
Chrome: Problem
Firefox: Works (you have to enable backdrop-filter first in about:config)
Webkit/Safari: Working
Edit: There is a confirmed bug-report on Chromium for this problem
https://bugs.chromium.org/p/chromium/issues/detail?id=1194050
Without using translate you can use positioning with left.
let translated = false
function translate3d() {
const div2 = document.getElementById("slider")
if (!translated) {
div2.style.left = '100px';
} else {
div2.style.left = '400px';
}
translated = !translated;
}
.slider {
display: flex;
align-items: center;
justify-content: center;
position: relative;
left: 400px;
/** Remove duration and blur works **/
transition-duration: 300ms;
/** Also disappears if you transform like this **/
/** transition: transform 0.3s **/
}
.slider__item {
height: 100px;
width: 100px;
background: rgba(206, 206, 206, 0.15);
backdrop-filter: blur(89px)!important;
margin: 6px;
margin-top: 200px;
}
.container {
height: 1900px;
width: 899px;
background-image:url(https://images.unsplash.com/photo-1616604745302-60a195c7061a?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1401&q=80);
}
.button {
position:absolute;
top: 350px;
left: 400px;
}
<div class="container">
<div class="slider" id="slider">
<div class="slider__item">1</div>
<div class="slider__item">2</div>
<div class="slider__item">3</div>
<div class="slider__item">4</div>
<div class="slider__item">5</div>
</div>
</div>
<div class="button">
<button type="button" onclick="translate3d()">Click to transform translate3d</button>
</div>
<div>
It's a bug !
I've encountered it myself while I was building my own portfolio.
The way I've solved it was by using an API for smooth scrolling that I wrote which is available here: https://github.com/CristianDavideConte/universalSmoothScroll
Basically the solution is: don't use translate and backdrop-filter:blur together, just make the parent container scrollable and smoothly scroll it whenever you need it.
I've rewrote your slider example so that it correctly works with the API:
const maxSlide = 5;
const minSlide = 1;
const visibleSlides = 3;
let numSlide = 0;
function init() {
let leftArrow = document.getElementById('left-arrow');
let rightArrow = document.getElementById('right-arrow');
let slider = document.getElementsByClassName('slider')[0];
let slides = slider.children;
leftArrow.onclick = (e) => {
e.preventDefault();
if(numSlide > minSlide - 1) {
numSlide--;
uss.scrollIntoView(slides[numSlide], true, null, null, true);
}
}
rightArrow.onclick = (e) => {
e.preventDefault();
if(numSlide < maxSlide - visibleSlides) {
numSlide++;
uss.scrollIntoView(slides[numSlide], true, null, null, true);
}
}
/* We apply an ease function to make it look pretty */
let whateverEaseFunctionYouLike = remaning => {return remaning / 15 + 1;};
uss.setXStepLengthCalculator(whateverEaseFunctionYouLike, slider);
}
* {
padding: 0;
margin: 0;
}
body {
height: 100vh;
overflow: hidden;
background: url(https://img.wallpapersafari.com/desktop/1366/768/66/57/D2uNEj.jpg) no-repeat; /* Moved from container */
background-size: cover; /* Moved from container */
}
.container {
width: 100vw; /* Added */
display: flex; /*Added */
height: 200px; /* Moved from slider__item */
/*height: 100%; Removed */
/*padding: 60px; Removed */
}
.slider {
width: 80%; /* Changed */
height: 100%; /* Added */
position: relative;
display:flex; /* Added */
overflow: hidden; /* Added */
/*margin-bottom: 60px; Removed*/
}
.slick-list {
overflow: hidden;
}
.slick-track {
display: flex;
}
.slider__item {
width: calc(74vw / 3); /* Added */
height: 80%; /* Added */
flex-shrink: 0; /* Added */
margin: auto 1vw; /* Changed */
display: flex;
align-items: center;
justify-content: center;
font: 24px arial;
background: rgba(255,255,255,0.5);
backdrop-filter: blur(10px);
}
.slick-dots {
display: flex;
justify-content: center;
position: absolute;
width: 100%;
}
.slick-dots li {
background: #fff;
width: 10px;
height: 10px;
margin: 5px;
border-radius: 50%;
cursor: pointer;
list-style:none
}
.slick-dots li.slick-active {
background: #000;
}
.slick-dots li button {
font-size: 0;
border: none;
opacity: 0;
}
.slick-arrow {
font-size: 0;
width: 30px;
height: 30px;
background: none;
border: none;
border-top: 4px solid #fff;
border-left: 4px solid #fff;
/*transform: rotate(-45deg); Moved */
/*position: absolute; Removed */
margin: auto; /* Added */
top: 100px;
cursor: pointer;
}
.slick-next {
transform: rotate(135deg);
right: -30px;
}
.slick-prev {
left: -30px;
}
#left-arrow {
transform: rotate(-45deg); /* Moved from slick-arrow*/
}
#right-arrow {
transform: rotate(135deg); /* Moved from slick-arrow and then changed */
}
<script src = "https://cdn.jsdelivr.net/npm/universalsmoothscroll#latest/universalsmoothscroll-min.js"></script> <!-- Added -->
<body onload="init()"> <!-- Added init -->
<div class="container">
<button id= "left-arrow" class ="slick-arrow"><</button> <!-- Added -->
<div class="slider">
<div class="slider__item">1</div>
<div class="slider__item">2</div>
<div class="slider__item">3</div>
<div class="slider__item">4</div>
<div class="slider__item">5</div>
</div>
<button id= "right-arrow" class ="slick-arrow">&gt</button> <!-- Added -->
</div>
</body>
The advantage of this aproach is that you can customize the scrolling related part of your slider just as much as you did with "slick-slider".
If you need one more example of a carousel built with this API you can take a look at here: https://cristiandavideconte.github.io/myPersonalWebPage/#home

React Carousel - images with different widths display wrong

I have an array of URLs that I am passing in as a prop to a slide component. Only one URL is passed at a time using a stateful index. Whenever the right button is clicked, the index is incremented by 1 and vice versa for the left. This is working to display the next URL in the array; however, the images seem to repeat even though I set no-repeat on the background. This is due to the images being different widths and some being smaller than the last. I've tried setting the parent container to a set width so that the slide class is 100% the width of the parent but this doesn't seem to be working for me. Below are the two components that I'm using and the styles file.
//Parent carousel component
const HeroImage = ({state}) => {
const [index, setIndex] = useState(0);
var imgUrls = [];
const goToPrevSlide = () => {
setIndex(index - 1);
}
const goToNextSlide = () => {
setIndex(index + 1);
}
return(
<React.Fragment>
<StyledHeroImage>
<div className="slider-wrapper">
{state.heroImage.map(image => {
imgUrls.push(`${IMAGE_BASE_URL}${BACKDROP_SIZE}${image.backdrop_path}`);
})}
<Slide title={state.heroImage.original_title} text={state.heroImage.overview} image={imgUrls[index]}/>
</div>
<LeftArrow goToPrevSlide={goToPrevSlide}/>
<RightArrow goToNextSlide={goToNextSlide}/>
</StyledHeroImage>
</React.Fragment>
)
}
//child Slide Component
const Slide = ({image, text, title}) => {
const styles = {
background: `linear-gradient(to bottom, rgba(0,0,0,0) 39%, rgba(0,0,0,0) 41%, rgba(0,0,0,0.65) 100%), url(${image})`,
backgroundSize: 'cover',
backgroundRepeat: 'no-repeat',
backgroundPosition: 'center',
}
return(
<div className="slide" style={styles}>
<div className="heroimage-content">
<div className="heroimage-text">
<h1>{title}</h1>
<p>{text}</p>
</div>
</div>
</div>
)
}
//Styles file
export const StyledHeroImage = styled.div`
height: 650px;
width: 100%;
margin: 0 auto;
.slider-wrapper{
height: 100%;
width: 100%;
}
.slide{
width: 100%;
height: 100%;
}
.arrow{
height: 50px;
width: 50px;
display: flex;
align-items: center;
justify-content: center;
background: #f9f9f9;
border-radius: 50%;
cursor: pointer;
transition: transform ease-in .1s;
}
.nextArrow {
position: absolute;
top: 50%;
right: 25px;
z-index: 999;
color: #fff;
}
.backArrow {
position: absolute;
top: 50%;
left: 25px;
z-index: 999;
color: #fff;
}
.fa-arrow-right:before, .fa-arrow-left:before {
color: #222
}
`;

How to set absolute positionning in react component

i'm trying to create a custom button for my portfolio.
This button will be animated, the top part sliding in from left and the bottom part sliding in from right when hovered.
At the center of it will be the Category linked to this button.
Here an illustration :
How can i set my text in the center for every that i will create ? I can't use "position: absolute" property cause the reference would be the webpage and not the position where the custom component is declared...
Here my actual code :
CSS
const CatStyle = styled.div`
box-shadow: 0px 0px 2px 2px rgba(0,0,0,0.75);
height: 50px;
max-width: 150px;
background-color: white;
display: flex;
justify-content: center;
flex-direction: column;
cursor: pointer;
transition : background-color 0.5s ease-out;
:hover div{
display: flex;
}
.catContent {
position: relative;
align-self: center;
font-size: 1.5rem;
}
.topSlideAnimation {
display: none
height: 50%;
background-color: green;
}
.botSlideAnimation {
transition : background-color 0.5s ease-out;
display: none;
height: 50%;
background-color: blue;
}
`
JSX
const ButtonCat = (props) => (
<CatStyle>
<div className="topSlideAnimation"></div>
<div className="catContent">Hello</div>
<div className="botSlideAnimation"></div>
</CatStyle>
)
Not done any jsx so not sure what your catstyle tag would render, but if you can just render a button (or div), I would do the following
Make an outer container that is flex (for aligning text in center)
Make an inner container that is for the text (so you can position it relatively and add a z-index to it)
Add pseudo elements to your outer container for the animated bits (rather than having 2 empty divs)
* {
box-sizing: border-box;
}
.button {
display: inline-flex; /* make flex for centring */
justify-content: center; /* vertically centre */
align-items: center; /* horizontally centre */
position: relative; /* for adding absolute positioned children */
min-height: 50px; /* test value to show text is centred */
overflow: hidden; /* hide pseudo elements when not shown */
}
.button:before,
.button:after {
content: ''; /* make coloured animatable bits */
display: block;
height: 50%;
width: 100%;
position: absolute;
transition: all 1s ease-in;
z-index: 1;
}
.button:before {
top: 0;
right: 100%;
background: grey;
}
.button:hover:before {
right: 0;
}
.button:after {
top: 50%;
left: 100%;
background: darkgrey;
}
.button:hover:after {
left: 0;
}
.text {
position: relative; /* needs to have a higher z-index than the pseduo elements so text appears on top */
z-index: 2;
}
<button class="button"><span class="text">test content</span></button>

Resources