What is the best way to align the form labels alongside and right justified with the form elements?

Could someone please help me with aligning my labels to be inline and right-justified against form elements? Currently, I'm facing this issue: this

section {
    display: flex;
    flex-direction: row;
    justify-content: center;
    width: 960px;
    margin: 0 auto;
}

.feedback_right {
    display: flex;
    flex-direction: row;
    flex: 1;
}

/* Contact Form Styling */

form#contact_form {
    display: flex;
    flex-direction: column;
    padding: 0;
    margin: auto;
}

form#contact_form p {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-around;
    padding: 0;
}

form#contact_form p > label {
    display: inline-block;
    padding-right: 2px;
    text-align: right;
}


form#contact_form p > input {
    font-family: monospace;
    font-size: 1em;
}

button {
    display: inline-block;
    width: 25%;
    margin: 0 0 0 auto;
    margin-bottom: 5px;
}
<section>
  <div class="feedback_right">
        <form id="contact_form">
                <p>
                    <label for="contact_name">Full Name:</label>
                    <input type="text" id="contact_name" name="contact_name" placeholder="Name">
                </p>
                <p>
                    <label for="contact_email">Email Address:</label>
                    <input type="email" id="contact_email" name="contact_email" placeholder="Email">
                </p>
                <p>
                    <label for="contact_number">Mobile Number:</label>
                    <input type="tel" id="contact_number" name="contact_number" placeholder="Mobile Number">
                </p>
                <p>
                    <label for="victoria">Victoria</label>
                    <input type="radio" id="victoria" name="victoria">
                </p>
                <p>
                    <label for="queensland">Queensland</label>
                    <input type="radio" id="queensland" name="queensland">
                </p>
                <p>
                    <label for="new_south_wales">New South Wales</label>
                    <input type="radio" id="new_south_wales" name="new_south_wales">
                </p>
                <p>
                    <label for="act">Australian Capital Territory</label>
                    <input type="radio" id="act" name="act">
                </p>
                <p>
                    <label for="tasmania">Tasmania</label>
                    <input type="radio" id="tasmania" name="tasmania">
                </p>
                <p>
                    <label for="south_australia">South Australia</label>
                    <input type="radio" id="south_australia" name="south_australia">
                </p>
                <p>
                    <label for="western_australia">Western Australia</label>
                    <input type="radio" id="western_australia" name="western_australia">
                </p>
                <p>
                    <label for="northern_territory">Northern Territory</label>
                    <input type="radio" id="northern_territory" name="northern_territory">
                </p>
                <p>
                    <label for="contact_message">Message:</label>
                  <textarea rows="4" cols="40" id="contact_message" name="contact_message" placeholder="Message"></textarea> 
                </p>
                    <button type="submit">Submit</button>
              </form>
          </div>
    </section>

I'm looking to keep the submit button in its current position while fixing the alignment of labels against the input elements. My goal is to achieve a layout similar to this, without the vertical divider line.

Answer №1

If you're not just a backend developer with limited CSS knowledge, aligning elements shouldn't be too difficult. Simply float them to the right and adjust the max-width of the form as needed.

I've demonstrated one method (#3) for right-justifying label items, but there are many other ways to achieve the same result.

/* Contact Form Styling */

section {
    display: flex;
    flex-direction: row;
    justify-content: center;
    width: 960px;
    margin: 0 auto;
}

.feedback_right {
    display: flex;
    flex-direction: row;
    flex: 1;
}

form#contact_form {
    display: flex;
    flex-direction: column;
    padding: 0;
    margin: auto;
}

form#contact_form p {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-around;
    padding: 0;
}

form#contact_form p > label {
    display: inline-block;
    padding-right: 2px;
    text-align: right;
}

form#contact_form p > input {
    font-family: monospace;
    font-size: 1em;
}

button {
    display: inline-block;
    width: 25%;
    margin: 0 0 0 auto;
    margin-bottom: 5px;
}

/*Considering floating elements*/

label {
  float: right;
}

label[for="contact_message"] {
  float: none;
}

input {
  float: right;
  margin-left: 52px;
}

input[type="radio"] {
  margin-left: 200px;
}

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

To create space between the input field and button, utilize the input-group class for

Is there a way to insert a space between the input field and the button while using input-group in Bootstrap 3? Check out this jsFiddle for more information. <div class="container"> <div class="row"> <div class="col-md-12"> ...

Is there a way to eliminate the gap under the image?

https://i.sstatic.net/d73jo.jpg Is there a way to eliminate the gap under the image? ...

What is the best way to check the CSS attributes of a React component with react-testing-library?

