Adjust for the region shifted using transform: translate

I'm currently working on adjusting the positioning of elements using transform: translateY in CSS. The challenge I am facing is eliminating the empty space that appears between elements after repositioning one element below another without altering the code for the surrounding elements.

My initial approach was to set a negative margin-bottom value based on the percentage of the element's size, but this calculation is based on the width rather than the height. Another consideration was to utilize absolute positioning, yet this strategy proved challenging due to unknown content sizes within the elements.

Do you have any suggestions on how to achieve seamless alignment between elements solely through CSS?

div {
  padding: 50px;
}

.wrapper {
  background-color: green;
  position: absolute;
  width: 500px;
  height: 500px;
}

.above {
  background-color: red;
}

.moved {
  background-color: blue;
  transform: translateY(-50%);
  
}

.below {
  background-color: yellow;
}
<div class="wrapper">
  <div class="above">
    
  </div>
  <div class="moved">
    
  </div>
  <div class="below">
    
  </div>
</div>

EDIT: To clarify further: The individual divs represent distinct sections within my template. As these components are designed for reuse, modifications to other sections (both above and below) are not an option. Therefore, I am seeking a solution where the repositioned section only occupies the necessary space post-translation.

Answer №1

With the height of each div set at 100px and a translation of 50% for the blue one equals 50px, you simply need to include a margin-bottom: -50px; to complete the setup.

Answer №2

If you want to eliminate the space between your yellow and blue elements, consider applying a translateY(-50%) transformation on the yellow element to move it upwards.

.below {
  background-color: yellow;
  transform: translateY(-50%);
}

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

Failure of Styling Inheritance in Angular 2 Child Components from Parent Components

My Parent Component utilizes a Child Component. I have defined the necessary styles in the Parent CSS file, and these styles change appropriately when hovering over the div. However, the Child Component does not inherit the styling classes of the Parent Co ...

Submitting user inputs from an HTML form into a MySQL database using PHP

I've successfully put together an HTML form that is meant to capture user input data and insert it directly into a table within mySQL. newuser.php file <?php //including the connection page include('./DB_Connect.php'); //create a n ...

Expanding the header in Ionic 3 with a simple click event

I have successfully implemented an Expandable Header in Ionic 3 following a tutorial from Joshmorony. The header expands perfectly on scroll, as you can see in this GIF: However, I am facing an issue where I want to expand the header on click instead of o ...

Looking to duplicate the elements with the click of a button?

I am in need of assistance with my registration form. My goal is to move the elements contained within the <fieldset> tags to the end of a row when the user clicks the + button. The result you see initially has been recreated. Thank you for your s ...

Having trouble with integrating user input from HTML into a JavaScript file to execute a GET request

I am currently working on a project to create a website that integrates the google books API for users to search for books. To start, I have set up a server using express in index.js at the root of the project directory, and all my static files are stored ...

When attempting to render HTML containing multiple CSS classes within a JSON request parameter, the styles are not being applied as expected

In my API, I'm dealing with a JSON request parameter that includes an HTML string. This HTML string references CSS styles from a separate .css file. However, when an HTML element has two CSS classes assigned to it, neither of the classes' styles ...

Is it viable to create an onClick event for the text content within a text area?

I'm working on a project that involves displaying XML data in a textarea and creating an onClick event for the content within the textarea. For example: <textarea>Hello Web, This is a simple HTML page.</textarea> My goal here is to create an ...

Developing a perpetually scrolling container within a webpage

I am attempting to implement a scrollable div on my webpage that can continuously load content. I am currently using the following code snippet for this --> http://jsfiddle.net/cyrus2013/Qq85d/ $(document).ready(function(){ function loadMoreContent() { ...

Instructions on integrating a column of buttons into a Bootstrap table containing information retrieved from a MySQL database

My bootstrap table is currently displaying data that is loaded from a MySQL database. I am looking to enhance it by adding a column with buttons, similar to the layout shown in this image. https://i.stack.imgur.com/8fWfR.png However, I am facing some dif ...

Tips for altering the color of an activated Bootstrap dropdown toggle

When the Dropdown menu is open, I would like to customize its default colors. Specifically, I want to change the border color and background using CSS. Below is the HTML code snippet: <div class="row menu"> <ul class="nav nav-pills pull-right"& ...

Why is my JavaScript code functioning properly on jsfiddle but failing to work when run locally?

After recently creating JavaScript code that allows for form submission using the <form> tag, I encountered an issue when trying to implement it within an HTML page. Here is a snippet of the code: <script type="text/javascript"> var m ...

Conceal the scrollbar and enable horizontal swiping using CSS

I have set up a container where I want the content to scroll horizontally from left to right on mobile devices, and I would like to be able to swipe to navigate while hiding the scrollbar. You can view the implementation on this JSfiddle link Do you think ...

What causes the difference in output between passing p=3+3 using GET, which results in 3 3, and using POST, which results in

It's really quite puzzling - I've noticed that sending the same parameter through various methods yields different results. ...

Rampant number of objects contained within box element

I am currently working on a box component that contains flex items. Unfortunately, I am facing an issue where the box width remains constant and causes overflow when I try to reduce its size in order to accommodate all the items. Can anyone provide guidanc ...

Different custom background images for every individual page in Nuxt

Looking for a unique background-image for each page in my app is proving to be challenging. Currently, I have defined the background-image in style/app.scss. @import 'variables'; @import 'page_transition'; @import url('https://f ...

Implementing a dynamic update of an HTML element's content with JSON data - Learn how!

My task involves creating a quiz application where I need to show the answers along with images of the choices stored in my JSON data. However, I encounter an error: Uncaught TypeError: Cannot set properties of null (setting 'src') when I attempt ...

Beginning the phone application using either HTML5 or Cordova

Is there a way to use either HTML5 or Cordova (PhoneGap) to initiate a call to a phone number? I am looking to create a button that says "Call 555-555-5555", and when clicked, it opens the phone app on the device with that specific number. ...

Creating AngularJS variables with dependencies on integer values

Hello, I am a brand new developer and I am currently working on creating an expenses calculator. The goal is to have the sum of inputted integers from a table, each with a default value, become the integer value of another variable. However, I seem to be m ...

Modifying the background image of div elements with JQuery in a loop function is ineffective when using Google Chrome

I am facing an issue in my application where I have a for loop to change the background image of two divs. The code snippet is as follows: for (var i = 0; i < length; i++) { $("div_" + (i + 1)).css("background-image", "../imageFile.png"); ...