What is the best way to ensure that my label and input element contents are centered and displayed on separate lines?

I need assistance with aligning the contents of my form. I want each label to appear on a separate line with the input content aligned below the label. Can someone help me achieve this? I have shared an image link and my code for reference.

My goal is to have labels displayed above the input elements, with the content centered. For example, radio buttons should be perfectly centered with their text description. If "male" is an option for a radio button, I want the input button to be centrally aligned with the word "male".

html,
body {
  height: 100vh;
  width: 100vw;
  margin: 0;
  padding: 0;
  display: block;
}

div {
  display: block;
}

.form-group {
  margin: 0 auto;
}

.container {
  max-width: 600px;
  margin: auto;
}

form {
  height: 100%;
  width: 100%;
  display: block;
}

label {
  padding: 0.25rem;
  display: flex;
  width: 100%;
}

input[type="radio"],
label {
  align-items: center;
}

input {
  width: 100%;
  display: block;
}
<body>
  <div class="container" style="text-align: center;">
    <h1 id="title">Studentas Survey Form</h1>
    <p id="description">Thank you for taking time to complete this survey</p>
  </div>
  <div class="container">
    <form id="survey-form">
      <div class="form-group">
        <label id="name-label">Enter your name
        <input type="text" id="name" placeholder="Enter your name" required></input>
        </label>

        <label id="email-label">Enter your Email 
        <input type="email" id="email" placeholder="Enter your email" required></input>
        </label>

        <label id="number-label">Enter your age 
        <input type="number" id="number" min="1" max="99" placeholder="Age" required></input>
        </label>

        <label>Favorite subject?
          <select id="dropdown">
            <option value="">Select an option</option>
            <option value="1">History</option>
            <option value="2">Math</option>
            <option value="3">Science</option>
            <option value="4">English</option>
          </select>
        <label>

        <label>What is your gender?
          <input type="radio" name="gender" required>Male</label>
        <input type="radio" name="gender" required>Female</label>
        </label>

        <label>What do you like about school?
          <input type="checkbox" value="lunch" required>Lunch Time</input>
          <input type="checkbox" value="social" required>Social Interaction</input>
          <input type="checkbox" value="work" required>Course Work</input>
          <input type="checkbox" value="home" required>Going Home</input>
        </label>

        <label>What are your thoughts on this survey?
          <textarea></textarea>
        </label>
        <input type="submit"></input>
      </div>
    </form>
  </div>
</body>

Answer №1

Take a look at this!

.Selected-label
{
    display:flex;
    justify-content:space-around;
    align-items:center;
    flex-direction:row;
}

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

Retrieving Files using Ajax Across Different File Types

