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:
but it looks like this:
2
Answers
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 isflex-shrink: 1;
, which tells each flex item to shrink to fit within the flex container. You can prevent your button from shrinking by settingflex-shrink: 0;
on it.Here is a working snippet:
Search icon needs flex-grow and flex-shrink set to 0. And the card needs to have min width set.