ReactJS CSS inline style position dynamically - javascript

I have this element in my React code:
{focusedAnnotationIconProps && (
<div
style={{
backgroundColor: "#333",
width: "100px",
position: "absolute",
left: focusedAnnotationIconProps.left,
top: focusedAnnotationIconProps.top + 25,
color: "#fff",
}}
>
{annotationIconPopoverText}
</div>
)}
this is a popover that will show when hovering over an icon, so I get the CSS left/top position numbers from that Icon (in another part of the code), and place it there, and currently, is showing like this:
I need to center this popover in the middle of the icon
but the thing is, this <div> element has variable width, depending on the text inside
so I was thinking of someway to do something like this:
left: focusedAnnotationIconProps.left - this.width/2
but I know that won't work.. I tried using the calc css3 function, but won't work inline with CSS, so there's my problem.. would appreciate any help

With position: absolute, you can use these methods to center the elements:
Center Vertically:
style={{
top: "50%",
transform: "translateY(-50%)"
}}
Center Horizontally:
style={{
left: "50%",
transform: "translateX(-50%)"
}}
Center to the parent div:
style={{
left: "50%",
top: "50%",
transform: "translate(-50%, -50%)"
}}

Related

How do I position my element to be sticky (stay on screen when I scroll pass them)

I'm coding a basic eCommerce website using NextJS and I'm having a problem
I want to have a sticky tab in the middle of my page but for some reason position: 'sticky' doesn't work
This is my code
[other stuff here]
<div className={classes.stickyTab}>
<Grid style={{ backgroundColor: '#FFFFFF' }}>
<div className={classes.outerTab}>
<div className={classes.innerTab}>
<div>Tab number 1</div>
</div>
</div>
</Grid>
<Grid style={{ backgroundColor: '#FFFFFF' }}>
<div className={classes.outerTab2}>
<div className={classes.innerTab2}>
<div>Tab number 2</div>
</div>
</div>
</Grid>
</div>
[other stuff here]
And this is the styles.js
stickyTab: {
position: 'sticky',
},
outerTab: {
display: 'flex',
paddingTop: 16,
paddingRight: 16,
paddingBottom: 16,
paddingLeft: 16,
},
innerTab: {
fontSize: '20px',
},
outerTab2: {
position: 'relative',
marginTop: 10,
marginBottom: 10,
},
innerTab2: {
position: 'relative',
marginLeft: 10,
marginRight: 10,
marginBottom: 10,
},
This is what it looks like visually
Basically all I want is for my 2 white bars to be sticky so that they stay on the screen when I scroll pass them. All I did was wrap both of them inside a div then gave it position: 'sticky' but like I said it didn't work like I thought it would. Perhaps I'm missing something here?
Any helps would be much appreciated, thanks.
Using position sticky property requires you to add the position also.
Try adding top: 0; and width: 100% and height: 50px. You can have the width and height according to your needs.
stickyTab: {
position: 'sticky',
top:"0",
width:'100%',
height: '50px'
}

Applying margin to a background image with fixed position

I have a background image that I want to cover my entire screen minus the header. I also don't want my background image to scroll. It should not move as the user scrolls over content.
For this reason I use the following to place my background:
<div
className={style.app}
style={{
backgroundImage: `url(${HomePhoto})`,
backgroundRepeat: "no-repeat",
backgroundAttachment: "fixed",
}}
></div>
I am attempting to add additional CSS properties to assume the proper height and width are used:
height: 100vh;
width: 100vw;
margin-top: 4rem;
background-size: 100% 100%;
However the margin-top is not applied unless I remove backgroundAttachment: "fixed". Is there a way I can achieve both styles?
You can use the backgroundPosition property which allows you to move a background image around in its container: https://developer.mozilla.org/en-US/docs/Web/CSS/background-position
<div
className={style.app}
style={{
backgroundImage: `url(${HomePhoto})`,
backgroundRepeat: "no-repeat",
backgroundAttachment: "fixed",
backgroundPosition: "100px 0", // or vh
}}
></div>

How can I fix tooltip with overflow-x and overflow-y?

