Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

css display block in order to stuck to input on top of each other

I’m try to understand how does CSS display property work, i wrote this simple html code:

<div>
  <form className="container" action="">
    <input name="fName" placeholder="First Name" type="text" value={props.fName}/>
    <input name="sName" placeholder="Second Name" type="text" value={props.sName} />
    <button>Submit</button>
  </form>
</div>

with this HTML the 2 input boxes are horizontally aligned but I want them to stay on top of each other, so I tried to apply some CSS style.

.container{
  text-align: center;
  display: block; 
}

why is the display block not working as expected? should not be applied to the child as well?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if I apply display: block; to the input then it works, but I would like to understand why.

>Solution :

Setting display: block; on the container doesn’t automatically make its children stack vertically because <input> elements are naturally inline-block, meaning they sit side by side by default. To stack them vertically, you need to apply display: block; directly to the <input> elements.

.container input {
  display: block; 
}
<div>
  <form class="container" action="">
    <input name="fName" placeholder="First Name" type="text" value={props.fName}/>
    <input name="sName" placeholder="Second Name" type="text" value={props.sName} />
    <button>Submit</button>
  </form>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading