Tips for creating proper spacing between text and adjacent boxes in CSS/HTML

I am currently working on a registration form and facing an issue with the spacing. The space between each credential and its corresponding input box appears irregular, which is not desired. I have tried using the <pre> tag to format the code, but the spacing remains inconsistent. Here is my current attempt:

<html>
    <head>
        <title>My first code</title>
        <style>
            h1 {
                color:blue;
                font-family:'Times New Roman', Times, serif;
                font-style: bold;
            }
            
            img {
                border-style:double;
                border-radius: 5px;
                border-color: blue;
            }

            p {
                color:blue;
                font-size: larger;
            }

            label {
                font-family: 'Times New Roman', Times, serif;
            }

            legend {
                font-family: 'Times New Roman', Times, serif;
            }
            
            input {
                text-indent: 30%
            }
        </style>


    </head>

<body>
   
    <pre>
        <form>
            <fieldset>
                <legend>Form:</legend>
                <label for="fname">First Name:</label>    <input type="text" id="fname" name="fname">
                
                <label for="lname">Last Name:</label>    <input type="text" id="lname" name="lname">
                
                <label for="Date of Birth">Date of Birth:</label>  <input type="date" id="Date of Birth" name="Date of Birth">
                
                <label for="email">Email:</label>        <input type="text" id="email" name="email">
                
                <label for="ps">Password:</label>     <input type="password" id="ps" name="email">
                
            </fieldset>
        </form>
    </pre>
    
</body>
</html>

If anyone has suggestions on how to improve the regularity of the spacing in this code, please let me know. Thank you!

Answer №1

Instead of using float and <br/>, a more effective approach is to utilize Flex.

This method also ensures responsiveness.

h1 
{
    color:blue;
    font-family:'Times New Roman', Times, serif;
    font-style: bold;
}
    
img 
{
    border-style:double;
    border-radius: 5px;
    border-color: blue;
}
     
p {
     color:blue;
     font-size: larger;
     }
     
label
{
    font-family: 'Times New Roman', Times, serif;
}

legend 
{
    font-family: 'Times New Roman', Times, serif;
}
form
{
    display:flex;
    flex-direction:column;
}
            
.input-container
{
    display:flex;
    flex-direction:row;
    margin-bottom:10px;
}
            
.input-container label
{
    width:100px;
    text-align:right;
    margin-right:10px;
}
              
.input-container input
{
    width:400px;
}
              
@media only screen and (max-width: 600px) {
.input-container 
{
    flex-direction:column;
}

.input-container label
{
    width:100%;
    text-align:left;
}
.input-container input
{
    width:100%;
}
}
<html>
    <head>
        <title>My first code</title>
    </head>
<body>
        <form>
            <fieldset>
            <legend>Form:</legend>
            <div class="input-container">
              <label for="fname">First Name:</label>
              <input type="text" id="fname" name="fname">
            </div>
            
            <div class="input-container">
               <label for="lname">Last Name:</label>
               <input type="text" id="lname" name="lname">
             </div>
             
             <div class="input-container">
               <label for="Date of Birth">Date of Birth:</label>
                <input type="date" id="Date of Birth" name="Date of Birth">   
              </div>
              
              <div class="input-container">
                <label for="email">Email:</label>
                <input type="text" id="email" name="email">
              </div>
              
              <div class="input-container">
                <label for="ps">Password:</label>
                <input type="password" id="ps" name="email">
              </div>

            </fieldset>
        </form>
</body>
</html>

Answer №2

There are various ways to accomplish this task easily. One method is to include the float attribute in the input tag and also add margin-right.

h1 {
  color: blue;
  font-family: 'Times New Roman', Times, serif;
  font-style: bold;
}

img {
  border-style: double;
  border-radius: 5px;
  border-color: blue;
}

p {
  color: blue;
  font-size: larger;
}

label {
  font-family: 'Times New Roman', Times, serif;
}

legend {
  font-family: 'Times New Roman', Times, serif;
}

input {
  text-indent: 30%;
  float: right;
  margin-right: 40%;
}
<form>
  <fieldset>
    <legend>Form:</legend>
    <label for="fname">First Name:</label>
    <input type="text" id="fname" name="fname"><br><br>
    <label for="lname">Last Name:</label>
    <input type="text" id="lname" name="lname"><br><br>
    <label for="Date of Birth">Date of Birth:</label>
    <input type="date" id="Date of Birth" name="Date of Birth"><br><br>
    <label for="email">Email:</label>
    <input type="text" id="email" name="email"><br><br>
    <label for="ps">Password:</label>
    <input type="password" id="ps" name="email"><br><br>
  </fieldset>
</form>

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

Stop jQuery from resetting the background image when using fadeOut()

Currently, I am using fadeIn and fadeOut functions with jQuery to create a fading effect. However, when the final fade out occurs, the entire CSS table fades out and the background image of the CSS body comes into view. Despite setting the background-posit ...

A guide on embedding text onto the bottom of an image in Material-UI GridList using Reactjs

