Getting Started with CSS Alignment - A Novice's Guide

Recently, I embarked on my journey to learn HTML and CSS with the ambition of creating a login page. Although I've successfully crafted a basic version, I'm encountering an issue where the input boxes and labels are misaligned, giving off an unprofessional look. I seek guidance on how I can rectify this through CSS.

Below is my code:

<br /> UserName:
<input type="text" name="user name" size="15" />
<br /> Password:
<input type="text" name="password" size='15' />
<input type="text" hidden name="Password" size="15" /> <br />
<input type="reset" value="Login" />
<input type="submit" value="Cancel" />

https://jsfiddle.net/nxgm33y7/8/

Answer №1

If you're looking to adjust your element positioning, it may require some changes to your markup. Below is a simple example where I've wrapped all elements in a div for easy manipulation. Using classes and ids will help in selecting the elements efficiently.

The structure includes using ul and li elements to display form fields vertically. Adding label tags with specified width can keep the form aligned properly.

To align the first button, such as a login button, with other elements, the selector

ul li:last-child input:first-child
is used. This can be replaced by simply adding a class.

ul {
  list-style-type: none;
}

ul li {
  margin-bottom: 10px;
}

ul li:last-child input:first-child {
  margin-left: 85px;
}

label {
  width: 80px;
  display: inline-block;
}
<div>
  <ul>
    <li>
      <label for="username">Username</label>
      <input type="text" name="username" id="username">
    </li>
    <li>
      <label for="password">Password</label>
      <input type="text" name="password" id="password">
    </li>
    <li>
      <input type="reset" value="Login" />
      <input type="submit" value="Cancel" />
    </li>
  </ul>
</div>

Answer №2

For a more professional appearance and responsive layout, it is recommended to utilize Bootstrap. However, in this example, only regular CSS has been used. You can view the fiddle here: https://jsfiddle.net/NayanaDas/5zg2qqum/. Give it a try:

HTML:

<body>

    <br />
    <div class="form-group">
    <label style="width:50%;">UserName:</label>
    <input type="text" name="user name" size="15" class="form-input"/>
    </div>

    <br />
    <div class="form-group">
    <label style="width:50%;">Password:</label>
    <input type="text" name="password" size='15' class="form-pswd"/>
    <input type="hidden" name="Password" size="15" /> <br />
    </div>

    <br>
  <div align="center" style="width:100%;">
    <input type="reset" value="Login" />
    <input type="submit" value="Cancel" />
</div>
</body>

CSS:

.form-group{

  width:100%;margin-left:5%;
}

.form-input{
  width:50%;margin-left:5%;
}

.form-pswd{
  width:50%;margin-left:6%;
}

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

Aligning vertical pills to the right using Bootstrap framework

Here is the relevant code snippet that I am working with: <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <scrip ...

Converting XML hierarchy into a flat HTML table using XSLT

Looking for a way to transform hierarchical XML into an HTML table? Here's the challenge: <node text="a" value="1"> <node text="gga" value="5"> <node text="dh" value="9"> <node text="tyfg" value="4"> < ...

Exploring outside iframe data

My webpage includes an iframe A with a src attribute that links to a URL containing another embedded iframe B. I'm trying to figure out if it's possible to access the src of this nested iframe B. However, it appears that browser security measures ...

jQuery: Extracting text labels from checkboxes

My custom select dropdown includes checkboxes that are hidden until the user clicks on the dropdown: ---Select---- -option1- -option2- -option3- When a user clicks on the Select, the options appear as checkboxes. I want to be able to retrieve the labels ...

Adjust the content to expand and occupy the entire height using Material UI cut-off Drawer

I am using the provided Material UI code for a persistent clipped drawer, but I am encountering an issue with the content height. I have set the background of the content section to red in my demo sandbox to showcase the problem. My goal is for the content ...

Discovering the meeting point where two dives converge

Is there a method to change the color of the overlapped common part of two animated circles? ...

Customize your form fields with Bootstrap 3 input groups

Currently, I am working on implementing a bootstrap 3 input group, but unfortunately, the outcome is not meeting my expectations. My goal is to create a select [input]-[input text]-[button] structure all in one line. However, in my scenario, the button has ...

Issues with Angular ngroute: controllers are not functioning properly

In order to route my application with different themes on the same page, I plan to utilize the ngroute module. Here's an example of how I intend to achieve this: <html> <head> <title>Angular JS Views</title> ...

The color of the element remains unchanged even when hovering the cursor over it

Below is an example of HTML code: <h1 class="one">Hello</h1> Here is the corresponding CSS style sheet applied to it: h1:hover { color: red; } h1.one { color: blue; } When this code runs, the h1 element should turn blue. Ho ...

What steps are involved in creating a table using Bootstrap framework?

Hey there! I need your expertise on how to create a specific layout using bootstrap: codepen.io/anon/pen/bqJdQJ <div class="container-fluid"> <div class="row"> <div class="col-md-12"> <div cl ...

Tips for disabling viewport resizer while accessing the Console panel in Chrome using Control+Shift+J

Currently, I am utilizing the viewport resizer in Chrome to preview how my code appears on various devices. However, I have encountered an issue - whenever I try to access the console using ctrl + shift + j, the viewport resizer opens instead. You can obs ...

Position the image on the right side of several lines of text

My webpage contains two div elements: one for displaying multi-line text and the other for showing an image which is positioned to the right of the text. The width of the image is not fixed and can be adjusted using a user-defined parameter. <div> ...

What is the best way to redirect to the index page after successfully submitting a new record on the front-end? [Using Rubymine 2020.2.3, Ruby 2.7.2p137, and gem 3.1.2]

For weeks now, I have been struggling with a college project course, unable to resolve an error despite the advice given by colleagues and tutors. In my create method for a specific table, I am facing an issue where the page should redirect back to the in ...

Tips for personalizing your Magento 2 theme

As someone new to Magento 2 and front-end development with a basic knowledge of HTML and CSS, I am eager to customize the blank theme in Magento 2 to gain a deeper understanding of how things work. However, despite reading through the documentation, I am u ...

The div content is aligning at the baseline rather than the middle

The social icons on my website are currently displaying at 40x40, with the text appearing below them. I am looking to center the text within the icons themselves. Any tips on how I can achieve this? Your help is much appreciated! Check out the fiddle here ...

The issue of JQuery UI Dialog remaining open even after triggering it through an Input focus event

I am facing an issue with JQuery and JQuery UI. I thought upgrading to the latest stable version would solve it, but unfortunately, that was not the case. I am currently using Chrome. When I initiate the dialog by clicking on a specific element, everythin ...

Detecting whether a browser is capable of supporting dark mode

One method to determine if dark mode is active is by using prefers-color-scheme: dark: const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches; Is there a way to detect if a browser supports dark mode as well? (By "supports ...

Internal VS External Stylesheets: What you need to know

I'm currently in the process of developing a website and I have started utilizing the style tag at the beginning: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" ...

How to style text in CSS with a numbered underline beneath

Is there a way to apply underlining to text and include a small number underneath the underline in a similar style shown in this image, by utilizing css, html, or javascript? ...

What steps should I take with my Android PWA manifest and service workers?

I created a web application that I want to prompt Android users to download when they visit. To build my service workers and manifest, I utilized PWA Builder which can be found at Now that I have the manifest file ready, should I simply upload it to the r ...