Aligning the text in the left div vertically based on the right div's position

I am currently working on aligning two parallel div boxes, each containing text. The width of these div boxes is not fixed and changes based on a percentage. My challenge is to make the text in the left div box start at the same position as a heading in the right box, but I am unsure how to vertically align them.

#left {
width: 19%;
height: auto;
margin-left: auto;
background-color: green;
float: left;
margin-left: 20%;
text-align: right;
padding-bottom: 20px;
padding-top: 10px;
}

#right {
float: left;
width: 38%;
height: auto;
background-color: blue;
margin-left: 1%;
text-align: left;
padding-bottom: 20px;
padding-left: 1%;
padding-right: 1%;
padding-top: 10px;
}
<html>
<head>
</head>
<body>
<div id="wrapper">

  <div id="left">
    <p>This text should align with the caption "here"</p>
<p>Involving images, this should align with the caption "here2"</p>
  </div>

  <div id="right">

    <p>Example text example text example text example text example text. </p>

    <h1>here</h1> 

    <p>Example text example text example text example text example text. </p>
 <h1>here2</h1> 

    <p>Example text example text example text example text example text. </p>

  </div>

</div>
</body>
</html>

The desired end result should resemble this: Example 1

It should also accommodate images like this:

Example 2

I am seeking guidance on achieving this layout or confirmation if it is not feasible. Any assistance would be greatly appreciated.

Answer №1

To achieve the first image example, one option is to use JavaScript to calculate the height of the right-hand box, subtract the height of the left-hand box, and then divide by 2 to determine the top margin needed to vertically align the left box. Alternatively, you can try the following CSS:

html, body {
  width: 100%;
  height: 100%;
}

#wrapper {
  position: relative;
  width: 100%;
}
#left {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);

  width: 19%;
  height: auto;
  margin-left: 20%;
  background-color: green;
  text-align: right;
  padding-bottom: 20px;
  padding-top: 10px;
}

#right {
  float: right;
  margin-right: 20%;

  width: 38%;
  height: auto;

  background-color: blue;
  margin-left: 1%;
  text-align: left;
  padding-bottom: 20px;
  padding-left: 1%;
  padding-right: 1%;
  padding-top: 10px;
}

For more complex layouts, I recommend exploring Bootstrap, which offers powerful options for building different types of layouts. You may find it useful, especially for example 2:

https://getbootstrap.com/docs/4.1/layout/grid/

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

Display web content using ASP.NET Razor cshtml files

I am currently working on a web application developed with ASP.NET 4.6.1 and utilizing the Razor view engine. Is there a way to trigger a command that will convert all my template files (.cshtml files) into static HTML? For specific stages in my build pro ...

Gaining a comprehensive understanding of media queries

Hello everyone, I need some help with media queries. I've been working on a website that is already responsive. However, I am required to write the code in a child theme rather than directly in the original files. When I attempt to add new media quer ...

Does "br" not play well with flexbox?

After creating a table using flexbox, I made an interesting observation: the br element seems to have no effect within the flexbox. For example: .flexbox { display: flex; flex-wrap: wrap; border: 2px solid red; padding: 2px; } .item { width: ...

What is the best way to navigate to a specific ID on the homepage using a navigation button on another page?

When a navigation button is clicked on any page other than the home page, I want the home page to load first and then scroll to the corresponding id mentioned in the navlink. Below is the code snippet: <Col sm={5}> <Nav className=& ...

Unable to see the tokens on the connect four board when using Javascript and HTML

I'm currently developing a game of connect four using javascript, html, and css. I'm encountering an issue with the refreshGrid() function in game.js. At the moment, when I run my html file, I only see an empty board. The function is meant to ena ...

I need to style a blockquote so that it floats to the right, but I also want it to be

I want to enhance the appearance of my blockquotes by giving them a different color background and ensuring they stand out from the regular text. Although using float:right helps achieve this effect, I prefer not to force the blockquote to the right side o ...

Executing Cross-Component Communication in Angular 7: A Quick Guide

I've encountered a challenge with a checkbox in the header component. If the checkbox is checked, I need to display an alert message when the application loads. The tricky part is that the checkbox is linked to the home component. Therefore, if I am o ...

Ways to align elements horizontally while preventing them from shifting positions

I'm faced with a layout challenge where I need to place 2 components side by side, but I want one component to stay on the right without affecting the orientation of the other component. Example: https://i.stack.imgur.com/T5Ram.png Currently, when I ...

Extracting text from HTML tags can be quite tricky, especially when you need the text within a specific tag like <td> but not within any nested tags like <

Is there a way to extract only the numbers from the <td> tag and ignore any text within the <strong> tag that is nested inside it? I am currently using selenium code but it returns the entire content of the <td> tag. Python Code: wait. ...

Enhance the performance of jQuery and CSS on your WordPress site

I'm currently working on a WordPress 4.3 website that utilizes over 20 plugins, each loading CSS and jQuery on every page. My main concern is how to optimize the styles and jQuery so that only the necessary ones are loaded on each page. I've at ...

Personalized hyperlink element with interjected spacing

I am currently in the process of designing a component library that is based on lit web components. However, I have a suspicion that my issue may not be directly related to Lit itself, but rather connected to shadow DOM. Within my link component, I have d ...

Interactive calendar feature displaying events upon hovering over a date

I need some assistance with displaying a drop-down list on full calendar events when hovering over the events. Can someone provide guidance? Here is a glimpse of what I currently have: https://i.sstatic.net/fZXMH.png I've attempted setting the z-in ...

Is there a way to determine the quantity of items within a sortable div?

In my HTML document, there are multiple divs containing smaller divs that can be rearranged within each other. Upon loading the document, a random assortment of these smaller divs are added to #boxtop. Below is a snippet of my HTML code: <html> &l ...

Tips for keeping a Youtube video playing even after the page is refreshed

Is it possible to save the current position of a Youtube video and have it resume from that point when the page is refreshed, instead of starting from the beginning? I am considering using cookies to store the last position or utilizing GET. Although my w ...

How to Align Two Headers Vertically Next to Each Other Using HTML and CSS

Looking for a solution for aligning the orange date: Here is the current setup: Check out the JsFiddle example: http://jsfiddle.net/N4gpA/ (Including code as jsfiddle-only is not permitted) HTML: <div class="Box1"><a href="#"&g ...

Array of materials for ThreeJS GLTFLoader

Attempting to load a model using GLTFLoader and apply different colors for each face of the object (cube) using a material array is not functioning as expected. var materials = [ new THREE.MeshPhongMaterial( {color: 0x552811,specular: 0x222222,shininess: ...

Different Types of Buttons in HTML

As someone who is new to the world of HTML coding and JavaScript, I have a question about button types. According to the W3Schools website, there are three types of buttons: <button type="button|submit|reset"> First question: Why do we need a for ...

Is there a way to perfectly center this image with the rest of my text content?

Seeking assistance in aligning this image to the right alongside other text content. Currently, when the code is executed, the image is pushed down and positioned below the text on the right side. Ideally, I would like the image to be aligned evenly with ...

Obtain the row ID from a database by selecting checkboxes

I am facing a challenge when trying to remove a row from my MS Access database using checkboxes on my JSP page. Each row in the displayed database has a checkbox, but I am struggling to retrieve the rowId and link each checkbox with its respective row. Any ...

Do css classes consistently provide a solution to issues with style overrides?

Would it be more beneficial to utilize css .class instead of #id to avoid issues with style overriding and importance? #content ul li a {font-size:10px} #content .demo ul li a {font-size:15px} ...