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

Using PHP to create custom HTML email templates and sending them out via

I have successfully used phpmailer to send normal pre-defined emails. Now, I am looking to enhance the email format by connecting my text editor with a PHP script to send HTML emails. The text editor is a template from CKEditor. https://i.stack.imgur.com/v ...

I'm having trouble getting the data to parse correctly in ajax. Can anyone explain why it's not working?

I have implemented a system where an excel file is uploaded using AJAX, and information about the success or failure of the upload is sent from the upload page to the PHP page using json_encode. However, I am facing an issue with accessing this data indivi ...

Enhance your product listings in Opencart 2.0 by adding supplementary images alongside the main product image

Can someone assist me with moving additional product images next to the main product image in the default opencart 2.0.1.1 theme? Currently, they are appearing below the product image and I would like them to be displayed alongside it instead. ...

Issues are arising with Angular Form where the FormControl is not being properly set up for the first field in my form

After grappling with this issue for several weeks, I am still unable to pinpoint the cause. (Angular version: 16.1.4) The form component is populated using a BehaviorSubject, and although the console prints out the correct values for both the form and dat ...

Alignment dilemmas

I'm experimenting with HTML and CSS and have a query concerning the text-align properties. As I work on constructing my personal website, I aim to align text content to start while having it centered in the middle of the page. Currently, I've ma ...

Issue with displaying Favicon on staging server

I need assistance with this issue. I am working to show a title and logo on a tab. It is functioning correctly on my local machine, but the logo is not appearing on the staging server. <title>Global</title> <link type="image/png" href ...

Modify the color of an Ionic button for a single button, rather than changing the color for all buttons

How can I modify this code so that only the clicked button changes its color, not all of them? Here is the current code: .html: <ion-col size="6" *ngFor="let data of dataArray" > <ion-card> <ion-card-header> ...

AngularJS enables the loading of directives dynamically, but sometimes encounters errors such as "Restricted URI access denied."

Presently, I am in the process of developing a small educational project using HTML5, CSS, JS and AngularJS. Hiccup: Loading an AngularJS Directive in my index.html file Error code [1] - Local browser Error: Access to restricted URI denied Various resp ...

List on the Left Side with Scroll Capability

In my ASP.NET web application that targets .NET 3.5 and utilizes an AJAX asp:UpdatePanel, I have structured the page layout using multiple div elements to divide the vertical space into sections - header, first content, second content, third content, and f ...

Determining the dimensions of form elements in an inline layout using Bootstrap 3.1

Currently in the process of upgrading my application from version 3.0 to 3.1. The application is still in its initial phase, resembling more of a prototype with only a few pages created. Therefore, I didn't anticipate encountering many issues during t ...

Using jQuery Datepicker, showcase two distinct datepickers on a single page

Currently, I am utilizing the jQuery [Datepicker][1] on a website, but I am facing challenges when attempting to display two datepickers on the same page in different manners. The first one should only appear once you click the text box, and once a date i ...

PersistJS callback function is malfunctioning

I stumbled upon a great library for managing client storage. You can find the latest version here: https://github.com/jeremydurham/persist-js However, I encountered an issue with the callback function. var result = store.get('saved_data', func ...

Wrapping content in flexbox and aligning it justified

I have an issue where I want to display a page title on the left side and a rating number with stars on the right side. My goal is to center all items once flexbox wraps. Specifically, when the "Longer Page Title" reaches the rating and stars and the flexb ...

Steps for compiling SCSS to CSS using Angular-CLI and moving it to the assets directory

I am currently working on an Angular 5 project with an assets folder where I store my common CSS file and reference it in index.html. I now plan to create a new folder called "sasstyles" and place some .scss files there. When I compile or run the project ...

Navigating through a PHP table, tracking your progress with a

I have encountered a problem that I need help with. I have been searching the internet for information on how to add a progress bar to my PHP code. Most solutions I found use the style method to display the progress percentage. In the image below, you can ...

Solutions for concealing the current div when clicking on a new div that reveals a fresh one

Is there a way to hide the current div once you click on a new div that reveals another one? The code below toggles the display of the div, but what I am attempting to achieve is that when you click on a new item after clicking on the first one, the first ...

The flask application encounters a 404 error when trying to access the favicon.ico file

Upon reviewing my logfile, I noticed numerous entries indicating attempts to load files like /favicon.ico GET - /favicon.ico GET - /apple-touch-icon.png GET - /apple-touch-icon-precomposed.png I have researched this issue extensively online, but have bee ...

The browser is displaying the raw text data of a file rather than the formatted HTML content

As a newbie in the world of webpage development, I am currently working on my very first webpage project. I have written some HTML code using a text editor and saved it as an HTML file. However, when I try to view it in my browser, all I see is plain HTML ...

Endless cycle of invoking functions in Angular.js

Issue with the template : <div class="container"> <h1>Process Definitions</h1> <table class="table table-stripped" > <tr> <td><strong>Name</strong></td> <td><strong>Id</st ...

The concept of CSS Parent Elements refers to the relationship

Is it possible to dynamically apply CSS style based on the parent property of a div element using JavaScript? Here is an example: <div clas="parent"> <div class="child"> <div class="x"></div> </div> </d ...