What is the best way to show the title when a user hovers over a word?

Below is an illustration of the code:

<p>Bob's <dfn title="Dog">canine</dfn> mother and <dfn title="Horse">equine</dfn> father sat him down and carefully explained that he was an <dfn title="A mutation that combines two or more sets of chromosomes from different species">allopolyploid</dfn> organism.</p>

How can I configure it so that when a user hovers over canine, equine, or allopolyploid, the title I designated using dfn title is shown to the user, providing the definitions of those complex words?

Answer №1

Utilize the title as content for the :after pseudo-element and showcase it as a tooltip!

p {
  margin: 60px auto;
}
dfn {
  position: relative;
}
dfn:hover:after {
  content: attr(title);
  background: #fff;
  padding: 5px 12px;
  border: solid 1px #ddd;
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
}
<p>Bob's <dfn title="Dog">canine</dfn> mother and <dfn title="Horse">equine</dfn> father sat him down and carefully explained that he was an <dfn title="A mutation that combines two or more sets of chromosomes from different species">allopolyploid</dfn> organism.</p>

Answer №2

Opt for using the data-title attribute instead of title and apply styling to it.

You can enhance the styling using .

If you stick to using only title, you may not be able to eliminate the default title tooltip.

dfn {
  border-bottom: 1px dotted black;
  position: relative;
}
dfn[data-title]:hover:after {
  content: attr(data-title);
  padding: 4px 8px;
  color: #333;
  position: absolute;
  left: 0;
  top: 100%;
  z-index: 20;
  white-space: nowrap;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -moz-box-shadow: 0px 0px 4px #222;
  -webkit-box-shadow: 0px 0px 4px #222;
  box-shadow: 0px 0px 4px #222;
  background-image: -moz-linear-gradient(top, #eeeeee, #cccccc);
  background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #eeeeee), color-stop(1, #cccccc));
  background-image: -webkit-linear-gradient(top, #eeeeee, #cccccc);
  background-image: -moz-linear-gradient(top, #eeeeee, #cccccc);
  background-image: -ms-linear-gradient(top, #eeeeee, #cccccc);
  background-image: -o-linear-gradient(top, #eeeeee, #cccccc);
}
<p>Bob's <dfn data-title="Dog">canine</dfn> mother and <dfn data-title="Horse">equine</dfn> father sat him down and carefully explained that he was an <dfn data-title="A mutation that combines two or more sets of chromosomes from different species">allopolyploid</dfn> organism.</p>

Answer №3

Everything is already set up the way you desire.

<span title="When I hover over this text, something special happens">This particular text is meant to trigger a special effect on mouseover</span>

When you hover over the text, the title will be displayed.

If you prefer, you have the option to utilize https://jqueryui.com/tooltip/. Don't forget to add jQueryUI to your project.

<script type="text/javascript>
   $(document).ready(function(){
   $(this).tooltip();
});
</script>

Another option is to incorporate bootstrap by visiting http://getbootstrap.com/javascript/#tooltips.

<button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="left" title="Hey, There's Some Important Info Here">Greetings...</button>

Answer №4

Adjusting the title to a data-attribute and implementing a pseudo-element appears to be the most practical solution

div {
  width: 50%;
  margin: 25px auto;
  bordeR: 1px solid grey;
  padding: 1em;
}
dfn {
  position: relative;
}
dfn:hover:after {
  display: block;
}
dfn:after {
  display: none;
  content: attr(data-title);
  position: absolute;
  left: 0;
  top: 0;
  margin-top: -2em;
  white-space: nowrap;
  background: lightgreen;
  padding: .25em;
  border: 1px solid green;
}
<div>
  <p>Bob's <dfn data-title="Dog">canine</dfn> mother and <dfn data-title="Horse">equine</dfn> father sat him down and carefully explained that he was an <dfn data-title="A mutation that combines two or more sets of chromosomes from different species">allopolyploid</dfn> organism.</p>
</div>

This change also means that the `title' will no longer be displayed, replaced by the new tooltip type object.

It's important to note, however, that this approach may have limitations, such as long title text causing overflow issues.

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 the best way to merge two AngularJS form validation directives criteria using CSS?

When both $dirty and $invalid are true, I would like the input fields to have a red background. However, I am only able to do this one by one, which is not ideal. input.ng-dirty && input.ng-invalid { background-color: red; } ...

Utilize Vue.js to sort by price bracket and category

Hello, I am currently new to working with Vue.js and I am attempting to create two filters - one for price range and the other for genre. Any guidance or assistance would be greatly appreciated. Below is a snippet of my code: ul class="filter-wrapper"&g ...

What are the benefits of utilizing the useStyles MaterialUI function instead of traditional CSS?

As I develop my MERN application, I am utilizing Material UI components along with inline attributes. However, I find myself questioning the necessity of creating a JavaScript file just to import the makeStyles function, which essentially outputs an obje ...

Responsive design is achieved through the use of mobile media

I am seeking assistance with optimizing my website for mobile devices as it currently displays like the image below. Here are the solutions I have considered so far: Duplicating the 680 lines of CSS within the same document between @media only screen ta ...

What is the best way to position an image alongside an h2 heading?

Perhaps the technology stack is not relevant in this case, but I am currently working on a nextjs project with tailwind for styling. I am attempting to place an image next to an h2 tag, but encountering unexpected behavior from the image. It seems as thoug ...

Show the selected checkbox options upon clicking the submit button

Having trouble setting up a filter? I need to create a feature where checked values from checkboxes are displayed in a specific div after submitting. The display should include a 'Clear All' button and individual 'X' buttons to remove e ...

Modifying the attributes/properties within a class of a bootstrap button

Here is the code that I have: <b-button id="search-button" size="md" class="mt-1 w-100" type="submit" @click="someEvent()" >Example </b-button If we imagine calling someEvent() and wanting to modi ...

Ensuring proper input with JavaScript form validation

Currently, I am working on implementing a basic form validation feature, but it is not functioning as intended. The desired behavior is for the field border to change color to green or red based on its validity, while displaying text indicating whether t ...

Gmail not displaying image - troubleshoot with Django Python-Postmark

Having trouble displaying static images in Gmail when using django python-postmark for mailing. Gmail is appending a url proxy to the src of the images, which causes them not to display properly. What might I be missing here? How can I solve this issue? Th ...

After the initial iteration, the .length function ceases to function properly when

A validation method is set up to check the length of a user input field. It functions properly on the initial try, but does not update if I go back and modify the field. $("#user").blur(function () { if ($("#user").val().length < 3) { $("#userval ...

Is it possible to apply CSS to only the first element?

I have designed a pricing form like the one shown below: To view a full page version of my code for easier reading, please click on the link provided. @import url('http://fonts.googleapis.com/css?family=Indie+Flower'); @import url('http: ...

What is the best way to send an HTML form variable to a Perl script using AJAX?

My goal is to pass the HTML variable from the list menu to the Perl script using POST. However, when I try to do this, the jQuery ajax() function executes the Perl script without including the value obtained from document.getElementById. Below is the sni ...

Resolve a container overflow problem within the footer section

Trying to adjust the footer (newsletter signup form) on this page to fall to the bottom of the page has been a challenge. The #container element appears to be larger than the body, causing layout issues. Any suggestions on how to resolve this? An image d ...

Error message: IndexError: list index out of range while attempting to trigger a click event using selenium

There are a total of 41 category checkboxes on the page, with only 12 initially visible while the rest are hidden. To reveal the hidden checkboxes, one must click on "show more." This simple code accomplishes that: [step 1] Loop through all checkboxes > ...

What is the best way to determine the variable height of a div in Angular 7?

I'm currently trying to use console.log in order to determine the height of a div based on the size of a table inside it. This information is crucial for me to be able to ascertain whether or not a scrollbar will be present, especially within a dialog ...

Tips for verifying the compatibility of elements as parent or child components

Is it possible in JavaScript to verify if an HTML element can be a child of another element? For instance: Can an unordered list (<ul>) contain a list item (<li>) as a valid child element? - Yes Can an unordered list (<ul>) contain ano ...

Removing lines of text from the output of a JavaScript retrieval

Currently, I have a working script that retrieves a complete address block to be inserted into a selected field. However, I am now attempting to remove unnecessary parts of the generated text block for better display. This is the code snippet: <script ...

AngularJs is being used to extract data from Firebase with the help of $value, $id, and

I have been working on retrieving data from Firebase in my HTML using AngularJS. Everything is functioning well, however, when I access the child node, the data is displayed in an unexpected format. Please refer to the images below for more details: This ...

Hover to stop menu movement

Can someone help me achieve a similar menu hover effect like this one? I'm trying to create a menu with a hold/pause effect on hover, specifically in the section titled "A programme for every vision". The goal is to navigate through the sub menus smo ...

Issue encountered while attempting to include a background image in React JS

I encountered an issue when attempting to include a background image in react js. ./src/index.css (./node_modules/css-loader/dist/cjs.js??ref--5-oneOf-4-1!./node_modules/postcss-loader/src??postcss!./src/index.css) Unable to locate module: An attem ...