0

Im trying to make a simple search bar in html and css. However, justify-content and align-items arent working. Could someone help me figure out why?

https://codepen.io/Lavender786/pen/yLYwRBP

input[type=text] {
  width: 400px;
  box-sizing: border-box;
  border: 2px solid #ccc;
  border-radius: 4px;
  font-size: 16px;
  background-color: white;
  background-position: 10px 10px; 
  background-repeat: no-repeat;
  padding: 12px 20px 12px 40px;
}

.container {
  align-items: center; 
  justify-content: center;
}
<div id = "container">
<form>
  <input type="text" name="search" placeholder="Search...">
</form>
</div> 
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
Manny
  • 35
  • 6

2 Answers2

0

In your code the form takes 100% of the width, that's why you can't center the search bar.

You should either set a width to the form (and let the bar take 100%) if you want to center the form, or as in the example below keep the form at 100% width and center its elements (I swapped .container with form and added display:flex;)

input[type=text] {
  width: 400px;
  box-sizing: border-box;
  border: 2px solid #ccc;
  border-radius: 4px;
  font-size: 16px;
  background-color: white;
  background-position: 10px 10px; 
  background-repeat: no-repeat;
  padding: 12px 20px 12px 40px;
}

form {
  display:flex;
  justify-content: center;
}
<div id="container">
  <form>
    <input type="text" name="search" placeholder="Search...">
  </form>
</div>
Balastrong
  • 4,336
  • 2
  • 12
  • 31
0

You have added CSS to the container but it has overlapped the form tag so apply CSS on form rather than a container. I hope it helps.

Amandeep
  • 41
  • 6