Is there a way to properly align text and input fields?

Here is my HTML code snippet:

<div id = "right">
<br /><br /><br />

<br />Parameter 1 : 10 <input type="range" min="10" max="40" step="0.01" value="10" class="slider" id="myRange" onchange="updateValue_Parameter_1(this.value);"> 40
<input type="text" id="textInput" value="">
<script>
function updateValue_Parameter_1(val) {document.getElementById('textInput').value="Parameter 1 = "+val;}
</script>

<br />Parameter 2 : -15 <input type="range" min="-15" step="0.01" max="-9" value="-15" class="slider" id="myRange1" onchange="updateValue_Parameter_2(this.value);"> -9
<input type="text" id="textInput1" value="">
<script>
function updateValue_Parameter_2(val) {document.getElementById('textInput1').value="Parameter 2 = "+val;}
</script>

<br /> Parameter 3 : -15 <input type="range" min="-15" step="0.01" max="-9" value="-15" class="slider" id="myRange2" onchange="updateValue_Parameter_3(this.value);"> -9
<input type="text" id="textInput2" value="">
<script>
function updateValue_Parameter_3(val) {document.getElementById('textInput2').value="Parameter 3 = "+val;}
</script>

<br />Parameter 4 : 7 <input type="range" min="7" max="9" value="7" step="0.01" class="slider" id="myRange3" onchange="updateValue_Parameter_4(this.value);"> 9
<input type="text" id="textInput3" value="">
<script>
function updateValue_Parameter_4(val) {document.getElementById('textInput3').value="Parameter 4 = "+val;}
</script>

<br />Parameter 5 : -8 <input type="range" min="-8" max="-5" step="0.01" value="-8" class="slider" id="myRange4" onchange="updateValue_Parameter_5(this.value);"> -5
<input type="text" id="textInput4" value="">
<script>
function updateValue_Parameter_5(val) {document.getElementById('textInput4').value="Parameter 5 = "+val;}
</script>

<br />Parameter 6 : 0 <input type="range" min="0" max="1" step="0.01" value="0" class="slider" id="myRange5" onchange="updateValue_Parameter_6(this.value);"> 1
<input type="text" id="textInput5" value="">
<script>
function updateValue_Parameter_6(val) {document.getElementById('textInput5').value="Parameter 6 = "+val;}
</script>

<br />Parameter 7 : 100 <input type="range" min="100" max="6000" step="0.01" value="100" class="slider" id="myRange6" onchange="updateValue_Parameter_7(this.value);"> 6000
<input type="text" id="textInput6" value="">
<script>
function updateValue_Parameter_7(val) {document.getElementById('textInput6').value="Parameter 7 = "+val;}
</script>

<br />Parameter 8 : 0 <input type="range" min="0" max="1" step="0.01" value="0" class="slider" id="myRange7" onchange="updateValue_Parameter_8(this.value);"> 1
<input type="text" id="textInput7" value="">
<script>
function updateValue_Parameter_8(val) {document.getElementById('textInput7').value="Parameter 8 = "+val;}
</script>

<br />Parameter 9 : 5 <input type="range" min="5" max="20" step="0.01" value="5" class="slider" id="myRange8" onchange="updateValue_Parameter_9(this.value);"> 20
<input type="text" id="textInput8" value="">
<script>
function updateValue_Parameter_9(val) {document.getElementById('textInput8').value="Parameter 9 = "+val;}
</script>

</div>  

This above code generates the result shown in this https://i.sstatic.net/cEohq.png.

My issue is that I want to align the range input and text input elements to the left, but previous attempts with margin-left have not yielded the desired outcome. How can I achieve this alignment?

Answer №1

To ensure proper alignment of the columns, consider assigning a fixed width to each one.

<label class='width1'>Parameter 1 : 10 </label>
<input class="width2 slider" type="range" min="10" max="40" step="0.01" value="10"  id="myRange" onchange="updateValue_Parameter_1(this.value);">
<span class='width3'>40</span>
<input class='width4' type="text" id="textInput" value="" >

For example, you can apply the following CSS:

.width1 {
 width: 100px;
}
.width2 {
width: 150px;
}
.width3 {
width: 20px;
}
.width4 {
width: 80px;
}

This approach will ensure that all columns align properly.

If setting a fixed width is not feasible, alternatives include using display:table for table-like behavior or organizing the values in an HTML Table.

Another option is utilizing Flexbox with justify-content:space-between;, although this may require more effort.

Answer №2

My approach to this problem involved utilizing Flexbox along with the flex-basis property to assign individual widths to each element, ensuring they are aligned neatly. Each piece of code has been enclosed within an element (labels for text) for easier styling.

In addition, I have restructured and properly indented the HTML code to enhance readability.

I want to highlight that employing Flex guarantees responsiveness to changes in the fixed width of the container, as well as adjusting the flex-basis which dictates distribution across the entire width of the parent/container.

body,
html {
  height: 100%;
  width: 100%;
  margin: 0;
}

.container {
  width: 600px;
  margin-top: 30px;
}

.line {
  display: flex;
  justify-content: space-between;
}

.line label:nth-of-type(1) {
  flex-basis: 25%;
}

.line input.slider {
  flex-basis: 25%;
}

.line label:nth-of-type(2) {
  flex-basis: 10%;
}

