What is the best way to incorporate a sliding div into these CSS Message Bubbles?

Just dipping my toes into web development, so please bear with me. I am working on a prototype of a Messaging Application and have most of the basics sorted out. Now, I am adding some finishing touches to enhance user experience. My goal is to make it so that when a message is hovered over, the time at which the message was sent will slide out from the message.

Here is the current state of my code: http://codepen.io/RBrNx/pen/GNzOWr (Note: Click on "Toni" again and his message will appear, minor bug. You can also send messages from the text box).

Below are some images illustrating what I am trying to achieve: https://i.sstatic.net/0ksGF.jpg

I personally think the second one would look better.

I attempted to implement this by including a span inside the message bubble like this:

<div class="bubble you">Test Message<span class="hover-time">13.45</span></div>

.hover-time{
   position: relative;
   left: 60px;
}

However, this caused the inside of the bubble to stretch to accommodate the Span.

Any suggestions on how I can achieve this?

EDIT: Thanks to Antidecaf, I managed to get the left side working and figured out the right hand side too. Here is the additional CSS I added:

.container .right .bubble.you .hover-time {
    display: none;
    position: absolute;
    left: 110%;
    color: #999;
    width: 100px;
}

.container .right .bubble.me .hover-time {
    display: none;
    position: absolute;
    right: 90%;
    color: #999;
    width: 100px;
}

These rules handle the left-hand messages (from the person you are messaging) and the right-hand messages (from me). I also included:

.container .right .bubble.you:hover .hover-time{
    display: inline-block;
}
.container .right .bubble.me:hover .hover-time{
    display: inline-block;
}

This ensures that the hover-time span is displayed upon hovering over the message.

Answer №1

If you want to achieve this effect, you can use the suggested markup and manipulate the positioning of the elements using CSS classes. Position .hover-time relative to .bubble by adding position: relative to .bubble and position: absolute to .hover-time. You can find more information on this technique here.

<div class="bubble you"><span class="hover-time">13.45</span>Test Message</div>

To position the timestamp to the right:

.bubble {
    position: relative;
}

.hover-time {
    position: absolute;
    left: 110%;
    color: #999;
}

For positioning it to the left, you'll need to adjust the margin of the bubble to make room for the timestamp:

.bubble {
    position: relative;
    margin-left: 50px;
}

.hover-time {
    position: absolute;
    left: -50px;
    color: #999;
}

Answer №2

<style>
.hover-time {
  position: relative;
  left: 60px;
  display: none;
}

.bubble:hover .hover-time {
  background-color: #ccc;
  color: #000;
  display: inline-block;
}
</style>
<div class="bubble you">Test Message <span class="hover-time">13.45</span></div>

It's effective for me. You may consider adding some transforms or other animated elements to enhance the design.

UPDATE: Maybe this is what you had in mind:

<style>
.bubble {
  border: 1px solid #ccc;
  width: 300px;
}

.hover-time {
  float: right;
  display: none;
}

.bubble:hover .hover-time {
  background-color: #ccc;
  color: #000;
  display: inline-block;
}
</style>
<div class="bubble you">Test Message <span class="hover-time">13.45</span></div>

The border and width are just for visual reference.

Answer №3

Am I mistaken in assuming that you are using the DIV as a representation of a speech bubble and then singling out the span within the div by essentially saying, 'not you buddy, you're different'?

If this is the case, wouldn't it be simpler and more efficient to place your text message in a span element as well, styling the span as a speech bubble, and leaving the div as an unseen structural component?

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

What is preventing the container slide from reacting solely to the image?

I've been working on an image and a slider, and everything seems to be functioning as intended. However, there is a strange reaction on the left side even before reaching the image. I've tried tweaking it, but I can't seem to figure out the ...

Avoiding Page Reloads with Ajax while Removing Entries from a Table in Laravel

I am encountering an issue where I can click the button, but it requires a refresh to execute the delete action. Is there something in my ajax code causing this problem and why is it refreshing? I want the button to instantly delete without the need for a ...

Is there a method to access a website, trigger JavaScript functions, and subsequently retrieve the HTML content using PHP?

I am currently exploring options to access a webpage, execute JavaScript functions on it (thus altering the HTML content), and eventually save the modified version of the page. I'm uncertain if this approach is feasible, and if not, are there alternat ...

I am having trouble getting the jsTree ajax demo to load

