skip to Main Content

The design I’m trying to code:

The design I'm trying to code

I attempted to code this design of form input, but I encountered a problem when I used display: flex. The width of the input shortens instead of expanding as I expected.

The result of my code:

The result of my code

Here are the results of what I did, the width of my form input is already set to 100%. I have it in display: flex because I need the dropdown, input, and search button to stick together. Anyway, here’s my code. Please let me know if anything is incorrect.

.search-button {
  display: flex;
  justify-content: left;
  margin-left: .4em;
  margin-bottom: 1em;
}

.div-1-s1 {
  font-family: 'Plus Jakarta Sans', sans-serif;
  font-size: 0.875rem;
  font-style: normal;
  font-weight: 400;
  line-height: 1.25rem;
  background-color: white;
  border-radius: 5px 0 0 5px;
  border: 1px solid white;
  padding: .8em .7em .8em .75em;
  margin-left: 1em;
  display: flex;
  justify-content: center;
  align-items: center;
}

.div-1-s1 img {
  padding-left: 0.6rem;
}

.div-2-s1 {
  display: flex;
}

.div-2-s1 input {
  font-family: 'Plus Jakarta Sans', sans-serif;
  font-weight: 400;
  font-size: 0.875rem;
  padding: .8em 0 .8em .9em;
  border: 1px solid white;
  line-height: 1.25rem;
  width: 100%;
  max-width: 100%;
}

#sent-icon {
  padding: .7em .5em .7em .5em;
  margin-right: 1em;
  border-radius: 0 5px 5px 0;
  border: 1px solid white;
  background-color: white;
}
<div class="search-button">
  <div class="div-1-s1">
    <span>All</span>
    <img src="t-icon.svg" alt="a svg icon" />
  </div>
  <div class="div-2-s1">
    <form action="#">
      <input type="text" id="label" placeholder="Search a 
             location">
    </form>
    <img src="f2-icon.svg" alt="a svg icon" id="sent-icon" />
  </div>
</div>

2

Answers


  1. You just need to add width: 100% to the form as well

    See this:

    body {
      width: 100%;
    }
    
    .search-button {
      display: flex;
      justify-content: left;
      margin-left: .4em;
      margin-bottom: 1em;
      width: 100%;
    }
    
    .div-1-s1 {
      font-family: 'Plus Jakarta Sans', sans-serif;
      font-size: 0.875rem;
      font-style: normal;
      font-weight: 400;
      line-height: 1.25rem;
      background-color: white;
      border-radius: 5px 0 0 5px;
      border: 1px solid white;
      padding: .8em .7em .8em .75em;
      margin-left: 1em;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    .div-1-s1 img {
      padding-left: 0.6rem;
    }
    
    .div-2-s1 {
      display: flex;
      width: 100%;
    }
    
    form {
      width: 100%;
    }
    
    .div-2-s1 input {
      font-family: 'Plus Jakarta Sans', sans-serif;
      font-weight: 400;
      font-size: 0.875rem;
      padding: .8em 0 .8em .9em;
      border: 1px solid black;
      /* change it back to white */
      line-height: 1.25rem;
      width: 100%;
    }
    
    #sent-icon {
      padding: .7em .5em .7em .5em;
      margin-right: 1em;
      border-radius: 0 5px 5px 0;
      border: 1px solid white;
      background-color: white;
    }
    <div class="search-button">
      <div class="div-1-s1">
        <span>All</span>
        <img src="t-icon.svg" alt="a svg icon" />
      </div>
      <div class="div-2-s1">
        <form action="#">
          <input type="text" id="label" placeholder="Search a location">
        </form>
        <img src="f2-icon.svg" alt="a svg icon" id="sent-icon" />
      </div>
    </div>
    Login or Signup to reply.
  2. Flex items are set, by default, to flex-basis: auto, which means they’ll be the length of their content. If there’s no content, they’ll be their natural (intrinsic) length.

    Flex items are also set, by default, to flex-grow: 0, which means they won’t consume free space.

    That’s what you’re seeing in your form: Both of these rules in play.

    You can switch to flex-basis: 100% (equivalent to width: 100%, in this case), but that’s just a hack, which can result in an overflow condition (by forcing sibling items out of the container).

    Instead, for a clean and efficient solution, use flex-grow to enable the items to consume available space.

    .search-button {
      display: flex;
    }
    
    .div-2-s1 {
      flex: 1; /* consume all available space */
      display: flex;
    }
    
    form {
      flex: 1;
      display: flex;
    }
    
    .div-2-s1 input {
      flex: 1;
    }
    <div class="search-button">
      <div class="div-1-s1">
        <span>All</span>
        <img src="t-icon.svg" alt="a svg icon" />
      </div>
      <div class="div-2-s1">
        <form action="#">
          <input type="text" id="label" placeholder="Search a 
                 location">
        </form>
        <img src="f2-icon.svg" alt="a svg icon" id="sent-icon" />
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search