Placing an absolute div directly at the boundaries of an inline span

In my project, I am working on adding a feature where text can be selected with the help of "handles." These handles have to be positioned either on the left or right side of the inline span. For reference, here is an image:

https://i.sstatic.net/U1XGe.jpg

Although I've made progress in styling this using CSS, I'm facing one issue – I am unable to position the right handle correctly on the text itself.

Below is the code snippet that I am currently utilizing:

.text {
      display: inline;
      background-color: #4d82f2;
      color: white;
      padding: 1px;
      border-radius: 2px;
      margin: 0;
      position: relative;
    }

.text>.handle-left {
  position: absolute;
  background-color: #757575;
  height: 25px;
  width: 30px;
  left: -30px;
  top: -25px;
  font-size: 10px;
  border-radius: 15px;
  border-bottom-right-radius: 4px;
}

.text>.handle-right {
  position: absolute;
  background-color: #757575;
  height: 25px;
  width: 30px;
  right: -30px;
  top: -25px;
  font-size: 10px;
  border-radius: 15px;
  border-bottom-left-radius: 4px;
}
<div>All the other text
  <div class="text">
    <span>blue text</span>
    <div class="handle-left">plus icon</div>
    <div class="handle-right">plus icon</div>
  </div>
  Maybe some more text, who knows?
</div>

The issue is well-represented in this JSFiddle: JSFiddle

Answer №1

To ensure the elements are positioned correctly, I would convert the element to inline and create one at the start and another at the end using pseudo elements:

.all-data {
  width: 400px;
  margin: 40px;
}

.data {
  display: inline;
  background-color: #4d82f2;
  color: white;
  padding: 1px;
  border-radius: 2px;
  margin: 0;
  position: relative;
}

.data>.handle-left,
.data>.handle-right {
  position: relative;
}

.data>.handle-left:before {
  content: "+";
  position: absolute;
  background-color: #757575;
  height: 25px;
  width: 30px;
  left: -30px;
  top: -25px;
  display: inline-flex;
  justify-content: center;
  font-size: 24px;
  border-radius: 15px;
  border-bottom-right-radius: 4px;
}

.data>.handle-right:before {
  content: "+";
  position: absolute;
  background-color: #757575;
  height: 25px;
  width: 30px;
  right: -30px;
  bottom: 18px;
  display: inline-flex;
  justify-content: center;
  font-size: 24px;
  border-radius: 15px;
  border-bottom-left-radius: 4px;
}
<div class="all-data">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laborum corporis enim doloremque perspiciatis, doloribus nemo commodi, consectetur
  <div class="data">
    <span class="handle-left"></span>
    <span> quaerat verit</span>
    <span class="handle-right"></span>
  </div>
  autem laboriosam est alias aspernatur deserunt quae, fugit eos? Lorem ipsum dolor sit amet,

  <div class="data">
    <span class="handle-left"></span>
    <span> quaerat verit consectetur adipisicing elit. Laborum corporis enim </span>
    <span class="handle-right"></span>
  </div>

  consectetur adipisicing elit. Laborum corporis enim doloremque perspiciatis, doloribus nemo commodi, consectetur
  <div class="data">
    <span class="handle-left"></span>
    <span> quaerat veritatis at unde</span>
    <span class="handle-right"></span>
  </div>
  autem laboriosam est alias aspernatur deserunt quae, fugit eos?</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

The challenge of incorporating mod_rewrite with CSS files

I am new to mod_rewrite. I have a page profiles.php?page=XXX and I tried to change its URL to something more friendly like /cars/XXX/ RewriteEngine on RewriteRule ^cars/([^/]+)/?$ profiles.php?page=$1 [L] The issue arises when including styles: <li ...

The camera will only activate once the page is manually clicked using Puppeteer

My recent project involved developing a NodeJS application that performs the following tasks: Setting up a server to serve static files using http.createServer Launching a puppeteer process to open Chrome browser in non-headless mode. The browser is direc ...

Hide the scroll bar in html/css while keeping the functionality of the arrows intact

Is there a way to remove the scroll bar but still be able to access overflown data using arrows? Any assistance would be greatly appreciated. Thank you in advance. ...

Enhance User Experience with a Multi Step Form incorporating a Progress Bar, designed with jQuery and CSS3. Resolve issues with

