How to properly position HTML elements and center text within paragraphs with inputs

Hello friends! I've been scouring Google and YouTube for a solution to my issue but no luck so far.

This is the first time I've encountered this problem. I want to align everything in the center, which it is, but there seems to be a vertical alignment issue with the paragraph and input text. They are not perfectly straight, even though they are centered. I'm aiming to achieve a neat layout for the text input like this:

                  Age:           ______________
                  Year:          ______________   
                  Group:         ______________

instead of the default center position like this:

                        Age:           ______________
                      Year:          ______________   
                        Group:         ______________

Here is my HTML and CSS code:

<html>
  <head>
  </head>
  <body>


    <div id="container">
      <h3 class="header"> Welcome to the registration form </h3>
      <div id="border"> </div>


        <div id="box">

         <p> Enter Name: <input class="in" type="text" name="name" value="${param.name}"> </p>
         <p> Enter Age: <input class="in" type="text" name="name" value="${param.name}"> </p>
         <p> Enter Address: <input class="in" type="text" name="name" value="${param.name}"> </p>
         <p> Enter Phone: <input class="in" type="text" name="name" value="${param.name}"> </p>

        </div><!--box -->


    </div><!--container -->
  </body>
</html>

And here is my CSS:

/* 
    Document   : style
    Created on : Jan 18, 2014, 9:53:43 AM
    Author     : will
    Description:
        Purpose of the stylesheet follows.
*/

*{
  margin: 0px;

}
body{
  background-color: black;
}




#container{
  color: black;
  margin: 50px auto;
  border: solid black 2px;
  text-align:center;
  width: 700px;
  height: 540px;
  background-image: url('http://www.kodemi.com/images/full/2013/12/20/white-background-wallpaper-image-pics-hdwallpc-White-Background-Wallpaper-Image-Pics.jpg');


}


#block{
  width: 300px;
  color: black;
  margin: 36px auto;

}



h3.header{
  color: black;
  font-size:25px;
  margin: 30px 30px 5px;

}

#border{
  width: 400px;
  border-top: solid black 1px;
  margin: auto;

}

#box p{
  margin: 20px;

}



#box input{
  margin-left: 10px;
}

Answer №1

To ensure consistency in the alignment of input boxes, consider adding a label tag with specified width and right-aligned text:

label {
    width: 115px;
    display: inline-block;
    text-align: right;
}

http://jsfiddle.net/Kiaaanabal/dQLzy/

Feel free to check out the provided link for further assistance!

Answer №2

You have the option to utilize table tags instead of paragraph tags in order to properly align input fields and text. Adjustments can be made using cellspacing and cellpadding for further customization.

<div id="box">
        <table>
            <tr>
                <td>Name:</td>
                <td><input class="in" type="text" name="name"
                    value="${param.name}"></td>
            </tr>
            <tr>
                <td>Age:</td>
                <td><input class="in" type="text" name="name"
                    value="${param.name}"></td>
            </tr>
            <tr>
                <td>Address:</td>
                <td><input class="in" type="text" name="name"
                    value="${param.name}"></td>
            </tr>
            <tr>
                <td>Phone:</td>
                <td><input class="in" type="text" name="name"
                    value="${param.name}"></td>
            </tr>
        </table>
    </div>

Give this a try!

Answer №3

It's as easy as pie: just put the input name inside the label and give some width to the ID box. Here is the code you need:

HTML CODE:

<html>
    <head>
    </head>
    <body>

        <div id="container">
            <h3 class="header"> Welcome to the registration form </h3>
            <div id="border"> </div>

            <div id="box">
                <p> <label>Enter Name:</label> <input class="in" type="text" name="name" value="${param.name}"> </p>
                    <p><label> Enter Age:</label> <input class="in" type="text" name="name" value="${param.name}"> </p>
                        <p><label> Enter Address: </label><input class="in" type="text" name="name" value="${param.name}"> </p>
                            <p> <label>Enter phone: </label><input class="in" type="text" name="name" value="${param.name}"> </p>
            </div><!--box -->

        </div><!--container -->
    </body>
</html>

CSS CODE:

/* 
Document   : style
Created on : Jan 18, 2014, 9:53:43 AM
Author     : will
Description:
Purpose of the stylesheet follows.
*/

*{
    margin: 0px;
}
body{
    background-color: black;
}

#container{
    color: black;
    margin: 50px auto;
    border: solid black 2px;
    text-align:center;
    width: 700px;
    height: 540px;
    background-image: url('http://www.kodemi.com/images/full/2013/12/20/white-background-wallpaper-image-pics-hdwallpc-White-Background-Wallpaper-Image-Pics.jpg');
}

#block{
    width: 300px;
    color: black;
    margin: 36px auto;
}

h3.header{
    color: black;
    font-size:25px;
    margin: 30px 30px 5px;
}

#border{
    width: 400px;
    border-top: solid black 1px;
    margin: auto;
}

#box p{
    margin: 20px;
    text-align:left;
}

#box label { width:95px; float:left }

#box { width:300px; /* or 42% width*/  margin:0px auto; }

#box input{
    margin-left: 10px;
}

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

Resolving Issues with Text Overlaid on an Image Hyperlink

