Preventing the background color from exceeding the text boundaries using CSS

I'm having trouble containing the background color within the text content. I want the right spacing to be equal to the left, but as you can see in this screenshot, the right side is not adjusting:

https://i.sstatic.net/cD4GK.png

Here's the code I'm using:

 <td>{user.workforce_roles.filter((wf_role) => wf_role.role.visible).map((wf_role) => {
    return <span key={wf_role.id} className="d-block">{wf_role.role.friendly_name}</span>;
  })}</td>

And here is the styling:

.d-block {
  background-color: map-get($theme-colors, 'gapped');
  border-radius: px-to-rem(4px);
  margin-bottom: px-to-rem(6px);
  margin-right: px-to-rem(6px);
  padding: px-to-rem(2px) px-to-rem(8px);
  padding-right: px-to-rem(8px);
  position: relative;
}

It seems like the background width is being based on the longest text, but I need it to be specific to each text case. Any suggestions would be greatly appreciated!

Answer №1

I think the reason is that your span elements have a display: block property. Try changing it to display: inline-block.

An example of display: block:

span {
  display: block;
  border-radius: 5px;
  background-color: gray;
  padding: 5px;
  margin: 20px;
}
<span>Text Text</span>
<span>Text Text Text Text Text Text Text Text</span>

An example of display: inline-block:

span {
  display: inline-block;
  border-radius: 5px;
  background-color: gray;
  padding: 5px;
  margin: 10px;
}
<span>Text Text</span><br>
<span>Text Text Text Text Text Text Text Text</span>

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

Guide on utilizing a search bar component for filtering results in a card component. ReactJS and NextJS integration

One component I have is the Search.jsx search bar: import { useEffect, useState } from 'react' export default function Search() { const [query, setQuery] = useState('') return ( <div className={styles.container}> ...

What is the best way to distinguish the Footer from the Content?

While I was honing my skills in crafting a website using only HTML and CSS, I encountered an issue. I couldn't figure out how to place the footer beneath the content properly. My goal was to clearly separate each section - header, content, and footer. ...

Encountering the error "Unable to access the 'pickAlgorithm' property of null" in a ReactJS context

Encountering an issue during npm install process. npm install -g netlify-cli The error message displayed is: npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1a2a5b0a5a2b5fcb2bdb8b4bfa591e1ffe5ffe6"> ...

Is it possible to include Ruby Sinatra Sessions in a JSX component?

Recently, I've been exploring the world of integrating react + ruby and came across an interesting challenge. I stored some content in my sessions and was wondering if there is a way to access it. For instance, in my app.rb, I saved sessions[:return_ ...

When using React Redux to update a FlatList item, the item is transformed into an array instead

Incorporating react redux with react native, my app features a flatlist of photos. I encountered an issue where upon clicking the like button for a photo, only that particular photo should be updated in the flatlist. Despite successfully updating the like ...

Is there a way to display the next/previous buttons separately from the scroller in my jQuery thumbnail scroller implementation?

Is there a method to display the next and previous buttons outside of the scroller frame when using jQuery thumbnail scroller by manos.malihu.gr? I have attempted to modify the button class in CSS to make them more prominent or visible, but unfortunately ...

After the scaffold generator, the Twitter-bootstrap buttons now feature text in a subtle gray

I seem to be facing a similar issue to what was discussed in this thread about Twitter Bootstrap displaying buttons with greyed text. The problem I am encountering is with a btn-info button that has a dropdown - after clicking on an item in the dropdown, t ...

Navigation Bar Revamp

Check out the colorful navigation bar on this page: The navigation bar appears distorted when I zoom in/out. Is there a way to resolve this issue? ...

Issue with visibility of Bootstrap modal title

The title in the modal isn't visible. Even when I highlight it with a mouse, it still doesn't show up. I attempted to add modal-content color: #000, but unfortunately, it had no effect. As of 01/11/16 1:28PM CST, none of the responses and answe ...

"What is the best way to switch between multiple data targets and toggles in bootstrap using the HTML data toggle attribute

I am currently coding an animated hamburger icon for the Bootstrap navbar, and I need to find a way to toggle both the hamburger icon and the responsive navbar. Is there a method or code snippet that allows me to toggle these two elements independently usi ...

What is the best way to combine strings to generate a unique key in React?

As I create a list of elements, I am aiming to assign a unique key to each one. However, when checking with the React Dev Tool, the key generated includes commas like "2016-10,-,football". Why is there a need for commas in the key? Is there a proper meth ...

I'm noticing that my variable is being updated with the new data after it has been saved to the file. Could this be a quirk of React

In the process of developing a note-taking application with React and NWJS, I have implemented a feature where notes are stored in a separate file named save.json. The app makes use of a useEffect hook to fetch initial data from this file, handles input ch ...

What are some ways to make the parent div grow based on the width, padding, margin, or box of the child div

Is it possible to expand a parent div with a child div and how can I achieve this? I have created a JSFiddle with some code included. CSS body { background: gray; } div.page { color: white; background: black; max-width: 72em; margin: ...

The navigation bar in responsive view on Bootstrap 4 causes the content below it to shift

Recently I have been exploring bootstrap-4 and encountered an issue while creating a nav-bar. The problem is that in responsive view, when toggling the nav-bar it simply pushes the content below it. Is there any way to address this issue within (bootstra ...

Displaying items in a row when the navbar is collapsed: Bootstrap 4 tips

Is there a way to change the display of items in the collapsed navbar so that they appear next to each other rather than in a column? You can see how it currently looks here: Navbar collapsed. My goal is to have the image displayed next to the username ins ...

The final element that fractures all its sibling elements positioned absolutely

I am experiencing an issue with a container div that contains 5 absolutely positioned images. When I specify the last image as absolutely positioned, all other images lose their positioning and stack at the top. The container itself is relatively position ...

How can the target pseudo-class be utilized to replace the 'active' state on navigation across several pages effectively?

Within a large application, I am managing a micro site and looking to implement tertiary navigation with an active state for pages. This involves inserting a single html blob into multiple pages. My goal is to use an active class to highlight the page in ...

Slide inside a div to move content

Check out the script I've been experimenting with for bringing in a menu from the left: Demo Fiddle To make this work, the sliding DIV needs to not only appear on the left but also push the 'left panel' and 'right panel' accordin ...

Content Blocks with Added Cushioning

I am striving for the cleanest code possible. Can an article and an aside have padding, margins, etc? I have been struggling to achieve this. I find myself having to add a div with a class that accepts padding. article { width:463px; float:left; ...

Encountering authentication issues with REST API in React

I'm currently working on a project that involves using Laravel 9 for the backend and React for the frontend. Whenever I attempt to make a request, it returns an unauthorized error from the backend. However, when I test this in Postman, it returns a su ...