When elements are arranged side by side without using floats, the alignment at the top is not correct

I am having trouble aligning multiple divs next to each other using inline-flex. They are not lining up properly on top (refer to the screenshot). Interestingly, if I remove the svg, the divs align correctly. 🤷‍♂️ Using float: right or a negative margin-top property works, but I prefer to avoid those as they are not intended for this purpose and feel hacky.

This is my code:

 .wrapper {
      height: 100%;
    }

    .item,
    form,
    .input-container {
      height: 100%;
      display: inline-flex;
      align-items: center;
    }

    button {
      border: none;
      background: #111;
      height: 100%;
      width: 16px;
      display: inline-block;
    }

    .icon {
      height: 16px;
      width: auto;
      float: left;
      display: inline-block;
    }
   <div class="wrapper">
      <div class="item">
        <!-- Search form with svg image -->
        <form>
          <button class="k-submit" for="search" id="k-submit" type="submit" aria-label="Open searchbox">
            <svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 55 55">
              <path d="M54.1 49.8L40 35.7c2.8-3.6 4.4-8.2 4.4-13.1 0-12-9.7-21.7-21.7-21.7S.9 10.6.9 22.6s9.7 21.7 21.7 21.7c4.9 0 9.5-1.6 13.1-4.4L49.9 54l4.2-4.2zM6.9 22.6c0-8.7 7.1-15.7 15.7-15.7 8.7 0 15.7 7.1 15.7 15.7 0 8.7-7.1 15.7-15.7 15.7s-15.7-7-15.7-15.7z" fill="#111"></path>
            </svg>
          </button>
          <div class="input-container">
            <!-- Search input field -->
            <input value="Suche" type="search">
          </div>
        </form>
      </div>
      <div class="item">
        <a href="http://www.example.com">
          English
        </a>
      </div>
    </div>

I have modified the screenshot to provide a clearer view of the containers. The issue lies with the containers themselves not lining up properly.

Answer №1

.container {
  height: 100%;
}

.item,
form,
.input-container {
  height: 100%;
  display: inline-flex;
  align-items: center;
}

button {
  border: none;
  background: #111;
  height: 100%;
  width: 16px;
  display: inline-block;
}

.icon {
  height: 16px;
  width: auto;
  float: left;
  display: inline-block;
}
.top{
  vertical-align: top;
}
<div class="container">
  <div class="item">
    <!-- Search form with svg image -->
    <form>
      <button class="search-btn" for="search" id="search-submit" type="submit" aria-label="Open searchbox">
        <svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 55 55">
          <path d="M54.1 49.8L40 35.7c2.8-3.6 4.4-8.2 4.4-13.1 0-12-9.7-21.7-21.7-21.7S.9 10.6.9 22.6s9.7 21.7 21.7 21.7c4.9 0 9.5-1.6 13.1-4.4L49.9 54l4.2-4.2zM6.9 22.6c0-8.7 7.1-15.7 15.7-15.7 8.7 0 15.7 7.1 15.7 15.7 0 8.7-7.1 15.7-15.7 15.7s-15.7-7-15.7-15.7z" fill="#111"></path>
        </svg>
      </button>
      <div class="input-container">
        <!-- Search input field -->
        <input value="Search" type="search">
      </div>
    </form>
  </div>
  <div class="item top">
    <a href="http://www.example.com">
      English
    </a>
  </div>
</div>

Answer №2

To ensure elements are aligned at the top, consider using "align-items: inherit;" instead of centering them. Here is an example:

.item, form, .input-container {
height: 100%;
display: inline-flex;
align-items: inherit;} 

Answer №3

Here is a possible solution:

To resolve the alignment issue, try moving your div with class='item' into the same parent div as your form.

This adjustment should result in proper alignment as the elements will now share the same parent div.

If you notice a slight discrepancy in the rendering, it may be due to how the browser handles underlines for the a tag containing "English". Applying text-decoration: none; to the a tag can help achieve perfect alignment.

.wrapper {
  height: 100%;
}

.item,
form,
.input-container {
  height: 100%;
  display: inline-flex;
  align-items: center;
}

button {
  border: none;
  background: #111;
  height: 100%;
  width: 16px;
  display: inline-block;
}

.icon {
  height: 16px;
  width: auto;
  float: left;
  display: inline-block;
}
  <div class="wrapper">
    <div class="item">
      <!-- Search form with svg image -->
      <form>
        <button class="k-submit" for="search" id="k-submit" type="submit" aria-label="Open searchbox">
          <svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 55 55">
            <path
              d="M54.1 49.8L40 35.7c2.8-3.6 4.4-8.2 4.4-13.1 0-12-9.7-21.7-21.7-21.7S.9 10.6.9 22.6s9.7 21.7 21.7 21.7c4.9 0 9.5-1.6 13.1-4.4L49.9 54l4.2-4.2zM6.9 22.6c0-8.7 7.1-15.7 15.7-15.7 8.7 0 15.7 7.1 15.7 15.7 0 8.7-7.1 15.7-15.7 15.7s-15.7-7-15.7-15.7z"
              fill="#111"></path>
          </svg>
        </button>
        <div class="input-container">
          <!-- Search input field -->
          <input value="Suche" type="search">
        </div>
      </form>
      <div class="item">
        <a href="http://www.example.com">
          English
        </a>
      </div>
    </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

