What is the best way to incorporate a scroll feature into the buttons container?

I have implemented buttons with a hover effect that displays a span on top of the button when hovered.

https://i.sstatic.net/YazEE.gif

Although it works well, the appearance gets distorted when the screen width changes:

https://i.sstatic.net/zaWoX.gif

Here are the elements involved:

<div className='buttons'>
                <button
                    className={editor.isActive('bold') ? 'is-active' : 'is-inactive'}
                >
                    <strong>N</strong>
                    <span className='popup'>Negrita (Ctrl+B)</span>
                </button>
                <button
                    className={editor.isActive('italic') ? 'is-active' : 'is-inactive'}
                >
                    <em>C</em>
                    <span className='popup'>Cursiva (Ctrl+I)</span>
                </button>
                <button
                    className={editor.isActive('strike') ? 'is-active' : 'is-inactive'}
                >
                    <s>T</s>
                    <span className='popup'>Tachado (Ctrl+Shift+X)</span>
                </button>
                ... (and so on) 
</div>

This is the SCSS styling:

.buttons button {
    position: relative;
    height: 29.19px;
    color: #000;
    border: 1px solid black;
    border-radius: 0.3rem;
    margin: 0.2rem !important;
    padding: 0.1rem 0.4rem !important;
    background: white;
    accent-color: black;
    font-weight: 500;
}

button .popup {
    visibility: hidden;
    width: 120px;
    background-color: black;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 5px 0;

    position: absolute;
    z-index: 1;
    bottom: 100%;
    left: 50%;
    margin-bottom: 5px;
    margin-left: -60px;
}

button .popup::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: black transparent transparent transparent;
}

button:hover .popup {
    visibility: visible;
}

Desired Outcome

I have decided to implement a horizontal scroll for this issue. However, due to the absolute positioning of the span based on the button, I am struggling to keep the span on top of the hovered button while enabling horizontal scrolling.

Update 1

After adding a scroll, this is the current result:

https://i.sstatic.net/5W9SV.gif

As shown in the GIF, the scroll works properly but the spans are getting 'covered'. Is there a solution to rectify this?

The following snippet was added to the code:

.buttons {
    position: relative;
    overflow-x: scroll;
    white-space: nowrap;
}

Answer №1

Take a moment to explore the meaning of the absolute here

The element is taken out of the normal document flow, leaving no space allocated for it in the page layout. It is positioned in relation to its nearest positioned ancestor, if one exists; otherwise, it is placed relative to the initial containing block.

And what exactly is a positioned ancestor?

A positioned element is an element with a computed position value of either relative, absolute, fixed, or sticky. Simply put, anything other than static positioning qualifies as a positioned element.

Hence, even when there's a horizontal scroll present, setting position:relative for .buttons should still function properly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

It seems like there could be an issue with the CSS file not properly connecting to the HTML, or possibly the Jumbotron is not allowing the

Need help with text color! Trying to set h1 element to #513271, but it's not working. Check out my code and failed solutions below. CSS saved as stylesheet.css in same folder as tributePage.html. jumbotron h1 { color: #513271; } <link rel="st ...

Oops! The `source` prop provided to the `Image` component in React Native is invalid

Is it possible to receive assistance in React Native? I keep getting this error from the compiler: "Failed prop type: Invalid prop source supplied to Image" The 'dec' variable is a string fetched from an API. Please provide some guida ...

Unable to modify the background image in CSS when hovering over

I am struggling with changing the background image of the included div when hovering over the col-lg-3 div. I have 6 col-lg-3 in my container, each containing another div with background images and various CSS properties. Despite trying to change the backg ...

What is the reason behind this useEffect triggering an endless cycle of requests to the '/analysis' API on the server?

