What is the best way to align a div in the middle of an input-group div?

When dealing with a nested div, where the parent div is styled with an input-group class, my goal is to have the nested div centered within the available line or on the next free line space.

This is the desired layout I want to achieve regardless of the number of labels present:

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

.small-numeric-container {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center
}

.small-numeric-container label {
  margin-right: 10px;
  margin-top: 10px;
}

.numeric-small {
  width: 85px;
}

.box {
  display: inline-block;
  margin-top: 10px;
  justify-content: center;
}

.rating {
  position: relative;
  display: flex;
  flex-direction: row-reverse;
  border: 1px solid #ffffff;
  box-sizing: border-box;
  background: linear-gradient(to right, #ff0000, #20c997, #0f5132);
}

.rating input {
  display: none;
}

.rating label {
  display: flex;
  cursor: pointer;
  width: 45px;
  height: 45px;
  justify-content: center;
  align-items: center;
  transition: 0.5s;
  background: #212529;
  border-right: 1px solid #ffffff;
}

.h-separator {
  width: 10px;
}

.rating input[type="radio"]:hover~label,
.rating input[type="radio"]:checked~label {
  background: transparent;
  color: #000000;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="63010c0c17101711021323564d514d50">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" />

<div class="input-group">
  <div class="small-numeric-container">
    <label>
      <input class="form-control numeric-small" max="15" min="2"
             name="0-scale" placeholder="Scale" type="number" value="10">
    </label>
  </div>
  <span class="h-separator"></span>
  <div class="box">
    <div class="rating">
      <input id="rating-label-10" name="rating" type="radio">
      <label for="rating-label-10">10</label>
      <input id="rating-label-9" name="rating" type="radio">
      <label for="rating-label-9">9</label>
      <input id="rating-label-8" name="rating" type="radio">
      <label for="rating-label-8">8</label>
      <input id="rating-label-7" name="rating" type="radio">
      <label for="rating-label-7">7</label>
      <input id="rating-label-6" name="rating" type="radio">
      <label for="rating-label-6">6</label>
      <input id="rating-label-5" name="rating" type="radio">
      <label for="rating-label-5">5</label>
      <input id="rating-label-4" name="rating" type="radio">
      <label for="rating-label-4">4</label>
      <input id="rating-label-3" name="rating" type="radio">
      <label for="rating-label-3">3</label>
      <input id="rating-label-2" name="rating" type="radio">
      <label for="rating-label-2">2</label>
      <input id="rating-label-1" name="rating" type="radio">
      <label for="rating-label-1">1</label>
    </div>
  </div>
</div>

Answer №1

Utilizing flexbox on the input group and applying auto margins to the inner box seems to achieve the desired result.

Furthermore, a considerable portion of your custom CSS can be substituted with Bootstrap classes for flex, margins, display, borders, etc. I recommend reviewing the library to familiarize yourself with its offerings. Avoid reinventing wheels that are already available to you.

.small-numeric-container label {
  margin-right: 10px;
  margin-top: 10px;
}

.numeric-small {
  width: 85px;
}

.rating {
  background: linear-gradient(to right, #ff0000, #20c997, #0f5132);
}

.rating input {
  display: none;
}

.rating label {
  display: flex;
  cursor: pointer;
  width: 45px;
  height: 45px;
  justify-content: center;
  align-items: center;
  transition: 0.5s;
  background: #212529;
  border-right: 1px solid #ffffff;
}

.h-separator {
  width: 10px;
}

.rating input[type="radio"]:hover~label,
.rating input[type="radio"]:checked~label {
  background: transparent;
  color: #000000;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="35575a5a41464147544575001b071b06">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" />

<div class="input-group d-flex">
  <div class="small-numeric-container d-flex justify-content-center align-items-center">
    <label>
      <input class="form-control numeric-small" max="15" min="2"
             name="0-scale" placeholder="Scale" type="number" value="10">
    </label>
  </div>

  <div class="box d-inline-block mt-2 justify-content-center mx-auto">
    <div class="rating d-flex flex-row-reverse border border-white">
      <input id="rating-label-10" name="rating" type="radio">
      <label for="rating-label-10">10</label>
      <input id="rating-label-9" name="rating" type="radio">
      <label for="rating-label-9">9</label>
      <input id="rating-label-8" name="rating" type="radio">
      <label for="rating-label-8">8</label>
      <input id="rating-label-7" name="rating" type="radio">
      <label for="rating-label-7">7</label>
      <input id="rating-label-6" name="rating" type="radio">
      <label for="rating-label-6">6</label>
      <input id="rating-label-5" name="rating" type="radio">
      <label for="rating-label-5">5</label>
      <input id="rating-label-4" name="rating" type="radio">
      <label for="rating-label-4">4</label>
      <input id="rating-label-3" name="rating" type="radio">
      <label for="rating-label-3">3</label>
      <input id="rating-label-2" name="rating" type="radio">
      <label for="rating-label-2">2</label>
      <input id="rating-label-1" name="rating" type="radio">
      <label for="rating-label-1">1</label>
    </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

Steer clear of utilizing CSS pseudo-elements like :before and :after

I am seeking a solution to hide a <label> when viewing on a small mobile device. Below is the HTML code: <label class="page_createpassword_label" for="page_createpassword"> <span class="page_label_main">Span Text</span> <span c ...

Using Angular to assign a CSS variable to the before/after pseudo-classes

Having trouble passing a variable with [ngStyle] and reading it on the ::before class, any guidance in the right direction would be much appreciated! CSS: .handle-slider__control { height: 7px; z-index:1; background: $colour-alto; positi ...

What is the most efficient method for integrating PHP script into HTML code?

When it comes to inserting the full path to every image/css file on my website due to url rewriting, I am searching for the most efficient method. I could simply use: <img src='<?php echo $full_path; ?>/images/theImg.jpg' alt='alt ...

A Step-by-Step Guide to Setting Up a Scoreboard for Your Rock Paper Scissors Game

I'm new to JavaScript and I want to create a rock, paper, scissors game from scratch. My Game Plan: Once the user clicks on an option (rock, paper, scissors), the game will display their score and the computer's score along with the result of t ...

The behavior of Mozilla in handling JQuery attr() function may result in variations in design elements such as font-family or font-size

I'm currently working on the login form. Below is my view in CodeIgnitor: <div id="login-form"> <?php echo heading('Login', 2); echo form_open('login/login_user'); echo form_input('email', set_value ...

Tips for avoiding a button reverting to its original state upon page refresh

I have a button with the ID #first that, when clicked, is replaced by another button with the ID #second. However, if I refresh the page after clicking on the second button, it goes back to displaying the first button. Is there a way to make sure that th ...

Is it possible to create a custom tag in HTML 4?

What is a custom tag in HTML (Kindly note that I am specifically talking about the differences from XHTML)? I need to include some additional text to meet StackOverflow's posting requirements. Thanks! ...

Transforming a canvas element into an image sans the use of toDataURL or toBlob

Recently, I developed a small tool which involves adding a canvas element to the DOM. Strangely, when trying to use toBlob or toDataURL, I encountered an issue with the canvas being tainted and received the error message (specifically in chromium): Uncaugh ...

Issue with Bootstrap carousel control not switching back to the previous slide

Interestingly, the carousel is functioning for sliding to the next slide but not for going back to the previous one. I'm still getting the hang of jsfiddle, as you can see, highlighting works for the next slide but not for the previous. Check out the ...

The responsive menu refuses to appear

my code is located below Link to my CodePen project I'm specifically focusing on the mobile view of the website. Please resize your screen until you see the layout that I'm referring to. I suspect there might be an issue with my JavaScript cod ...

Immersive jQuery slideshow embellished with an interactive counter, captivating thumbnails, dynamic progress bar,

Hey there! I'm currently working on my very first website and I could really use some assistance in creating a slider with images. I've tried searching for a solution to my problem online, but even after attempting to fix the suggested plugin, I ...

Retrieve the outer-HTML of an element when it is clicked

I am working on a project to develop a tool for locating xpath, and I am seeking the most efficient and user-friendly method for allowing the user to select the desired element on a webpage. Ideally, this selection should be made with just a single click, ...

Highcharts: Show tooltip above all elements

Is there a way to configure tooltip display above all elements? I want to define specific coordinates so that the tooltip remains open even if it is covering the chart, and only closes when interacting with the top block. Screenshot 1 Screenshot 2 For e ...

The concept of recursion in AngularJS directives

I developed a unique custom directive. angular.module('menu',[]) .directive('MenuDirective',function(){ return{ restrict:'E', replace:'true', scope:{ m ...

Link embedded within a list item

How can I make the hyperlink element fill up the width and height of the li element inside? <ul> <li><a href="#" style="display: block; width: 100%; height: 100%;">this link to fill up the li element width and height</a><l ...

What is the best way to ensure that every item on a list can be

Is there a way to make both the left and right sides of a list item clickable? Currently, only the right side is clickable in my case. Any suggestions? This is my HTML: <body> <aside id = "aside"> <div id="column"> ...

Every time I input information into the form, it keeps coming back as undefined

function displayProduct() { var product = document.getElementById("productForm"); var item = product.elements["item"].value ; document.getElementById("outputResult").innerHTML = item ; } <div> <p id="outputResult"></p> </di ...

Issue with using float-right in Bootstrap v4. [Alternative to pull-right]

I am currently using the most recent version of Bootstrap, which is v4.0.0-beta.3. I am having trouble getting elements to align properly according to the official documentation. Despite searching online for solutions, I have not been able to find any that ...

How can I arrange individual events in agendaWeek view on FullCalendar.io and then merge them all into one line?

I am using Fullcalendar.io version 2 When I switch to the agendaWeek mode, all my events are displayed on one line in each day square. As a result, the more events I have, the thinner the event blocks become. Is there a way for me to display only one eve ...

How to Implement Dotted Line Styling for Row Data in ReactJS with CSS?

I'm working on a condensed design with Reactjs Monday......................08:00 AM - 21:00 PM I have a lot of dots in the display above. How can I make it shorter without using so many dots? Entering numerous dots like this is not ideal for the webs ...