Aligning an element vertically with the surrounding text on the same line

Trying to incorporate inline input range elements in a way that aligns with the line while also aligning with the right edge of the outer div has proven to be challenging. The HTML on the site is formatted using prettify, resulting in <span> elements within a <pre> element where whitespace, especially linefeeds, plays a significant role.

The aim is to insert <input type="range"> elements enclosed by 2 divs such that they align smoothly with the content on the same line but at the right side of the outer container.

Despite attempts, no viable solution has been found yet. Initially, adjusting the font size and inserting extra content inside the .holder elements seemed to improve alignment visually, but upon increasing the font size, it became evident that the alignment was not exact.

Is there a practical way to address this issue?

Normally, one might opt for a table (separate column for sliders) or a flexbox layout, but these solutions necessitate parsing all the code to identify line breaks and then generate additional HTML, which isn't ideal.

.outer {
  position: relative;
  background: pink;
  padding: 1em;
  width: 200px;
}
.holder {
  display: inline-block;
}
.holder>div {
  position: absolute;
  display: inline-block;
  right: 0;
}
.holder input {
  position: absolute;
  display: inline-block;
  right: 1em;
}
<h1>test</h1>
<div class="outer">
  <div>
    <span>foo: </span>
    <div class="holder">&nbsp;
      <div>
        <input type="range">
      </div>
    </div>
  </div>
  <div>
    <span>bar: </span>
    <div class="holder">
      <div>
        <input type="range">
      </div>
    </div>
  </div>
</div>

Note: The displayed code exemplifies the problem, but the real code consists of whitespace-sensitive pre and span elements, making it hard to decipher. This representation simplifies the structure.

body {
  font-size: 35pt;
}
.outer {
  position: relative;
  background: pink;
  padding: 0.2em;
  width: 300px;
}
.holder {
  display: inline-block;
}
.holder>div {
  position: absolute;
  display: inline-block;
  right: 0;
}
.holder input {
  position: absolute;
  display: inline-block;
  right: 1em;
}
<pre class="outer"><span>foo: </span><div class="holder">&nbsp;<div><input type="range"></div></div></div>
<span>bar: </span><div class="holder"><div><input type="range"></div></div>
</pre>

After generating <pre> and <span> elements, the range inputs are introduced into the equation.

