What are the steps for transitioning with JavaScript?

My goal is to make both the <hr> elements transition, but I'm struggling with only being able to select the lower <hr> using CSS.

html {
  margin-bottom: 0;
  height: 100%;
  min-height: 100%;
}

body {
  margin-top: 0;
  height: 100%;
  min-height: 100%;
  background-color: red;
}

hr {
  transition: width 1s;
  width: 5rem;
}

#container {
  width: 50%;
  height: 100%;
  padding: 5rem;
  background-color: white;
  margin: auto;
}

#expand {
  width: 100%;
  height: 100px;
  background: red;
  transition: height 1s;
  text-align: center;
}

#expand:hover {
  height: 200px;
}

#expand:hover~hr {
  width: 20rem;
}
<div id="container">
  <hr>
  <div id="expand">
    Learn More
  </div>
  <hr>
</div>

Is there a way to achieve the desired transition for both the <hr> elements using plain JavaScript? If not, which JavaScript library would be suggested?

Answer №1

Consider using pseudo elements instead of the <hr> tag

html {
  margin: 0;
  height: 100%;
  min-height: 100%;
}

body {
  margin: 0;
  height: 100%;
  min-height: 100%;
  background-color: red;
}

#container {
  width: 50%;
  height: 100%;
  padding: 5rem;
  background-color: white;
  margin: auto;
}

#expand {
  width: 100%;
  height: 100px;
  background: red;
  transition: height 1s;
  text-align: center;
  position: relative;
}

#expand:before,
#expand:after {
  content: '';
  width: 5rem;
  height: 1px;
  background: #000;
  transition: width 1s;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

#expand:before {
  top: -10px;
}

#expand:after {
  bottom: -10px;
}

#expand:hover {
  height: 200px;
}

#expand:hover:before,
#expand:hover:after{
  width: 20rem;
}
<div id="container">
  <div id="expand">
    Learn More
  </div>
</div>

Answer №2

To achieve this effect with an extra container element, you can follow these steps:

body {
  margin: 0;
}

.container {
  width: 50%;
  height: 100%;
  padding: 3rem;
  background-color: lightgray;
  margin: auto;
}

.expander {
  width: 100%;
  height: 50px;
  background: blue;
  color: white;
  text-align: center;
  line-height: 50px;
  cursor: pointer;
  transition: height 1s;
}

.expander:hover {
  height: 100px;
}
<div class="container">
  <div class="expander">
    Click to Expand
  </div>
</div>

Answer №3

When using #expand:hover~hr, you are targeting any following siblings of #expand:hover. Unfortunately, CSS does not have a way to select previous siblings. To fix this, move both hr elements under a shared parent and use the hover hr selector in this case.

Take a look at the codepen example: https://codepen.io/pranaynailwal/pen/wvgwvea

Instead of:

#expand:hover~hr {
  width: 20rem;
}

Use:

#container:hover hr {
  width: 20rem;
}

Answer №4

Enhance your website with JavaScript:

<script>
  const hrElements = document.querySelectorAll("hr");

  function addClass() {
    hrElements.forEach(el => el.setAttribute("class","bigger"))
  }

  function removeClass() {
    hrElements.forEach(el => el.setAttribute("class",""))
  }
<script>

UPDATE to provide clear CSS instructions

html {
  margin: 0;
  height: 100%;
  min-height: 100%;
}

body {
  margin: 0;
  height: 100%;
  min-height: 100%;
  background-color: red;
}

hr {
  transition: width 1s;
  width: 5rem;
}

#container {
  width: 50%;
  height: 100%;
  padding: 5rem;
  background-color: white;
  margin: auto;
}

#expand {
  width: 100%;
  height: 100px;
  background: red;
  transition: height 1s;
  text-align: center;
}

#expand:hover {
  height: 200px;
}

.bigger {
  width: 20rem;
}

Add the following code snippet to the #expand div for mouseover and mouseout effects:

<div id="container">
  <hr />
  <div id="expand" onmouseover="addClass()" onmouseout="removeClass()">
    Learn More
  </div>
  <hr />
</div>

Answer №5

It is not possible to apply styles to previous siblings in CSS.

One workaround could be to modify the code from:

#expand:hover~hr {
    width: 20rem;
}

to:

#container:hover hr {
    width: 20rem;
}

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 steps should be taken to ensure that Google effectively crawls through an AngularJS application?

According to a source at Allotment Digital, it is unnecessary to provide different or pre-rendered content to Google when using html5mode and removing hashtags from URLs. While some websites state that ajax crawling documents are deprecated, others such a ...

What is the best approach for inserting multiple files into MongoDB with just one JavaScript, Node JS, Shell Script, or mongofile CLI script?

