What is the best way to use CSS to align an HTML form input box and buttons to the center?

Looking to replicate the design of the Google homepage on a certain HTML page. Struggling with centering the search bar and buttons using CSS.

div.search {
  display: block;
  margin-left: auto;
  margin-right: auto;
}
<div class="search">
  <form action="https://google.com/search">
    <br><input type="text" name="q"></br>
    <input type="submit" value="Google Search">
    <input type="submit" value="I'm Feeling Lucky">
  </form>
</div>

Struggling with getting the search box and buttons to be centered correctly.

Answer №1

You're attempting to center internal elements using margin, but you are applying the styles to their parent element.

If you wish to center internal elements using margin, you must apply it directly to those elements, not the parent.

Here's what you can do:

  1. Add margin: 0 auto; to all your input fields (to center them).
  2. Change the display style of the search bar to display: block (so it takes up the entire line)
  3. Set the submit buttons' display property to display: inline-block; (so they appear inline)
  4. Delete the unnecessary <br> tags around your search input field.

Updated Code:

div.search{
  width: fit-content;
}

input[type="text"]{
  display: block;
}

input[type="submit"]{
  display: inline-block;
}

input{
  margin: 0 auto;
}
<div class="search">
  <form action="https://google.com/search">
    <input type="text" name="q">
    <input type="submit" value="Google Search">
    <input type="submit" value="I'm Feeling Lucky">
  </form>
</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

Utilizing SVG elements for interactive tooltips

Can the < use > element show the rect tooltip on mouseover in modern browsers? Refer to 15.2.1 The hint element. <svg id="schematic" version="1.2" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <symbol i ...

Switch between hiding and showing the DIV element flaw

I am currently working on a piece of code that involves simple HTML and JS to show/hide elements. Here is the code snippet: // Enabling forEach for 'querySelectorAll' and 'getElementsByClassName' HTMLCollection.prototype.forEach = No ...

Can someone explain the purpose of `svg:not(:root) {overflow: hidden}` in simple

I am facing an issue with displaying my .svg file in my next-js project. It seems that the CSS code svg:not(:root) { overflow: hidden; } is causing the problem. Could someone explain what this particular CSS rule is for? After changing the code to svg:no ...

How can you change the width of <td> and <th> elements?

I am trying to keep a table header fixed at the top of a window, but for some reason, setting the widths of the <td> and <th> elements as shown below is not resulting in them having the same width (the column headers are misaligned with other c ...

Unable to specify the width of my website using full-width in Bootstrap

Using the bootstrap grid system has caused a slight issue for me. Whenever I scroll to the right side of the window, there is a white space that appears: https://i.sstatic.net/YHj92.png I have set the body width as 100vw; Is there a way to eliminate this ...

Issues with CSS columns in Firefox version 22 have been causing some problems

Trying out css columns: implementing columned content with a wrapper div: Click here to view the demo Everything seems to work fine in webkit and older versions, but in Firefox v22 there seems to be an issue (no columns displayed and behavior varies with ...

Is there a way to apply border-radius to the header of a table in Bootstrap 5?

My use of Bootstrap 5 for styling a table has encountered an issue. Even with the border-radius set on the table-dark class, the thead element's border does not change. I am looking for a way to address this problem. Any suggestions? .table-dark { ...

Mysterious obsidian container lurking in the background of the video on Chrome version 67.0.3396

Displayed on the page is an HTML Video tag, which streams a video from the speaker (WebRTC). <div id="remoteVideoContainer"> <video id="remotevideo" autoplay="autoplay" controls="" loop="loop" preload="true" height="500" width="100%"> ...

Properly aligning sub-divs within a parent div using CSS

I'm facing a problem with my CSS styling that I need help with. Here is the screenshot of the issue I am trying to center align the inputs, each placed in its own div, within my form. Below is the HTML markup for the form: HTML Markup Code CSS st ...

triggering a button click event in HTML

Below is the creation of a div: <div class="area1"> </div> By using AJAX, I was able to alter the HTML for the aforementioned div with this call: $('#abutton').click(function(e) { e.preventDefault(); ...

Activating development mode for LessCSS in Node.js

I'm encountering some major difficulties identifying LESS parse errors within a large CSS compilation. The error messages aren't providing much help (at least not for me). Here are my questions: 1. Is there a method to activate more debugging ...

The align-middle Bootstrap class does not seem to be working within the specified div

I want to vertically align text (<span class="align-middle">middle</span>) in the center of a div. Here is the code snippet: <div class="col-4 shadow product-added"> <div class="row"> <div cla ...

Angular 9: The instantiation of cyclic dependencies is not allowed

After transitioning from Angular 8 to Angular 9, I encountered an issue with a previously functioning HTTP communication service. The error message now reads: Error: Cannot instantiate cyclic dependency! HttpService at throwCyclicDependencyError (core ...

What is the best way to evenly space out divs filled with images in a horizontal layout?

On my website, the header consists of text and a logo. Since the font used is not standard, the text is displayed as an image. I am looking to create a design in which the elements of the site adjust according to the size of the browser window. Is this wh ...

Creating a dynamic visual effect with image overlap in the Sencha Touch carousel

I've created a carousel that loads multiple images, but I'm experiencing an issue where each image is overlapping with another image from a different card. Here's a visual of the problem: As shown in the screenshot, parts of one image are a ...

How can I implement a feature in Django where clicking on a form will automatically clear the initial text within it?

One common feature seen on websites is the username form box that initially displays "username" as a placeholder text. Upon clicking on the box, the word "username" disappears to allow users to input their own username. Appreciate it! ...

How can we use jQuery to target an element based on the text content of the one preceding it?

For example: <div id="input-content" contenteditable="true"> <p>Some text<p> <p>Some text<p> <p><br></p> <p>Some text<p> <!-- I would like to target the p tag following the p tag with br --> ...

The Zoom CSS property seems to be experiencing some issues on the Firefox browser

After researching the issue, I discovered several solutions involving css3 transition techniques. Currently, I have been using {zoom:1.5} for all of my buttons. However, this method does not work on Firefox. When attempting to implement the transition pro ...

The implementation of classList.toggle in my JavaScript code is not functioning as expected

I created a button that toggles the visibility of a div. It works fine the first couple of times, but then it stops working. When I click on column1, the hidden div inside column1 doesn't appear. But when I click on column2, the hidden div inside colu ...

Determine the image's position in relation to its parent element while factoring in any vertical offset

Within my HTML, I have arranged two images to sit adjacent to one another. Interestingly, one image happens to be taller than the other. Despite assigning a CSS property of vertical-align: middle to both images, the result is that the shorter image appears ...