I'm currently developing a Mern-stack application and using Material-UI for the frontend. My GridList is functioning correctly, but I want to position my description text at the bottom of the image rather than in front of it. However, when I try to a ...

having difficulty selecting a list item within the <nav> and <ul> tags using Selenium WebDriver

Within the nav tag, there is a list of elements. The goal is to click on the first element in the list. Below is the given HTML: <aside id="left-panel" style="overflow: visible;"> <div id="selectedBrand" class="custum-brand-info login-info"> ...

Personalize the color scheme of your HTML template

I recently downloaded a template from html5up.net, specifically the one located here. My intention was to add my own touch by changing the blue color to orange. I managed to do so by tweaking the main.css file and it worked like a charm. However, the res ...

The transformation rotation applied in CSS must not have any impact on the styling of my span and paragraph

Snippet: .details-section{ background-color: rgb(83, 83, 83); height: 200px; } .icon-container{ border: 2px solid #c49b63; width: 90px; height: 90px; transition: 0.5s ease; } .box i{ font-size: 60px; color: black; margin-top: 13px ...

Implementing a function on various pages using the pagebeforeshow event handler in jQuery

I am facing an issue with the execution of the pagebeforeshow command in my webapp. I have built a webapp consisting of three html5 pages (page1, page2, and page3.html) with similar structures. page1.html <!DOCTYPE html> <html> <head> ...

Creating two horizontal flex containers with an input box that is responsive can be achieved by setting the display property

I need some assistance with a layout issue I am facing. Currently, I have two flex containers in a row - one set to flex-start and the other to flex-end. Both of these containers are within a wrapping div that is also set to flex. When the width of the wi ...

Identifying sluggish hardware or unresponsive browsers using JavaScript

My site features numerous CSS animations and transitions that run very slowly on specific browsers and older hardware. I want to avoid user-agent sniffing; is there a way to identify browsers or hardware configurations using JavaScript or a JS library, and ...

What role does the sequence play in matrix transitions that involve rotating and translating simultaneously?

Attempting to animate a matrix3d with both rotation and translation concurrently has yielded unexpected results for me. It seems that changing the order of applying rotation and translation produces vastly different outcomes. http://jsfiddle.net/wetlip/2n ...

When using percentages in HTML, the height and width of a background picture may not scale correctly

Currently, I am utilizing Webstorm software purely for HTML and CSS without any additional plugins. In my code, there is a <section> element that only has the <html> and tags as its parent elements. This particular section is assigned an ID th ...

Add a new string to a class within a Vue.js component

Hey there, currently working on a Vue component where I am iterating through a list of countries. Here's how it looks: <div v-for="i in countries" :key="i.id"> {{i.name}} <span class="flag-icon-gr"></span> I want to dynamically ...

Unable to integrate an if/else statement into the JSON reply

Working with an API to retrieve data and display images from it. I am attempting to implement an if/else statement that will show photos if the search term yields results in the response, and display a message indicating no results if it does not. Curren ...

The use of absolute positioning in conjunction with overflow:hidden

<div id="container" style="overflow:hidden; position:relative;"> <div id="content" style="position:absolute;"> </div> </div> Is it possible to display the content element which is larger than its parent container, while still k ...

The combination of jQuery, using .load method in javascript to prevent scrolling up, making XMLHttpRequest requests, updating .innerHTML elements, and troubleshooting CSS/JS

While utilizing this code, CSS and Javascript are disabled (only HTML loads): function loadContent(limit) { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status ...

When using html-pdf-chrome to print to an A4 PDF, there appears to be a significant space below the A

In my Laravel project, I am currently utilizing the westy92 / html-pdf-chrome library to convert HTML to PDF. The front-end of my project is based on React. I have defined a series of divs to act as A4 pages: <div id="pdf" className="pri ...

What could be the reason that str_replace is failing to replace this particular string?

I am facing an issue with my PHP code that is intended to load data from a CSS file into a variable, find the old body background colour, replace it with a new colour submitted through a form, save the updated CSS file, and update the colour in a database. ...

Explaining the precise measurements of font size in CSS

I recently started diving into the world of CSS and picked up a copy of Eric Meyer's 3rd edition of CSS: The Definitive Guide. While reading through his section on font sizes (page 107), I came across an interesting concept - that the font size deter ...

"Navigate back with just a click - XSL button

My expertise in this area is limited. After some searching, I couldn't find exactly what I need. Hopefully, the solution is simple. I am currently developing a software control system with a built-in web server. Certain points during navigation requi ...

Failure to Link Different Files Using HTML Links

Organizing the different pages of my website, I initially stored HTML files in a folder called X, with index.html, main.html, and other pages. To streamline access, I decided to separate the articles into folders A, B, and C within folder X. However, this ...

Unable to adjust the height of the border when hovering over a P tag

Why doesn't the P tag text and border height increase work when it is hovered over by a mouse? What could be causing this issue? Here is the HTML code snippet: <body> <div class="cheap"> <p id= "pen">hello hannan .hello ...