skip to Main Content

I’m trying a tutorial and am currently stuck. My problem is that the search icon in my button is messing up and overlapping instead of sitting nicely in the center. Tried a bunch of CSS fixes, but no luck.

The Tutorial:
https://www.youtube.com/watch?v=MIYQR-Ybrn4&list=PLjwm_8O3suyOgDS_Z8AWbbq3zpCmR-WE9

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

body {
  background: #222;
}

.card {
  width: 90%;
  max-width: 470px;
  background: linear-gradient(135deg, #00feba, #5b548a);
  color: #fff;
  margin: 100px auto 0;
  border-radius: 20px;
  padding: 40px 35px;
  text-align: center;
}

.search {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.search input {
  border: 0;
  outline: 0;
  background: #ebfffc;
  color: #555;
  padding: 10px 25px;
  height: 60px;
  border-radius: 30px;
  flex: 1;
  margin-right: 16px;
  font-size: 18px;
}

.search button {
  border: 0;
  outline: 0;
  background: #ebfffc;
  border-radius: 50%;
  width: 60px;
  height: 60px;
  cursor: pointer;
}

.search button img {
  width: 16px;
}
<body>
  <div class="card">
    <div class="search">
      <input type="text" placeholder="Enter City Name" spellcheck="false">
      <button><img src="images/search.png"></button>
    </div>
  </div>
</body>

It should look like this:
enter image description here

but it looks like this:
enter image description here

2

Answers


  1. As stated in this answer to a similar issue, when you apply display: flex; to an element, there are default properties that are set on the element. One of these properties is flex-shrink: 1;, which tells each flex item to shrink to fit within the flex container. You can prevent your button from shrinking by setting flex-shrink: 0; on it.

    .search button {
      border: 0;
      outline: 0;
      background: #ebfffc;
      border-radius: 50%;
      width: 60px;
      height: 60px;
      cursor: pointer;
      flex-shrink: 0;
    }
    

    Here is a working snippet:

    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
      font-family: 'Poppins', sans-serif;
    }
    
    body {
      background: #222;
    }
    
    .card {
      width: 90%;
      max-width: 470px;
      background: linear-gradient(135deg, #00feba, #5b548a);
      color: #fff;
      margin: 100px auto 0;
      border-radius: 20px;
      padding: 40px 35px;
      text-align: center;
    }
    
    .search {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    
    .search input {
      border: 0;
      outline: 0;
      background: #ebfffc;
      color: #555;
      padding: 10px 25px;
      height: 60px;
      border-radius: 30px;
      flex: 1;
      margin-right: 16px;
      font-size: 18px;
    }
    
    .search button {
      border: 0;
      outline: 0;
      background: #ebfffc;
      border-radius: 50%;
      width: 60px;
      height: 60px;
      cursor: pointer;
      flex-shrink: 0;
    }
    
    .search button img {
      width: 16px;
    }
    <body>
      <div class="card">
        <div class="search">
          <input type="text" placeholder="Enter City Name" spellcheck="false">
          <button><img src="images/search.png"></button>
        </div>
      </div>
    </body>
    Login or Signup to reply.
  2. Search icon needs flex-grow and flex-shrink set to 0. And the card needs to have min width set.

    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
      font-family: 'Poppins', sans-serif;
    }
    
    body {
      background: #222;
    }
    
    .card {
      width: 90%;
      max-width: 470px;
      background: linear-gradient(135deg, #00feba, #5b548a);
      color: #fff;
      margin: 100px auto 0;
      border-radius: 20px;
      padding: 40px 35px;
      text-align: center;
    }
    
    .search {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    
    .search input {
      border: 0;
      outline: 0;
      background: #ebfffc;
      color: #555;
      padding: 10px 25px;
      height: 60px;
      border-radius: 30px;
      flex: 1 1 auto;
      margin-right: 16px;
      font-size: 18px;
    }
    
    .search button {
      border: 0;
      outline: 0;
      background: #ebfffc;
      border-radius: 50%;
      width: 60px;
      height: 60px;
      cursor: pointer;
    }
    
    .search button img {
      width: 16px;
    }
    
    
    /* rules to solve the issues */
    
    .search button {
      flex: 0 0 auto;
    }
    
    .search input {
      flex: 1 1 auto;
    }
    
    .card {
      min-width: fit-content;
    }
    <body>
      <div class="card">
        <div class="search">
          <input type="text" placeholder="Enter City Name" spellcheck="false">
          <button><img src="images/search.png"></button>
        </div>
      </div>
    </body>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search