What is the best way to ensure an input field and a button are aligned perfectly within the same div tag and of equal height?

During a recent HTML-CSS project, I encountered an issue where I struggled to ensure that two elements within a div tag were the same height. The elements in question were an input field and a button with an icon. Here is a snippet of the relevant HTML code:

.wrapper {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 75vh;
  display: grid;
  grid-template-columns: 1fr 1fr;
  column-gap: 30px;
}

... (CSS styling continued)
<label class="team_form_label" for="team_players">
     Enter the name of the players:
 </label>
 <div class="names_input_feature">
     <input type="text" id="team_players" class="team_form_input" name="team_players" placeholder="Must match the total"/>
     <button id="name_submit_btn">
         <i class="fa-solid fa-plus" style="color: #000000"></i>
     </button>
 </div>

The desired outcome was for the input field and button to have equal heights, as illustrated in this screenshot: enter image description here

Despite my efforts using flex-box properties and researching online resources, I failed to achieve the intended result.

If you have any suggestions or solutions, please feel free to share. Your help is greatly appreciated!

Answer №1

If you want to ensure that input and label elements are the same height, just add this simple CSS:

label, div {display: flex;}

label, div {
  display: flex;
  gap: 5px;
}
<label class="team_form_label" for="team_players">
     Enter the name of the players:
 </label>
 <div class="names_input_feature">
     <input type="text" 
     id="team_players" 
     class="team_form_input"
      name="team_players" 
      placeholder="Must match the total"/>
     <button id="name_submit_btn">
         <i class="fa-solid fa-plus" style="color: #000000">+</i>
     </button>
 </div>

Answer №2

Is this what you're looking for?

.wrapper {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 75vh;
  display: grid;
  grid-template-columns: 1fr 1fr;
  column-gap: 30px;
}

.form_section {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border: 2px solid rgb(255, 255, 255);
  border-radius: 4px;
  padding: 20px 20px;
  margin-top: 70px;
  margin-bottom: 20px;
  min-width: 400px;
}

#team_form {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: start;
}

.team_form_label {
  color: white;
  font-size: 26px;
}

input[type="number"],
input[type="text"],
select {
  padding: 10px 15px;
  margin-bottom: 40px;
  box-sizing: border-box;
  border: 2px solid rgb(255, 0, 0);
  border-radius: 4px;
  background-color: rgb(255, 255, 255);
  color: black;
  font-size: 16px;
  width: 100%;
  height: auto;
  
}

.names_input_feature {
  width: 100%;
  display: flex;
  /*Trouble with CSS?*/
  align-items: flex-start;
  align-content: center;
}

/*Player names add button*/
#name_submit_btn {
  padding: 10px 20px;
  margin-left: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: none;
  border-radius: 4px;
  color: black;
  font-size: 22px;
  cursor: pointer;
  transition: 0.4s ease;
}

#name_submit_btn:hover {
  color: #fff;
  background-color: #3da736;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<label class="team_form_label" for="team_players">
     Enter the name of the players:
 </label>
 <div class="names_input_feature">
     <input type="text" id="team_players" class="team_form_input" name="team_players"     placeholder="Must match the total"/>
     <button id="name_submit_btn">
         <i class="fa-solid fa-plus" style="color: #000000"></i>
     </button>
 </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

Tips for using a button to update data without triggering a postback

Within the GridView in my ASP.net project, I have an ASP.net button with the following code: <asp:Button UseSubmitBehavior="false" runat="server" ID="btnShow" CssClass="btnSearch" Text="View All" CommandName="ViewAll" OnCommand="btnShow_Command" Comman ...

What is the best way to centrally position a <mat-card> on the screen using Angular 9?

* I'm struggling to center a <mat-card> on the screen, specifically within the toggle-device-toolbar. It's intended for mobile phones and I could use some guidance. I'm working with angular-material and ng-bootstrap* Here is the code ...

Creating an event within a class that is generated dynamically when hovering

I'm trying to create a hover effect where an image moves around the page in a pattern (top left corner -> top right corner -> bottom right corner -> bottom left corner -> then back up to the top left corner). To achieve this, I am adding ...

Correct the string based on a character error

