Center the div vertically aligned with the following element

Seeking guidance on aligning an element to the vertical center of another element.

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

In the provided scenario, the goal is to align the top of text with the vertical center of the adjacent image.

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

The challenge lies in the variability of text height, requiring the margin to align only with the image's vertical center.

An attempted solution involved utilizing Flexbox, but it demanded a fixed height for the text:

.container {
  display: flex;
  flex-flow: row wrap;
  max-width: 800px;
  width: 800px;
  margin: 0 auto;
}

.container .image {
  flex: 0 0 50%;
}

.container .image img {
  width: 100%;
  height: auto;
}

.container .text {
  flex: 0 0 50%;
  display: flex;
  flex-flow: row wrap;
  align-items: flex-end;
}

.container .text .inner {
  height: 50%;
  background: #ccc;
}
<div class="container">
  <div class="image">
    <img src="http://via.placeholder.com/800x500">
  </div>
  <div class="text">
    <div class="inner">
      <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo ...</p>
    </div>
  </div>
 </div>

Resorting to JavaScript for determining and applying dynamic margins based on calculated image height may be a potential workaround.

Answer №1

You can achieve the same layout using CSS Grid as well. Create a 2x2 grid, where the element on the left spans across both rows, and the element on the right is aligned to the top of the bottom right quadrant.

.container {
  display: grid; 
  grid-template-columns: repeat(2, 1fr); 
  grid-template-rows: repeat(2, 1fr); 
  gap: 0px 0px; 
}
.image { grid-area: 1 / 1 / 3 / 2; }
.text { grid-area: 2 / 2 / 3 / 3; }

.image {
  display: grid;
  place-items: center;
}

.text {
  align-self: start;
}

/* misc */
.image {
  background: salmon;
  resize: vertical;
  overflow: hidden;
}
.text { background: beige }
<div class="container">
  <div class="image"><p>This content will be centered.</p></div>
  <div class="text"><p>This content will be aligned to the center of the item on the left.</p></div>
</div>

Answer №2

I prefer utilizing CSS Grids in situations like this, although it's important to have a clear understanding of the parent element's dimensions

.my-grid {
  display: grid;
  width: 500px;
  height: 500px;
  grid-template-areas: 
            "a x"
            "a b";
}

.left {
  grid-area: a;
  background-color: #ffa08c;
}
.right {
  grid-area: b;
  background-color: #8ca0ff;
}
<div class='my-grid'>
  <div class='left'></div>
  <div class='right'></div>
</div>

Answer №3

Might be better with some extra padding

* {
  box-sizing: border-box;  
}

.container {
  display: flex;
  margin: auto;
  max-width: 600px;
  min-height: 300px;
  border: 1px solid black;
}

.container div {
  flex: 1 0 200px;
}

.left {
  background-color: rgb(255, 179, 164);
  display: flex;
  justify-content: center;
  align-items: center;
}

.right {
  padding: 1em;
  padding-top: 25%; /* a useful trick */
}

p {
  margin: 0; /* eliminates extra space at the top of text */
}
<div class="container">
  <div class="left">
    <img src="https://via.placeholder.com/150">
  </div>

  <div class="right">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
  </div>
</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

Challenges Encountered with jQuery/AJAX Form Processing

I am currently facing an issue with two files in my project. The main file, index.php, contains JavaScript code within the head section. Here is the snippet of JavaScript: <script src="http://code.jquery.com/jquery-latest.min.js" type= ...

Laravel throws an error message "expression expected" when trying to use the @

I keep encountering an issue when attempting to pass a variable from PHP code to JavaScript. Expression expected Here is the code snippet I am using in Laravel 7.0 : <script> let variable = @json($array); </script> Although the code still ...

The clientHeight property is yielding a result of zero

I'm using JavaScript to create DOM elements dynamically. In order to animate a slide down effect, I need to retrieve the height of a specific div element. However, both clientHeight and offsetHeight are returning 0. I have confirmed that the div eleme ...

What is the process for transferring a file to a different directory?

<html> <head> <title>Main Page</title> </head> <body> <h2>Main Page</h2> <form method="post" action="index.php" enctype="multipart/form-data"> <input type="file" name="filename"> <input type=" ...

