Align a text box of variable height vertically next to an image of fixed height

While I understand this is a common question, I am still facing difficulties even after reading similar queries.

In my situation, I have two divs (one with a variable height text box paragraph and the other with a fixed height image) within a container div, set up as shown below:

<div class="error-row row">
    <div class="error-value-col">
        <p class="error-value">{{error.message}}</p>
    </div>
    <a class="cross-link">
        <img class="cross" src="/assets/cross.png" alt="close">
    </a>
</div>

This snippet of LESS code accompanies the HTML structure:

.error-row {
  border: 1px solid @po-yellow;
  border-width: 0px 1px 1px 1px;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  position: relative;
  margin: 0px;

    .error-value-col {
      float:left;
      vertical-align: middle;
      display: inline-block;
      width: calc(~'100% - 70px');

        .error-value {
          font-size: 10px;
          padding: 5px;

            p {
              margin-bottom: 0px;
            }
        }
    }

    .cross-link {
      padding: 0px;
      float: right;
      vertical-align: middle;
      display: inline-block;
      height: 70px;

        img.cross {
          margin: auto;
          width: 70px;
          height: 70px;
          padding: 28.5px 27.5px 26.5px 27.5px;
          color: black;
        }
    }
}

I have experimented with various configurations but none seem to be effective. My goal is to align the element with the smaller height next to the taller element in the center.

Thank you for any assistance provided.

EDIT: To clarify...I aim to center the error-value-col and cross-link elements inside the error-row container. This container will adjust its size based on the larger of the two elements, which could vary.

Answer №1

After adjusting my approach, I utilized display:table and display:table-cell to achieve the desired behavior. Check out this revised jsFiddle to see if it meets your requirements (converted LESS to CSS there).

In addition to design guidelines, some new additions to the LESS code include:

.error-row {
  ...
  display:table;
  width:100%;

    .error-value-col {
      ...
      display:table-cell;
      vertical-align:middle;

        .error-value {
          ...

            p {
              ...
            }
        }
    }

    .cross-link {
      ...
      display:table-cell;
      width:70px;
      vertical-align:middle;

        img.cross {
          ...
        }
    }
}

For a detailed comparison, please refer to jsFiddle to see all the differences, including the removal of floating elements.

ALTERNATIVES:

  • Vertical alignment can be challenging in CSS, especially without utilizing the relatively new Flexbox model.
  • A common method involves absolutely positioning an inner DIV with top:50%, but since the reference point is the top-left corner, you need to adjust it by "half of its height" using a negative margin-top. This approach requires a fixed height for the inner DIV to accurately set the negative margin to half of that height.

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

In Internet Explorer, when utilizing a table-cell with a variable height, the position should be set to absolute for

I'm struggling with getting a uniform height in my horizontal layout using display: table-cell. While it looks great in Chrome, I can't seem to get Internet Explorer to display it the same way. Check out this link for reference This is how I wa ...

li tag style

I need to insert a check mark inside the <li> tag on the right side. How can I do this? The check mark looks like this: For example: http://jsfiddle.net/HpVcy/ ul li{ padding: 3px 10px 3px 10px; background:url(http://img4up.com/up2/730897458613759 ...

The new FormData(form) method unexpectedly returns an empty object

In this scenario, I am aiming to retrieve key-value pairs. The form on my page looks like this: <form id="myForm" name="myForm"> <label for="username">Enter name:</label> <input type="text" id="username" name="username"> ...

FancyBox: Can You Achieve the Same Scrolling Effect with HTML Fragments Instead of Images?

Is it possible to display HTML fragments instead of images with the same scrolling effect (when using the next() method)? I am struggling to figure out how to make this work. Currently, I am able to display a single HTML fragment using fancybox: $.fancyb ...

Is there a way to prevent the ENTER key on the keyboard from interacting with

When visiting http://api.jqueryui.com/datepicker/, I came across the following information: Keyboard interaction While using the datepicker, various key commands are available: PAGE UP: Move to the previous month. PAGE DOWN: Move to the next month. CTRL ...

