HTML Academy
Flexible menu with overflow, part 2
Flexbox, part 223/28
Back to the list of tasks
  • 1. Flex items and the block model
  • 2. Features of the margin property
  • 3. Alignment and margins
  • 4. The flow direction of the main axis and margins
  • 5. The initial main size of flex item, flex-basis
  • 6. Test: Cubism
  • 7. The item growth factor, flex-grow
  • 8. Growing items proportionally
  • 9. Calculating the total size with flex-grow
  • 10. The item shrinkage factor, flex-shrink
  • 11. Shrinking items proportionally
  • 12. Calculating the total size with flex-shrink
  • 13. flex-shrink and min-width
  • 14. Test: Mastering factors
  • 15. The flex shorthand property
  • 16. Multi-line flex containers and flex-shrink
  • 17. Multi-line flex containers and flex-grow
  • 18. flex-basis: 100% and flex-wrap
  • 19. The heading with description in flexboxes
  • 20. Heading with description, part 2
  • 21. Flexible menu with overflowing content
  • 22. Flexible menu with overflow, part 2
  • 23. Input fields with dynamic width
  • 24. The course card
  • 25. Course card, part 2
  • 26. Course card, part 3
  • 27. So many cards
  • 28. Test: Flexible flows
The course card
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Input fields with dynamic width

Another case where Flexbox can come in handy is for input fields with dynamic width. The requirements for them are as follows:

  • Various items can occupy a single line together with a field, including buttons, links, and signatures.
  • The size of the additional items is not defined. It depends on their content.
  • In this case, the field should be stretched to fit the entire space remaining in the parent container.
  • And it should change its width when the container size is changed.

You can only use a flexbox to solve this task. Convert the input field container into a flex container. All of the items inside it will be transformed into flex items, and their basic sizes will be resized depending on their contents (flex-basis: auto;). All that is left is to assign a non-zero growth factor to the input fields.

Everything will work fine in a sufficiently wide container. You will only start to encounter problems in containers that are too narrow: by default, input fields will not be compressed after a predetermined width, which will cause text to overflow the remaining items.

In order to cope with these problems, you need to assign a zero factor to all of the items with the exception of the input fields, and you clearly need to assign a minimum width to the input fields themselves.

Comments

  • index.html
  • style.css
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Input fields with dynamic width</title> <link href="course.css" rel="stylesheet"> <link href="style.css" rel="stylesheet"> </head> <body class="subtle"> <div class="card"> <h1 class="card-title">Input fields with dynamic width</h1> <div class="input-container"> <input class="input"> <button class="add-on icon-start">Start</button> <a href="#more">More</a> </div> <div class="input-container"> <span class="add-on">Amount</span> <input class="input"> </div> <div class="input-container"> <span class="add-on icon-search">Search</span> <input class="input"> <button class="add-on button">Go</button> </div> </div> </body> </html>
CSS
.input-container { margin: 30px 0; width: 450px; outline: 2px dashed rgba(0, 128, 0, 0.2); outline-offset: 5px; } .add-on { display: inline-block; box-sizing: border-box; padding: 8px 12px; min-width: 40px; min-height: 36px; border: 2px solid #cccccc; background-color: #eeeeee; color: #666666; vertical-align: top; } .input { padding: 8px 12px; border: 2px solid #cccccc; } .input:not(:first-child) { border-left: 0; } .input:not(:last-child) { border-right: 0; } .input-container a { margin: 0 10px; color: #666666; font-size: 12px; line-height: 36px; }

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

The code has changed, click “Refresh” or turn autorun on.

You’ve gone to a different page

Click inside the mini-browser to shift the focus onto this window.

100%
Goalscompleted
0
    1. Assign a flexbox layout to the .input-container block,
    2. and assign a growth factor of 1 for .input.
    3. And reduce the width of .input-container to 200px,
    4. assign a zero shrinkage factor to .add-on,
    5. and assign a minimum width of 50px to .input.

    Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2025

    VISAMastercard

    Log in

    or

    Forgot your password?

    Sign up

    Sign up

    or
    Log in

    Restore access

    Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

    Forgot to connect your email to the profile? Email us and we’ll help.