Create a dynamic effect by adding space between two texts on the page

const Button = () => {

    const options = ['test1', 'test2', 'test3'];

    return (
         <div style={{ position: 'absolute', left: '8px', width: 'auto', flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', backgroundColor: '#006C84' }}>
            {options.map(opt => (<span style={{ paddingRight: '10px' }}>{opt}</span>))}
         </div>
     )
    }

I have a code snippet above which is showing some extra space at the end of the text. How can I remove that space?

https://i.stack.imgur.com/07uX0.png

Answer №1

When applying paddingRight: 10px to the span, there is some leftover space shown at the end of the last child.

There are a couple of ways to address this:

  1. Using JavaScript
  2. Utilizing CSS

JavaScript Approach

{options.map((opt,index) => (
        <span style={{ paddingRight: options.length - 1 === index ? '10px' : "0px" }}>{opt}</span>)
   )}

CSS Approach To resolve this issue, consider switching from inline styles to an explicit style or creating a separate style object for better organization and maintenance.

  <div className="parent">
        {options.map(opt => (
            <span style={{ paddingRight: '10px' }}>{opt}</span>)
        )}
     </div>

.parent{//parent css goes here}
.parent span:not(::last-of-type){padding-right: 10px}

Answer №2

const ButtonComponent = () => {

    const buttonOptions = ['option1', 'option2', 'option3'];

    return (
        <div style={{ display: 'flex', gap: '10px', backgroundColor: '#006C84', alignItems: 'center', justifyContent: 'space-between', flexDirection: 'row', position: 'absolute', left: '8px', width: 'auto' }}>
            {buttonOptions.map(option => (
                <span>{option}</span>)
            )}
        </div>
   )
}

Include the use of display: flex and gap in the styling of the parent div

Visit this link for a live demo

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

I am facing an issue with my React Native splash screen

I am facing issues with the React Native splash screen implementation. I followed all the instructions on the GitHub repository, but the problem persists. The problem arises after setting up React Native Splash Screen in my app. Despite following all the ...

Is there a way to align an image to the center using the display grid property?

Having some trouble centering an image within a 'display: grid' layout. Screenshot I attempted to use 'align-items' but it didn't seem to work either. Code: <section style="padding-top: 30px;"> <h4 sty ...

Adjusting height in Google Maps to fill the remaining space

Currently, I am developing a map application where I want the Google Maps DIV to occupy the remaining height under the header. Here is the code snippet: <!DOCTYPE html> <head> <title>Map Application</title> <style type ...

What could be causing my grid to malfunction once the media query is triggered?

I am in the process of creating a straightforward navigation bar. My goal is to have the <nav> content, which includes the navigation links, positioned inside the header along with the logo (a random image from unsplash.it). To achieve this layout, I ...

Making the text gradually disappear at the bottom of a section using a see-through overlay, while ensuring the height remains within the boundaries of the section even

Trying to achieve a sleek fade-out effect at the end of a text section for a 'read more' indicator. I've been studying various tutorials, including this, and my current code is as follows: html <section> <p>Lorem ipsum dol ...

Tips for modifying padding in HTML and CSS?

I'm struggling to add padding around the image without affecting the mobile view. Here's what I've tried: float-right or padding-right:250px; // but nothing happens and it affects the mobile view. Please review my code: .fontProfile ...

Encountering an unusual hash code when implementing Google Tag Manager in a Next.js project was

I am currently using Next.js and have added Google Tag Manager through a script <script dangerouslySetInnerHTML={{ __html: `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});var ...

Unable to serve certain file extensions from the .NET Core local server even after making changes to the <requestFiltering> section in the applicationHost.config file

I am currently working on a website and encountering an issue with accessing/downloading certain file extensions such as .py and .cs. For instance, when attempting to fetch a .cs file, the following code is used: fetch("https://localhost:44310/userdat ...

Encountering error while processing order in React.js using Stripe

const processPayment = async () => { try { const paymentResponse = await stripe.confirmCardPayment(clientSecret, { payment_method: { card: elements.getElement(CardElement) } }); ...

Unleash the Power of Connecting Web Browsers with Local Filesystems (

I'm currently utilizing a powerful tool called VoTT, which can be found at the following link: https://github.com/microsoft/VoTT. Impressive enough, this tool is crafted using react/redux. While this tool functions flawlessly as a web application, on ...

Inexplicably bizarre HTML glitch

I am currently utilizing a comment system that involves adding a new row to a SQL database. The system seems to be working fine; however, when I try to display the comments, the formatting of the comment div becomes all jumbled up. You can view the page wh ...

In ReactJS, the date range picker allows users to select a maximum date that is restricted to 30 days from

Is it possible to automatically set the maximum date to be 30 days from the selected date in a date range picker? <LocalizationProvider dateAdapter={AdapterDateFns}> <DateRangePicker startText="Start date" e ...

NextJS useState data being accessed incorrectly by Axios

My axios patch request is returning a 200 response, but the data being sent is not updated with the new form data. Even though my console.log shows that the variables are updated correctly, axios is still accessing the initial data from the get request. T ...

It's impossible to center text in Bootstrap 5

Creating a website for my school project has been challenging, especially when it comes to centering text. I have been struggling to position the text in the middle of the background picture despite trying various methods. Here is the HTML code: pastebin ...

Encountering a 500 Error and an Unexpected token < issue in the JSON at position 0 while deploying a MERN stack

After successfully deploying my first MERN application on Heroku, I encountered a 500 (Internal Server Error) and SyntaxError: Unexpected token < in JSON at position 0 when attempting to log in. Oddly enough, other GET requests return a status code of ...

Steps to deploy a create-react-app-2 build on an IIS server

Currently, I am attempting to deploy a React.js application that was developed with create-react-app (react-scripts v2.1.5) on an IIS server. Despite my best efforts, I continue to encounter frustrating type errors like the ones shown in this image: I ha ...

How to target only the parent div that was clicked using jQuery, excluding any

I have attempted to solve this issue multiple times, trying everything I could find on Google and stack overflow without success. At times I am getting the span element and other times the div - what could be causing this inconsistency? $(".bind-key"). ...

What is the correct way to generate a normal map using THREE.js?

Experimenting with the Normal map Ninja demo, I attempted to apply it to a cube in my scene using the most recent version of Three.js from the development branch: // Setting up common material parameters var ambient = 0x050505, diffuse = 0x331100, specul ...

What are the steps for creating an animated visualization of the peak chart?

As a newcomer to CSS and Javascript, I am currently struggling with animating a peak (bar) chart that I came across on codepen. If anyone can provide assistance or guidance, it would be greatly appreciated! The chart can be found here: http://codepen.io/An ...

Retrieve information according to the object ID with AngularJS UI-Router and $stateParams

I have a unique application that directs to a custom URL based on a specific employee ID parameter when an individual employee is clicked in a list. Upon clicking the employee, users are redirected to a detailed employee page with their ID property as a pa ...