skip to Main Content

enter image description here

I respect your help, I do not have an experience React and Java Script.
I am studying by my self so far so sorry for basic(?) question.
I have been struggling with this problem.

In the screenshot,

Distance, Time and Pace are center.
I want to make Distance: Time: Pace: left, and then align input boxes indivisually like below

[enter image description here](https://phpout.com/wp-content/uploads/2023/12/11TGP.png)

// to use jsx 
import React, {useState, useEffect} from 'react';
import Logo from '../components/Logo';
import './styles.css'


// to reactcomponent(by function)

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 */
}

2

Answers


  1. A slight change in the HTML structure to wrap the input group title (e.g."Distance" and the first colon) in their own span together with display: contents to allow the children to inherit the CSS-Grid layout when using subgrid.

    This method require some very new CSS properties and frankly a re-organisation of the whole HTML structure and layout would be better but this does seem to achieve what you are looking for.

    * {
      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>
    Login or Signup to reply.
  2. one of the simplest approach can be to set width to the labels(Distance/Time/Pace) also making sure that they are left aligned, rest all other inputs fields will also be aligned as per your requirements.

    FYI: the JSX elements was not well structured, so i have refactored it should be 1 parent node and only 2 direct child nodes, plz refer the code snippet below Thanks

    enter image description here

    .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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search