Click on the first jQuery element

I'm currently dealing with a menu that has multiple levels. My goal is to create a functionality where clicking on a first-level element will add the class .ubermenu-active to it, while also removing this class from any other first-level elements. $( ...

Can you extract debugging details from an ajax preflight inquiry?

I am currently working on a JavaScript project that involves making an AJAX request. However, I am encountering issues with the preflight OPTIONS call for this request failing. To provide some transparency to the user, I would like to display debug infor ...

A new value was replaced when assigning a JSON value inside a loop

Is there a way to generate a structure similar to this? { "drink": { "2": { "name": "coke", "type": "drink" }, "3": { "name": "coke", "type": "drink" } }, "food": ...

Rails 7: It appears that the browser is disregarding all importmap JavaScript packages

Last week I worked through two different tutorials, successfully achieving most of the tasks. However, three crucial issues remain unresolved. Upon closer inspection, it became evident that all three problems revolve around the browser seemingly disregardi ...

Issues with Cordova file transfer not connecting to server endpoint

I've been attempting to utilize FT (cordova file-transfer) to create a file and send it to my express app. However, I've encountered an issue where express does not receive the request. Previously, it was functioning correctly, but now it has sto ...

What is the best way to create a function that will initiate the download of a particular file based on a specific key provided by the

I need assistance in creating a function for my text box and button setup. Can someone please guide me on how to achieve this? The user will be provided with a specific alphanumeric key from the website. They must input this key into the text box and then ...

Validation Express; the error variable is not defined in the EJS

Struggling with a perplexing issue that has been eluding me for quite some time now, I am hopeful that someone out there might be able to provide me with some guidance. The variable (error) that I am passing in the res.render{} object seems to be unattain ...

Troubleshooting problem with ngb carousel timing interval

I am currently developing a web app for selecting maps in Escape from Tarkov. To enhance the visual appeal of the app, I decided to incorporate an ngb-carousel feature for a smooth animation effect. However, I have encountered a problem where the slides ar ...

Tips for customizing the sidebar menu in R Shiny to use an "angle-right icon" instead of the default "angle-left icon"

I am trying to modify the default arrow orientation (shown in the image), so that it points towards the right. https://i.sstatic.net/c0ulj.png This is the current snippet of code: sidebarMenu( menuItem("Market data", tabName = &qu ...

Utilize the form to sort through entries

I am in the process of developing an ExpressionEngine website using Bootstrap. The site is designed for volunteers to discover projects that they can assist with. One feature I have implemented is a modal on the home page containing a form where users can ...

After the re.redirect() has loaded, remember to refresh the page

Currently, I am in the process of creating a website solely for educational purposes, focusing on articles. After a user adds a new article, the intended action is to redirect them back to the homepage seamlessly. Upon testing this functionality and addi ...

What is the best way to update Mongoose models?

Here is the task at hand: Delete all existing documents from the model Save new documents for the model This is an example of how this can be accomplished: Model.remove().then(function () { Model.save(new Model({ *some valid data* }); } ...

Is it possible to modify the icon of a date picker in Ant Design?

Is it feasible to switch the icon of a date picker from the ant design framework to a material ui icon? https://i.sstatic.net/blbH1.png Material UI icon for reference: https://i.sstatic.net/zpJsZ.png ...

The error code net::ERR_ABORTED 500 popped up indicating an Internal Server Error within the bootstrap

After running the project, I encountered the error shown in the attached image. I have double-checked the existence of the files in the specified folders, and everything seems to be in place. However, I am unable to figure out how to resolve this issue. ...

What is the best way to transfer JSON data to a different controller in AngularJS?

Hello, I'm still learning AngularJS and facing an issue with the following code snippet. app.config(function($routeProvider) { $routeProvider .when('/', { templateUrl: "partials/home.html", controller: "mainControlle ...

Seeking assistance with changing the pages

Looking for assistance with troubleshooting my function. All variables are accounted for. var swith = document.getElementById("sub"); var one = document.getElementById("rule"); var two = document.getElementById("cool"); var three = document.getElementByI ...