Align the form and button HTML elements in the center of the page horizontally

I have been utilizing bootstrap for the styling of my page. However, I have successfully centered other elements vertically and horizontally, except for the input field and button.

The input field and button are currently being displayed on the left side of the page. I have experimented with various bootstrap class options, but unfortunately, I haven't been able to achieve the desired centering effect.

body {
                background-color: whitesmoke;
                height: 90%;
                display: flex;
                align-items: center;
            }

            html {
                height: 90%;
            }

            .row {
                text-align: center;
            }

            .random {
                text-decoration: none;
                margin-bottom: 50px;
            }

            .icon {
                max-width: 25%;
            }
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class="col random">
      <button class="btn btn-primary">
                    <a href="https://en.wikipedia.org/wiki/Special:Random" target="_blank">Click here for a random article</a>
                </button>
    </div>
  </div>
  <div class="row">
    <form class="col icon">
      <input type="text" class="form-control">
    </form>
    <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i></button>
  </div>
  <div class="row">
    <div class="col">
      Click icon to search
    </div>
  </div>
</div>

https://i.sstatic.net/e65Wb.png

Answer №1

body {
  background-color: whitesmoke;
  height: 90%;
  display: flex;
  align-items: center;
}

html {
  height: 90%;
}

.row {
  text-align: center;
}

.random {
  text-decoration: none;
  margin-bottom: 50px;
}

.icon {
  max-width: 25%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class="col random">
      <button class="btn btn-primary">
                    <a href="https://en.wikipedia.org/wiki/Special:Random" target="_blank">Click here for a surprise page!</a>
                </button>
    </div>
  </div>

    <form class="col icon">
    <div class="row">
      <input type="text" class="form-control">
    </div>
    <div class="row">
     <div class="col">
    <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i></button>  
     </div>
    </div>
  </form>
  
  
  <div class="row">
    <div class="col">
      Tap the icon to start searching
    </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

Alignment and spacing in a flexible direction

Hello, I am developing a bar app. <AppBar position="sticky" className={classes.appBar} > <Container maxWidth="lg"> <Toolbar className={classes.root}> <Typography noWrap> <img src={require("./node ...

Struggling to concentrate on the branding

I've been using materializecss for some time now and recently encountered an issue while trying to navigate my website using keyboard tabbing. The checkbox in materializecss is a custom CSS checkbox, but when I set the tabindex for the label of the c ...

Using CSS to Showcase the Art Gallery Page

This is my unique gallery display: https://i.stack.imgur.com/RddD8.png Here is the look I am going for https://i.stack.imgur.com/kuOye.png I want to showcase images in a slightly messy yet awesome way However, I encounter an issue when some images have ...

Items in a pair of distinct columns

I want to enhance a picture by adding three paragraphs on the left-hand side. Below is my HTML code: <md-toolbar layout-align="center center"> <h3>Traffic Light Component</h3> </md-toolbar> <md-grid-list md-cols ...

Tips for achieving a Google Map that smoothly slides behind a sticky header as you scroll

I recently added a sticky header using CSS to my long HTML page. At the bottom of the page, there is a <div> element with a Google Map embedded in it. As I scroll down the page, the content scrolls behind the sticky header as expected, but the Googl ...

Utilizing the Command Line/Window feature within Visual Studio Code

As a newcomer to Visual Studio Code, I'm currently using the latest Version: 1.29.1. When I used Matlab, I had access to a script window for writing code, a Command Window for testing code snippets and viewing variable values, as well as a workspace ...

Modal box fails to refresh content

Currently, I am in the process of learning how to modify my blog using a modal window. However, when I click on the edit button within the modal, an error message 'Failed to execute 'fetch' on 'Window': Invalid name' appears i ...

Conflicts in SwiperJS Timeline Management

Having a Timeline on my Website using SwiperJS presents challenges with conflicting functions. The goal is to navigate swiper-slides by clicking on timespans in the timeline. The desired functionality for the timeline includes: Sliding to the correspondi ...

An inexplicable right margin

I am facing an issue with centering a table (a tracklist) under the album cover and title. There seems to be an unexpected margin on the right side that I can't seem to figure out. Any help in identifying the cause of this issue would be greatly appre ...

move position when clicked-javascript animation

Is there a way to create a button that changes a div's position with a 2-second transition on the first click and then returns it back on the second click? I tried implementing this code, but unfortunately, it doesn't bring the div back. va ...

The Bootstrap select feature does not override the original dropdown menu; instead, it adds its own functionality alongside it

I have implemented a select combo using this library. After adding the necessary CSS and JS files, I encountered the following result. Here is what I have attempted so far: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.mi ...

What is the best way to include an arrow in a dropdown menu?

I've been working on restyling a Select box and I want to add a CSS arrow that rotates as the Select box expands. However, I'm struggling to align the arrow properly with the Select box. I've tried using code from the internet, but it hasn&a ...

Tips for modifying Capybara selectors on a website with css-modules

When writing our automated tests, a typical line of code we use looks like this: find('.edit-icon').click As we move towards incorporating css-modules in our project, I've been informed that class names could undergo significant changes. A ...

Changing the text color of an active button in Bootstrap

Hello everyone, I've been struggling to find a solution to my issue and I could really use some help. The problem I'm facing is changing the text color of the active button in Bootstrap. Specifically, after clicking on a button, I want it to tra ...

Persistent extra pixels found in CSS radiobutton group buttons that persist despite attempts to remove them

UPDATE: I have corrected the title of this post, as it was mistakenly taken from another post. For the past few months, I've been working on a sports app using React. However, I am facing a minor cosmetic problem with my radio buttons. Despite my eff ...

Unlawful CSS Selector

Wanting to utilize this particular CSS selector in conjunction with Selenium webdriver #coordinatonTable .odd:not(:has(.dataTables_empty)) Encountering an "An invalid or illegal string was specified" error. I've attempted the jquery selector test fr ...

Trouble displaying a cloned HTML element

I used jQuery to create a copy of a div and positioned it on the page, but for some reason, it's not displaying. When I checked using F12 tools, all the properties such as top, left, height, and width seemed to be set correctly. <div id="divLine" ...

Tips on incorporating toggle css classes on an element with a click event?

When working with Angular typescript instead of $scope, I am having trouble finding examples that don't involve $scope or JQuery. My goal is to create a clickable ellipsis that, when clicked, removes the overflow and text-overflow properties of a spec ...

Optimizing VueJS applications with browser caching features for faster loading

I've encountered an issue with my VueJS app. After running npm run build, a new set of dist/* files are generated. However, when I upload them to the server (replacing the old build) and try to access the page in the browser, it seems to be loading th ...

"Exploring the process of implementing a fixed method POST in Angular 5

When developing an application for Portal, I encountered an issue where a newly created role is not displayed without refreshing the browser. How can I ensure that the added element is directly displayed in the table without needing to refresh the browser? ...