Enclose text with a background color that is positioned to the right

My Objective:

I aim to accomplish the following outcome:

div {
  text-align: right;
}

span {
  display: inline-block;
  background: #000;
  color: #fff;
}
<div>
  <span>abc</span><br/>
  <span>Hello world this is some text</span><br />
  <span>hello world</span>
</div>

However, in the above scenario, I have segmented my lines individually by enclosing each line within a separate span.

I desire to attain the same (or similar) result as above by utilizing a width to specify when a new line should start/end, rather than encapsulating each new line in its own span.

My Attempts So Far:

I've experimented with the following approach:

div {
  text-align: right;
}

span {
  display: inline-block;
  background: #000;
  color: #fff;
  width: 200px; /* Width to determine where line break should occur */
}
<div>
  <span>abc</span><br/>
  <span>Hello world this is some text hello world</span>
</div>

Nevertheless, in the above instance, the black background covers a "block" rather than wrapping around each line of text.

Therefore, how can I make my second snippet behave similarly to the first one?

Answer №1

Include an additional wrapper to maintain inline positioning:

div {
  text-align: center;
}

span {
  display: inline-block;
  line-height:1; /*eliminate spacing*/
  color: #fff;
  width: 200px; /* Set the width for line break position */
}
span > span {
  display:inline;
  background: #000;
}
<div style="text-align: right;">
  <span><span>abc</span></span><br>
  <span><span>Hello world this is some text hello world</span></span>
</div>

You can also modify padding while maintaining the default line-height:

div {
  text-align: center;
}

span {
  display: inline-block;
  color: #fff;
  width: 200px; /* Set the width for line break position */
}
span > span {
  display:inline;
  background: #000;
  padding:2px 0;
}
<div style="text-align: right;">
  <span><span>abc</span></span><br>
  <span><span>Hello world this is some text hello world</span></span>
</div>

Answer №2

Another method that avoids using nested spans.

.wrapper {
  display: inline-block;
  color: white;
  width: 200px; /* Set width to determine line break position */
  text-align: right;
  line-height: 2;
}
.text {
  display:inline;
  background: black;
  padding:2px 0;
}
<div style="text-align: right;">
  <div class="wrapper">
    <span class="text">Hello world this is some text hello world. Hello world this is some text hello world. </span>
  </div>
</div>

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

Issue encountered when submitting form: Error identified as "Unexpected string expression without template curly in string"

As a novice in React.js, I am facing an issue where setState is not functioning properly when submitting a form. Any suggestions on how to resolve this problem? > class Home extends Component{ constructor(props){ > super(props) > ...

When incorporating script tags in React, an "Unexpected token" error may arise

I am in the process of converting my website to a React site, but I am encountering an issue with the script tags not working. It keeps showing an unexpected token error. Here is the code snippet: <div className="people"> How many people are you ...

Disable the activation of the <a> tag when clicking on a child video element, while keeping the video controls unaffected

I have a collection of photos and videos that are gathered from social media platforms using a hashtag search. Below is the structure of the HTML: <div class="media-holder"> <a href="--mediaURL here---"> <div class="description-bar>&l ...

The ReactJS Application Cache Client fails to display the most recent updates

One issue I'm currently facing is with my application in production. Some clients have reported that they are unable to see the new changes unless they clear their cache completely. Using Techs: React Any suggestions on what steps I should take to a ...

Enhancing Images with Dynamic Captions Using JQuery

I have created a code to add image captions to different div blocks, and it works as intended. However, I am looking for ways to optimize the code and make it shorter. If you have any advice on how to improve it, please let me know! Thank you in advance. ...

Quickest method to detect IE

Is there a way to detect IE and prompt the user to change their browser? <!--[if IE]> <p>rrff</p> <script type="text/javascript"> alertify.alert("Please change your browser"); </script> <![endif]--> <!--[if gte IE 8] ...

The alignment of the horizontal menu items is off

Currently, I am working with asp.net and dealing with a horizontal menu issue. The alignment problem arises where the right side of Menu B appears higher than the left one (Menu A). I have included my code below for reference. Any suggestions or assistance ...

Issue with Inline forms in Ruby on Rails/HTML: The 'checked' attribute with the value of 'false' is

Desired Setting: I am trying to set the default option for this form to be "No" (or false), however, when I set it as such, my application loads with no option selected. When I use :check => true, the default option selected becomes "Yes" (or true). I ...

Tips for transferring variables as arguments in javascript

Currently, I am immersed in a test project aimed at expanding my knowledge of web development. Unexpectedly, I have encountered an issue that has left me puzzled on how to proceed. Within this project, there exists a table consisting of 11 cells. While 9 ...

Ensuring Date Data Integrity with HTML5 Validations

I need to set up validation for a mobile website that includes two input fields. The first field should validate that the value is not later than today's date, while the second field should validate that it is not later than one year in advance of the ...

Steps for Adjusting Column Heights in Bootstrap 4

Currently, I am experimenting with Bootstrap 4 and facing some challenges in setting the correct heights for my layout. The elements in my design include a "side nav", "header", "main", and "footer". My goal is to make the "main" section occupy most of th ...

assigning a label to a DropDownList

When attempting to place a label in front of a DropdownList, the dropdownlist appears on the next line. I have tried various suggestions from different sources but have not been able to find a solution. Here is the simple code I am currently using: Group ...

The integration between React hook form and reactstrap input components is not functioning properly

Having an issue with react-hook-form and reactstrap. The component List.jsx is causing trouble: import { useContext, useEffect } from "react"; import { ListContext, ADD_LIST } from '../providers/ShoppingListProvider'; import { Link } from "react- ...

CSS trick that works on iOS16 as well

Previously, this CSS workaround was effective up to iOS version 15.6; however, it does not seem to be functioning in iOS 16. @media not all and (min-resolution:.001dpcm) { @supports (-webkit-appearance:none) {} } Are there any updated techniques avail ...

Transforming a triangular shape into a square using Plane Geometry in THREE.js

I have an animation created with triangles in the link below. Is there a way to convert these triangular areas into squares? The animation currently has a line running through the middle when using "PlaneGeometry". Removing this line would turn the triangl ...

Ways to swap out unique symbols for HTML elements

I'm currently facing a challenge where I need to locate all strings between two specific characters and then replace those characters with HTML tags. For example, $str = "dog *cat* ping goat *pizza* cow rabbit*"; I would like a function tha ...

Is it possible to call a service within an NgFor loop in Ionic/Angular 2?

I am facing an issue with my ngFor directive. Here is the code snippet: <ion-card *ngFor="let review of reviews"> <ion-card-content> {{getUserName(review.user_ID)}} </ion-card-content> </ion-card> The challenge I a ...

Creating a countdown timer and looping through the seconds using HTML and JavaScript

I tried to incorporate a countdown on my website, but I'm facing an issue where the seconds freeze instead of continuously running. Here's the code snippet I used: <span id="dhour"></span> h <span id="dmin"></span> min & ...

Nav links in Bootstrap are seriously misplaced

My navigation bar, created with Bootstrap 4 CSS, consists of 3 links and a brand logo. Despite my expectations of all elements aligning properly, they appear disorganized and not in line. Please see the current layout here: https://i.sstatic.net/OeKcf.pn ...

Content will adjust to the screen size after a specific re-sizing

When the width is under 1024, I want the body of the page to scale to fit the user's screen without a scrollbar. My goal is to adjust the entire body so that it fits within the browser window without a scrollbar when the width is under 1024. I attempt ...