CSS - Yet another perplexing question that shouldn't have arisen

While working on building this website in CSS, I hadn't encountered any major issues up until now. Suddenly, I'm facing a problem with basic positioning within my body-3 class div. Instead of pushing it downward and elongating the page as expecte ...

Attempting to recreate the dynamic banner featured in the video

Looking to replicate a setup similar to what was demonstrated in the video. I have two div blocks - one with a random image and the other with a video, and I want them to be as flexible and identical as possible to the video layout. How should I go about a ...

Tips on aligning a v-btn alongside v-expansion-panels on the same row

Struggling with aligning my layout. Trying to get a single set of v-expansion-panels and a v-btn in the same row, both visually centered within a card. I managed to almost achieve it in this codepen: https://codepen.io/anzuj/pen/PoPPbdw with the following ...

Prevent sidebar from adjusting size according to text length

Just started exploring Bootstrap 5 and noticed some significant differences from version 4. Check out this jsfiddle for a demonstration of my issue. The problem occurs when the lorem ipsum text exceeds a certain length, causing the sidebar width to be pu ...

A guide on switching the status of various inputs in a table based on the selection of a radio button

In the scenario below, let's consider the following HTML structure: <tr> <td> <label for="c1_testRdio">Have you taken any tests in this class?:</label> <br> <label>Yes<input type="rad ...

Steps to create an iframe that opens in a new window

I'm facing an issue with the iframe sourced from flickr. My website includes an embedded flickr iframe to showcase a gallery without the hassle of creating a slider, resizing images, and extracting thumbnails. Given that this site belongs to a frien ...

Can someone guide me on how to make an array filled with dates using Javascript?

I am working on developing a Javascript code that can identify all the days leading up to the current date. Here is what I have managed so far: var titleArray = [ "title1", "title2", ]; var pictureArray = today.toString(); var thumbArray = today.toString ...

increasing the size of the input field on the lower row of the form

I have a form with two rows of text boxes. The first row contains three text boxes, while the second row should only have one text box that spans 100% of the width. Here is the current code: <div class="row"> <div class="col-md-4"> ...

Manipulating JSON Elements using JavaScript in HTML

My objective is to alternate the visibility between the "prev" and "ext" elements retrieved from the JSON File in the following manner: Here is the link to JS Fiddle The menu bar appears as follows: [ "prev_Person1" "Person1" "ext_Person1" ... "Person2" ...

An error occurs when attempting to assign a value to a MUI file TextField

Struggling with setting the value of a MUI Textfield that has type="file" props, resulting in the following exception being thrown: Uncaught DOMException: An attempt was made to use an object that is not, or is no longer, usable Interest ...

Struggling to make the height attribute work in Bootstrap 4 alpha

I'm currently in the process of learning Bootstrap, and I've encountered an issue with the height attribute in version 4 (for example: "row h-25") not functioning properly. I attempted to add another CSS rule that sets the height of "container-f ...

Is there a more effective way to structure my code than using multiple "IF/ELSE" statements?

Here is the current code snippet that I have: (category=="Ljud & Bild") ? byId("nav_sub_ljud_bild").style.display='block' : byId("nav_sub_ljud_bild").style.display='none'; (category=="Datorer") ? byId("nav_sub_datorer").style.disp ...

Troubleshooting problem with Angular 2 in Internet Explorer related to the use of [style]="

I've encountered a challenge while using angular 2 (2.0.0-beta.17). The app works perfectly on most browsers, but as expected, IE 11 is causing trouble. The scripts included in the head for angular are: <script type='text/javascript' sr ...

Sprockets could not locate the file for jquery.atwho

I have been attempting to integrate the jquery-atwho-rails into my application, a Rails gem designed for at.js. I have followed all the steps provided, executed bundle install, included the necessary code in both application.js and application.css, stopped ...

Using PHP to swap out HTML elements

I need a solution to display text with HTML tags as plain text, without the browser interpreting them. I want the output to be similar to how HTML code viewers render it. For example: replacing <div> with <span><</span><span>d&l ...

Troubleshooting puppeteer PDF: Unable to set table height using CSS for tr:last-child { height: 100%; }

My Node.js code snippet is as follows: await page.setContent(html, {waitUntil: 'networkidle2'}); const pdf = await page.pdf({ format: 'A4', printBackground: true, margin: { top: '14mm', // default is 0, units: mm, c ...

Having trouble aligning the unordered list with the input

Can anyone help me troubleshoot my CSS so that the list appears below the input field? Check out this example The nested menu in this example is positioned too far to the left and slightly too high. It should be aligned with the bottom of the containing ...

I am facing an issue with the responsiveness of the mat-card component within a div in

Is it possible to display several small paper cards in a div so that they wrap around the container? ...

Steps to center an HTML canvas on the screen

I attempted to center my canvas horizontally using <div style="text-align:center;">, but the y axis remains unchanged. How can I position the HTML canvas in the center of both the x and y axes? Any suggestions on how to achieve this alignm ...

The issue of column sizing in bootstrap 4 needs to be resolved

I'm having trouble making the form input fill the width of the screen on small devices. It works fine on larger screens but doesn't respond properly on phones like it should. <div class="container-fluid"> <div class="row"> ...