最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Width of flex container is behaving differently in 2 cases - Stack Overflow

programmeradmin2浏览0评论

In my first case there is a gallery which is declared a flex container. Its width grows as you increase the width of the viewport.

<div class="gallery">
      <img src="1.jpg">
      <img src="2.jpg">
      <img src="3.jpg">
      <img src="4.jpg">
      <img src="5.jpg">
      <img src="6.jpg">
      <img src="7.jpg">
      <img src="8.jpg">
      <img src="9.jpg">
    </div>

CSS for the above html

.gallery {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 16px;
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px 10px;
}

.gallery img {
  width: 100%;
  max-width: 350px;
  height: 300px;
  object-fit: cover;
  border-radius: 10px;
}

In the second case, there is a input container which is a flex container which is inside a parent flex container , but its width is not growing and is fixed when the viewport width changes.

<main>
<section id="input-container">
    <label for="input-decimal">Enter the decimal number</label>
    <input type="text" >
</section>
</main>

css for the above.

main{
    display:flex;
    align-items: center;
    flex-direction: column;
}
#input-container{
    display:flex;
    flex-direction: row;
    flex-wrap:wrap;
    max-width: 500px;
    margin:0 auto;
    justify-content: center;;
}

I was wondering why is this happening.

In my first case there is a gallery which is declared a flex container. Its width grows as you increase the width of the viewport.

<div class="gallery">
      <img src="1.jpg">
      <img src="2.jpg">
      <img src="3.jpg">
      <img src="4.jpg">
      <img src="5.jpg">
      <img src="6.jpg">
      <img src="7.jpg">
      <img src="8.jpg">
      <img src="9.jpg">
    </div>

CSS for the above html

.gallery {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 16px;
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px 10px;
}

.gallery img {
  width: 100%;
  max-width: 350px;
  height: 300px;
  object-fit: cover;
  border-radius: 10px;
}

In the second case, there is a input container which is a flex container which is inside a parent flex container , but its width is not growing and is fixed when the viewport width changes.

<main>
<section id="input-container">
    <label for="input-decimal">Enter the decimal number</label>
    <input type="text" >
</section>
</main>

css for the above.

main{
    display:flex;
    align-items: center;
    flex-direction: column;
}
#input-container{
    display:flex;
    flex-direction: row;
    flex-wrap:wrap;
    max-width: 500px;
    margin:0 auto;
    justify-content: center;;
}

I was wondering why is this happening.

Share Improve this question edited Feb 8 at 7:21 Temani Afif 273k28 gold badges363 silver badges482 bronze badges asked Feb 8 at 5:58 madhavmadhav 771 silver badge8 bronze badges 1
  • 1 Please, there is nothing silly here. Flex behaves, well... differently. :-) – Sergey A Kryukov Commented Feb 8 at 6:04
Add a comment  | 

2 Answers 2

Reset to default 1

the input-container is a flex item (unlike the gallery which is not inside a flex container) so its width will be "shrink-to-fit" especially that you are centering it.

Either remove the centering and the default "stretch" behavior will give you want you want.

main {
  display: flex;
  /*align-items: center; removed */
  flex-direction: column;
}

#input-container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  max-width: 500px;
  /*margin: 0 auto; removed */
  justify-content: center;
  border:1px solid red;
}
<main>
  <section id="input-container">
    <label for="input-decimal">Enter the decimal number</label>
    <input type="text" >
  </section>
</main>

or add width: 100% in case you want to keep the element centred (which I think you want)

main {
  display: flex;
  align-items: center; 
  flex-direction: column;
}

#input-container {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  max-width: 500px;
  /*margin: 0 auto; no needed, align-items is enough */
  width: 100%;
  justify-content: center;
  border:1px solid red;
}
<main>
  <section id="input-container">
    <label for="input-decimal">Enter the decimal number</label>
    <input type="text" >
  </section>
</main>

In your first example, gallery is a flex container having max-width 1400px, that means it can grow upto 1400px of viewport.

But in your second example, input-container flex container has a max-width of 500px, so it cannot grow beyond 500px even if your viewport is 1000px.

If you want the input text box inside your input-container to grow based on viewport then you can add flex-grow property to input box's CSS because it need to be explicitly specified for input box.

#input-container input {
    flex-grow: 1;  /* Allow input to stretch */
}
发布评论

评论列表(0)

  1. 暂无评论