Align a block at the vertical center above the bottom line of another block

Here is the HTML code I am working with:

<div class="image">Image</div>
<div class="legend">
  Legend<br/>
  Width can vary, but height needs to be adjustable as well.<br/>
  The middle of the legend should align exactly at the bottom of the image, regardless of their respective heights.
</div>
<div class="following">The following text must come right after the legend, irrespective of their sizes.</div>

This is my desired outcome :

https://i.sstatic.net/iQAfZ.png

This is what I attempted :

.image {
  height:100px;
  background-color:red; 
}
.legend {
  transform:translateY(-50%);
  background-color:blue; 
  width:300px;
  margin:0 auto
}
.following {
  background-color:yellow;
  margin-top:-45px;
}

This is what I ended up with : https://i.sstatic.net/8IJ4N.png

The issue is that I do not want any gap between the legend and the following text.

You can find the complete code attempt on CodePen here.

Question : Is there a way to achieve the desired outcome without using JS?

(Just for reference, I have also provided a solution involving JavaScript)

Answer №1

Are you familiar with determining the height of an element? Do you specifically require it to be exactly 50%?

Check out this example featuring a fixed 50px-negative top margin:

.image {
  height:100px;
  background-color:red; 
}
.legend {
  background-color:blue; 
  width:300px;
  margin:-50px auto 0;
}
.following {
  background-color:yellow;
}
<div class="image">Image</div>
<div class="legend">
  Legend<br/>
  variable height <br/>
  ensure the middle of the legend aligns precisely at the bottom of the image, regardless of their respective heights
</div>
<div class="following">The following text should directly follow the legend, irrespective of their heights.</div>

Alternatively, here is another approach (although it may not be exactly what you are seeking) but still a noteworthy solution :)

.image {
  height:100px;
  background-color:red; 
}
.legend {
  background-color:blue; 
  width:300px;
  margin:0 auto;
  transform: translateY(-50%) translateX(-50%);
  position: absolute;
  left: 50%;
}
.legend:after {
  content: attr(following);
  display: block;
  width: 100vw;
  clear: both;
  position: absolute;
  background-color:yellow;
  height: auto;
  transform: translateX(-50%);
  left: 50%;
  
}
.following {
}
<div class="image">Image</div>
<div class="legend" following="The following text should follow immediatly the legend, regardless of the height of the legend or the image">
  Legend<br/>
  variable height<br/>
  ensure the middle of the legend aligns precisely at the bottom of the image, regardless of their heights.
</div>

Answer №2

If you want to achieve this layout, you can use a simple positioning technique by enclosing your legend and subsequent text within a div element. Set the parent div's position to relative, and the following text's position to absolute.

Take a look at this code snippet for reference:

.image {
  height: 100px;
  background-color: red;
}
.legend {
  transform: translateY(-50%);
  background-color: blue;
  width: 300px;
  margin: 0 auto;
}
.following {
  background-color: yellow;
  position: absolute;
  top: 50%;
  width: 100%;
}
.container {
  position: relative;
  left: 0;
}
<div class="image">Image</div>
<div class="container">
  <div class="legend">
    Legend
    <br/>width variable height
    <br/>the middle of the legend need to be exactly on the bottom of the image, regardless of the height of the legend and image
  </div>
  <div class="following">The following text should follow immediately after the legend, regardless of their heights.</div>
</div>

Alternatively, you can also achieve this layout using flexbox:

.image {
  height: 100px;
  background-color: red;
}
.item {
  transform: translateY(-50%);
}
.center {
  background-color: blue;
  width: 300px;
  margin: 0 auto;
}

.Aligner {
  display: flex;
  flex-direction: column;
}


.Aligner-item--top {
   width: 100%;
   background: red;
}

.following {
  width: 100%;
  background-color: yellow;
}
<div class="Aligner">
  <div class="Aligner-item Aligner-item--top image">Image</div>
  <div class="item">
    <div class="Aligner-item center">Legend<br/>width variable height<br/>the middle of the legend needs to be aligned with the bottom of the image, regardless of their heights.</div>
    <div class="Aligner-item Aligner-item--bottom following">The following text should come right after the legend, regardless of their heights.</div>
  </div>
</div>

I hope this explanation and example help clarify things for you!

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

A bottom border that spans the entire width, complete with padding

I am attempting to create a uniform border for each item on my list. However, due to the nested structure of the list, I have used padding to achieve this. As a result, the appearance is as expected and behaves normally. https://i.sstatic.net/cHNb3.png ...

Tips for creating various column widths within a single row

Hey there, I'm currently working on a web application using Bootstrap and running into an issue with the grid system. Whenever I add elements to a column, all the columns in the same row stretch together, which is not the desired behavior. Take a look ...

Exploring the beauty of background color curves in React Native

