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

Facing a blank page with no errors displayed during the HTML rendering process in Angular version 6

One of the most frustrating aspects of working with Angular is the lack of information provided when there is a render error in an HTML page. Instead of specifying which page the error is in, Angular defaults to the route page and provides no further detai ...

Is there a way to easily duplicate this and add a navigation bar and logo to every page?

Why is it that when I copy and paste this code into any .html document, it only runs correctly on the resources tab? The logo is only showing up on the resources page, even though it's using the same css stylesheet and html structure. <div class=" ...

Styling elements using the nth-child selector in JavaScript

I am trying to apply a CSS class based on the combined height of the 2nd and 3rd child elements. For example, if the height of the 2nd child plus the height of the 3rd child equals a certain value, I want to add a specific class. Here is my JavaScript cod ...

Adjusting the size of a Google map based on the size of the browser

Currently, I am working on implementing Google Maps API v3. The map is displaying perfectly on my page, but the issue arises when I resize the browser. Upon resizing, the map reverts to its original size as it was when the page initially loaded. This is t ...

Python code for clicking a button using Selenium

I'm having trouble closing popup windows in Selenium with Python. The button labeled "joyride-close-tip" is functioning correctly and closes the window, but the other two buttons are not working. What could be causing this issue? Even though I copied ...

Display a Java applet when HTML text elements are clicked

I am attempting to create a custom jQuery plugin, but I don't have much experience with jQuery and could really use some assistance. Here is the Java Applet that I am referencing: The applet performs certain operations and displays the result in a t ...

My attempts to decrease the volume of an HTML5/CSS3 Audio element have been unsuccessful

Hey there! I'm currently working on my very first webpage and recently added an audio element that is functioning perfectly. The only issue I'm encountering is trying to position the element at the bottom of my page. I've already attempted ...

Changing the color of a tooltip for a specific element using Bootstrap 4

Is there a way to customize the tooltip background-color for icons with the classes "alert-danger" to red and "alert-success" to green using the following CSS commands: .alert-danger + .tooltip > .tooltip-inner { background-color: red !important; } ...

Avoid clicking in areas outside the perimeter of the blue circle in the SVG

Is there a way to restrict clicking outside the blue circle? The goal is to only allow opening by clicking on the svg itself, This includes everything, even white space inside the svg. How can this be achieved in the code? The current behavior is that ...

Position a Paper component in the center of a div

I'm having trouble centering my <Paper> element inside a div. Can anyone help me with this? I have placed both the <Paper> and <RaisedButton> elements inside the div and applied the following CSS: .timerArea { text-align: center; ...

Tab button that can be easily printed

I'm currently facing a challenge with my HTML code on my website. I have 5 tabs already set up, but I want to add one that redirects users to a printable page that opens the print menu specific to their browser. It seems like there might be an issue i ...

Show off a sleek slider within a Bootstrap dropdown menu

Is there a way to display a sleek slider within a Bootstrap dropdown element? The issue arises when the slider fails to function if the dropdown is not open from the start, and the prev/next buttons do not respond correctly. For reference, here is my curr ...

Python regular expressions: eliminate specific HTML elements and their inner content

If I have a section of text that includes the following: <p><span class=love><p>miracle</p>...</span></p><br>love</br> And I need to eliminate the part: <span class=love><p>miracle</p>.. ...

Having Trouble Styling Radio Buttons with CSS

Hello, I'm facing an issue with hiding the radio button and replacing it with an image. I was successful in doing this for one set of radio buttons, but the second set in another row is not working properly. Additionally, when a radio button from the ...

Creating beautiful prints with images

I have been tasked with developing an MVC C# application where the contents of a div are dynamically created using an AJAX call to fetch data from a database. Upon successful retrieval, the content is then displayed as a success message in JavaScript. Here ...

Floating and scrolling navigation bar not functioning properly in bootstrap framework

I've been dabbling in coding for a while now, practicing at my current job. I managed to create a floating and scrolling navigation bar on the right side of my website using bootstrap. However, after taking a 3-week break and returning to my site, I n ...

Tips to detect a specific animation completion on an element?

How can I ensure that a specific animation ends when multiple animations are triggered on an element? My scenario involves an overlay song list that appears when a list icon is clicked. The challenge lies in closing the menu smoothly. I have implemented a ...

Showcasing a checkbox with the chosen item marked as selected

My goal is to allow users to select hobbies from a checklist provided. The chosen hobbies should then be added to the user table. I want the checkboxes to display the user's current hobbies - checked if selected, unchecked if not. Users should be able ...

What are the steps to storing numerical inputs as variables using jQuery and then using them to perform calculations in order to update the input numbers

Currently, I am in the process of creating a BMI calculator using HTML and jQuery. <form id="bmiCalculator"> <fieldset> <legend>BMI Calculator:</legend> <p> <label>Height: ...

Dragging and dropping elements onto the screen is causing them to overlap when trying

I am having trouble merging the drag and drop functionality from Angular Material with resizing in a table. Currently, both features are conflicting and overlapping each other. What I am hoping for is to automatically cancel resizing once drag and drop s ...