Proceed with the document's flow following the transformation: shift vertically by 50%

Is there a way to eliminate the gap after applying a CSS transform: translateY(-50%) so that the content flows seamlessly?

I have experimented with various methods but haven't been successful in moving the element up by 50% of its own height. Using negative margin as a percentage doesn't work effectively, and setting a negative margin on the following element isn't feasible as it should be based on the header's height.

HTML:

<div class="header">
  <div class="featured-image">
    <!-- Placeholder for featured image -->
  </div>

  <div class="title-container">
    <h1>Title<br />goes<br />here</h1>
  </div>
</div>

<div class="article-body">
  <p>There is excessive space above the "article-body" class. I want the content to flow naturally after the "title-container", but using translateY only affects visual presentation. An alternative method is required to move the yellow block up by 50% of its own height.</p>
</div>

CSS:

.header {
  width: 100%;
  position: relative;
}

.featured-image {
  width: 100%;
  height: 200px;
  background-color: blue;
}

.title-container {
  width: 50%;
  margin: 0 auto;
  transform: translateY(-50%);
  background-color: yellow;
}

JS Fiddle:

https://jsfiddle.net/robertirish/tyh18orq/16/

Perhaps achieving this requires Javascript, but it would be preferable to accomplish it using pure CSS without relying on JS or media queries.

Thank you for your assistance.

Answer №1

For a different approach, consider replacing transform: translateY(-50%) with margin-top: -25vh.
By doing this, the .title-container will remain in its original position while ensuring the .article-body sits nicely below it:

.header {
  width: 100%.
  position: relative;
}

.featured-image {
  width: 100%;
  height: 200px;
  background-color: blue;
}

.title-container {
  width: 50%;
  margin: 0 auto;
  /*transform: translateY(-50%);*/
  margin-top: -25vh;
  background-color: yellow;
}
<div class="header">
  <div class="featured-image">
    <!-- this is in place of the featured image -->
  </div>
  
  <div class="title-container">
    <h1>Title<br />goes<br />here</h1>
  </div>
</div>

<div class="article-body">
  <p>There is too much space above class="article-body". I want the content to flow on naturally after class="title-container", however using translateY is purely visual so an alternate method of moving the yellow block up by 50% of its own height is necessary.</p>
</div>

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

How to Center a DIV both Vertically and Horizontally with CSS Positioning?

I'm currently working on a webpage layout where I want to have a main div centered horizontally with three smaller divs inside, aligned both vertically and horizontally with equal spacing. I tried using margin: auto on an empty div to center it in the ...

What is the best way to adjust the background color of Material UI's theme based on different screen sizes?

Having trouble replacing hard-coded CSS breakpoints with Material UI theme settings. @media screen and (min-width: 0px) and (max-width: 400px) { body { background-color: red; } } @media screen and (min-width: 401px) and (max-width: 599px) { body { ...

Issue with fixed sidebar on brief content

It's interesting how the sticky widget performs differently on long articles compared to short ones on my website. Here is an example of a long article where the sticky widget works well without any lag: . Now, let's take a look at a shorter art ...

The usage of jQuery slideUp and slideDown is resulting in a bouncing animation effect

Hey there! I've been working on a cool slide up effect for an image using jQuery's slideUp and slideDown functions. It's working well, but I've noticed that when you hover over certain areas of the div/image, the effect becomes jumpy or ...

How come user CSS in Stylish takes precedence over the author's CSS?

Here's a sample page: <!DOCTYPE html> <html> <head> <style type="text/css"> body { background: black; } </style> </head> <body> </bod ...

What are the steps to make a counter-clockwise dial with jQuery Knob?

Is there a way to use multiple instances of jQuery.countdown? Can I create countdown circles using jQuery Knob? Find examples here and here. Using Multiple instances of jQuery.countdown <div data-countdown="2016/01/01"></div> <div data-co ...

Utilizing JavaScript to display numerous variables within a text box

After creating an HTML form, I encountered an issue where only one selected item was displayed in the text field. Can anyone help me solve this problem so that multiple names can be printed in the textfield? function myFun(extras) { document.get ...

What is the best way to create a full-width dropdown menu with a nested dropdown in React JS?

https://i.sstatic.net/uoX2x.png Looking for a dropdown menu with nested dropdowns inside, I have tried using the standard bootstrap navbar method but haven't been successful. Here is my current code: <div class="dropdown"> <button class= ...

Ultimate Solution - Next Level jQuery Mega Menu

I've been on the hunt for a way to create a mega menu similar to Mashable's without relying on PHP or MySQL, and I'm hitting a roadblock. If anyone has any insight, it would be greatly appreciated. I feel like I've been staring at my s ...

"Ascend beyond the constraints of fixed measurements with dynamic

My JQuery image fader has a width of 100% and height set to auto. I'm thrilled that it works perfectly on the iPhone as well. Now, I am facing a challenge while trying to make content "float" after my JQuery slider. The problem arises because I use ...

To incorporate node_modules CSS into a Vue.js application that is utilizing SCSS, follow these steps

After deploying my application to AWS, I noticed that the application was rendering correctly except for the Syncfusion controls, which were not rendering correctly. Surprisingly, there were no errors shown in the Google Chrome console. On my local machine ...

optimal application of css with jquery

I have a question about using jQuery to set the padding of a class or id. I am able to successfully change the height of an element with this code: $('.menu').css({ height: '90px' }); But now, I need to apply a specific CSS rule in jQ ...

Is anyone else seeing a mysterious gray outline surrounding the image?

When visiting my website, I encountered an issue with the main menu. Specifically, when hovering over 'Populaire boeken', the first 2 menu items display properly with an image on mouseover. However, there seems to be a pesky grey border surroundi ...

Is there a way to create a smooth opacity animation for a background image?

https://i.sstatic.net/PuxYm.jpg I am attempting to create a simple animation on my header using pink red lines that I want to make disappear and reappear on a keyframe. However, when I apply my styles, the entire container is animated instead of just the ...

The anchors seem to be malfunctioning, causing the page to remain stagnant

After spending countless hours on this issue, I am still unable to solve it. The problem seems simple enough - a few modifications were made to the site and now the anchors do not work properly unless the page is refreshed. Visit www.boldcreative.co.nz fo ...

Finding solutions for activating items and setting data sources in Bootstrap carousel thumbnails for Wordpress

I recently attempted to incorporate a bootstrap-powered carousel with thumbnails into my product slider. I managed to get it working for the main image (large image), but ran into issues when trying to resolve problems with the thumbnail section. Here is ...

"Trouble with 3D hexagonal grid in CSS - unable to achieve desired display

In order to create a dynamic Hexagonal Grid using CSS, the objective is to automatically reorganize hexes in a circular or rectangular manner when a new div with a specific class (e.g. 'hexes') is added to the view. Currently, there is a grid wi ...

Deleting a <div> element solely by its unique identifier in plain HTML text without any tags can be achieved in the following way

After my previous question was closed, I realized that the provided answer did not meet my needs. I am actually looking to hide not just the HTML text Received, but the entire row containing 0 <3 Received. A simple display: none; would suffice for this ...

Struggling to keep text aligned to the left?

Check out the image at https://i.sstatic.net/Qu7hT.png in my jsfiddle. It remains fixed in its position regardless of the screen width. However, the adjacent text does not behave in the same way. I aim to have the text positioned DIRECTLY next to the htt ...

Utilizing the Bootstrap grid system with one full-width column and two vertically stacked columns

Can we achieve a layout like this in Bootstrap? One full column followed by two stacked columns next to it. I attempted using nested grids but encountered some issues. https://i.sstatic.net/yYkOW.png ...