Why am I still unable to achieve vertical centering despite setting the height of the parent element?
P粉493534105
P粉493534105 2023-09-11 17:27:43
0
1
553

I am able to use the mx-auto class, but although my myContainerFixed class successfully displays a fixed height, I cannot get the my-auto class to work .

Example css

.mx-auto {
    margin-left: auto;
    margin-right: auto;
}

.my-auto {
    margin-top: auto;
    margin-bottom: auto;
}

.myContainerFixed {
    height: 90vh;
}
@screen lg {
    .myContainerFixed {
        height: 85vh;
    }
}

.w-max {
    width: max-content;
}

Although this is certainly not the best option in this case, I also tried using absolute positioning, but that didn't work either Here is an example

.absoluteCenter {
  position: absolute;
  left: 50%;
  -webkit-transform: translateX(-50%);
  transform: translateX(-50%)
}

HTML code

        return (
            <div className='bg-yellow-400 myContainerFixed'>
                <form onSubmit={handleSubmit(formFunction)} className=" bg-pink-400 w-max p-2 mx-auto my-auto rounded-sm [&>input]:mb-4  [&>label]:mr-2">
                    <label>Name</label>
                    <input type="text" {...register("name")} />
                    <div className='italic text-myDarkRed'>{errors.name?.message}</div>
                    <label>Email</label>
                    <input type="email" {...register("email")} />
                    <div className='italic text-myDarkRed'>{errors.email?.message}</div>
                    <label>Available Weights</label>
                    <input type="text" {...register("availableWeights", { setValueAs: (v: string | Array<number>) => Array.isArray(v) ? arrayToString(v) : v.split(",").map((weight) => Number(weight)) })} />
                    {/*  */}
                    <div className='italic text-myDarkRed'>{errors.availableWeights?.message}</div>

                    <button type="submit" className="p-2 rounded shadow-sm bg-gradient-to-b from-myRed to-red-400">Submit</button>
                </form>
            </div>
        );

P粉493534105
P粉493534105

reply all(1)
P粉106711425

It is related to the display: block (default) of the top element (myContainerFixed).

For example, giving it display: flex; will fix this

.myContainerFixed {
    display: flex;
    height: 90vh;
}

I recommend removing mx-auto and my-auto and using the flex attribute to position the form

.myContainerFixed {
    height: 90vh;
    display: flex;
    justify-content: center;
    align-items: center;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template