Alright, so this problem might be a bit too complex for CSS/HTML to handle, but I'll give it a shot anyway. I have some images that are dynamically placed using the code from this website. What I want to do with these images is add a text overlay w ...

Enhance your Underscores Wordpress Theme by incorporating a secondary sidebar option

Started a new Wordpress site using the Underscores theme (_s) Successfully added one sidebar, but now looking to add a second one with different widgets on the same page. After adding the new sidebar to the functions.php file and seeing it in the Wordpre ...

Avoiding conflicts between banners, text, and images for HTML/CSS design

I'm having an issue with the banner I created for my project. It seems to be overlapping with text and images, hiding behind them. How can I fix this? Unfortunately, I can't post the link to my project here due to other files present. The specif ...

verified that all form checkboxes were selected in angularJS

Hey there, I have a form in AngularJS with multiple checkboxes. When the first checkbox (Check All) is clicked, I want all the checkboxes to be checked. How can I achieve this using Angular for each field loop? Below is a snippet of my code: vm.asas = f ...

Is there a way to eliminate the blue formatting on my phone?

I've encountered a styling issue on my website when viewing it on mobile, specifically with links on iPhone. I've already attempted the solutions mentioned in this discussion: How do I remove the blue styling of telephone numbers on iPhone/iOS? ...

What is preventing Bootstrap properties from being applied to HTML elements appended to the DOM through JavaScript?

Currently, I am immersed in a project that utilizes Bootstrap to generate various nested components styled in accordance with Bootstrap's class system. I triumphantly crafted a card component using Bootstrap, but encountered a challenge when attemptin ...

What is the best way to use jQuery to display the character represented by an ASCII code &###?

Struggling with printing text? I am trying to append some content to an array, but it's showing special characters like &aacute; instead of the actual accented letters. Any suggestions on how to fix this issue? This is for a select tag, so I&apos ...

Compatibility with various browsers for the style 'cursor:pointer'

While exploring, I stumbled upon some CSS attributes that can turn the cursor into a hand: For IE - style="cursor: hand;" For NS6/IE6 - style="cursor: pointer;" Cross Browser - style="cursor: pointer; cursor: hand;" Interestingly, I noticed that Stack O ...

Editable content tag

Is there a way to set the maximum number of rows in the contenteditable attribute? I would like to limit users once they reach the character capacity for sending data via POST method. I am currently working with HTML 5. Contenteditable is a new attribute ...

Issues with the performance of input range sliders in iOS Safari is causing frustration

I recently developed a basic range slider component for an application built with NextJS. This component utilizes an <input type="range"> element. While the range slider functions properly on Desktop and Android devices, I encountered sign ...

Generate a dynamic animation by combining two images using jQuery

My attempt to animate two images is not working. I received some help on Stack Overflow but still facing issues with my CSS and HTML code. This is the code I am using: $(document).ready(function() { $(".animar").click(function() { $("#img4" ...

Capture cached images stored in the browser memory without relying on the source attribute of the <img> tag

Imagine you're managing a website, samplewebsite.com and you find that the images on it have been resized to very low quality. When you click on an image, you are directed to a 'view_image' page, for example samplewebsite.com/view?image=u ...

CSS selector for detecting elements with empty or whitespace-only content

A special selector called :empty allows us to target elements that are entirely devoid of content: <p></p> However, in some cases, the element may appear empty due to line breaks or white spaces: <p> </p> In Firefox, a workar ...

A guide on using jQuery to cycle through every row of every table on an HTML page

I am trying to figure out the correct syntax for nesting two loops within each other in jQuery. The first loop should iterate over each table in an HTML page that does not have any IDs or classes, while the second loop should go through each table row of t ...

Using AngularJS, display content only if the JSON response is [false]

Recently, I started learning AngularJS and am currently working on a project where I need a div to only be visible when the JSON data returns [false]. Sometimes, the JSON does return [false] particularly when there are no results from the query. JavaScrip ...

Conceal the pagination element within the Bootstrap aside on smaller screens

I am experiencing an issue with my web page layout. I have a DIV and an Aside element, inside the DIV there is a list with dynamic pagination. Everything seems to be functioning correctly, however on small devices, the Aside section is covering up the pa ...

Tips for clearing state when simply refreshing a DataTable on the page

When it comes to a datatable on a page, I am facing a unique challenge. I want the datatable to be refreshed with a clear state (no column order, etc.), but if the page is accessed by pressing the back button, it should retain its state. I have experiment ...

What is the best way to modify the underline style of a tab in Material UI?

I'm trying to customize the underline of: https://i.stack.imgur.com/J2R1z.png Currently, I am using material ui version 4.12.3 The code snippet for generating my tabs is below: function renderTabs(): JSX.Element { return ( <Tabs className={cla ...

Requesting HTML content from a PHP file using JQuery Ajax callback

For an ajax request, I am entering a name in an input box to search for data from a database. Everything is functioning properly. <script> $(document).ready(function(){ $('input#name-submit').on('click', function() { ...

Creating HTML code from a multidimensional array is a useful skill to

I am looking to use PHP to generate HTML code similar to the following: <li id="821">Accessories for tablets and cell phones <ul> <li id="426">Cell Phone Accessories <ul> <li id="675">Stands and Docks</ ...