Struggling to figure out how to create a curved background color on the top (header part) of my design. Check it out https://i.sstatic.net/nNGPK.png I'm looking to achieve this curved design for my header part https://i.sstatic.net/K8wKV.png Here is ...

Shrink Font-Awesome icon sizes using a Node.js and Express.js web application

Currently, I am in the process of developing a website using node.js + express.js and implementing font-awesome for icons. The local hosting of Font-awesome has resulted in a 1.2 MB JS file [font-awesome.js], even though we are only utilizing 10-15 icons. ...

Element was removed upon clicking only once

Can anyone help me figure out why the behavior of .remove() with $postNav.remove(); is different here? When you click on "I'm a tag" for the first time, both the <li> and <ol> are deleted as expected. However, on the second click, only the ...

Center align Bootstrap 4 dropdown menu

I am attempting to center align my dropdown menu using Bootstrap 4. Following the given example: [ This is the result I am achieving: [ This is the code I have implemented: <div class="container"> <div class="row"> <div class=" ...

How can you determine if a DIV element is within view in HTML (without being overflowed)?

Is there a way to determine if a DIV is within its container and therefore visible? In this scenario, the active button (highlighted) can be seen because it is contained within the boundaries: However, in this case: The active button cannot be viewed as ...

preventing elements from moving unexpectedly as the navigation bar becomes fixed at the top of the page

I have a website that features a Bootstrap 3 navbar. This navbar is positioned 280px below a block div and becomes sticky at the top of the page when scrolled to that point. HTML (within < head > tags) <script> $(document).ready(function() ...

The Enigma of the Empty Gap When Employing "display:table/table-cell"

Struggling to understand why there is a blank space between the image and the menu. Here is the HTML and CSS related to it: <header class="header_nav"> <img src="img/logo.png" class="logo"/> <nav class="nav_bar"> <u ...

Using attribute value for CSS background-image styling is a handy technique to

In my current situation, the design calls for the use of two different images as background images - one for desktop and another for mobile. These images are uploaded through a CMS, allowing me to retrieve the URLs for both using PHP. Is there a way to pa ...

Getting the most out of setInterval() - a guide in jQuery

I am currently working on an auto slide feature and could use some guidance with setInterval(). From my understanding, setInterval() is used to repeat functions infinitely, which is exactly what I need for this project. Here is the current layout: HTML & ...

Using CSS min-height: 100vh will ensure that the container has enough height to fill the entire viewport

Utilizing min-height: 100vh; in my inner call to action div placed on top of a background video has been mostly successful. However, I've encountered an issue where using 100vh with the absolutely positioned video seems to introduce an unwanted margin ...

Implementing a CSS codepen within a React Material-UI component

I'm struggling to implement this effect on my material ui card component using react and makeStyles. I am having difficulty translating this effect to the cards, could someone assist me in simplifying it into a CSS code that can be used in MakeStyles? ...

Updating the size of the caret in input fields with Bootstrap 5

Is there a way to adjust the height of an input field (form-control) using Bootstrap 5? I'd love to see an example of how to make it taller or shorter - any suggestions? https://i.sstatic.net/YM1Os.png Here's a sample code snippet for reference ...

Achieving functionality with dropdown menus in jQuery

I am facing an issue with a dropdown menu that works perfectly in jsFiddle during testing, but does not function as expected when I run it on my testing server. jsFiddle: http://jsfiddle.net/cyberjo50/39bu8/2/ HTML <!doctype html> <html> < ...

Creating a dynamic navbar in an ASP.NET website by populating it with data from a

Seeking guidance on creating a dynamic Bootstrap navbar in an ASP.NET website based on the user's role stored in a database, while maintaining the same style. I have extensively searched for a solution without success. Any assistance would be greatly ...

Struggling with creating a website: CSS and HTML headaches

I'm facing an issue with designing a website: The top navigation bar on this site consists of the number of search results found, a search bar, and login methods. My question is how can I align the "websitelocation" div and search input to the left, ...

Spaces between dotted outlines featuring a gradient backdrop

What causes the top edge gaps of the dashed border to be filled with blue color? Why are the bottom edge gaps of the dashed border filled with white color? ...even when the background is set as a linear white-to-blue top-to-bottom vertical gradient. ...

CSS: "Magazine Writer" design featuring a floating box and diverse HTML elements

OBJECTIVE: My aim is to replicate a "newspaper author" layout using HTML/CSS: SOLUTION/ISSUE: I floated a box to the left and added text and other content that wraps around it. However, some of the content's background-color and CSS effects are overf ...

After examining the width properties in the CSS code, it was discovered that one particular icon is larger in

Is there a way to adjust the width of the first icon in my container using CSS? The first icon appears larger than the others, and I'm having trouble making it the right size. #features { margin-top: 35px; } .grid { display: flex; } #feat ...