Centered on the screen are the input field and corresponding label

I am in the process of creating a signup form, and I have encountered an issue. How can I make the input wider without using a fixed width like width: 420px? Additionally, I would like to center both the input field and the label. I envision something similar to this: image. However, my current layout looks like this: image.

This is my HTML code:

  <div className="container">
    <h1 className="title">Create an account</h1>
       
     <form className="form">
        <div className="form-outline">
            <label className="input-label">Your Name</label>
            <input
            name="username"
            type="text"
            className="input"
            value={input.username}
            onChange={onInputChange}
            onBlur={validateInput}
           />           
        </div>

        ...

    </form>

Here is my CSS code:

.container {
padding: 16px;
width: 80%;
margin: 5% auto 0 auto;
}

...

.login-here:hover {
color: #0a58ca;
}

Edited:

I also attempted the following:

.input-label {
color: #6c757d;
}

.form-outline {
margin-bottom: 1.5rem;
flex: 1 1 auto;
justify-content: center;
align-items: center;
display: flex;
flex-direction: column;
}

However, my label ends up in the middle of the screen as shown in this image. Using float: left for the label did not achieve the desired effect. I need the label to be positioned on the left side while making the input wider.

Answer №1

To improve the layout, it is advised to remove the `align-items` property. Instead, consider setting a media breakpoint for wider screens.

.form-outline {
margin-bottom: 1.5rem;
flex: 1 1 auto;
justify-content: center;
display: flex;
flex-direction: column;
}

Please use the corrected CSS provided below:



.form {
max-width: 500px;
margin: 0 auto;
}

.container {
padding: 16px;
width: 80%;
margin: 5% auto 0 auto;
}


.title {
text-align: center;
font-size: 2.5rem;
color: #0d6efd;
}

.input-label {
color: #6c757d;
}

.form-outline {
margin-bottom: 1.5rem;
flex: 1 1 auto;
}

.input {
padding: 0.45rem;
margin: 0.3rem 0 1.3rem 0;
font-size: 1.25rem;
border-radius: 0.5rem;
display: block;
color: #212529;
line-height: 1.5;
border: 1px solid #ced4da;
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
width:100%;
}

.input:focus {
color: #212529;
border-color: #86b7fe;
outline: 0;
box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25);
}

... (CSS continues)

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

What is the best way to display a welcoming message only once upon visiting the home page?

After gaining experience working with rails applications for a few months, I have been tasked with adding a feature that displays a welcome message to users visiting the site home page for the first time, but not on subsequent visits or page reloads. What ...

Encountering obstacles with asynchronous requests while attempting to retrieve an Excel file using ajax

I am coding JavaScript for a SharePoint project, focusing on creating a function to retrieve data from an Excel document in a library. While I have successfully implemented the basic functionality, I am facing an issue with large file sizes causing the dat ...

Importing a Vue.js component: Troubleshooting MIME type error

I recently started using Vue.js and decided to try out the https://github.com/olefirenko/vue-google-autocomplete component. I went ahead and downloaded this file from: https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email ...

Using Jquery to toggle columns based on their index and custom attribute

My table has two rows in the header: the first row with colspan and the second row containing headers. You can see an image below for reference. With a jQuery function, I extract all the table cell values from the second header row and add them to a div w ...

why is the fetchNextPage method in react-query not functioning as expected?

I've been attempting to implement infinite scrolling using useInfiniteQuery, but every time I call fetchNextPage, it just ends up re-fetching the initial page. I've scoured the React Query documentation and meticulously reviewed my code, yet I ha ...

What are some solutions for resolving a background image that fails to load?

HTML: `<div class="food-imagesM imagecontainer"> <!--Page info decoration etc.--> </div>` CSS: `.food-imagesM.imagecontainer{ background-image: url("/Images/Caribbean-food-Menu.jpg"); background-repeat: no-repeat; backgroun ...

The yup schema .compact() function processes form data that has been submitted

I have a form that utilizes react-hook-form for submission validation, which is checked against a schema defined in yup. Everything works fine, but when I use .compact() to remove false values and then use .min(1) to count the number of true values in the ...

Automatically populate fields with pre-filled information

In my database, I have a table named "Produits": public function up() { Schema::create('produits', function (Blueprint $table) { $table->id(); $table->string('reference')->nullable(); ...

Tips for handling data strings using axios

In my node.js project, I am following a manual and attempting to display data obtained from jsonplaceholder app.get('/posts', async (req, res) => { const response = await axios.get('https://jsonplaceholder.typicode.com/posts'); ...

Adjacent components

I have a query regarding the utilization of Angular. I aim to align the sentences side by side. These are two distinct components that I developed. I wish for recipe-list and recipes-details to function properly, with both statements displayed alongside ...

Scrolling without limits - cylindrical webpage (CSS/JS)

Is it possible to create a page that infinitely scrolls from top to top without going straight back to the top, but instead showing the bottom content below after reaching it? Imagine being able to scroll up and see the bottom above the top like a 3D cylin ...

How can I display the Bootstrap 5.3.0 Collapsible button on this basic website?

I've been struggling to implement a Bootstrap Collapsible on my web page using Bootstrap version 5.3.0. I've tried different approaches but I can't seem to get it to work. All I need is a Collapsible that contains a few links, which should b ...

Prevent redundancy by ensuring unique attribute names in React JSX

Utilizing Nextjs has me questioning how to simplify my code. For example, I have the following snippet: const pathname = router.pathname <NavItem pathname={pathname} ... /> Is there a way to avoid duplicating the name 'pathname'? Perhaps s ...

JavaScript regular expression for detecting valid characters without repeating a specific character

let rx = /(\/MxML\/[a-z0-9\[\]@/]*)/gi; let s = 'If (/MxML/trades[1]/value== 1 then /MxML/trades[type=2]/value must be /MxML/stream/pre/reference@href'; let m; let res = []; while ((m = rx.exec(s))) { res.push(m[1]); ...

Using jQuery to show text upon hover using a `for` loop

I'm currently working on a webpage and incorporating a feature where hovering over specific div elements triggers the display of certain text in another div. There are 5 elements that I want to make hoverable and show text upon interaction. After imp ...

Difficulties arise when trying to pass all data inputted on a form using formData in conjunction with the fetch API

Why is my formData appearing empty when sent to my express+mongodb server? I'm having some issues with querySelector and addEventListener, but for now that's manageable. However, I am struggling to find a way to successfully send all the values o ...

Issue with Adding Additional Property to react-leaflet Marker Component in TypeScript

I'm attempting to include an extra property count in the Marker component provided by react-leaflet. Unfortunately, we're encountering an error. Type '{ children: Element; position: [number, number]; key: number; count: number; }' is n ...

Substituting Hebrew characters for Mandaic characters within certain HTML tags can cause links to malfunction

What causes the JavaScript code provided to replace Mandaic characters with Hebrew characters on a webpage to also disable all links on the page? The code snippet is as shown below: function replaceMandaicCharacters() { const mandaicLetters = "&bsol ...

Align a component vertically in a FlexBox using Material UI

I have a React app where I created a card with specified min width and height, containing only a header. I want to add flexbox that occupies the entire left space with justify-content="center" and align-items="center". This way, when I insert a circular pr ...

Automating testing for numbered lists with CSS list-style-type decimal

I need help with a JavaScript and CSS project. I have a situation where I have a numbered list like this: Coffee Tea Cola Here is the code structure I am using: <!DOCTYPE html> <html> <head> <style> ul.a {list-style-type ...