Text overflow occurs when content overflows the space of its container, causing the HTML element to

My English isn't the greatest, but I hope to convey the question clearly.

I am attempting to create an editable text within a div. Typically, the text will overflow the size of the div, so I need to contain the text in the div using overflow:scroll. However, I also want to allow HTML elements in that div, specifically a tooltip. The issue is that this HTML element (the tooltip) tends to extend beyond the boundaries of the div. Using overflow:scroll to manage the text causes the tooltip to be cut off. Here is an image illustrating the problem:

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

You can view the corresponding codepen with the code shown in the image here: https://codepen.io/anon/pen/jwXGRE?editors=1100

#Out {
        background: black;
        color: white;
        width: 200px;
        height: 200px;
        margin-top: 30px;
        overflow: scroll;
      }

      #noOut {
        background: black;
        color: white;
        width: 200px;
        height: 200px;
        margin-top: 30px;
      }

      body {
        background: red;
      }

      .tooltip {
        position: relative;
        display: inline-block;
        border-bottom: 1px dotted black;
      }

      .tooltip .tooltiptext {
        visibility: hidden;
        // More CSS...
      }
      
      /* Additional CSS rules... */
    
<div id="Out" contenteditable="true">
      <div class="tooltip">Hover over me
        <span class="tooltiptext">Tooltip text</span>
      </div>
      <!-- Long text goes here -->
    </div>

    <div id="noOut" contenteditable="true">
      <div class="tooltip">Hover over me
        <span class="tooltiptext">Tooltip text</span></div>
      <!-- Another long text section goes here -->
    </div>

In conclusion, my goal is to have the tooltip overflow while containing the main text within the div.

Once again, apologies for any language barriers, and I would greatly appreciate any helpful tips or advice :).

Answer №1

Updated: Your solution has been revised:

body {
  background: turquoise;
  padding: 15px;
  font-family: verdana;
}

.container {
  position: absolute;
}

#Output {
  background: #555;
  line-height: 18px;
  color: black;
  width: 250px;
  height: 180px;
  margin-top: 25px;
  overflow: scroll;
  padding: 8px;
}

.hover-box {
  cursor: pointer;
  background: lightgray;
  color: #555;
  margin: 6px 0px;
}

.popover {
  position: absolute;
  display: none;
  top: -35px;
  left: 0px;
  border-bottom: 1px dotted red;
  width: 100px;
  background-color: gray;
  color: #000;
  text-align: center;
  padding: 4px 0;
  border-radius: 5px;
  z-index: 1;
}

.hover-box:hover .popover {
  display: block;
}
<div class="container">
  <div id="Output" contenteditable="true">
    <div class="hover-box">1- Hover here
      <div class="popover">Popover text</div>
    </div>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever
    <div class="hover-box">2- Another hover
      <div class="popover">Popover text</div>
    </div>
    Additional text goes here It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
  </div>
</div>

Code pen link: https://codepen.io/JaneDoe/pen/XyZabC?editors=1100

Answer №2

There was an issue with the positioning of .tooltiptext. The first example had a parent with relative positioning that was closed after the span. In the second example, there was a missing closing div. After adding it at the end and adjusting the position of the tooltip, everything worked correctly.

#Out {
  background: black;
  color: white;
  width: 200px;
  height: 200px;
  margin-top: 30px;
  overflow: scroll;
}

#noOut {
  background: black;
  color: white;
  width: 200px;
  height: 200px;
  margin-top: 30px;
}

body {
  background: red;
}

.tooltip {
  position: relative;
  display: inline-block;
  border-bottom: 1px dotted black;
}

/* Tooltip text */
.tooltip .tooltiptext {
  visibility: hidden;
  width: 120px;
  background-color: black;
  color: #fff;
  text-align: center;
  padding: 5px 0;
  border-radius: 6px;
  position: absolute; 
  z-index: 1;
}


/* Show the tooltip text when you mouse over the tooltip container */

.tooltip:hover .tooltiptext {
  visibility: visible;
}

.tooltip .tooltiptext {
  width: 120px;
  top: -2em;
}
<div id="Out" contenteditable="true">
  <div class="tooltip">Hover over me
    <span class="tooltiptext">Tooltip text</span>
  </div></div>

<div id="noOut" contenteditable="true">
  <div class="tooltip">Hover over me
    <span class="tooltiptext">Tooltip text</span></div></div>