This Node.js route is handling the backend processing. app.post('/analysis' , async (req , res) => { const {temp , humidity} = req.body.data.current let predictedCrops:any[] = [] let humidityDifference1 let humidityDifference2 let ...

The function _this2.setState does not work properly in asynchronous situations within React

I have an onChange handler that triggers a Promise Resolve function. This function fetches data array using "PromiseValue", and I use ".then( result )" to retrieve the array field. It works when I print "result" with console.log. However, the issue arises ...

Guide to defining font style in vanilla-extract/CSS

I'm trying to import a fontFace using vanilla-extract/css but I'm having trouble figuring out how to do it. The code provided in the documentation is as follows: import { fontFace, style } from '@vanilla-extract/css'; const myFont = fo ...

Can the navbar hamburger in Bootstrap be displayed at a screen size of 992px?

How can I display the bootstrap navbar Hamburger at md(992px) Screen? Here is the code snippet I am currently using: https://i.sstatic.net/OqBTQ.png Any suggestions on what steps I should take? ...

What are the best ways to handle localStorage in nextJS?

My usual approach involves building applications with Django and React, utilizing JWT for authentication. Currently, I'm exploring nextJS. One challenge I've encountered is the inability to access local storage in order to retrieve my two tokens ...

Tips for incorporating a new element into Material-UI TablePagination

I am currently working on a project that involves Material-UI Table with TablePagination. The layout looks like this: https://i.sstatic.net/mooQL.png My goal is to align the pagination to the left instead of the right, and also add another element, such ...

Implementing conditional visibility toggling in React

I am experiencing an issue where I am attempting to change a div's visibility from hidden to visible upon button click. Despite clicking the button, the visibility of the div does not change as expected. Upon inspecting the console after executing the ...

Ways to utilize a React custom hook that returns a value within the useEffect hook?

I'm dealing with a custom hook that returns a value based on the parameters it receives. In order to update this value dynamically, I attempted to use useEffect but encountered issues as the hook cannot be called within it. How can I work around this ...

Having difficulty integrating MaterialUI 5 checkbox with Formik 2

I'm running into an issue trying to make Formik 5 and MUI 5 checkboxes work together. In the Codesandbox example below, there is a form with two unchecked checkboxes: "with MUI" and "without MUI". The "without MUI" checkbox functions as expected: I c ...

Tips for aligning chip items at the center within a Material-UI autocomplete box

Is there a way to center align the chip items within an autocomplete Material UI Sandobox? Check out this example: https://codesandbox.io/s/multiline-autocomplete-with-chips-forked-mm2zsm I attempted to achieve this using a Box component and passing the f ...

Building with bricks and mortar does not involve organizing visual content

I'm currently trying to organize some fairly large images using masonry, but the code below doesn't seem to be working. I'm using node.js with express and have installed the masonryjs package in an attempt to make it work, but that approach ...

Obtain a file from React Native in order to upload an image, just like using the `<input type="file" onChange={this.fileChangedHandler}>` in web development

After experimenting with different methods, I attempted to achieve the desired result by: var photo = { uri: uriFromCameraRoll, type: 'image/jpeg', name: 'photo.jpg', }; and integrating axios var body = new FormData( ...

How can I get Keyboard.dismiss to function properly in React Native?

I'm attempting to implement a feature where the keyboard automatically dismisses when a user logs in. Despite using Keyboard.dismiss, I'm facing an issue where the keyboard remains on screen and needs to be manually closed on the next page. Can a ...

Stretching the height of a table without altering the position of its contents

While working on a project using Angular and Angular Material, I have realized that the solution might actually lie in pure CSS. The issue I'm facing is with a table that currently has only 2 or 3 rows. I want this table to have a fixed height of 700 ...

Is it possible to utilize the useEffect hook with an empty array and with [props and states] values within the same JSX file?

Currently, I am developing a search bar filter module for my ecommerce project. As the page loads, I need to retrieve data from an API call using useEffect hooks. However, there is a text field where users can input search queries for categories or product ...

What could be causing only certain portions of my CSS to be applied?

I've been struggling to get rid of the gray border around the SIGN UP button in the tbody section. I've tried applying classes (along with the ID) and using pseudo classes like :4th-child {border:none}, but nothing seems to work. The only solutio ...

Error encountered in Typescript while attempting to update local state with the useState hook

There is a component that retrieves messages and stores them in a local variable using the useState method. const [localMessages, setLocalMessages] = useState<Message[]>([]) When new messages are fetched, they are stored in the localMessages variabl ...