Change the background color to be visible when there is text overflow with CSS

When I set my <P> tag with a fixed height and overflow:hidden, the text is displayed. However, when I apply a hover effect to it, I set overflow:visible;

The issue I am facing is that the text is displayed, but I would like to adjust the z-index and background color of the <p>. The background should only be displayed for the maximum width of <P>, changing the background color at the height where the text ends.

CSS:

.oe_description {   
   overflow:hidden;
   max-height:4.2em;
   text-overflow: ellipsis;
}
.oe_description:hover {
   overflow:visible;
   position:relative;
   z-index:100;
   background: #e5f2f7; 
   font-weight:bold;
}

View example here: FIDDLE

Answer №1

JSFiddle - DEMO

To achieve the desired effect for .oe_description:hover, you can set max-height: none;.

CSS:

.oe_description {   
   overflow:hidden;
   /* 4 times the line-height to show 4 lines */
   /*max-height: 5.6em;*/
   /* 3 times the line-height to show 3 lines */
   max-height:4.2em;
    text-overflow: ellipsis;
}
.oe_description:hover {
   overflow:visible;
   position:relative;
   z-index:100;
   background: #e5f2f7; 
   font-weight:bold;
   max-height: none;
}

HTML:

<div class="col-xs-10 non_padding">
    <p class="oe_description">
       When text must stay on one line, but there is not enough room for it all, this article provides a solution.
       When text must stay on one line, but there is not enough room for it all, this article provides a solution.
       When text must stay on one line, but there is not enough room for it all, this article provides a solution.
       When text must stay on one line, but there is not enough room for it all, this article provides a solution.
       When text must stay on one line, but there is not enough room for it all, this article provides a solution.
       When text must stay on one line, but there is not enough room for it all, this article provides a solution.
       When text must stay on one line, but there is not enough room for it all, this article provides a solution.
    </p>
</div>

Answer №2

In order to maintain the div height and resolve the content display problem, you may opt for adding a scroll bar by changing the overflow property to :auto. This approach ensures that the background color adapts accordingly without compromising the overall design.

.oe_description:hover {
   overflow:auto;/*add scroll functionality*/
   position:relative;
   z-index:100;
   background: #e5f2f7; 
   font-weight:bold;     
}

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

If the overflow-x property is set to hidden for the html and body elements, the links in the footer will become unclickable as they will

Situation: Consider the following scenario with a simplified HTML example: position the footer behind the content and make it stick to the bottom when reaching the end of the page, the footer should slide up from behind the content I achieved this layo ...

What is the best way to place a dropdown menu in front of buttons?

Here is an example snippet of HTML code that showcases a SweetAlert2 date picker popup for a button titled "Click me!": <!DOCTYPE html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf ...

Tips for removing Blogger post snippet code from the template below:

Check out my blog at: . I have also posted the full Template code at https://pastebin.com/GXuPFKzH I want to display the full post on the homepage instead of just a snippet. Can you help me figure out which code needs to be removed? [insert image of the ...

How can jQuery be utilized to dynamically update the text in a navbar?

<div id="page1" data-role="page"> <div data-role="header" data-position="fixed" align="center"><img src="img/VAWE-long-300x30-transparent.png" width="300" height="30" alt="" /></div> <div data-role="content" style="margin ...

Transform image to text upon hovering

Is there a way to transform an image into text upon hovering, and revert back when the mouse moves away using HTML, CSS, and JavaScript? I am specifically working with HTML5UP's Aerial theme, in case that has any impact. ...

Divide the PHP code into separate files

Can someone help me with a coding dilemma I have? I couldn't find any information on Google or this site about it. A friend of mine who has experience in website development and runs his own business uses an interesting system that is new to me. He ...

Organize your HTML & CSS by placing the <link> tag outside of the <head> for better structure

Would it be acceptable to place the <link> to a CSS file outside of the <head/> tag, maybe in the footer section? What are the potential advantages and disadvantages of doing this? I am considering this because I have a CSS file that only con ...

Creating a responsive canvas and image container in fabric.js involves adjusting the layout and size of the

I am working with fabric.js and canvas to dynamically add an image from a URL. I would like to make the canvas responsive based on the resolution. How can this be achieved? Here is my current code: <canvas id="panel" width="700" height="350"></ ...

A straightforward jQuery conditional statement for dynamically generated content

If the div '#ajax' contains content, I want to make the div '.container' clickable. Here is the basic jQuery script I have implemented: if ('#ajax:empty') { $('.container').css('pointer-events', ' ...

The variable $_FILE fails to retrieve the file, resulting in PHP displaying an undefined variable error

I am facing an issue where I am unable to get the image file sent from an HTML form to insert it into a database. The other variables are being posted successfully, as I have checked them by echoing, but the image file is not getting posted. I suspect that ...

Utilizing HTML5 Input Types with Struts2 Tags in JSP: A Step-by-Step Guide

When taking age as input from a JSP, HTML5 provides the following: <input type="number"> Is there an equivalent tag for this in Struts2? ...

The Element fails to go back to its original position due to CSS Float

After changing the screen resolution, it appears that the nav-search-ul element fails to return to its correct position. It seems like the media query is not working properly. This issue only occurs in Chrome, as everything works fine in Firefox and IE. ...

Unable to successfully Uploading an Image using PHP

I've encountered a new issue. I'm trying to upload an image from my webpage into a folder on the server (using XAMPP). Despite trying multiple examples, including those from php.net and w3schools.com, the code isn't working and I can't ...

Carousel position images off-center to the right on smaller screens

I created a fullscreen slider using the carousel bootstrap feature. However, when the screen size shrinks, the background image resizes and centers itself. Instead of centering the image on smaller screens, I want it to focus on the right side. Here is ho ...

The text in the Bootstrap 4 card spills over the card-text boundary

I've encountered an issue when trying to insert text into a card-text paragraph. Here's what happens: https://i.sstatic.net/CWGh2.png I've been attempting to resolve the problem without success. I'm completely stumped on how to fix it ...

Unable to populate HTML dropdown using AJAX

<script type="text/javascript"> $(document).ready(function () { $.ajax({ url: '/Umbraco/api/RegisterUser/GetCountry', type: 'GET', // Using GET method data: '{}', ...

Distance from the border

Having a small issue with margin, here is the code: echo '<p class="news_post">'; $stripped = strip_tags($post,'<img>'); $break = str_replace('>','><br>',$stripped); ...

Building an Image/Grid system using CSS and Javascript

Currently in the process of constructing a fresh website, I find myself in need of a solution to a particular issue (specifically involving PHP, MySQL, and Zurb Foundation). The challenge at hand is to fashion an image comprised of 1,000,000 pieces, each ...

Looking for assistance with CSS to properly align a dozen items

UPDATE: See the current layout in this JFiddle based on feedback received from others, still a few tweaks needed. In my setup, there are 12 divs housed within one container div. These 12 divs consist of 6 text components and 6 corresponding input fields. ...

Creating a CSS animation to repeat at regular intervals of time

Currently, I am animating an SVG element like this: .r1 { transform-box: fill-box; transform-origin: 50% 50%; animation-name: simpleRotation,xRotation; animation-delay: 0s, 2s; animation-duration: 2s; animation-iterat ...