Answer №3

It's time to add a little something extra.

CSS hasn't fully embraced this yet, but don't worry.

Even though it may not be mainstream, achieving this with pure CSS is still very much possible.

If you're looking for a tooltip to show up without causing any horizontal overflow in the div or affecting other elements, then things can get tricky when manually coding CSS positioning.

But fear not, there's a solution - Hint.css:

A CSS-only tooltip library to enhance your websites

While Hint.css can help achieve the desired effect, it might not cover all aspects as per your description.

Here's a sample implementation of how I would display a tooltip for the specified element while maintaining simplicity and cleanliness:

The tooltip will always stay fixed at the bottom of the screen, never interfering with anything else.

[class*="hint--"]:after {
  position: fixed !important;
  width: 200px;
  transform: none !important;
  bottom: 0px;
  left: 0px;
  margin: 0 auto;
  right: 0px;
  text-align: center;
  background: radial-gradient(circle, darkred 30%, #131418)!important;
  border-radius: 100vw 100vw 0 0;
}

#Out {
  background: black;
  color: white;
  width: 200px;
  height: 200px;
  overflow-x: hidden;
  overflow-y: scroll;
}

#Out>span {
  margin: 0 auto;
  padding: 10px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/hint.css/2.4.0/hint.min.css" rel="stylesheet" />

<div id="Out">
  <span contenteditable="true" class="hint--" aria-label="Tooltip text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam accumsan aliquet justo, nec facilisis est malesuada sed. Vestibulum pharetra accumsan nisi ac feugiat. Quisque et tempor tortor.

Phasellus tincidunt scelerisque mi sit amet varius. Sed vulputate rutrum cursus. Integer non vehicula odio. Fusce vestibulum metus felis, vel dapibus ante ultricies eu. Donec vitae justo eget diam condimentum gravida ut ut turpis.

Duis ante libero, ullamcorper vel ultricies sed, pulvinar pretium velit. Phasellus mollis, nunc quis fermentum imperdiet, arcu ex sollicitudin risus, at consequat purus justo a nibh. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</span>
</div>

Answer №4

Check out the final solution I mentioned in our conversation.

body {
  background: pink;
  padding: 40px;
  font-family: arial;
}

#Out {
  background: #333;
  line-height: 22px;
  color: white;
  width: 300px;
  height: 200px;
  overflow: scroll;
  padding: 10px;
}

.hover-div {
  position: relative;
  cursor: pointer;
  background: white;
  color: #333;
  margin: 8px 0px;
}

.tooltip {
  position: absolute;
  display: none;
  left: 0px;
  border-bottom: 1px dotted black;
  width: 120px;
  background-color: black;
  color: #fff;
  text-align: center;
  padding: 5px 0;
  border-radius: 6px;
  z-index: 100;
}

.hover-div:hover .tooltip {
  display: block;
  top: -40px;
}
<div class="outContainer">
  <div id="Out" contenteditable="true">
    <div class="hover-div">Hover over me
      <div class="tooltip">Tooltip text</div>
    </div>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
    <div class="hover-div">Hover over me
      <div class="tooltip">Tooltip text</div>
    </div>
    Additional text goes here It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout.
  </div>
</div>

Note: Customize the tooltip positioning by adjusting the values for left, right, and top according to your requirements.

Codepen Link: https://codepen.io/Omi236/pen/KqbQoO?editors=1100

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

"Is it possible to conditionally render a component depending on the state in

One of the challenges I'm facing is customizing a filter icon from MaterialUI. My goal is to change its color to black when a checkmark is checked, and adjust its position accordingly. Additionally, when a box is checked, it should fill in setFilters. ...

Can someone guide me on how to make a Carousel responsive using Angular?

HTML: <div class="voxel-row"> <div class="voxel-col-4"><h2 id="vagas_recentes">vagas recentes</h2></div> </div> <div id="carouselExampleControls" cl ...

``Is there a way to position an image at the center in Python dash?