I recently came across the following code snippet: DOM_imgDir = "img/UI/DOM/"; fileextension = ".jpg"; $.ajax({ url: DOM_imgDir, success: function (data) { $(data).find("a:contains(" + fileextension + ")").each(function () { filename = thi ...

Guide to making a toggle button with a Light/Dark mode functionality

I have implemented a light/dark toggle button and now I want to integrate the functionality for switching between light and dark themes on my webpage. The switch should toggle the theme between dark and light modes when clicked. Some basic styling has been ...

A comparison between centering an SVG element: Chrome versus Safari

Attempting to align an SVG element in the center of its parent container is proving challenging due to inconsistencies between Chrome and Safari. In Chrome, the text is perfectly centered inside the square, but not so in Safari: <svg width="200px" heig ...

Ways to center card components (rmwc) horizontally within a React application

Within my parent component: { this.state.projectList.map(data => <Item details = {data}/>) } Inside the child component Item render() { return ( <div style={{'width':'100%', 'tex ...

Having trouble with positioning divs on top of each other? Struggling to get position absolute to work correctly?

Currently, I am utilizing flexbox to construct a grid layout. However, I have hit a roadblock when attempting to make the divs stack on top of each other. The overflow:auto is hidden and position relative has been added to the carddiv. While the divs are s ...

Generate a new JSON reply using the current response

I received a JSON response that contains values for week 1, week 2, week 3, and week 4 under the 'week' key, along with counts based on categories (meetingHash) and weeks. I attempted to merge this data using the .reduce method but without succes ...

Is there a way for me to detect if the user has manipulated the CSS or JavaScript in order to alter the look of my website?

Is there a way to determine if someone is utilizing script or CSS extension tools? For instance, if they have adjusted alignments, applied display: none; to multiple elements, or utilized jQuery's .hasClass to manipulate divs. ...

Can a list be transformed into a toolbar in a responsive design?

Here is my card with a nav section that includes multiple items (although I've only included one here for simplicity). <div class="col-md-3"> <div class="card"> <div class="card-header"> ...

jQuery does not support animations for sliding up or down

I'm struggling to conceal a navigation element once the top of the #preFooter section is scrolled to. In order to make the nav element mobile-friendly, I have designed both the .tab-wrap and .tab-wrap-mobile. To enable these elements to slide away w ...

What could be causing the malfunction of the .setAttribute() and .removeAttribute functions in my JavaScript program?

Below is a flashcard game I have developed: Once a user enters the correct answer in the text box, "Correct!" is expected to be displayed in bold green font. The CSS attributes are then supposed to be removed shortly after. Here is the HTML code for the i ...

Why does Angular Redirection initiate both my method and NgOnInit?

As a beginner in Angular, I am encountering an issue with Angular Redirection. Whenever I manually clear storage, the code executes ngOnInit, prompts an error, and redirects to the Login page as expected. However, when I attempt to do the same using the lo ...

Particle JS - Taking over the entire screen

I have incorporated Particle JS into the banner using the link provided. It should be confined within the banner, with a white background underneath displaying the header text "hello there." However, the Particle.JS effect is currently taking over the enti ...

Which takes precedence: the end of the script tag or the backtick?

Currently, I am working on developing a page builder widget. My goal is to save the entirety of the HTML code for the edited page to both local storage and a database. The PHP script will load the saved HTML from the database, while JavaScript will handle ...

Utilizing CSS to Select Specific Sections

I'm curious about how to target a section element with a specific id like #dress in CSS3. Here is my code snippet: <section id="dress"> <p>Lorem Ipsum..................................of Lorem Ipsum.<> </section> Here's ...

How to turn off span text in AngularJS

What is the best way to deactivate the text within a span using angularjs? I'm having trouble with ng-disabled in my situation. Below is the code for the span element: <span ng-click="clickMe()" ng-if="true" ng-disabled="true">Click Me!</sp ...

Error importing reach-router in Gatsbyjs causing website to break

While working on my Gatsby project, I decided to incorporate the React Cookie Consent package. However, upon installation and implementation attempt, my website crashed, displaying this error message: warn ./.cache/root.js Attempted import error: &a ...

How can I make CSS/HTML tables display inline until they are wider than the parent div?

Is there a way to position two tables next to each other until one table grows wider than the set minimum width, and then have the second table appear below the first, with both tables centered within their parent <div>? This is the current setup: ...

Automatically update the selection in an md-select dropdown

I am dealing with the following HTML structure <md-select id="testSelect" ng-model="objElements" ng-change="onElementChange()" name="objElements" placeholder="Select Stuff"> <md-option id="testOption" ng-r ...

how to use AngularJS filter and ng-options to format specific options as bold in a select dropdown

I am currently utilizing AngularJS ng-options to populate specific select elements. I am interested in bolding and disabling certain options. Despite trying to achieve this using the filter along with ng-options, I have been unsuccessful so far. While I ca ...

Having trouble configuring AJAX and PHP to work together

Here's the situation I'm dealing with: I have HTML, JS, and PHP files. In the PHP file, there is an associative array containing default values to populate form elements in the HTML file. I'm trying to use AJAX to retrieve data from the PHP ...