Ways to retrieve HTML content from various spans and incorporate the values as classes

Here is my custom HTML: <div class="box"> <ul class="fourcol first"> <li class="feature-item">Description: <span>0</span></li> <li class="feature-item">Description: <span>0</span></ ...

Implemented an HTML hyperlink element precisely positioned

I have the following HTML code. I am trying to fix the position of an anchor tag called "View All" so that it stays in a specific location. However, whenever the value in the dropdown menu changes, the anchor tag moves according to the length of the new se ...

What is the best way to ensure a dropdown menu stays visible while navigating through various If/Else blocks?

I am fairly new to working with PHP and MySQL, so I have been using the mysql_* functions despite knowing that they are no longer supported. However, I can't seem to find anyone else who has had a similar question to mine, which is why I am reaching o ...

Ways to display or conceal a textbox depending on the choice made from a dropdown list

I'm a beginner in using jquery. I'm trying to create a dropdown menu with different options. When "Others" is selected from the dropdown, I want a text box to be displayed. Can someone provide guidance on how to achieve this? Here is the code sni ...

Technique for structuring and managing CSS resources in Angular.js

I am exploring ways to load CSS files in a modular manner within my Angular application. My goal is to have each module with its own dedicated CSS file for easy management - allowing me to add, remove, or move modules effortlessly. After some research, I ...

Can you please provide instructions on how to expand the view in the title bar for a JavaScript/CSS/HTML UPW project?

Struggling to integrate the title bar in a UWP project using JavaScript/CSS/HTML and having trouble accessing the necessary APIs. Came across a custom helper class written in c++ in UWP samples on Github, but unable to reference it in my javascript code. H ...

What is the best way to ensure the options/choices div reaches its ideal width?

Check out my StackBlitz project that I've set up In the styles.css file, you'll notice that I defined a fixed width of 650px for the option dropdown div, which is currently working fine @import '~bootstrap/dist/css/bootstrap.min.css'; ...

Having trouble with your contact form sending messages?

Looking for some assistance with the Contact form on this Codepen page: https://codepen.io/ssbalakumar/pen/uzDIA The form is displaying correctly on my website, but when I try to submit it, the page reloads and I'm unsure where the message is being ...

What is the best way to apply color to a Rectangle created using html5?

I am currently studying Html5. <html> <body> <canvas id="canvas1" width="300" height="200"></canvas> <script> var canvas = document.getElementById("canvas1"); var cxt = canvas.getContext("2d"); context.fillRect(0, 0, 150, ...

What is the best way to create space between two flex elements that doesn't follow a

Dealing with a challenge in Flexbox. I am trying to establish a specific distance between two flexible elements. I understand that I should use justify-content, but unfortunately cannot set an exact distance using it. Avoiding the use of margins or othe ...

What is the method for changing the color of a specific section of a header?

My header design consists of two lists of buttons, one on the left and one on the right. I am looking to customize the background color of the "Sign Up" button to make it stand out. Below is an example of what I want and the current design of my header alo ...

Issue with Electron Remote process failing to take user-defined width and height inputs

I'm currently facing an issue with utilizing remote windows in electron. I am attempting to process user input and use that input to generate a new window with specific width and height. However, when I hit submit, nothing happens. I can't figur ...

Tips for using CSS to adjust the width of a parent element based on the width of an image

I'm currently working on developing my own lightbox using React, and I am looking to adjust the width of a div with the class .lightbox-active based on the width of the image it contains. Right now, I have set the parent element to have a max-width: 5 ...

Mismatched photo caption positioning beneath image (html, CSS)

Question from a beginner: I'm attempting to position a photo on the right side of a webpage, with text wrapping around it on the left, and the photographer credit directly under the photo. The issue is that the credit is appearing on the left side ab ...

Problem with ngStyle: Error indicating that the expected '}' is missing

I encountered an error in the Chrome console when trying to interpret the code below: <div [ngStyle]="{'width': '100%'; 'height': '100%'; 'background-size': 'cover'; 'background-repeat&ap ...