Tips for making CSS Hover Transform functional when using relative positioning

JSFiddle

I am trying to create a heading text with an arrow that translates when hovered over. Currently, it is achieved using position: absolute, but I need to make it work with position: relative.

Any suggestions on how this can be accomplished?

#container {
  width: 130px;
  border: 1px solid red;
  padding: 10px;
}

#container h2:after {
  transition: all 1s ease
}

#container:hover h2:after {
  transform: translateX(30px);
  transition: all 1s ease;
}

#container h2:after {
  content: "->";
  position: absolute;
  /* It currently works with absolute positioning, but I want it to work with relative positioning. */
}
<div id="container">
  <h2>Go to my website</h2>
</div>

Answer №1

Adjust the CSS property #container h2::after to have a value of display: inline-block

#container h2::after {
  transition: all 1s ease;
  display: inline-block;
}

Side note: While using h2:after is not incorrect, pseudo-elements created with before and after should be written with two colons. Pseudo-classes like :hover only use one colon.

Answer №2

Merged two h2:after styles to streamline the code, switched from transform to margin-left in CSS for smoother transition effects, and updated position to relative on the h2 element. Feel free to ask if you have any inquiries.

#container {
  width: 130px;
  border: 1px solid red;
  padding: 10px;
}

#container h2:after {
  transition: all 1s ease;
  content: "->";
  position: relative;
}

#container:hover h2:after {
  margin-left: 30px;
  transition: all 1s ease;
}
<div id="container">
  <h2>Go to my website</h2>
</div>

Answer №3

Simply include display:inline-block in the pseudo element style

#box {
  width: 150px;
  border: 2px solid blue;
  padding: 15px;
}

#box p:before {
  transition: all 1s ease
}

#box p:before {
  content: "<-";
  position: relative;
  display: inline-block;
  /* It currently uses absolute positioning, but I prefer using relative positioning. */
}

#box:hover p:before {
  transform: translateX(-30px);
  transition: all 1s ease;
}
<div id="box">
  <p>Click here</p>
</div>

Answer №4

Instead of using transform, try utilizing margin-left

Visit this link for more details

#container h2:after {
  transition: all 1s ease;
  margin-left:5px;
}

#container:hover h2:after {
  margin-left:25px;
  transition: all 1s ease;
}

#container h2:after {
  content: "->";
  position: relative;

}

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 responsively center an after pseudo-element above its parent?

Looking to create a dynamic tooltip without any fixed widths or constraints on the parent element. The process seems simple enough, but I'm facing an issue with centering the after element due to an existing transform attribute of transform: translat ...

What are the HTML5 elements that include the "load event" within their onload attribute?

Mozilla's MDN provides information regarding the load Event with this brief explanation: The load event is triggered when a resource and its dependent resources have completed loading. and references standard 1, which states Trusted Targets: ...

What is the most effective method for preserving RichText (WYSIWYG output)?

I am currently using a JavaScript-based rich text editor in my application. Could you suggest the most secure method to store the generated tags? My database is MySQL, and I have concerns about the safety of using mysql_real_escape_string($text);. ...

Switching Div Elements Created by PHP

Utilizing MySQL, I am fetching data to dynamically generate nested div tags in a hierarchical structure. This structure consists of div tags within div tags within div tags, all uniquely identified through PHP-generated ids: <div class="holder"> ...

Tips for resizing a larger image to display only a specific portion in CSS (and incorporating JS if needed)

I need to resize an image that measures 1024x1024 and is segmented into 4 quadrants: My goal is to reduce the size of this image so that quadrant 2 is 256x256 while masking out or hiding the remaining 3 quadrants, displaying only the desired section on th ...

Steps for stopping the sass --watch function on a specific folder

Successfully got my SASS up and running with the help of fantastic documentation available everywhere. The guide I followed was http://sass-lang.com/guide. sass --watch myfolder/css Running a specific command worked like a charm, enabling every change in ...

What are the steps to adjusting page margins for Kindle devices?

While converting an HTML page for Kindle, I encountered a problem with multiple CSS files. The Kindle Guide recommends setting the left and right margins to 0 for normal body text in order to prevent content from falling off the edge of the screen or overl ...

Utilizing background images in conjunction with media queries

I have a div <div id="page"> </div> Containing the following css code: #page { background: url('images/white-zigzag.png') repeat-x; } @media (max-width: 600px) { #page { background: url('images/white-zi ...

Button not triggering .hide() function as expected

Today, I've been attempting to implement a feature that involves making an image disappear when a button is clicked. Despite its simplicity, it doesn't seem to be functioning as expected. :/ Below is the HTML code snippet: <img id="project ...

One of the paragraphs refuses to align with the others by floating left

Greetings everyone! This is my first time posting here, although I have been a regular visitor looking for answers on this site. Hopefully, someone can assist me with my current issue. Let me explain my problem: I've been attempting to align all the ...

submit the JSON formatted data to the server

I am attempting to extract data from a form and transmit it to a remote server: Below is the code snippet: <html> <head> <script src="http://code.jquery.com/jquery-latest.min.js"></script> </head> ...

Using data fetched from PHP in HTML via jQuery post: A step-by-step guide

I have created a function in jQuery that uses $.post to send data to a PHP file. The query in the PHP file is working fine and returning the data back successfully. JavaScript code: function send_agenda_data(cidade_data){ var data = {'cidade_dat ...

Innovative Features in Google Chrome

There is a nifty effect on Google Chrome that makes tabs light up when new content appears on the page and you are not currently viewing that tab. It can often be seen on sites like grooveshark. If anyone knows how to recreate this effect, I would greatl ...

Conceal a different div unless it includes

Hi everyone, I need help with a filter code snippet: $(".title").not(":contains('" + $("[name=filter]").val() + "')").hide() The issue I'm facing is that the .title class is nested within the .sortAll class along with many other divs. I w ...

Is there a way to customize the dot in a JavaFx RadioButton?

Hello fellow coding enthusiasts, I am looking to find out how I can customize the color of a RadioButton dot. I want to have 3 RadioButtons, each with a different color to represent a state. Do I need to use CSS for this task? If so, could someone please ...

Preventing Adjacent Text from Moving Due to a Floated i Element

I have a div with a bootstrap icon positioned at the top right corner. However, I want to center the text horizontally without shifting the position of the icon. Is there a way to achieve this? See my sample code here: https://jsfiddle.net/x1Lvyu6t/3/ ...

Is there a way to prevent inheriting width properties? Or perhaps how can one revert back to the original width value

After installing the Thank you plugin, I noticed that the button on my test site appears to be strange. Upon further investigation, I discovered that it is inheriting #commentform textarea { width: 45%; } from my theme. While removing the width in the CSS ...

Is it possible to selectively disable the <span> tag in Angular?

I have encountered a situation where I need to dynamically disable a button and show a tooltip based on specific conditions in an Angular application. Here is the current code that I am using: <span *ngIf="(hourEnabled && theTest && ...

Issue with Bootstrap 5 collapse not toggling back on second button press

I am encountering an issue with a button that is supposed to show a div on click using Bootstrap 5's "Collapse" class. The problem is that the content shows up correctly on the first click, but it doesn't hide again when the user clicks the butto ...

The issue arises when HTML tables with unique IDs are all displaying the same value from an AJAX callback

This situation has been incredibly frustrating for me!! Let me explain what's going on - I have a callback data coming from my AJAX post method that needs to be passed to seven different HTML tables. Although I successfully received the data from the ...