Could you please let me know how I can align underneath?

Image description can be found here

Your assistance is greatly appreciated, as I am currently inexperienced with React and JavaScript. I am self-studying, so please excuse any basic questions. I have encountered a challenge with this issue.

In the image below,

Distance, Time, and Pace are centered. I would like to align Distance: Time: Pace: left, followed by individual input boxes arranged like this:

https://i.sstatic.net/11TGP.png

// Using JSX 
import React, {useState, useEffect} from 'react';
import Logo from '../components/Logo';
import './styles.css'


// Defining a functional React component

function Home() {
   const [message, setMessage]=useState([]);
   useEffect(()=>{
     fetch("/test/hello")
         .then((response)=>{
           return response.json();
         })
         .then((data)=>{
             setMessage(data);
         });
   },[]);
   return (
      <div>
         <div className="logo-container">
         <Logo />
         <h1 style={{ fontSize: '40px', color: '#333333' }}>Pace Calculator</h1>
         </div>

         <div className="input-container">
      <div className="input-group">
        <label className="label">
          Distance<span className="colon">:</span>
          <input className="input-field" type="text" placeholder="min" />
          : 
          <input className="input-field" type="text" placeholder="sec" />
          /km
        </label>
      </div>

      <div className="input-group">
        <label className="label">
          Time<span className="colon">:</span>
          <input className="input-field" type="text" placeholder="hr" />
          : 
          <input className="input-field" type="text" placeholder="min" />
          : 
          <input className="input-field" type="text" placeholder="sec" />
        </label>
      </div>

      <div className="input-group">
        <label className="label">
          Pace<span className="colon">:</span>
          <input className="input-field" type="text" placeholder="min" />
          : 
          <input className="input-field" type="text" placeholder="sec" />
          /km
        </label>
      </div>
    </div>
    <button className="calculate-button">Calculate</button>

    </div>
   );
 }
 
export default Home;

/* styles.css */

.input-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  border: 2px solid #333; /* Add a solid border with the color of your choice */
  border-radius: 10px;
  padding: 10px; /* Add padding for better visual appearance */
  max-width: 500px;
  margin: auto;
  margin-bottom: 20px;

}

.input-group {
  display: flex; /* Display the input groups in a row */
  align-items: center; /* Align items vertically in the input groups */
  margin-bottom: 10px;
}

.label {
  font-size: 30px; /* Adjust the font size as needed */
}


.input-field {
  width: 40px; 
  height: 25px;
  border-radius: 5px;
}

.calculate-button {
  margin-top: 30px; 
  background-color: #808080; 
  color: #fff; 
  padding: 10px 20px; 
  font-size: 30px; 
  cursor: pointer;
  border: none;
  border-radius: 5px;
  display: block;
  margin: 0 auto; /* Center the button */
}
.calculate-button:hover {
  background-color: #1b1b1b; /* Darker green color on hover */
}

Answer №1

Modifying the HTML structure slightly to enclose the input group title (e.g. "Distance" and the first colon) in their own span along with display: contents to enable the children to adopt the CSS-Grid layout when using subgrid.

While this approach entails utilizing some advanced CSS properties and suggests a reorganization of the entire HTML structure and layout for better results, it does appear to accomplish the desired outcome.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

 ::before,
 ::after {
  box-sizing: inherit;
}

.input-container {
  display: grid;
  grid-template-columns: min-content 40px .75em 40px .75em 40px;
  align-items: center;
  border: 2px solid #333;
  /* Add a solid border with the color of your choice */
  border-radius: 10px;
  padding: 10px;
  /* Add padding for better visual appearance */
  max-width: 500px;
  margin: auto;
  margin-bottom: 20px;
}

.input-group {
  display: contents;
  margin-bottom: 10px;
}

.label {
  font-size: 30px;
  /* Adjust the font size as needed */
  display: contents;
}

.colon {
  grid-column: 1;
  margin-right: .5em;
}