I did horizontal scroll and also I added tooltip, but tooltip is not working. I am using react-lightweight-tooltip library. I trying with with z-index but it is not working also, I don't know why. Can anybody help me with that. Thank you !
Playground: https://codesandbox.io/s/cool-mendeleev-jb1be
Your custom styles are hiding the tooltip, specifically the bottom: 100% on tooltip.
Remove that or adjust it so that the tooltip shows.
I tested this by using bottom: 0, and that seems to fix it.
Here:
export const underlyingsTooltipStyle = {
/* OMITTED IRRELEVANT STYLES */
tooltip: {
position: 'absolute',
zIndex: '99',
background: '#000',
bottom: '0',
left: '50%',
marginBottom: '10px',
padding: '5px',
WebkitTransform: 'translateX(-50%)',
msTransform: 'translateX(-50%)',
OTransform: 'translateX(-50%)',
transform: 'translateX(-50%)',
},
/* OMITTED IRRELEVANT STYLES */
]

How to vertically and horizontally center a component in react?

How can I achieve absolute centering with CSS-in-JS? When I use the following code, my component moves across the screen. I guess the translate is being applied many times instead of just once. What's going on, and how can I fix it without using a library?
render() {
return (<ComponentASD
style={{
position: 'absolute', left: '50%', top: '50%',
transform: 'translate(-50%, -50%)'
}} />);
}
Another option is to use flex-box.
<div style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}>
Hello world
</div>
Your example code works well:
ReactDOM.render(
<div
style={{
position: 'absolute', left: '50%', top: '50%',
transform: 'translate(-50%, -50%)'
}}
>
Hello, world!
</div>,
document.getElementById('root')
);
https://codepen.io/anon/pen/JaLLmX
It has to do something with your layout.
Thanks for the testing+comments! The styling ended up being fine, the issue was somehow caused by the layout of ComponentASD, which is actually MUI's CircularProgress https://material-ui.com/api/circular-progress/#demos
I wrapped that component in a div inside of render and applied the styling to the div, which fixed the problem (keeps it stationary and properly aligned).
This is flexbox you will need to use this on the parent Component.
.Parent {
display: flex,
flexFlow: row nowrap,
justifyContent: center,
alignItems: center,
{
It will center the children vertically and horizontally
You can also align each component as I've shown you below
render() {
return (<Childcomponent
style={{
display: flex,
flexFlow: row nowrap,
justifySelf: center,
alignSelf: center,
}} />);
}
If you want to have a properly scrollable list if the window's height is smaller than the content's height you want to center, you can do this (based on this CSS answer):
<div
style={{
display: "table",
position: "absolute",
height: "100%",
width: "100%",
top: 0,
left: 0
}}
>
<div
style={{
display: "table-cell",
verticalAlign: "middle",
textAlign: "center"
}}
>
// Here comes the content you want to center
</div>
</div>
If you want to use it in a component just call it in the component:
.Login {
text-align: center;
background-color: #2681C6;
min-height: 100vh;}
Your jsx file sould have something like just to call it, using "className":
<div className="Login"><div>

Resized image takes up too much UI space

I have an absolutely positioned round image. The image needs to take only 17% of screen width, and also be positioned 5 pixels from the top.
Problem is, when I resize the image to take up 17% of screen width, it does so but at the same time the container becomes elongated. The image itself does not stretch, but is positioned in the middle of the elongated Image container.
Styles:
const styles = StyleSheet.create({
imageBase: {
position: 'absolute',
left: 15,
top: 5,
width: '17%',
resizeMode: 'contain',
backgroundColor: 'red',
}
});
What I'm rendering:
<Image
style = { styles.imageBase }
source = { require('roundImage.png') }>
</Image>
Current result:
I need it to either cut off the extra space from the Image container, or position the image at the top of the container, instead of middle.
What I'm trying to do:
One way to solve this is to wrap your image in a View with an aspect ratio of 1:
<View style={{ flex: 1 }}>
<View
style={{
position: 'absolute',
left: 15,
top: 5,
width: '17%',
aspectRatio: 1,
}}
>
<Image
style={{
width: '100%',
height: '100%',
backgroundColor: 'red',
resizeMode: 'contain',
}}
source={require('roundImage.png')}
/>
</View>
</View>

Categories