What causes the user interface to change its appearance when the input is nested within a label?

I'm just starting to learn about bootstrap forms. I've noticed that the UI looks different when the input is nested inside a label. Can someone shed some light on this for me?

Below is the code snippet:

<form [formGroup]="loginForm" (ngSubmit)="onSubmit()">
     <div class="form-group">
        <label>userName:
            <input class="form-control col-sm-5" formControlName="userName" type="text">
       </label>  
     </div>


     <div class="form-group">
        <label>userName:</label>  
        <input class="form-control col-sm-5" formControlName="userName" type="text">    
     </div>
</form>

https://i.sstatic.net/3zwUn.png

The image shows how my page is rendered differently. My question is, why does the input element appear shorter in the first row and longer in the second row?

Answer №1

When it comes to HTML, the treatment of codes as block and inline elements is quite specific.

A block-level element always begins on a new line.

A block-level element always expands to fill the entire available width (extending left and right as much as possible).

A block level element includes top and bottom margins, which an inline element does not have.


An inline element does not begin on a new line.

An inline element only occupies as much width as needed.

Source 1 Source 2

Bootstrap treats these elements in accordance with the principles of flexbox.

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

Having trouble getting a resizable DIV inside a 100% height DIV with margin to work properly. Any tips or assistance would be greatly

I've encountered a common issue that is slightly different from others I've come across. Despite my best efforts, I have been unable to find a solution on my own. If anyone could offer me some guidance on this, I would greatly appreciate it. The ...

When a user clicks on an anchor tag, close the current window, open a new window, and pass the

I have a scenario where I have an anchor tag that triggers the opening of a window on click. In this newly opened window, there is a table with a column containing another anchor tag. Here is what I am trying to achieve: Code for the anchor tag: functio ...

CSS: Placing text within an icon - A simple guide

Hello, I am currently working on an Ionic application where I have rounded areas to display information. My challenge is to incorporate a number inside a heart icon within the second area. I want to ensure that the alignment of the number and the heart is ...

Stop the execution of javascript code based on the screen width

On my website, I have two menus located in different sections of the page. One menu is shown when the screen width is greater than 555px, while the other menu appears when the screen width is less than or equal to 555px. Although the menus are essentially ...

The modal window displays an error upon submission and prevents itself from closing

Below is the HTML code for a modal form: <form action="" class="cd-form" method="POST" id="signinform" onsubmit="return: false;"> <p class="fieldset" id="warningPanel"> <?php ShowAlertWarning(); ?> </p> <p ...

What is the significance of vh and percentage values in CSS?

Can anyone help me figure out why the .navbar is not displaying correctly on half of the screen? I think there might be another container causing the issue, but I'm unsure. I've set the body to 100vh to cover the entire screen The .navbar ...

Percentage-based vertical scroll bar

Is there a way to add a vertical scroll bar using percentages for height and width instead of pixels? Check out this link to see an example on my website: http://codepen.io/anon/pen/FLlvG. I would like the scrollbar to be in the leftcontent section. ...

Connecting an HTML box to a JavaScript function for the desired outcome: How to do it?

My goal is to connect the input box with id="b" (located inside the div with id="but1") with a JavaScript function that calculates values within an array. Although my junior logic review didn't detect any issues in the code, what mistakes could I have ...

Transmitting information from JavaScript to PHP server results in receiving a 500 Error

I have been exploring various code examples and techniques to transmit data from my website to the server's database. After evaluating different options, I concluded that making an ajax call to send the data would be the most efficient method. Here i ...

What could be causing my CSS navbar with display: inline-block to not center properly?

How can I use display:inline-block in CSS to center my navigation bar? nav { position: fixed; width: 100%; } nav ul { width: 100%; margin: auto; } nav ul li { display: inline-block; margin: 5px 20px 5px 20px; text-align: center; } ...

Looking for assistance in turning this curl script into one that can handle multiple links

Below is a script that has the ability to download files from a given URL. However, I am looking for a way to include multiple links for downloading files. Instead of having a button to add another input box for URLs, I want users to be able to input three ...

Is it possible to modify a Facebook post in editing mode using JavaScript?

I am trying to edit the content of a Facebook post dynamically using JavaScript. https://i.sstatic.net/pyO9h.png My goal is to be able to select the text in edit mode and make changes to it programmatically. The structure of the HTML code for a Facebook ...

Random Image Generator in Javascript with Links

I am attempting to load one image at a time along with its corresponding link. My goal is to display the image in a div with an ID of 'ads'. Here is the code I have so far, but I am not proficient enough in JavaScript to achieve my desired outcom ...

What is the best way to ensure that the output responds to the function?

I have a total value output that needs to decrease when a checkbox is unchecked. The current function I have for unchecking the box is not working as expected. Jquery Total value calculation: $(document).ready(function() { var initialTotal = 720; $(" ...

Tips for displaying Ajax response in a modal popup

I have a link that, when clicked, sends an AJAX request and successfully receives a response in the form of an HTML file, which I then append to a div. However, I want to display this div as a modal popup and I have attempted the following: In the HTML fi ...

Symfony 2 form data now properly sanitized - issue resolved

When testing my Symfony 2 form that POSTs data to change user passwords, I discovered that certain characters entered in the password field were being escaped when it reached the validator. For example: If a user enters a password like RzB&EUgbrhqJRt ...

Python Selenium: Having trouble navigating through two menus using ActionChains before selecting an element

I've encountered an issue where I am attempting to click on an element that is located two levels down in a menu that only appears when hovering over it. For example: Menu -> Sub-Menu -> Element to be clicked content_menu = driver.find_element ...

Disable automatic submission of XMLHttpRequest

I've been testing this script and it seems to be working fine, but I'm not a fan of the auto-submit feature. I tried to remove/disable it and add a regular submit button instead, but that didn't work as expected. <div id="div_data"> ...

Using Javascript to apply unique styling to CKEditor text

Currently, I am working on a self-contained web page and utilizing CKEditor 4.6.2 for input purposes. The issue I am facing at the moment is attempting to apply a CSS class style to text that has been inserted via JavaScript. My CSS is located in the head ...