.input-field {
  width: 40px;
  height: 25px;
  border-radius: 5px;
}
<div class="input-container">
  <div class="input-group">
    <label class="label">
      <span class="colon">Distance:</span>
      <input class="input-field" type="text" placeholder="min" />
      :
      <input class="input-field" type="text" placeholder="sec" />
      /km
    </label>
  </div>

  <div class="input-group">
    <label class="label">
      <span class="colon">Time:</span>
      <input class="input-field" type="text" placeholder="hr" />
      :
      <input class="input-field" type="text" placeholder="min" />
      :
      <input class="input-field" type="text" placeholder="sec" />
    </label>
  </div>

  <div class="input-group">
    <label class="label">
      <span class="colon">Pace:</span>
      <input class="input-field" type="text" placeholder="min" />
      :
      <input class="input-field" type="text" placeholder="sec" />
      /km
    </label>
  </div>
</div>

Answer №2

One way to simplify the design is by setting the width of labels (Distance/Time/Pace) and ensuring they are left-aligned. All other input fields will follow suit based on your specifications.

Just a heads up: The JSX elements were not well structured initially, so I made some changes to ensure there is one parent node and only two direct child nodes. Please see the updated code snippet below. Thank you!

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

.input-field-label {
  border: 1px solid green;
  display: inline-block;
  width: 100px;
  margin-bottom: 16px;
}

.input-field-wrapper {
  .input-field {
    border: 1px solid silver;
    display: inline-block;
    width: 40px;
    height: 20px;
  }
}
    <div class="input-container">
      <!-- Parent -->
      <div class="input-group">
        <!-- Child 1 -->
        <label class="input-field-label"> Distance: </label>
        <!-- Child 2 -->
        <span class="input-field-wrapper">
          <input class="input-field" type="text" placeholder="min" />
          :
          <input class="input-field" type="text" placeholder="sec" />
          /km
        </span>
      </div>

      <div class="input-group">
        <label class="input-field-label"> Time: </label>

        <span class="input-field-wrapper">
          <input class="input-field" type="text" placeholder="hr" />
          :
          <input class="input-field" type="text" placeholder="min" />
          :
          <input class="input-field" type="text" placeholder="sec" />
        </span>
      </div>

      <div class="input-group">
        <label class="input-field-label">Pace:</label>
        <span class="input-field-wrapper">
          <input class="input-field" type="text" placeholder="min" />
          :
          <input class="input-field" type="text" placeholder="sec" />
          /km
        </span>
      </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

Using Jquery to toggle classes between "display block" and "display none

Hello friends, I'm in need of some help with my code. $(document).ready(function() { var circle = $('.circle'); $(".send a").click(function(e) { e.preventDefault(); $('.wrap').css('display', 'bl ...

Is there a way to reset useQuery cache from a different component?