Looking for a way to transfer HTML files from a directory to MongoDB using pure JavaScript, NodeJs, shell script, or mongofile CLI. Any assistance would be greatly appreciated. Thank you in advance. ...

I require the ability to retrieve only the data from a UI grid column, specifically based on the column name selected

I need help with my angularjs application that utilizes Ui grid. There is an external dropdown menu located outside of the ui grid, which lists all the column names in the grid. I want to be able to select a specific column name from this dropdown and retr ...

Discovering the wonders of React and Javascript through the powerful Map function

I've exhausted all my knowledge of map functions and syntax, but I keep encountering the error TypeError: Cannot read property 'action' of undefined. This issue seems to stem from this line: constructor(data=[]). Typically, I am only familia ...

Passing JSON data with special characters like the @ symbol to props in React can be achieved

Using axios in React, I am fetching JSON data from a database. I have successfully retrieved the JSON data and stored it in state to pass as props to child components within my application. However, the issue arises when some of the objects in the JSON s ...

Utilizing helper functions in Node based on their specific types

In my helper module, I have different files like content, user, etc. These files define various helpers that can be used in the router. Below is the code for the router: router.js var helper = require("./helper"); function index(response) { response ...

Tips for validating date input in a TextBox using JQuery on an ASP.NET platform:

Here is some code I wrote: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="datetime.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <tit ...

Sending the value from a for loop through AJAX and populating it into a form field

Currently, I have implemented a piece of JavaScript code that captures user input, sends a request to an endpoint using AJAX, and appends a specific field from the returned results as an option within a datalist. This functionality is working perfectly fin ...

The PHP file on the server is missing the mandatory "event" parameter for the EventSource

Explaining this issue was a bit of a challenge. I've set up a Javascript EventSource object with some customized event handlers like so: var source = new EventSource('updates.php'); source.addEventListener('add', addHandler, fals ...

What is the best way to stop an embedded mp4 video from playing when a dynamically generated button is clicked?

After making modifications to the QuickQuiz code found at https://github.com/UrbanInstitute/quick-quiz, I have replaced the img with an embedded mp4 video that loads from a JSON file. Additionally, I switched from using the original SweetAlert library to S ...

Using Conditional Tags for CSS Display

I'm working on implementing Conditional Tags to toggle a CSS class on or off based on the current displayed page. However, I'm running into an issue where the code isn't executing properly. There might be a syntax or logic error causing the ...

Having trouble determining the total amount in my online shopping cart

I am facing an issue with the shopping cart I created, which consists of two components (Productlist and Cart List). When I click on the 'add to cart' button in the Product List, it successfully moves into the service file and then to the Cart Li ...

Is it possible to obtain the index of a table row using querySelector?

Is it possible to find the rowIndex of the first occurrence of a table row within the #myTable using JavaScript? http://jsfiddle.net/bobbyrne01/fmj6np6m/1/ html .. <table id="otherTable"></table> <table id="myTable"></table> js ...

What is the outcome of using VueUse.useMouse() if it is not made reactive?

Whenever I use VueUse.useMouse(), the results in console.log() change as I move the mouse. However, the results within <span></span> always remain 0. This is quite confusing to me, can someone please explain why this is happening? ...

JavaScript: Adjust DIV Width Based on Window Height

Is there a way to make the width of a div equal to the height of the window in jquery? I attempted this method: <script type="text/javascript"> $(".rt-container").style.width = $(window).height(); </script> Unfortunately, it did not work. I ...

A guide on initiating a get request with URL in React

Utilizing React for the frontend and express for the backend has been my approach. By defining get methods in my express like this:- app.get('/addBill',(req,res)=>{ // --first method res.render("addBill"); }); app.get(&a ...

Why is the inline-block element in the list displaying on two lines? The text contains a number, but does it affect the layout?

What could be the reason for 'Maroon 5' moving down to a second line? Despite adding extra characters and testing with an integer, the issue persists. <ul id="soundsLike"> <li>Foo Fighters</li> <li>Maroon 5</ ...

Even after assigning the class "img-fluid" to my image, it still fails to properly adjust to the screen size

I added the class "img-fluid" to my image in Bootstrap, but it's not fitting on my webpage. What should I do? <img src="images\406201.jpg" class="img-fluid" alt="..."> <div style="margin-top: 0p ...

What causes the exception in JavaScript to be an empty object?

try { let temporary = null; temporary.split(','); } catch (error) { Logger().info('caught error: ', error, error.constructor); } output: caught error: {} undefined I attempted to use JSON.stringify and encountered the sa ...

Creating a string specifically for implementing regular expressions in JavaScript

I'm currently working on a custom jQuery plugin known as jQuery-Faker. This plugin is designed to produce fake data for populating form fields based on their respective field names. One of the tasks I have set out to accomplish is generating phone num ...