Tips for Aligning Form Elements in the Middle using Bootstrap

What could be the issue?
I can clearly see the container border, indicating the space available for my work. However, when I insert the row (div) and offset (div), the content gets left-justified.

<div id="page" class="container" style="border:solid">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="form-group">
                <label for="firstName">First Name:</label>
                <input type="text" id="firstName" class="form-control" />
            </div>
            <div class="form-group">
                <label for="lastName">Last Name:</label>
                <input type="text" id="lastName" class="form-control" />
            </div>
        </div>
    </div>
</div> 

Answer №1

It seems like I understand what you're asking, but I'm a bit unsure. Are you referring to something like this?

If that's the case, the class you're looking for is:

class="text-center"

You can update your code as follows:

<div id="page" class="container" style="border:solid">
<div class="row">
    <div class="col-md-6 col-md-offset-3 text-center">
        <div class="form-group">
            <label for="firstName">First Name:</label>
            <input id="firstName" class="form-control text-center" type="text">
        </div>
        <div class="form-group">
            <label for="lastName">Last Name:</label>
            <input id="lastName" class="form-control text-center" type="text">
        </div>
    </div>
</div>

You also have the option to apply it only to specific controls, such as the input form controls.

I hope this information proves helpful!

Answer №2

While Bootstrap is known for its mobile-friendly design, it can also enhance the user experience on desktops with its scaffolding capabilities. When creating forms, my focus is on optimizing space usage. Building upon Demonic Penguin's code and adding my own touch, here is an improved version:

<div id="page" class="container" style="border:solid">
  <div class="row" style="padding-top: 12px;"></div>
  <div class="row">
    <div class="col-md-6 col-md-offset-3 text-center">
      <div class="form-group">
        <input id="firstName" class="form-control text-center" placeholder="First Name" type="text">
      </div>
      <div class="form-group">
        <input id="lastName" class="form-control text-center" placeholder="Last Name" type="text">
      </div>
    </div>
  </div>
</div>

Answer №3

To enhance the appearance of your form, consider incorporating icons into the fields. Here is an example:

<div id="page" class="container" style="border:solid">
  <div class="row" style="padding-top: 12px;"></div>
  <div class="row">
    <div class="col-md-6 col-md-offset-3 text-center">
      <div class="form-group">
        <div class="input-group">
          <span class="input-group-addon" id="firstName-addon"><i class="fa fa-user"></i></span>
          <input id="firstName" class="form-control text-center" placeholder="First Name" type="text">
        </div>
      </div>
      <div class="form-group">
        <div class="input-group">
          <span class="input-group-addon" id="lastName-addon"><i class="fa fa-user"></i></span>
          <input id="lastName" class="form-control text-center" placeholder="Last Name" type="text">
        </div>
      </div>
    </div>
  </div>
</div>

If you choose to implement this feature, remember to include a reference link to FontAwesome CSS on your page(s). You can obtain the link from here:

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

Prevent the overlap of the float right span with text using CSS

I'm currently using rickshaw and have a graph hover/annotation where a color swatch (defined with a <span>) is overlapping text within the same <div>. Check out my HTML code below: <div class="detail" style="left:250px"> <d ...

Utilize an AngularJS controller to verify the presence of a cookie and display a modal pop-up

In the process of developing a Single Page Application (SPA), I have encountered an issue with an HTML element calling an AngularJS controller. Here is what I need: I want the controller to check for a specific cookie: - If the cookie exists, call a ...

CSS overflow property does not take effect when set to hidden

I'm having trouble understanding why the overflow: hidden property is being overridden by a descendant element. Take a look at this example I've put together http://jsfiddle.net/2fRxc/. HTML <div class="relative"> <div class="overf ...

What is the best way to shade the background when a hyperlink is pressed?

I am currently working on customizing the navigation bar for my website. My navbar consists of five elements, and I want to make an element appear darker when it is clicked. Here's what I have attempted so far: Utilizing jQuery to modify the ac ...

Fixed Positioning Div to Stay at the Top while Scrolling