I downloaded a similar "Multi Step Form with Progress Bar using jQuery and CSS3". I managed to display the fieldsets with divs for additional forms or images, so showing the content in the fieldset is not an issue. However, the problem lies with the actua ...

Are numerous h1 tags allowed?

Many debates have arisen regarding the use of multiple h1 tags on a single web page. What if additional semantic meaning is provided using hgroup, article, header, footer, and section tags? Despite numerous discussions, a definitive answer remains elusiv ...

Can the font size of a container be adjusted proportionally to its width or height using only CSS?

When it comes to ensuring that the font always fits nicely within its container, I rely on using Javascript to set divs or specific class to have their font-size dynamically equal to their width, and adjust their inner text elements accordingly. If you wa ...

Clicking on a select box causes the scrollbar area to vanish, shifting all the page content to the right

I have successfully implemented overflow-y: scroll; to keep the scrollbar space always reserved and prevent content from shifting when the scrollbar appears. However, I noticed that when I click on a select element, this scrollbar space is lost. How can I ...

Spinning icons in React: A step-by-step guide

Currently, I am attempting to create a spinning icon using React, but it seems like I might be overlooking something in my approach. Here is the return function that I have: return ( <div className="loader"> <p>Loading Dat ...

The issue with Open Sans and sans-serif fonts not rendering properly in all web browsers

I am having trouble with websites that use the Open Sans font appearing with a strange handwriting style. I'm not sure where this issue is originating from. Here are some things I have tried: Using In-Private browsing Disabling all extensions Testin ...

Submitting the form for validation to parse.com

Is there a way to ensure that my form is only submitted to parse.com when all fields are filled in? Despite using a validation script, the form still submits even if the fields are empty when I click the submit button. I attempted using a button instead o ...

Obtaining the URL from the anchor tag

I am currently working on a project that involves scraping data from the cinch.co.uk website. My tools of choice are Python along with the BeautifulSoup4 and Request libraries. My goal is to extract car data from each advertisement, starting by navigating ...

Eliminate any excessive space located at the bottom of the table

How can we adjust the spacing at the bottom of the table to remove extra space? Currently, it looks like this: https://i.stack.imgur.com/DwkbG.png We want it to look like this: https://i.stack.imgur.com/extDP.png CSS .wk_mp_body td { background: ...

Guide to setting up jQuery Mobile with bower

For my project, I'm interested in utilizing jquery-mobile through bower. In order to do so, I need to execute npm install and grunt consecutively within the bower_components/jquery-mobile directory to access the minified .js and .css files. This pro ...

Expanding DIV to cover entire width of screen

Allow me to explain my current predicament: In the following code snippet, I have a simple div element. However, upon running the code, I am presented with a gray box that does not span across the entire browser window as I had intended. Instead, it appea ...

In what way does Google+ make their logo come to life on the left side of the navigation bar when the scrollbar is minimized?

I'm curious about the animation Google+ uses to make their logo slide in on the navigation bar when it shrinks. I've managed to shrink the scrollbar on scroll, but I can't quite replicate their technique for animating the icons. I'm eag ...

Dragging an image in KineticJS gets stuck after the drag operation is completed

My experience with KineticJS has been positive so far, but I have encountered an issue with dragging images. In the example below, I have set the image to be draggable, but after the initial drag, it seems like the image can't be moved again. var sta ...

In Angular, when a component is clicked, it is selecting entire arrays instead of just a single item at a

I am currently working on implementing a rating feature using Angular. This component will be used to rate different languages based on how proficient I am with them. My issue lies in the fact that when I click on one array element, it automatically selec ...

Display the contents of an HTML Object tag without relying on any external packages

Is there a way to print a PDF without opening it in a new tab? In my project, I have embedded a PDF using the HTML Object tag: <> <object id="documentObject" data={fileSrc} type={fileType} height="100%" width="100%" ...

The appearance of the Bootstrap button is not displaying correctly

My current button looks like this: https://i.sstatic.net/PyP8v.png As seen in the image above, there is something strange at the back of the button. How can I resolve this issue? I am working on a small project using Django and Bootstrap. Thank you in a ...

What is the method for creating an element on the right side of a heading using jQuery/JavaScript?

I am seeking assistance with a project of mine. To illustrate, I have provided a brief code snippet below. Let's consider a scenario where we have two sections denoted by the class "box". Inside each box, there are elements containing a heading and s ...