Guide on positioning an SVG button within a form

I am attempting to design a form with two input fields and a button. Rather than using a plain text button, I prefer to utilize an SVG graphic for the button; however, it is not aligning correctly.

Consider the following HTML and CSS:

body {
  background: #222;
  margin: 25px;
}

form {
  border: 1px solid #444;
  padding: 2px;
}

input {
  border: 1px solid transparent;
  background: #444;
  color: #ddd;
  margin-left: 6px;
  padding: 3px 5px;
}

input:focus {
  outline: 0;
  border: 1px solid #666;
}

button {
  border: 0;
  padding: 0;
  background: transparent;
  margin-left: 6px;
  height: 23px;
  width: 23px;
  fill: #bbb;
}

button:focus {
  outline: 0;
  fill: #ddd;
}
<form>
  <input type="email" id="email" />
  <input type="password" id="password" />
  <button type="submit">
   <svg viewBox="0 0 1024 1024">
    <path d="M384 512h-320v-128h320v-128l192 192-192 192zM1024 0v832l-384 192v-192h-384v-256h64v192h320v-576l256-128h-576v256h-64v-320z" />
   </svg>   
 </button>
</form>

Upon running the snippet, the input fields are aligned at the bottom while the SVG-button is aligned at the top. When I substitute the SVG with text, the alignment issue is resolved.

Answer №1

To position the button alongside the input elements, we apply vertical-align: middle to all elements within the form.

body {
  background: #222;
  margin: 25px;
}

form {
  border: 1px solid #444;
  padding: 2px;
}

form * {
  vertical-align: middle;
}

input {
  border: 1px solid transparent;
  background: #444;
  color: #ddd;
  margin-left: 6px;
  padding: 3px 5px;
}

input:focus {
  outline: 0;
  border: 1px solid #666;
}

button {
  border: 0;
  display: inline-block;
  padding: 0;
  background: transparent;
  margin-left: 6px;
  height: 23px;
  width: 23px;
  fill: #bbb;
}

button:focus {
  outline: 0;
  fill: #ddd;
}
<form>
  <input type="email" id="email" />
  <input type="password" id="password" />
  <button type="submit">
   <svg viewBox="0 0 1024 1024">
    <path d="M384 512h-320v-128h320v-128l192 192-192 192zM1024 0v832l-384 192v-192h-384v-256h64v192h320v-576l256-128h-576v256h-64v-320z" />
   </svg>   
 </button>
</form>

Answer №2

Add the following style rule to your selector: vertical-align: middle;

body {
  background: #222;
  margin: 25px;
}

form {
  border: 1px solid #444;
  padding: 2px;
}

input {
  border: 1px solid transparent;
  background: #444;
  color: #ddd;
  margin-left: 6px;
  padding: 3px 5px;
}

input:focus {
  outline: 0;
  border: 1px solid #666;
}

button {
  border: 0;
  padding: 0;
  background: transparent;
  margin-left: 6px;
  height: 23px;
  width: 23px;
  fill: #bbb;
  /*Apply this line to vertically align the element*/
  vertical-align: middle;
}
svg{margin:auto;}
button:focus {
  outline: 0;
  fill: #ddd;
}
<form>
  <input type="email" id="email" />
  <input type="password" id="password" />
  <button type="submit">
   <svg viewBox="0 0 1024 1024">
    <path d="M384 512h-320v-128h320v-128l192 192-192 192zM1024 0v832l-384 192v-192h-384v-256h64v192h320v-576l256-128h-576v256h-64v-320z" />
   </svg>   
 </button>
</form>

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

Switching hover behavior on dropdown menu for mobile and desktop devices

I have implemented a basic JavaScript function that dynamically changes HTML content based on the width of the browser window. When in mobile view, it removes the attribute data-hover, and when in desktop view, it adds the attribute back. The functionalit ...

Ways to avoid the cascading of CSS styles onto multiple Three.JS scenes

It seems like I might have to take the longer route to solve this issue, but let's give it a shot... I'm facing a challenge when applying CSS to control the opacity of a specific HTML element that acts as a container for a Three.JS scene. In thi ...

What is the method for configuring automatic text color in CKEditor?

https://i.sstatic.net/yEM3p.png Is there a way to change the default text color of CKEditor from #333333 to #000000? I have attempted to modify the contents.css in the plugin folder: body { /* Font */ font-family: sans-serif, Arial, Verdana, "Tr ...

Leveraging CamanJS for canvas filtering

