Ways to use flexbox to position an image beside an input field in the center

I'm struggling to center an image next to an input field. Even when I try using vertical-align: bottom;, the image doesn't end up centered where I want it to be.

I attempted to use position:relative and adjust my image using top, bottom, etc., but I'd prefer to accomplish this using flexbox.

How can I achieve this layout and what changes should I make to the divs?

Here is the layout I'm aiming for.

Check out my Jsfiddle for reference.

HTML

<div class="container">
    <h1>Inputs</h1>
    <p class="spacing">multiple inputs...</p>
    <div class="searchinput">
        <input type="text" name="search" id="google-input" placeholder="Google search...">
        <img src="logos/google.png" alt="youtube" class="logos">
    </div>
</div>

CSS

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.logos{
    width: 90px;
    vertical-align: bottom;
}

.container {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 90vh;  
}

.searchinput {
    margin-top: 20px;
}


input {
    padding: 20px;
    height: 30px;
    background-color: transparent;
    border: 2px solid black;
    border-radius: 5px;
    color: black;
    font-size: 20px;
    vertical-align: bottom;
}

Answer №1

It is important to ensure that the immediate parent of the elements you want to apply flex properties to has the display:flex property. In your scenario, this would be the .searchinput class. Therefore, your CSS for .searchinput should look like this:

.searchinput {
    margin-top: 20px;
    display:flex;
    align-items: center;
}

Below is a snippet of the complete code:

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.logos{
    width: 90px;
    vertical-align: bottom;
}

.container {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: 90vh;  
}

.searchinput {
    margin-top: 20px;
    display:flex;
    align-items: center;
}


input {
    padding: 20px;
    height: 30px;
    background-color: transparent;
    border: 2px solid black;
    border-radius: 5px;
    color: black;
    font-size: 20px;
  vertical-align: bottom;
}
<div class="container">
    <h1>Inputs</h1>
    <p class="spacing">multiple inputs...</p>
    <div class="searchinput">
        <input type="text" name="search" id="google-input" placeholder="Google search...">
        <img src="https://i.imgur.com/dpkbGqu.png" alt="youtube" class="logos">
    </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

Page Refresh Causes Angular Count to Reset to Zero

I'm facing an issue in my code where the counts for total and package get reset to 0 when the page is refreshed, even though labels have been scanned and added to the list. Below is a snippet of my code. Any suggestions on how to resolve this problem? ...

Chrome causing footer problem in Prestashop theme

My footer is not displaying properly on Chrome, but it looks fine on other browsers. I am using a paid theme on my PrestaShop installation, and the support team claims it's an issue with a module, but I'm not convinced. You can visit the website ...

Tips for removing two specific "th" elements from a table that is generated dynamically

I have a challenge where I need to remove 2 specific elements from a table depending on which delete button is clicked Check out the code snippet below : <!--HTML--> <button type="button" class="collapsible">Manage Formulas</button> < ...

Challenging CSS Design

Currently, I am facing a challenging task of creating a website with a layout that includes four corner images - TL, TR, BL, and BR marked by black blocks. The main content area, highlighted in dark orange (up to 960px wide), is surrounded by the green arr ...

The text box isn't displaying the value, even though it was functioning properly just two days back

<html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript"> function insertParamIntoField(anchor, param, field) { ...

Body Zoom Browser

My website features buttons for zooming in and out, labeled as A + and A-. I wanted to make sure that the entire body of the website could be magnified easily. That's why I came up with this code: <html> <body> <style> .cont ...

I am looking to modify the highlighted table cell whenever the value within it changes

I am currently working on a small project related to the Stock Market. In this project, I need to dynamically change the style of the td element based on data fluctuations - green highlight for an increase and red highlight for a decrease. In the provid ...

What is the best way to retrieve the updated window dimensions following a rotation of an iPad screen?

I need to update the size of a specific element on a webpage based on screen rotation, especially for iPads and all other mobile devices. The new dimensions should be calculated according to the updated screen size. While attempting to implement this, I e ...

Utilizing AngularJS $anchorScroll for navigating to an anchor tag on a different page

On my AngularJS Home Page, I am trying to scroll to an anchor tag on another page. Both pages are loaded as partial html files into the ng-view component based on the URL. When I start the server, the home page loads and then I need to navigate to the FAQ ...

What are some ways to bypass a closed Google form that is no longer accepting responses? I need to find a way to submit my form even after the deadline

Is there a way to trick a closed Google form that says it is "no longer accepting responses"? I'm looking to submit a form past the deadline. Is there a method to access and submit a form that has already been closed? The code for the closed form appe ...

Can you help me modify the navbar color in Bootstrap 4 based on the screen width?

For example, if I use the navbar-dark and bg-dark classes in a nav tag, I want to switch them to navbar-light and bg-light when the screen width is changed to approximately 600px (using Bootstrap 4). ...

Are there methods to individually style components that have been generated through the .map() function?

My goal is to style each <Tuner /> component separately, which are being rendered for each item in the this.state.notes array using this.state.notes.map(). I want to position them individually on the page, but currently they all appear together. This ...

What strategies can I use to enhance the efficiency of my code using AngularJS?

After conducting extensive research, I am still unable to find a solution or comprehend the code written by someone else - it simply does not work. As a result, my codebase has grown significantly. I am interested in optimizing my code using AngularJS func ...

Choose the option for overseeing safaris

Hello there! I need some help with Safari. Can you please guide me on how to disable the arrows? https://i.stack.imgur.com/1gzat.png ...

Using ngModel with a dropdown list

I am facing an issue when trying to retrieve a value from a select element. <select ([ngModel])="office_hour_start" name="office_hour_start"> <option *ngFor="let time of times" value="{{time.i}}"> {{ time.i }} </option> ...

Ensure that columns are neatly arranged horizontally when they do not all fit in one row

I currently have a row with 6 columns, but when they can't fit next to each other on the screen, 2 of them get pushed below, resulting in a messy layout. I could use a media query to solve this issue, however, I am curious if there is a better way to ...

How to Emphasize Html Select Element on Mobile Devices?

Is there a way to make the HTML select element on Android appear more distinguishable as a dropdown list, rather than just looking like plain text? Any suggestions for highlighting it so users can easily identify and select an option from this element? Up ...

Temporary redirection of external links page

There is a specific feature I am trying to implement on my website, but I am having trouble figuring out how to make it work seamlessly. When a user connects to my website and starts navigating through the menus, I want them to be redirected to a specific ...

Create a jQuery dropdown menu within the same division element

I need help displaying a drop-down menu on mouseover using only one div. Currently, I am employing 2 divs and sliding up another div to show the submenu; however, I want to achieve this with just 1 div. Is there a way to do that? Below is my code snippet: ...

using `clear: both` does not stop text from wrapping

Currently, I am facing an issue with positioning 3 divs in my layout. Two of the divs are floating to the left and right respectively, while the third one should be placed under the two others. I tried using clear: both but it doesn't seem to work as ...