What is the best way to ensure that a <label> remains vertically aligned with its <form>?

I'm struggling to incorporate their suggestions into my design. How can I align my label to the left of my input field on the same line? Also, I'm a beginner in coding so please excuse any mistakes in my code:

body {
  background-color: #f5f5f5;
}

.rng_box {
  background-color: white;
  width: 70%;
  max-width: 800px;
  left: 50%;
  top: 460px;
  border-radius: 20px;
  padding: 20px;
  position: absolute;
  transform: translate(-50%, -50%);
  box-shadow: 0px 2px 6px 2px #E5E5E5;
  border: solid 3px #DEDEDE;
}

.title {
  text-align: center;
}

.form-control {
  width: 180px;
  height: 30px;
  margin-left: 45px;
}

.btn {
  margin: 1px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3b5954544f484f495a4b7b0f150e1508">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">

<div class="rng_box">
  <h1 class="title">Random Number Generator</h1>
  <div>
    <form id="minAndMax">
      <label for="min">Minimum:</label><br>
      <input type="number" id="min" name="min" class="form-control" value="1">
      <label for="max">Maximum:</label><br>
      <input type="number" id="max" name="max" class="form-control" value="10">
      <br>
      <div class="buttons">
        <button type="button" class="btn btn-success" onclick="generateRN()">Generate &#x1F389;</button>
        <button type="button" class="btn btn-danger" onclick="ignoreFields()">Ignore Fields</button>
        <button type="reset" class="btn btn-primary">Clear</button>
      </div>
    </form>
  </div>
  <hr>
  <h1 id="rng_answer">0</h1>
</div>

Answer №1

If you want to make it simple, just put your label and input inside a div and utilize flexbox.

https://www.w3schools.com/css/css3_flexbox.asp

Take a look at the example code provided below:

.label-wrap{
  display:flex;
}

<div class='label-wrap'>
  <label for="min">Minimum:</label><br>
  <input type="number" id="min" name="min" class="form-control" value="1">
</div>

https://i.sstatic.net/vwU4H.png

Answer №2

The issue stems from the .form-control css class in the _forms.scss file, which includes a display: block property. This causes the component to move to a new line. To resolve this, you can use display: inline-block !important instead, allowing the input and label to be aligned on the same line. Furthermore, there are unnecessary <br> tags that should be removed to improve the layout.

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef8d80809b9c9b9d8e9fafdbc1dac1dc">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
    <link rel="stylesheet" href="RNG.css">
    <title>Random Number Generator</title>
    <link rel="icon" type="image/png" href="/Users/trippyrock/Desktop/ToolScape/favicon.png">
<style>
body {
    background-color: #f5f5f5;;
}
.rng_box {
    background-color: white;
    width: 70%;
    max-width: 800px;
    left: 50%;
    top: 460px;
    border-radius: 20px;
    padding: 20px;
    position: absolute;
    transform: translate(-50%,-50%);
    box-shadow: 0px 2px 6px 2px #E5E5E5;
    border: solid 3px #DEDEDE;
}
.title {
    text-align: center;
}

.form-control {
    width:180px;
    height:30px;
    margin-left: 45px;
    display: inline-block !important;
}
.btn {
    margin: 1px;
}
</style>
</head>
<body>
    <div class= "rng_box">
        <h1 class="title">Random Number Generator</h1>
        <div>
            <form id="minAndMax">
                    <label for="min">Minimum:</label>
                    <input type="number" id="min" name="min" class="form-control" value="1">
                    
                    <label for="max">Maximum:</label>
                    <input type="number" id="max" name="max" class="form-control" value="10">
                    
            <div class="buttons">
                    <button type="button" class="btn btn-success" onclick="generateRN()">Generate &#x1F389;</button>
                    <button type="button" class="btn btn-danger" onclick="ignoreFields()">Ignore Fields</button>
                    <button type="reset" class="btn btn-primary">Clear</button>
            </div>
            </form>
        </div>
        <hr> 
            <h1 id="rng_answer">0</h1>
    </div>

    <script src="RNG.js"></script>
</body>
</html>

Answer №3

Utilize the bootstrap forms in conjunction with other utility classes for enhanced functionality.

<div class="shadow-sm w-75 mx-auto mt-4 p-3 rounded-lg mw-75 text-center">
    <h1>Random Number generator</h1>
    <form class="form-horizontal" id="minAndMax">
        <div class="form-group row">
            <label class="col-4 col-form-label">Minimum:</label>
            <div class="col-8">
                <input type="number" id="min" name="min" class="form-control" value="1" />
            </div>
        </div>
        <div class="form-group row">
            <label class="col-4 col-form-label">Maximum:</label>
            <div class="col-8">
                <input type="number" id="max" name="max" class="form-control" value="10" />
            </div>
        </div>
        <div class="d-flex justify-content-center">
            <button type="button" class="btn btn-success mr-2" onclick="">Generate &#x1f389</button>
            <button type="button" class="btn btn-danger mr-2" onclick="">Ignore Fields</button>
            <button type="reset" class="btn btn-primary" onclick="">Clear</button>
      </div>
  </form>
</div>

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

Stopping an HTML5 range input at a specific number: Tips and tricks

Does anyone have suggestions on how to use angularjs to stop a range slider at 75? I attempted it but it doesn't seem like the best approach. Any guidance would be appreciated. [EDIT for clarification after max=75 answer] Just to clarify, I want to di ...

Achieve the hidden div scroll effect in React by initially hiding the div and then allowing

I am currently working on a web chat application using next.js. One of the features is an emoji picker button that, when clicked, displays a menu of emojis. The issue I am facing is that in order for the user to see the emoji menu, they have to scroll down ...

When setting the margin to auto, it perfectly centers elements on servers and IE, but on Chrome off the server, it appears to be aligned to the

There seems to be an issue with the display of my page in Chrome - it starts at the middle and ends on the right side. However, when I view it on my server or try it on Internet Explorer 11, it appears centered. Why is that? This problem only arose after ...

Avoiding duplicate borders in grid layout

I'm working on a crossword puzzle design using css grid The blank cells have no color, while the other cells have a black border. The issue I'm facing is that the borders are overlapping. I've referred to similar questions on Stack Overfl ...

Leveraging CamanJS for canvas filtering

These are my HTML elements: <div class="wrapper"> <nav class="navbar navbar-transparent"> <div class="container-fluid"> <div class="navbar-header"> <span class="btn btn-white btn-file"> ...

Adjust text alignment of SVG elements using CSS

I am facing an issue where I am unable to change the x and y variables of Svg text tags using CSS. This should work as it does with Svg and . However, I am only able to make changes if I directly add the x and y positions in the HTML code. Html: <svg ...

How can I apply both Css and Bootstrap styles to an element in Next.js?

I'm having trouble incorporating the Bootstrap class sticky-top into another CSS style element. I attempted to achieve the same effect without Bootstrap by adding position: sticky, top: 0 in the CSS, but it didn't work as expected. The issue aris ...

Avoiding text from overflowing a DIV when the parent DIV's width is specified as a percentage

After extensively searching through over 20 similar posts, I have not been able to find a solution that works for my specific issue. The layout of some content I'm working with resembles the example provided here: Check out the Fiddle Example In th ...

Div containing a centered span with a background

<div> <span style= "background: red; text-align: center;"> There seems to be an issue here </span> </div> The text alignment is not functioning as expected. I need the text to be centered along with the background. https://jsfiddl ...

Aligning the tooltip element vertically

I've created a unique flexbox calendar layout with CSS tooltips that appear on hover. One challenge I'm facing is vertically aligning these tooltips with the corresponding calendar dates effectively. Although I prefer to achieve this alignment u ...

Turn off the scrollbar without losing the ability to scroll

Struggling with disabling the HTML scrollbar while keeping the scrolling ability and preserving the scrollbar of a text area. Check out my code here I attempted to use this CSS: html {overflow:hidden;} Although it partially worked, I'm not complete ...

Trouble encountered with bootstrap toggling when multiple identical inputs are used

Problem with Bootstrap toggle not working on multiple identical inputs unless the first input is pressed. Even then, not all inputs are checked/unchecked. I've created a small example I want to ensure that when one input is toggled, all others have ...

When the add button is clicked, I would like to implement a feature where a checkbox is added

When the user clicks on the link "출력하기", I want the web page to add a checkbox to all images. I wrote this code, but it's not working. Can anyone help me? This is my JS: $(document).ready(function(){ $("#print").on('click', fu ...

Mapping memory for FirefoxOS is like equivalent strides

Is there a way to create a memory mapped file in FirefoxOS, Tizen or other pure-JS mobile solutions? In the scenario of a mobile browser where you have large amounts of data that can't fit in RAM or you prefer not to load it all at once to conserve R ...

Verify if there are DOM elements located within a DIV container, execute the functions associated with those elements sequentially

I am in the process of creating a game using HTML, CSS, and JavaScript. My focus right now is on manipulating DOM elements without relying on the canvas tag. The goal is to develop a pseudo graphical programming language, similar to the environment provide ...

Jquery draggable droppable does not support displaying multiple divs simultaneously

I tried to implement the jquery ui draggable and droppable features to display 3 divs within a designated area. Below is the code snippet: --CSS: #content-1 { width: 200px; height: 100px; border: 1px solid red; display: none; } #content-2 { width: ...

The functionality of changing the category in the second carousel slider on click using JavaScript appears to

Creating a bootstrap carousel slider that dynamically changes based on the selected category. For example, clicking on the hammer category will display only hammer images, while selecting the compass category will show compass images. Everything worked sm ...

Selenium alert: element click blocked: Another element is in the way of receiving the click

I'm currently using Selenium in an attempt to click on the "show more" option under the "about this show" section on the following URL: https://www.bandsintown.com/e/1024477910-hot-8-brass-band-at-the-howlin'-wolf?came_from=253&utm_medium=we ...

Content not being hidden by Twitter Bootstrap tabs

I am encountering an issue with my code. When I click on an inactive tab, the content of the first tab does not hide and both tabs' content is displayed simultaneously. Is there a solution to this problem? Thank you for your help! <ul style="l ...

`Why won't the checkbox uncheck when the dropdown is changed in jQuery?`

I have a roster of users, each with a unique checkbox for selection. When I adjust the dropdown menu, a new group of users is chosen and marked as selected. However, I am struggling to uncheck the previously selected checkboxes based on the last dropdown c ...