Display updated text on hover over button panel

Is it possible to achieve the following using only CSS without any JavaScript?

The desired outcome is a div containing two font awesome icons aligned to the right side.

Specifically, I am looking for:

- Icon of a pen that reacts on mouse hover

- Icon of an X that also reacts on mouse hover

I attempted to accomplish this by utilizing transforms and transitioning from display: none to display: block. However, it seems like animating this CSS property is not achievable. My current code snippet is as follows:

section.container {min-width: 500px; margin: 10% auto; text-align: center;}
.text-on-hover {cursor: pointer; font-size:20px;}

.text-on-hover:hover:before {opacity: 1;}
.text-on-hover:before {
  content: attr(data-hover);
  margin: 0 10px;
  opacity: 0; 
  transition: all .5s ease-in-out;
}
<section class="container">
  <i class="text-on-hover" data-hover="A long text">A</i>
  <i class="text-on-hover" data-hover="Another long text">B</i>
</section>

Is there a way to achieve this effect using pure CSS? If not, what are the limitations preventing it?

Answer №1

UPDATE: Take a look at the second piece of code.

Here is a possible approach to achieving this.

Visit the CodePen link

@import url(http://fonts.googleapis.com/css?family=Ubuntu:400,500,700);
html, body {
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
  font-family: 'Ubuntu', sans-serif;
  font-weight: bold;
}
body {
  background-color: #e58139;
}
header {
  background-color: #333;
  width: 100%;
  height: 65px;
  overflow: hidden;
}
.menu-links {
  padding-right: 20px;
}
.toggle-menu {
  position: relative;
  float: right;
  right: -220px;
  transition: right 0.5s ease-in;
}
.content {
  line-height: 43px;
}
.toggle-menu li {
display: inline-block;
list-style-type: none;
}
.toggle {
  padding-top: 5px;
  cursor: pointer;
}
li.toggle:hover span.toggle-menu-bar {
  background-color: #ddd;
}
.toggle-menu-bar {
display: block;
width: 25px;
height: 4px;
background-color: #e58139;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
margin: 0 auto 3px;
transition: background-color 0.2s;
}
a {
  font-size: 25px;
  text-decoration: none;
  color: #e58139;
  padding-right: 20px;
  transition: color 0.2s;
}
a:hover {
  color: #ddd;
}
.menu-links:hover > .toggle-menu {
  right: 0;
}
<header>
  <div class="menu-links">
    <ul class="toggle-menu">
      <li class="toggle">
        <span class="toggle-menu-bar"></span>
        <span class="toggle-menu-bar"></span>
        <span class="toggle-menu-bar"></span>
      </li>
      <li class="content">
        <ul>
          <li><a href="#">Link 1</a>
          </li>
          <li><a href="#">Link 2</a>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</header>

UPDATE: Duplicating the following:

View in CodePen

body {
  background: #222222;
}
div.edit,
div.delete {
  position: absolute;
  font-size: 22px;
  color: #989898;
  display: inline-block;
  cursor: pointer;
  transition: all 0.5s;
  right: 5px;
}
div.edit {
  transform: rotate(90deg);
  right: 30px;
}
.btn-container {
  margin: 2px 0 0 155px;
}
.box {
  position: relative;
  background-color: #F5F5F5;
  width: 200px;
  height: 30px;
  border: 10px solid #DDDDDD;
}
div.edit:hover,
div.delete:hover {
  color: #272727;
}
div.edit:hover:before {
  content: 'Edit';
  position: absolute;
  color: #989898;
  font-size: 12px;
  right: 0px;
  top: 30px;
  transform: rotate(-90deg);
}
div.delete:hover:before {
  content: 'Close';
  position: absolute;
  color: #989898;
  font-size: 12px;
  right: 22px;
  top: 5px;
}
.delete:hover + .edit {
  right: 60px;
}
<div class="container">
  <div class="box">
    <div class="btn-container">
      <div class="delete">&#10006;</div>
      <div class="edit">&#9998;</div>
    </div>
  </div>
</div>

Answer №2

It is not possible to animate the display attribute in CSS.

However, in this particular scenario where you are floating right, you can animate the menu's right property like this:

.toggle-menu {
    right: -220px;
    position: relative;
    -webkit-transition: all 1s ease;
    -moz-transition: all 1s ease;
    -o-transition: all 1s ease;
    -ms-transition: all 1s ease;
    transition: all 1s ease;
}

.toggle-menu:hover {
    right: 0;
}

You can see an example here: http://jsfiddle.net/v8t2td3b/1/

(Please note that I have also removed the style="display: none;" from the .content li)

Answer №3

Building a Responsive Menu

<div class="menu">
    <div class="hamburger"></div>
    <div class="menu-items">
        <a href="">...</a>
        <a href="">...</a>
    </div>
</div>

CSS Solution

.menu-items {
    opacity: 0;
    width: 0;
    overflow: hidden;
    transition: all 0.5s ease;
}

.menu:hover > .menu-items {
    opacity: 1;
    width: 200px; /*or adjust the width*/
}

This method provides a way to create a menu that is responsive without relying on JavaScript.

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

Designing a webpage compatible across different browsers that features an image positioned relatively over two rows of a table

I am working on creating a unique table layout for a web page that resembles the design linked below: Click here to view the image http://kelostrada.pl/upload/test.png Your assistance in achieving this would be greatly appreciated. Currently, I have imp ...

Steps to easily modify the color of Font Awesome stacked icons when hovering over them

I have incorporated two Font Awesome icons into my design: fa-circle-thin fa-user-plus These icons are stacked to create a circular icon appearance. <span class="fa-stack fa-sm"> <i class="fa fa-circle-thin fa-stack-2x"></i> <i c ...

Transitioning from Bootstrap 4 to Bootstrap 5 raises concerns about container compatibility

Interested in transitioning from Bootstrap 4 to Bootstrap 5. Noticed that the container max-width appears different on the default setting. Any insight on why this is and how it can be adjusted to resemble Bootstrap 4? I'm a beginner with Bootstrap, s ...

Should scripts be replayed and styles be refreshed after every route change in single page applications (SPA's)? (Vue / React / Angular)

In the process of creating a scripts and styles manager for a WordPress-based single page application, I initially believed that simply loading missing scripts on each route change would suffice. However, I now understand that certain scripts need to be ex ...

Tips on creating adaptable images for mobile viewing

My coding conundrum involves the use of two columns - one for an image and the other for a description of that image. However, when viewing my site on mobile devices, the image is cut off at only half its height. Adjusting both columns to col-sm-6 results ...

What can be done to prevent inputs from shifting when resizing a browser window?

I am struggling to align checkboxes and text on a PHP form I am creating. No matter what I try, they end up misaligned when the browser is resized. Any suggestions on how to solve this alignment issue without changing the format of the rest of my page wou ...

Utilizing Node.JS Nodemailer for sending emails with extensive HTML code

I am encountering an issue. I am working with Node.JS nodemailer. const url = `${url}`; var mailOptions = { from: 'stackoverflow', to: email, subject: 'test', html: 'Please click this email to confirm your email: &l ...

Mobile Image Sizing with Bootstrap 4 Cards

I am currently developing a responsive web application using Bootstrap 4 and incorporating cards with images on one of the pages. The layout functions properly when resizing the window on desktop view; however, upon viewing it on mobile devices, I noticed ...

What steps are involved in creating a mobile web app using Visual Studio 2012, jQuery, and ASP.NET MVC?

As a beginner in asp.net, I am tasked with creating a mobile web app project. Before diving into development, I would like to first focus on designing my app. Can anyone provide guidance on how I can achieve this? Any help is appreciated. ...

Having trouble accessing the information stored in the Firebase Database?

As a newcomer to Firebase and JS, I am attempting to showcase user information on a webpage that is stored within the Firebase database. The data format resembles the image linked here I have written this Javascript code based on various tutorials. Howev ...

Content on the right floating underneath content on the left

Here is the HTML code snippet I am working with: <div id="main_content"> <div id="left_side> <h1> MY BIG HEADER </h1> </div> <div id="content_area"> SOME CONTENT HERE SOME CONTENT HERE S ...

Expanding your selection capabilities using BeautifulSoup

My objective is to harness the power of BeautifulSoup in a unique way. Within my HTML files, there are two specific tags that catch my interest. The first one, which I'll refer to as TagA, is <div class ="A">...</div> The second tag, k ...

minimize the size of the image within a popup window

I encountered an issue with the react-popup component I am using. When I add an image to the popup, it stretches to full width and length. How can I resize the image? export default () => ( <Popup trigger={<Button className="button" ...

What causes the unset width or height to impact object-fit, and is there a workaround for this issue?

Query Beginnings Encountering unexpected behavior while using the value unset with the rule object-fit led me to question if there might be a rationale or workaround for this peculiar issue. Demonstration .block { &__img { width: 100%; hei ...

Troubleshooting issue: Bootstrap not loading with custom CSS on Flask platform

Recently, I started learning Flask and created a basic web application. Utilizing Bootstrap 5 for my project, I encountered an issue where my custom CSS fails to work when the Bootstrap stylesheet is loaded (removing it resolves the problem). My folder st ...

Creating a textbox with pre-filled content

Seeking assistance with a Java compiler app I am developing through phonegap. Need help setting the default Java Class name in a textarea without it disappearing. This is what I have: <div> <label for="source">Source Code:</label> ...

Can you provide guidance on how to successfully transfer an array of JSON objects from the script section of an HTML file to the JavaScript

My webpage contains an array of JSON objects that I need to send to the server. The array, stored in a variable called results, appears normal when checked in the console before trying to POST it. Here is a sample of the data: 0: {id: 02934, uName: "Ben", ...

Shrink video size directly in the browser using JavaScript and HTML5 before storing or sharing it

Is there an optimal method for resizing and potentially trimming a video using Javascript, HTML5 or Webassembly in modern browsers before saving it to the Filesystem? What is the most effective way to edit a video within the browser? Here are some differe ...

Styled-components is not recognizing the prop `isActive` on a DOM element in React

In my code, I have an svg component that accepts props like so: import React from 'react'; export default (props) => ( <svg {...props}> <path d="M11.5 16.45l6.364-6.364" fillRule="evenodd" /> </svg> ) ...

Can the tab button be used to move to the next field?

Is it possible to navigate to the next field by pressing the tab button? If so, how can we achieve this? Currently, I am utilizing Bootstrap classes col-md-6. Thank you! <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4. ...