I am having trouble running the basic demo "ajax demo" below, as the file does not load and the loading icon continues to churn. // ajax demo $('#ajax').jstree({ 'core' : { 'data' : { ...

Content moves as you scroll, not within a lightbox

I have implemented lightbox style overlays for my 'Read More' content on my website. However, I am facing an issue where the content within the lightbox does not scroll; instead, the background page scrolls. Any assistance with resolving this p ...

Steps for closing an Android dialog opened by an HTML select tag

I'm in the process of developing a mobile web application for both Android and iPhone. Currently, my HTML page includes a select tag that, when selected on an Android browser, triggers the default spinner dialog with options like Previous, Next, and D ...

When utilizing drag and drop in HTML5, the items.webkitGetAsEntry() method is not available

Hey there, I am attempting to drag and drop files into my file system using Chrome, but I encountered the following error in the console: var dnd = new DnDFileController('body', function(files, e) { var items = e.dataTransfer.items; for ...

What is the best method for showcasing numerous dropdown lists using JavaScript along with conditional if-else statements?

Hello everyone, I am currently new to javascript and I'm attempting to build a small web application using javascript. However, I am facing an issue with printing the drop-down list output. Could someone please assist me in resolving this problem? < ...

The clipPath feature in webkit fails to display correctly

After reading this insightful article, I decided to experiment with applying a gradient clip-path to a simple shape (an O letter converted to curves). To my frustration, the technique worked perfectly in Firefox but failed to render anything in webkit bro ...

I want to create a Bootstrap 4 card deck that includes expand/collapse functionality for its content. The expanded content should only impact the current

I am experiencing an issue with my card decks and expand/collapse functionality. Currently, when I expand one card in the deck, all the other cards in the row also expand, leaving a large blank area. This becomes problematic especially when the content is ...

Creating an animated transition for an element's height with CSS

I need to animate the height of a div that doesn't have a specified height. Using max-height isn't an option due to multiple potential height amounts. I attempted adding transition: height 0.2s ease to the div, but it didn't work as expecte ...

Guide to Sending and Scheduling Notifications through HTML 5's Notification Web APIs

Is it possible to utilize Web APIs to Schedule Notifications and send them at specific times? I am working on a CMS application that allows users to schedule and send push notifications for their mobile apps. I am interested in implementing something sim ...

Removing a CSS Class Using Tampermonkey: A Step-by-Step Guide

I'm completely new to CSS and javascript, so please bear with me. My goal is to remove the class disable-stream from each of the div elements located under the div with the class "stream-notifications". Below is an image for reference: Even though I ...

You can interact with our dropdown menu using the tab key, even when it is

We are looking to make our dropdown tabbable when expanded and non-tabbable when collapsed. We attempted using tabindex="-1" on the content inside the expandable div, but this resulted in it being non-tabbable even when expanded. Any suggestions on how t ...

The issue of custom breakpoints not functioning properly with Bootstrap 4's navbar-expand-* feature

Here is my customized Bootstrap navbar: <div class="container-fluid bg-secondary"> <nav class="navbar utep-nav navbar-inverse navbar-expand-lg"> <button class="ut-toggler navbar-toggler ml-auto" type="button" data-toggle="collapse ...

Can CSS be used to automatically focus on a div element?

Can CSS be used to set autofocus on a div element? <div class="form-group" style="margin-left:10px"> <label for="organizationContainer" class="nmc-label">@Res.GroupStrings.CreateNewGroupOrganization</label> <div id="organiza ...

I am looking for a way to batch upload several images from an HTML page to a nodejs server, where I can then rename and

Exploring the world of nodejs, I am seeking guidance on updating multiple images from HTML to node js. Specifically, how can I rename each image differently and save them to a directory? Alternatively, is there another solution that would better suit my ne ...

Tips for disabling default browser input validation in React

https://i.stack.imgur.com/834LB.png Is there a way to remove the message "Please fill out this field" while still keeping the "required" attribute intact? I have my own validation system in place, so I need to use the "required" attribute to determine whe ...

Tips for displaying just the image name without the entire image file path

When I try to upload an image, the path displayed is "C:\fakepath\promotion1.JPG". This fake path is causing the image not to upload to my project media folder. How can I solve this issue? Instead of the complete path, I only want to capture the ...

Utilizing MUI for layering components vertically: A step-by-step guide

I am looking for a way to style a div differently on Desktop and Mobile devices: ------------------------------------------------------------------ | (icon) | (content) |(button here)| ----------------------------------------- ...