Using CSS3 to shift a div in relation to another moving div

Take a look at this JSfiddle. I am trying to make the "Push this!" div move when the menu resizes/expands on hover. Is it possible to achieve this effect using only CSS?

<div id="nav">
<ul>
    <li><a href=""><span>01</span> HomePage</a>
    </li>
    <li><a href=""><span>02</span> SubPage 1</a>
    </li>
    <li><a href=""><span>03</span> SubPage 2</a>
    </li>
    <li><a href=""><span>04</span> SubPage 3</a>
    </li>
    <li><a href=""><span>05</span> SubPage 4</a>
    </li>
</ul>

<div id="pad">push this!</div>

Answer №1

To achieve the desired effect, adjust the style of #nav from position: absolute to float: left. This will ensure that #nav remains within the document flow and can impact the positioning of other elements.

Once this change is made, remove the padding-left property from #pad:

Fiddle 1


You don't need jQuery to achieve the same outcome. Simply add the following CSS:

#nav {
  width:35px;
  transition: width 0.5s;
}

#nav:hover {
  width: 200px;
}

Fiddle 2

Answer №2

If you're eager to utilize jQuery as shown in the jsFiddle example, then try this out:

$(function () {
$('#menu').hover(function () {
    $(this).animate({
        width: '180px'
    }, 400);
    $('#content').animate({'padding-left':'180px'},400);
}, function () {
    $(this).animate({
        width: '30px'
    }, 400);
    $('#content').animate({'padding-left':'30px'},400);
}).trigger('mouseleave');

});

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

Retrieve the data contained within a displayed embed tag that includes a src attribute

Can anyone provide guidance on how to retrieve the content of an embed tag using src attribute? I have an embed tag pointing to a custom page as src. I tried to use jquery to access the rendered content but I am not getting anything back. Any suggestions ...

Banner with video background (not taking up the entire page)

I'm currently working on a website project that involves using a YouTube video as the banner, but I'm facing some challenges in making it work correctly. So far, this is what I have: #video-background { position: absolute; right: 0; top:1 ...

What is the best way to link data from SQL Server to an HTML webpage?

Seeking assistance on how to display data from a SQL Server database in an HTML page using JavaScript, AJAX, and jQuery without the need for C#, PHP, VB.NET etc. Can someone please provide some reference links or share example code? ...

How can you transform attribute and value pairs from a DOM element into a JavaScript object?

Is there a quick method to transform a DOM element along with its attributes and values into a JavaScript object? For example, converting this snippet from the HTML page: <div id="snack" type="apple" color="red" size="large" quantity=3></div> ...

Background color vanishes (occasionally) on the contact section of my website when viewed in Chrome

My contact page has an issue that only occurs in Chrome, where the background disappears most of the time. This problem is not present in Firefox, Edge, or Safari on my TA's MacBook, but interestingly, hovering over a box resolves the issue on her dev ...

What are the steps to increase the size of the search bar?

I have been working on this code, but I am struggling to align the search bar properly within the navigation bar. Can someone please guide me on how to achieve this? I apologize for my lack of coding expertise, as I am new to Information Technology and s ...

Stopping a Bootstrap Modal Closure When Validation Errors are Present

I'm dealing with a bootstrap model within a .NET MVC5 app. Although my client-side validation is functioning properly (using jquery unobtrusive in MVC), I have encountered an issue where the modal keeps closing even when there are errors present. How ...

clearInterval function is not functioning

Could this be a simple syntax error causing my frustration? The resizeTime variable seems to persist despite multiple attempts to clear it using clearInterval. Any thoughts on what may be going wrong here? Below is the code snippet: var resizeTime; // e ...

Measuring Metrics for Twitter Shares, Facebook Reactions, and Google +1's using Jquery and AJAX

My goal is to retrieve the counts for Twitter, Facebook, and Google using jQuery.getJSON requests, inspired by the helpful guide created by John Dyer on achieving the same with C# and PHP. Currently, Twitter and Facebook counts are working fine, but I am ...

Spooky results displayed on website from NightmareJS

Is there a way to display the output from nightmareJS onto a webpage when a button is clicked using HTML, CSS, and JS? This is my nightmareJS code: var Nightmare = require('nightmare'); var nightmare = Nightmare({ show: false}) nightmare .go ...

The login form for the MVC website will only auto-submit on IE

I have developed a cutting-edge MVC website that allows users to securely access specific information. However, I am encountering an issue with Internet Explorer - the browser seems to automatically submit the login form when users navigate to the login pa ...

Eliminate the display of image thumbnails located at the bottom of images

When I hover over the image, thumbnails are being displayed at the bottom of the image which I do not want. How can I remove that feature from prettyphoto? jQuery.prettyPhoto.js I have shared my .js file, click here ...

Received an unexpected GET request while attempting to modify an HTML attribute

After clicking a button in my HTML file, a function is called from a separate file. Here is the code for that function: function getRandomVideoLink(){ //AJAX request to /random-video console.log("ajax request"); var xhttp = new XMLHttpRequest( ...

Safari's problem with CSS transitions

This issue is specific to Safari, as it has been tested and works fine in Chrome, Opera, and Firefox. Upon hovering over a certain div (1), a child div (2) slides out by adjusting the left property. However, the child elements (buttons) within the second ...

Floating footers with centered content - they remain aligned in the center even when zoomed out

Behold my unique and straightforward footer design (100% zoom above, 70% zoom below) : To keep things simple, I aimed to center content with a single div having a fixed width and margin: 0 auto, all while maintaining the same color for the outer footer la ...

Customizing the COREui Admin Template to replicate the demo's design

Currently, I am delving into the world of web development and experimenting with integrating the COREui 4.0.1 Bootstrap admin template within an existing Symfony 5.3 project instead of utilizing standard Bootstrap 5 components and utilities. My goal is to ...

The side menu in Bootstrap dropdown experiences a one-time functionality

When navigating through a responsive top menu with Bootstrap, everything works seamlessly - from toggling the menu to dropdown functionality. However, I encountered an issue with the side menu as nav-pills used to select tab-panes. <div class="containe ...

Exploring CSS Shapes: Unveiling the secrets behind the star. What's the mechanism at

https://i.stack.imgur.com/0BgQr.png Take a look at the code snippet below showcasing The Shapes of CSS. I'm interested in delving into the intricacies of these CSS properties. How exactly do shapes in CSS function? This includes pseudo CSS, borders, ...

Need help with setting or resetting dialog radio buttons group?

My goal is to create a script where I can customize parameters before calling a dialog box. The dialog box consists of only one button, OK, and a group of radio buttons. I want to be able to change the title of the dialog box before calling it and also pro ...

After the page reload, the text box remains filled and does not reset to null

Even after refreshing the page, the text box does not reset to null. It retains the value that was entered previously. This issue occurs while implementing pagination. <form method='post' class="search" action="<?= base_url() ?>my-subsc ...