When I have text to display in HTML, for example: var htmlStr = "1.first thing %10-15%. 2.second thing %25-44%. 3.third" And I want to display it in a div: $('#div1').html(htmlStr); However, when I display it in a DIV1 in HTML5 on a mobile pho ...

I'm still trying to figure out the property position. Why won't my page scroll? (My first time using HTML)

I'm having an issue with my webpage where it doesn't scroll even when the content exceeds the page length. I've tried adjusting the position settings for the body, html, and div elements but haven't seen any changes. Here is the HTML ...

Tips for displaying images dynamically in HTML using AngularJS

src={{logo}} var logo = 'localhost:3000/modules/images/default.png' I'm trying to display the image path dynamically, but it's not showing up in the HTML. Do I need to use quotes for src? Can someone please assist me with this issue? ...

The comparison between createElement() and AJAX response

Currently, my website has a login form that updates the page with user data through AJAX after successful login. To maintain semantic integrity, I use JavaScript to remove the login form from the page once the user is logged in. However, when the user lo ...

How do I directly display a variable in index.html using node.js?

Is there a way to retrieve user inputs from the index.html file, process them in Node.js, and then display the result back on the index.html page instead of redirecting to a new page? Form Handling with Express var express = require('express'); ...

An easy way to incorporate a fade-in effect for every word in a text

I am trying to make the text "Eat. Sleep. Repeat." slide up and fade in one word at a time. I have experimented with various methods like anime.js, keyframes, and adding css classes, but so far none of them have worked for me. Here is the code I currently ...

Encountering issues with updating state object in setState function

Review the code snippet below: {split.participants.map(friend => { return <div key={Math.random()} className="form-check my-2 d-flex align-items-center justify-content-between"> <div ...

CSS: Preserve HTML elements and only conceal the text within an <a> tag

Inside an "a" element, there's an image followed by text as a link. I'm looking to only hide the text of the link without affecting the display of the image. <a href="https://www.example.com/page"> <img src=" ...

The functionality to deselect multiple options in a select box is not functioning properly

There seems to be an issue with removing the selected attribute from the selected values in a jQuery multiselect box. The console is not showing any errors. You can view a working example here The problem lies in this code snippet: $("#mltyslct option ...

Identify the name in the list by highlighting it when its content matches the text in a specific div id

I have an unordered list structured like this: <ul> <li id="projectrow_364"> <span>Forest</span> </li> <li id="projectrow_365"> <span>Life</span> ...

The integration of CSS into Laravel was unsuccessful

Below is the content of my index file located in views.admin: <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet&qu ...

What is the actual storage mechanism for HTML5 WebStorage data?

While utilizing the HTML5 WebStorage functionality, I am aware that certain browsers such as Chrome offer developer tools for users to navigate through their WebStorage contents for debugging and troubleshooting purposes. I am curious if it is feasible to ...

How can you capture the VIRTUAL keyCode from a form input?

// Binding the keydown event to all input fields of type text within a form. $("form input[type=text]").keydown(function (e) { // Reference to keyCodes... var key = e.which || e.keyCode; // Only allowing numbers, backspace, and tab if((key >= 48 && ke ...

Certain CSS styles for components are missing from the current build

After building my Vue/Nuxt app, I noticed that certain component styles are not being applied. In the DEVELOPMENT environment, the styles appear as expected. However, once deployed, they seem to disappear. All other component styles render properly. Dev ...

Is the order of elements in a CSS file important?

In my situation, I am dealing with a nested list and enclosing div that was created by Drupal's menu system, so I am unable to modify it. My goal is to replicate the menu from a hardcoded website (link removed). How can I include the sub-items (ul l ...

What is the method to switch between radio buttons on a webpage?

Here is an example of HTML code: <input type="radio" name="rad" id="Radio0" checked="checked" /> <input type="radio" name="rad" id="Radio1" /> <input type="radio" name="rad" id="Radio2" /> <input type="radio" name="rad" id="Radio4" /& ...

What is the best way to retrieve a variable from a .php file that is included in a different .php file?

My question is how can I retrieve a value entered in a text field (with the field name = usn) in my_file.php file, from another file called demo.php without re-entering it every time? I have multiple files and I want to access this value without using tex ...