.line input[type=text] {
  flex-basis: 25%;
}
<div class="container">
  <div class="line">
    <label for="myRange">Parameter 1 : 10</label> 
    <input type="range" min="10" max="40" step="0.01" value="10" class="slider" id="myRange" onchange="updateValue_Parameter_1(this.value);"> 
    <label>40</label>
    <input type="text" id="textInput" value="">
    <script>
      function updateValue_Parameter_1(val) {
        document.getElementById('textInput').value = "Parameter 1 = " + val;
      }
    </script>
   </div>

   // Remaining lines follows a similar structure
</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

Disabling the beforeunload confirmation popup

How can I prevent the appearance of this popup modal in Firefox and other browsers while working in React or vanillaJS? I attempted to remove the returnValue property of the event in my component using the snippet found here: https://developer.mozilla.org ...

Bootstrap allows for the use of video backgrounds, offering the option for borders to be included

Having trouble with a Bootstrap HTML page featuring a video background? The issue is that sometimes the video has borders on the left and right, and occasionally on mobile devices as well. Oddly enough, when the browser (Chrome or Firefox) is refreshed, th ...

What is the best way to utilize ng-if to conceal a component in AngularJS when a button in a separate component is clicked?

I am facing a challenge with two child components within a parent component. My goal is to hide component1 when a button in component2 is clicked. Below is an illustration of the current code I am handling: Parent Component HTML: <div ng-app='ap ...

Verify the presence of installed software on the client's machine through ASP.Net

Does anyone have any recommendations on how to verify if an application is installed on the client side in an ASP.Net application? Since ASP.Net operates on the server side, I think it would need to be accomplished using some sort of client-side scripting ...

The alignment of Bootstrap input fields is off center

@import url( 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' ); <div class="row"> <div class="col-xs-12 text-center btn-group" data-toggle="buttons"> <label class="btn btn-primary active"> ...

Concealing elements with duplicate attributes using jQuery

Is there a way to hide duplicate elements in a list based on a specific attribute value using jQuery? Here is an example list: <div data-title="big hero 6">title</div> <div data-title="big hero 6">title</div> <div data-title="a ...

The CSS variable for the Bootstrap Modal header padding, var(--bs-modal-header-padding), is missing or not defined

Is there a way to use the modal-header CSS property inside a normal div instead of inside a modal? When I try to do so, I am getting an error saying that the variable is not defined. Can someone please help me understand what I am missing? <html lang= ...

Having trouble downloading an Excel file with custom headers. Can anyone help me solve this issue?

Currently, I am exporting an excel file for a client user where the user enters their data based on the corresponding headers in the excel file. Once the user completes entering their data, they upload the excel back to the system. The issue I am facing is ...

The transmission of form post-data is not successful

I encountered an error of 'undefined index' related to the 'shop' when trying to submit the form below. Surprisingly, after assigning a name to the submit button and testing its existence (isset), I received a negative response. Here&ap ...

Utilizing React Material UI for form validation with error handling and informative helper text

I recently created a form in which I needed to validate the inputs. During my research, I came across the material UI library that has an attribute called error boolean, and another attribute called helperText for the TextField input of the form. However ...

When running the test, the message "Unable to resolve all parameters for BackendService" is displayed

Upon executing the ng test command, the following error was displayed. This is my service specification: describe('BackendService', () => { beforeEach(() => { TestBed.configureTestingModule({ providers: [ { p ...

Troubles with Marquee Element

Being a beginner in the world of html and css, I have encountered an issue while creating a responsive web page. At the top of my page, there is a text within a div which gets cut off from the right side as I decrease the browser width. Initially, I cons ...

"Internet Explorer: Unleashing the Magic of Card Fl

I'm encountering a problem that I can't seem to solve. Everything works perfectly in Chrome, FF, Safari, etc., but in Internet Explorer, I see the image on the front side instead of the text on the backside. Can anyone please assist me with this ...

Manipulating CSS for a certain ID that is applied to multiple elements with the help of JQuery

I am attempting to conceal all div elements except the initial one within a specific ID. Despite my efforts to create a custom jQuery script, it does not appear to be functioning. $("div.hero-featureSwap:not(:first)").css({display:none}); Is this scr ...

Creating CSS layouts without the need for using the ctrl+space function

Currently, when I am working in a CSS file and begin typing, if I press ctrl + space, I receive autocomplete suggestions. Is there a way to enable this feature without needing to press ctrl + space each time? ...

Canceling a window in JSP and navigating back to the previous page using JavaScript

Here is my Java class controller: public class Controller extends HttpServlet { private Chooser chooser = Chooser.INSTANCE; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOExcep ...

Is there a way to select the webkit-datetime-edit-year-field of Google Chrome in a disabled input using CSS?

I have a CSS class defined in my module.css file that alters the color of yyyy when an input is in range: .field input[type='date']:in-range::-webkit-datetime-edit-year-field { color: transparent; } This styling works as expected when the inpu ...

JavaScript - Getting the name of a sub-class when extending the Error class

In my application, I have custom Errors that I wanted to check for later using the constructor name. However, when I extend Error in my classes, the constructor name always displays as "Error" instead of the actual name I assigned to it. I conducted some ...

Calculating velocity and displacement with a specific acceleration in JavaScript: A step-by-step guide

I am faced with a challenge involving a JSON file containing acceleration values in m/s^2 along with timestamps. On the other hand, I have a ThreeJS mesh that needs to translate and rotate based on input values. My goal is to pass velocity and displacement ...

Difficulty encountered while trying to access JSON object

I've been utilizing the LinkedIn JS API to retrieve a list of individuals. The data is returned in JSON format, and here is the callback function: .result(function (result) { profile = result.values[0]; // Perform an action with the f ...