I understand the philosophy behind the react-testing-library, but I am having difficulty implementing it with CSS properties. For instance, consider a basic toggle component that changes its background color when clicked: import React, { useState } from ...

Tips for interpreting Json data received from a WCF service in HTML with the help of AJAX technology?

Can anyone assist me with retrieving data details from a JSON model? I am utilizing a WCF service that returns JSON data and it works fine as I have tested it using WebClient. However, I am having trouble displaying the data on my HTML site. Despite trying ...

What is the method to retrieve the file name when a user chooses a file using <input type="file" />?

In the past, I've encountered similar inquiries that have remained unresolved due to security concerns. However, I recently discovered that HostMonster has effectively tackled this issue when I submitted a ticket and attached a file through their bac ...

Ways to emphasize an HTML element when clicked with the mouse

Can JavaScript be used to create code that highlights the borders of the HTML element where the mouse pointer is placed or clicked, similar to Firebug, but without needing a browser extension and solely relying on JavaScript? If this is possible, how can ...

Article discussing the Facebook like button

I've recently incorporated Facebook like buttons into the articles on my online store. It's great because when someone shares a post about a product or article, it shows up on their wall. Users receive notifications on Facebook when someone else ...

Using bootstrap to offset columns fails to function properly in the second row

I'm currently developing a weather application and I'm facing a challenge with centering columns using Bootstrap's grid system. I have two rows that display the current local weather conditions. In order to center these columns on the page, ...

What could be causing the blurriness in the image?

I'm having trouble displaying an image below my navigation bar. The image I'm trying to display is located at https://i.sstatic.net/8PUa3.png and has dimensions of 234 x 156 px. However, the image appears blurry and only a portion of it can be s ...

Avoid applying the active class to specific elements while scrolling

I encountered the following issue: While trying to add logic to apply the active class on list elements in my navigation bar as I scroll, I utilized this code snippet: $(window).bind('scroll', function () { var scrollPos = $(window).scrollTop ...

How do I retrieve the controls of a reactive form nested component in Angular?

I'm facing an issue with accessing the statuses of FormControl 'moreFields' in exampleForm. The problem arises when I try to view the controls of FormControl 'moreFields'. I can't seem to see them as per the code snippets prov ...

As I populate my mobile content, background images are being stretched out

I'm currently in the process of enhancing my website by incorporating background images using the background property. My goal is to ensure that each image fills the width and height of its container without sacrificing quality or aspect ratio. Here i ...

developing a seamless and uncomplicated banner text animation

Greetings everyone! I am diving into the world of JavaScript and recently attempted to create a banner carousel animation. Specifically, I have 3 banners in rotation where each slide reveals text with an animation effect. However, I'm encountering an ...

Issues arise when attempting to run JQuery functions with the $ symbol in the head tag of HTML

Because of the limitations imposed by Squarespace, I am only able to include code via the Head tag. When the script reaches the $ part of JQuery, it seems to not execute at all. After testing with numerous console.log() statements, I observed that the webp ...

Is it possible to have a variable number of dynamic CSS Grid columns with the option of including a header or footer

Implementing a dynamic number of columns with CSS Grid is a breeze. .grid { grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); } // These .cells are beautifully aligned <div class="grid"> <div class="cell"></div> ...

The page switch with a jittery effect

Could really use some assistance with this code that has been giving me trouble for quite a while now. It's a simple HTML, CSS, and JS code involving two pages. [The second page overlaps the first by default, but adjusting the z-index of the first p ...

How can CSS be utilized to style the visited links that are hovered over

Is there a way to change the font color specifically for visited hyperlinks that are being hovered over by the mouse? I am trying to achieve this: a:visited:hover {color: red} ...

Customizing the date-select widths: A step-by-step guide

I've been struggling with adjusting the width of different instances of this line: <%= f.date_select :deadline, :order => [:month, :day, :year], class: 'date-pick', id: 'goal' %> Despite creating unique ids for select, d ...

HTML - Using Tables to Add and Edit Rows

Take a look at this demo on JSFiddle for my project: Demo jsfiddle I'm currently in the process of creating a table with various functionalities. The function "sortTable" is responsible for sorting the table and it's functioning as expected. T ...

Differences in behavior of multiple select with track by in Angular versions above 1.4.x

I recently upgraded my product from Angular 1.2.x to 1.4.x. Since updating to angularjs 1.4.x, I've encountered an issue: What I have: I included code snippets for both angular 1.2.0 and 1.4.8. You can check out the comparison on JSFIDDLE. Explanat ...