Another aspect to consider is that on the live site, if a line exceeds a certain length, the <pre> section acquires scrollbars (similar to S.O.'s code area). However, even in such cases, the intention is to keep the input areas aligned to the right boundary of the visible region (without scrolling).

body {
  font-size: 35pt;
}
.outer {
  position: relative;
  background: pink;
  padding: 0.2em;
  width: 300px;
  overflow: auto;
}
.holder {
  display: inline-block;
}
.holder>div {
  position: absolute;
  display: inline-block;
  right: 0;
}
.holder input {
  position: absolute;
  display: inline-block;
  right: 1em;
}
<pre class="outer"><span>longlongline: </span><div class="holder">&nbsp;<div><input type="range"></div></div></div>
<span>bar: </span><div class="holder"><div><input type="range"></div></div>
</pre>

A visual representation of the final outcome could provide clarity.

To prevent sliders from being pushed off screen when the window size is restricted, they are made transparent and positioned absolutely instead of relying solely on CSS (display: inline-block). This approach guarantees both readability of the code and interactiveness with the sliders.

Though the current appearance suggests alignment, resizing the window under specific conditions exposes unintended misalignments. Referencing the page provided and adjusting settings as described will highlight the existing discrepancies.

Rather than resorting to reorganizing each line into separate containers, striving to utilize appropriate CSS rules, if available, to achieve precise alignment remains preferable despite potential constraints.

Answer №1


    .container {
      background: pink;
      padding: 1em;
      width: 200px;
    }

    .box {
      display: inline-block;
      vertical-align: middle;
    }

    .box > div {
      display: inline-block;
    }

    .box input {
      display: inline-block;
    }

Answer №2

One possible solution is to include the CSS rule "vertical-align:middle"

    .outer {
        position: relative;
        background: pink;
        padding: 1em;
        width: 200px;
    }
    .outer>div{
        position: relative;
        display: inline-block;

    }
    span{
        vertical-align:middle;
    }
    .holder {
        vertical-align:middle;
        display: inline-block;
    }
    .holder>div {
        position: relative;
        display: inline-block;
        right: 0;
    }
    .holder input {
        position: relative;
        display: inline-block;

    }


<h1>test</h1>
<div class="outer">
<div>
    <span>
        foo: 
    </span>
    <div class="holder">
        <div>
            <input type="range" />
        </div>
    </div>
</div>
<div>
    <span>
        bar: 
    </span>
    <div class="holder">
    <div>
        <input type="range" />
    </div>
    </div>
</div>
</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

a guide on accessing key value elements within an array using Ionic 3

Just diving into the world of Ionic, I am currently working on a task to showcase products on the cart page that have been added to the cart. Upon fetching data from a REST API, I can see the response below in the console. "items": { "29.2.2.0.YTowOnt ...

Improve the loading speed of large datasets into HTML tables

I have a problem where I generate an HTML table from code behind to display data like a grid (Note: I am not using GridView). When the data is large, around 2000 rows, it causes the application to display a white page as if it has hung. What is the best w ...

Learn how to call class methods using jQuery events by utilizing the on() method

I'm attempting to create a reusable "component" using both HTML5 and accompanying JavaScript code that can be utilized multiple times. First, let's examine the HTML5 component code: <div class="listEditor" id="myStringList"> <div id="li ...

What is the best way to utilize a single component from Twitter Bootstrap?

Is there a way to use the Twitter Bootstrap plugin from this website ( https://github.com/silviomoreto/bootstrap-select ) for only one specific element on my webpage? I don't want all elements on my site to adopt the Twitter Bootstrap styling (such as ...

Exploring the world of animation with Jquery and images

I'm delving into the world of jQuery to create some captivating animations. Currently, I've been tasked with an assignment involving images that expand, contract, change opacity, and eventually hide. Below is my code snippet: function play_pic1 ...

When the button with an image is clicked, it will display a loading element

I have developed an application that heavily utilizes ajax, and I am looking to implement the following functionality: I would like to have a button with both text and an image. When this button is clicked, it should be disabled, and instead of the origina ...

Eliminate the margin on the subnavigation menu

Hey there, I'm looking to adjust the layout of my menu buttons by removing the left and right "margins" (if that's the correct term), so they all fit neatly inside the navigation bar. https://i.sstatic.net/udes8.png I want to ensure that all th ...

Problem with Scroll Listener on Image in Angular 4: Window Scroll Functioning Properly

Hello, I am a newcomer who is currently working on creating a small application that allows users to zoom in on an image using the mouse scroll. <img (window:scroll)="onScroll($event) .....></img> The code above works well, however, it detec ...

CSS-only countdown animation

I've been experimenting with a pure HTML/CSS countdown animation, but I'm facing challenges in getting it to run smoothly. Currently, I'm focusing on minutes and seconds only. One issue I encountered is that the digit representing tens of mi ...

Angular 5 mobile row aligns vertically, not inline with Bootstrap

Is there a way to make the row inline when the width is less than 579px? I want it to look the same on both mobile and desktop. "styles": [ "../node_modules/font-awesome/scss/font-awesome.scss", "../node_modules/angular-bootstrap-md/scss/bootstra ...

Tips on how to update the styling of an active link

http://jsfiddle.net/G8djC/2/ Looking to create a tabbed area where content changes based on the tab clicked. The Javascript function switches the link class to active upon clicking. However, struggling to change the color of the active tab beyond the firs ...

Using Bootstrap to create a fixed footer on a webpage

Is there a substitute for navbar-static-bottom? I currently have my footer set up like this: <div class="navbar navbar-default navbar-fixed-bottom"> <div class="container"> <p class="navbar-text pull-left">&a ...

Identifying copy and paste behavior within an HTML form using .NET CORE MVC 2.1

I inherited a project at my current company and one of the tasks involves allowing users to paste multiple ISBN numbers from Excel into a search field on my Index page (HTML). Sometimes, the ISBNs are poorly formatted with letters or special symbols mixed ...

Troubleshooting Problem with Website Responsiveness on iPhones Using Bootstrap 5

I am currently experiencing a challenge involving the responsiveness of a website I am developing, particularly when it comes to iPhones. The site utilizes Bootstrap 5 and displays correctly on Android devices and in Chrome Dev Tools. However, upon testing ...

Guide to updating 2 iframes simultaneously with a single link/button using HTML and JavaScript

Just joined the community and looking for help on how to refresh two different iframes within a single page. I came across a solution on Google that involves using getElementById. However, I've heard Firefox can be tricky with IDs. Appreciate any as ...

"Want to make a phone call with an extension? Just click here

The issue at hand is rather straightforward. Is there a way to create a link for mobile browsers that would automatically dial a phone number and input an extension without requiring any action from the user? I am familiar with using tel:, as well as spe ...

What is the best way to move the numerical range value into a span element?

How can I implement the "update" function to retrieve the current slider value and transfer it to a Meter element with the message "Value: [current value]". The indicator color should be #ffff00 if the current value is at least 85, otherwise, it should b ...

Break the line after every space in words within an element

I have a table/grid view with two words in the <td> like "C2 Sunday". I need a line break after C2 so that it appears as: C2 Sunday Is there a way to achieve this? Please assist me with this issue. Currently, it is showing as "C2 Sunday" and I wou ...

What is the alternative to using echo when outputting HTML?

I am working on a function... $post_text .= '<div style="font-size: 15px; color: blueviolet">'; $post_text .= "<br>"; $post_text .= 'Text number 1.'; $post_text .= "<br>"; $post_text .= 'Text number 2.'; $po ...

Increase the padding on Bootstrap 4 Sticky footer for a more polished look

While working on my Bootstrap 4 website hosted on Apache (Ubuntu Linux), I encountered an issue with the sticky footer. When there is only a small amount of content on the page, I have to scroll through excessive white space to reach the footer. I'm ...