I am facing an issue with my parent component attempting to invalidate the query cache of a child component: const Child = () => { const { data } = useQuery('queryKey', () => fetch('something')) return <Text>{data}& ...

What is the reason behind the failure of next/script with Google reCAPTCHA?

Currently, I am in the process of upgrading from next js version 8 to version 11. I wanted to take advantage of the amazing next js feature for "next/script". However, when I tried to implement it for Google reCAPTCHA using "react-recaptcha": "^2.3.10", th ...

Enliven the character limit reaction by incorporating a thrilling shake animation when it reaches

I have implemented a feature in my component where if a user reaches the character limit, the component should shake. However, despite my efforts, the shaking effect is not working in the current code. const useStyles = makeStyles(() => ({ shake ...

Tips for securely storing and retrieving access tokens in NextJS server-side rendering

My current setup involves using NextJS for Server-Side Rendering. For authentication, I rely on an authentication service that provides an access_token (JWT). Due to the limitations of using localStorage in the getServerSideProps function, I have opted t ...

Ways to troubleshoot the following issue:u003cemailu003e: The module is not compatible with the "node" engine

linix@linix-HP-ProBook-6475b:~/projects/builds$ npx create-next-app hellochat npx: installed 1 in 2.333s Creating a new Next.js app in /home/linix/projects/builds/hellochat. Installing react, react-dom, and next using yarn... yarn add v ...

Incorporating a new element into the property

Just getting started with React, so please forgive me if I mess up the terminology! I've brought in some images at the beginning of my file using import face_1 from "./assets/face-1.png"; (with numbers going up to 6 since I'm creating dice) Lat ...

What is the best way to inject child nodes into parent nodes without causing the view to shift to the location where the element is being

While I am rendering elements and appending them to a parent div, my screen keeps jumping to the bottom-most element as it loads instead of maintaining the current view. Ideally, it should start at the top of the screen and remain there without any sudden ...

How can I ensure that my background image always stretches to full width, no matter how small the site format becomes in CSS?

I'm currently working on making my website responsive, but I'm encountering an issue. When the website width gets smaller, the background doesn't take up the full width as intended. I've tried various methods to address this problem, bu ...

Transform a desktop site into a mobile-friendly version

After completing my website, I noticed that it looks great when viewed on a desktop browser. Unfortunately, the mobile version is completely unreadable... I really need a mobile-friendly version of my site but don't have the time or knowledge to lear ...

Guide to building a straightforward line graph using JSON data from a server

When I retrieve price change data from an API endpoint using axios, the response I receive is in the following format: 0: (2) [1639382650242, 48759.49757943229] 1: (2) [1639386250855, 49131.24712464529] 2: (2) [1639389730718, 48952.65930253478] 3: (2) [163 ...

How to Upload an Image with React and Express

I'm facing difficulties with uploading images using React and Express. I have been using javascript FileReader to set the image state for a real-time preview before form submission. Here is the code snippet: uploadImage = (e) => { const reader ...

Changing the dimensions of the table border and its color in DataTables

Looking at the provided table structure: library(DT) datatable( iris, style = "default", filter = "top", class = "hover stripe compact" ) I successfully modified the footer border with the following CSS code: table.dataTable.no-footer { bor ...

Filtering function that works without specific knowledge of keys

I'm working on a JavaScript method to filter a list dynamically without knowing the specific key (s). I've made some progress, but I'm stuck and not sure how to proceed. The foreach loop I have isn't correct, but I used it for clarity. ...

Modify CSS when scrolling, based on the size of the screen

I've implemented this JQuery script to modify elements in my header as the user scrolls. Is there a way to limit these modifications based on the screen size? Here's the code snippet: $(window).scroll(function() { // Once the user has scro ...

Why is my React app opening in Google Chrome instead of the API?

I have a link in my React app that is supposed to open a PDF in another page, but for some reason Google Chrome is redirecting me to the React application instead of opening the API endpoint that renders the PDF. The URL for the PDF is /api/file/:_id and m ...

Issue: My Mini-Chat Box is experiencing overflow problems in Internet Explorer

Encountering another cross-compatibility glitch with IE on my website, www.zerozaku.com. While it works fine on Chrome and Firefox, IE is having trouble with the Mini-Chat box overflowing. Any assistance would be greatly appreciated! For testing purposes, ...

Can the language of the iOS actions menu be customized?

I am currently utilizing a React Native app with ReactNativeWebView. When I click on the following code snippet: <input ref={hiddenFileInput} hidden accept='.pdf, .png, .jpg, .jpeg' type ...

The issue of the React input inside the modal component losing focus and triggering state re-rendering

Recently, I've encountered an issue with a modal component that I'm working on. Whenever I type characters into either input field, the focus is lost after entering just one character. I tried adding some console.logs to debug the problem and no ...

I need help figuring out how to create dynamic bars that change colors based on their values. Any suggestions on how to

Seeking a solution to create interactive live-charts similar to the image provided. I've explored various chart libraries like Highcharts, fusioncharts, raphaeljs and many others, but haven't found one that fits my needs perfectly. I experimented ...