These are my HTML elements: <div class="wrapper"> <nav class="navbar navbar-transparent"> <div class="container-fluid"> <div class="navbar-header"> <span class="btn btn-white btn-file"> ...

Executing jQuery post request on a div loaded via ajax

I'm facing a challenge with my webpage. I have a section where the content of a div is loaded via ajax. This div contains forms, and after submission, it should update to display the new content without refreshing the entire page. Can anyone guide me ...

Organize the HTML output generated by a PHP array

There must be a simple solution to this, but for some reason, it's escaping me right now. I've created custom HTML/CSS/JS for a slider that fetches its content from an array structured like this: $slides = [ [ 'img' = ...

Creating a dynamic HTML table by dynamically populating a column

I have limited experience with javascript, and I am working on a personal project that I enjoy. In the table below, you will find various location names along with their distances from the initial location. This serves as a travel companion for me to refe ...

I'm looking to divide the background horizontally into two sections, with one side being a solid black color and the other side incorporating a gradient of two colors, such as purple transitioning into pink

body { height:100%; background: linear-gradient(top, #d808a4 50%, black 50%); background: linear-gradient(top, #d808a4 50%,black 50%); background: linear-gradient(to bottom, #d808a4 50%,black 50%); height: 229vh; } I am trying to creat ...

How can we stop the jumping of images in the grid? Is there a way to eliminate the jump effect?

Here is a script I am working with: <script src="http://static.tumblr.com/mviqmwg/XyYn59y3a/jquery.photoset-grid.min.js"></script> <script> $(document).ready(function() { $('.photoset-grid').photose ...

Has the zip creation tool in the Google Doodle on April 24th been coded entirely in JavaScript?

Was the Gideon Sundback Google doodle created using JavaScript? I attempted to follow along in Firebug, but I couldn't quite grasp its implementation details. Any ideas on how it was possibly implemented? Any insights on the potential techniques use ...

Perform a jQuery computation using a CSS style

Having trouble with a calculation using a CSS property that is not returning any value. Seems like the issue might be due to the CSS property returning '200px' instead of just '200'. Any suggestions for a workaround? Thanks in advance. ...

IItemTransform and minified files that are already in use

Summary: IItemTransform is not triggered when a minified file already exists in the same directory as the original (non-minified) file. Issue Description The problem arises due to CSS relative image references, impacting both JavaScript and CSS files when ...

What is the correct way to incorporate scrollIntoView() using jQuery?

I'm trying to implement a jQuery function that will enable the last reply to scroll into view. This is my current code: function displayLastReply(replies){ replies.each(function() { if($(this).index() < nIniRep || afterReply){ $( ...

Why is it that when I refresh the page in Angular, my logged-in user is not being recognized? What could be causing this

I am developing a Single Page Application using the combination of Angular JS and Node JS. In my HTML file, I have defined two divs and based on certain conditions, I want to display one of them using ng-if. However, I noticed that the model value used in ...

What's preventing the buttons from shifting positions?

Having some trouble with my Minesweeper Web Game setup. My buttons are stuck in place and won't budge. Can someone help me figure out what's wrong? Here's the code snippet I've been working on (FYI, using NetBeans 8.1 IDE) HTML: <! ...

What is the best way to set the width of an inner element as a percentage of the Body's width?

Applying a percentage width to any inner element of a page will typically result in that element taking a percentage of its parent container's width. Is there a way to specify the width of an inner element as a percentage of the overall Body width, r ...

Eliminating the standard padding on a Vuetify v-app-bar

When using Vuetify's v-app-bar, there are default css classes named v-toolbar__content and v-toolbar__extension that apply padding of 16px on the x-axis and 4px on the y-axis, which I aim to eliminate. I attempted to override these classes in my CSS ...

Transition one background image into another using background positioning

Snippet of code: https://jsfiddle.net/foy4m43j/ HTML: <div id="background"></div> CSS: #background { background-image: url("http://i.imgur.com/hH9IWA0.png"); background-position: 0 0; background-repeat: repea ...

Implement a hover animation for the "sign up" button using React

How can I add an on hover animation to the "sign up" button? I've been looking everywhere for a solution but haven't found anything yet. <div onClick={() => toggleRegister("login")}>Sign In</div> ...

"Customizing the properties of an Angular Material mat-slide-toggle: A step-by-step

<mat-slide-toggle>Slide Me!</mat-slide-toggle> https://i.stack.imgur.com/p1hzD.png https://i.stack.imgur.com/aCzs1.png Is it possible to customize the toggle-thumb-icon to increase its length and position it at the end of the bar? ...