Can someone help me with centering an image in my Flask dash app? Here is the code I have: import dash from dash import html, dcc import dash_bootstrap_components as dbc app = dash.Dash(__name__) app.layout = html.Div([ dbc.CardImg(src=r'assets/ ...

Arrange the DIVs in the identical spot and switch them back and forth

I'm struggling with a dilemma involving two DIVs. I am looking to have two DIVs positioned in the exact same spot on the page and toggle between them. When one div disappears, the other should appear using jQuery toggle(). The challenge lies in havi ...

Issues arise when attempting to animate letters using jQuery and CSS

My goal is to create a dynamic animation effect on a string when a user hovers over it. This involves turning the string into an array and applying different animations to each letter. Although the animations are working perfectly, I'm facing one issu ...

Using gradient colors for the background on the ::selection pseudo-element

Can you apply a gradient color to CSS ::selection? I tried using the following code: ::selection { background-image: -webkit-linear-gradient(left, #ee4037 0%, #eb297b 100%); } However, it did not have the desired effect. ...

Switching from portrait to landscape view on a mobile website

Currently, I am working on a mobile website where the navigation bar looks great in portrait mode with 15px of padding around the top and bottom. However, when I switch to landscape mode, it occupies almost half of the screen. Does anyone have suggestions ...

How do I dynamically incorporate Tinymce 4.x into a textarea?

I am encountering a small issue when trying to dynamically add tinymce to a textarea after initialization. tinymce.init({ selector: "textarea", theme: "modern", height: 100, plugins: [ "advlist autolink image lists charmap print p ...

Javascript clock problem, kick off on click

Currently working on a game and in need of a counter that starts when clicked and stops at 00 (From 1m to 00). It is currently starting onload and cycles back to start when it reaches 00. HTML: <body> <div> <div class="timer" >Battle t ...

Stop rows from causing the table to stretch in width

We have a unique table structure where the first row contains two cells and the second row contains only one cell. The width of each cell is determined by its content. Our goal is to prevent the cell in the second row from expanding the overall table widt ...

Preventing Paste Function in Electron Windows

Currently, I am utilizing Electron and attempting to prevent users from pasting into editable content. While paste and match style remains enabled, the functionality is flawless on Mac devices. However, there seems to be an issue on Windows where regular ...

Loading animation reminiscent of a whirlpool, similar to the movement

In my quest for the perfect animation, I have scoured far and wide. Unfortunately, the one I found at http://jsfiddle.net/pedox/yed68/embedded/result/ is created using css, which many browsers do not yet support fully. I also explored , but found it to be ...

"Experience a unique website layout with varying designs on desktop and mobile devices while using the React technology to enable desktop

Just starting out with React and I've noticed something strange. When I view my website on a PC, everything looks normal. However, when I switch to the desktop site on my phone, things appear differently. It seems like moving an element by 20px on mob ...

Horizontal Accordion Design for Cascading Style Sheets (CSS) Books

I am currently working on developing a page that features a book layout. This page will include tabs that users can expand individually. If you would like to see a working example, you can check out this link: https://codesandbox.io/s/book-layout-l28gh?fi ...

Is it possible to process HTML and JavaScript as a request in JMeter?

After receiving a response, I noticed that the HTML body contains a hidden form along with an internal JavaScript function to submit the form (view snapshot here). Is there a method in JMeter to execute JavaScript code that directly submits a form? I am ...

Is there a way to alter the styling of a React element without resorting to querySelector?

Just diving into React and I'm faced with a challenge - how to update the CSS of a class based on a condition. let customizeColumn = document.querySelector(".main-table-body"); !expandFilter ? customizeColumn.setAttribute("style", ...

Sliding Feature

Can someone assist me with implementing a jQuery half slide function from left to right? Please check out the following URL for more information: http://jsfiddle.net/prabunivas/Fy9Hs/2/ <div> <div id="col1" style="float:left"> &l ...

The drop-down list was designed with a main button to activate it, but for some reason it is not functioning properly. Despite numerous attempts, the list simply will not display as intended

<html> <body> <button onclick="toggle()" class="nm" > main</button> <div class="cntnr" style="display : none;"> <ul class="cnkid"> <li class="cnkid"><a class="cnkid" ...

Customizing the appearance of jQuery accordion components

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js" language="javascript"></script> <script type="text/javascript" src="http://www.compactcourse.com/js/accordionNew.js" language="javascript"></script> < ...

Encountering a database deletion error with Spring Boot Thymeleaf

I'm currently facing an issue where I am attempting to remove an entry from my database. However, when I try to delete it on my html page, I end up being redirected to an error page instead of the intended destination. The entry I am trying to delete ...