Currently, I have successfully implemented the functionality to stick the div to the top once it scrolls down by 320px. However, I am curious if there is an alternative approach to achieving this effect. Below is the code snippet I am using: jQuery(functi ...

Issues arise when using the "placeholder" attribute within a <p:inputText tag while the inputText is devoid of any value

A current project involves implementing JSF Mojarra 2.3.9.SP02 alongside PrimeFaces 7.0 on Wildfly 17 using the Sapphire template provided by PrimeFaces. However, I've encountered a significant issue with a specific <p:inputText element within my f ...

Ensure that the HTML image and text are positioned side by side, with the text appearing alongside the picture rather than below

Looking for a way to keep the text aligned to the right of an image, regardless of length? Check out this exercise: https://www.example.com/css/exercise-1 Is there a method to maintain the text layout next to an image while accommodating varying amounts o ...

Navigate to the line situated at the exact midpoint vertically

I have a div with child elements, each containing a line of text. <div id="aboutxt"> <div class="line">TEXT_LINE_ONE</div> <div class="line">TEXT_LINE_TWO</div> <div class="line">TEXT_LINE_THREE</div> ...

Tips for accessing the @keyframes selector and extracting the value from it

In my CSS code, I have a shape element with an animation that spins infinitely for 50 seconds. #shape { -webkit-animation: spin 50s infinite linear; } @-webkit-keyframes spin { 0% { transform: rotateY(0); } 100% { transform: rotateY(-360deg ...

Personalize the way UIkit tooltips appear

I've been working on customizing a uikit tooltip for my checkbox. I managed to change the font and background color, but for some reason, I can't adjust the font size. I even tried adding a custom class without success. Do you think it's pos ...

jQuery: Select the parent element of a focused form input and apply styling

Recently, I encountered a situation where I have a form containing a DIV along with 3 INPUTS, each enclosed within a LABEL element. My goal is to alter the background image of the DIV whenever an INPUT receives focus. Due to limitations in navigating up t ...

Horizontal Screen Sharing on iPad

Currently, I have a two-column website set up using Wordpress. However, I am facing an issue with the right side column scrolling over on iPads. While the page behaves properly on desktops, on iOS devices, the div is able to scroll over the navigation ba ...

Display or conceal a div based on the size of the screen using HTML and CSS

Hey there, I recently finished my first React Project and I’m wondering if there’s a way to hide the 'side-menu' section on mobile screens using CSS. Any suggestions? <div className='side-menu'> <SiderComponent /> < ...

Issue with CSS transition bug in Offcanvas on Android version 4.1.x

I am attempting to develop an offcanvas menu similar to the one featured in the Google Plus app. Currently, my code functions properly on most devices (android/ios) and browsers (ff, chrome, IE8+). The only issue I am encountering is on a Samsung Galaxy ...

Place form at the center of the Bootstrap Modal

My question is, how can I center the entire form, including the input fields and labels, within the modal? Here is also a helpful Fiddle: http://example.com, although the snippet provided should work as well. <script src="https://example.com/bootstr ...

Display or conceal div elements using JavaScript

Is there a way to hide certain divs when clicking on a specific div box? Clicking on the box will trigger the hiding of some application divs. See the first image below: When the boxes are hidden, the other divs will readjust in place of the current divs ...

What is the best way to display a div beneath the highlighted text within a textarea field?

I have encountered a situation where I want to display a div (like a popup) when text is selected in a text area. However, when using mouse-down for this action, the position of the div sometimes does not align directly below the selected text. Below is t ...

It appears that the font in style.css is not being updated properly and seems to resemble

My issue lies within my CSS code. The text on my website is not displaying correctly and seems to be ignoring the styling that I have applied. Even though I have used similar styling on other buttons which look fine, this specific text element is causing p ...

Creating a Sticky Header and Footer with Responsive Design: Ensuring Content Div Expansion between Them

Greetings. I am working on creating a simple responsive HTML layout as described below: HTML: <div id="header"></div> <div id="content"></div> <div id="footer"></div> CSS: #header{ wi ...

Show an image when hovering over a dot using CSS - without hovering directly on the image

I'm currently working on a merchandising page that involves photoshopping several products onto a background image. I want customers to be able to hover over a dot positioned on each product to reveal its information and provide a link similar to . Ho ...