如何使<div>的宽度动态,并在其最小宽度和最大宽度之间取任何值?

huangapple go评论90阅读模式
英文:

How to make a <div> width dynamic and take any value between its min-width and max-width?

问题

&lt;div&gt; 如何在屏幕尺寸变化时响应并接受其最小宽度和最大宽度之间的连续数值范围?

我当前的HTML结构示例如下,也可在此JSFiddle中查看。最终目标是使具有蓝色背景的&lt;div&gt;的宽度在屏幕尺寸减小时从最大宽度逐渐减小到最小宽度。

HTML

&lt;div class=&quot;bar-container&quot;&gt;
  &lt;div class=&quot;left-bar&quot;&gt;
    &lt;div class=&quot;grey&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;right-bar&quot;&gt;
    &lt;div class=&quot;wrapper&quot;&gt;
      &lt;div class=&quot;red&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;blue&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;yellow&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

CSS

.bar-container {
  background-color: black;
  height: 50px;
  display: flex;
  justify-content: space-between;
}

.left-bar {
  display: flex;
}

.right-bar {
  display: flex;
}

grey {
  width: 100px;
  background-color: grey;
}

.wrapper {
  display: flex;
}

.red {
  width: 100px;
  background-color: red;
}

blue {
  min-width: 100px;
  max-width: 200px;
  width: 100%;
  background-color: blue;
}

yellow {
  width: 100px;
  background-color: yellow;
}

如何使<div>的宽度动态,并在其最小宽度和最大宽度之间取任何值?

英文:

Is there a way to make &lt;div&gt; be responsive and take the continuous range of values between its min-width and max-width as the screen size is changed?

A sample of my current HTML structure is below and in this JSFiddle playground. The end goal is to make the width of the &lt;div&gt; with the blue background slowly decrease from its max-width to its min-width as the screen size is reduced.

HTML

&lt;div class=&quot;bar-container&quot;&gt;
  &lt;div class=&quot;left-bar&quot;&gt;
    &lt;div class=&quot;grey&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;right-bar&quot;&gt;
    &lt;div class=&quot;wrapper&quot;&gt;
      &lt;div class=&quot;red&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;blue&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;yellow&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

CSS

.bar-container {
  background-color: black;
  height: 50px;
  display: flex;
  justify-content: space-between;
}

.left-bar {
  display: flex;
}

.right-bar {
  display: flex;
}

.grey {
  width: 100px;
  background-color: grey;
}

.wrapper {
  display: flex;
}

.red {
  width: 100px;
  background-color: red;
}

.blue {
  min-width: 100px;
  max-width: 200px;
  width: 100%;
  background-color: blue;
}

.yellow {
  width: 100px;
  background-color: yellow;
}

如何使<div>的宽度动态,并在其最小宽度和最大宽度之间取任何值?

答案1

得分: 1

以下是翻译的内容:

/* 用于小于768px的屏幕的媒体查询 /
@media (max-width: 767px) {
.blue {
flex-grow: 1; /
占用所有可用空间 /
transition: width 0.5s ease-out; /
添加过渡效果 */
}
}

/* 用于768px及更大屏幕的媒体查询 /
@media (min-width: 768px) {
.blue {
flex-grow: 0; /
不增长或缩小 /
width: 200px; /
设置宽度为最大宽度 */
}

.wrapper {
flex-grow: 1; /* 占用所有可用空间 /
transition: padding-right 0.5s ease-out; /
添加过渡效果 */
}

/* 使右侧边栏灵活缩小 */
.right-bar {
flex-shrink: 1;
}

/* 在包裹器的右侧添加一些填充以腾出空间给黄色条 /
.wrapper:after {
content: '';
flex-grow: 9999;
padding-right: 100px; /
黄色条的宽度 */
}

/* 添加黄色条 */
.yellow {
flex-grow: 0;
flex-shrink: 0;
width: 100px;
background-color: yellow;
}
}

英文:

You want the blue div to fill up the entire screen for displays smaller than 768px and gradually go narrower as the screen size is shrunk. You can accomplish this by setting the width property's transition effect to 1 and utilizing the flex-grow: 1 style.
Then add wrapper div to occupy all available space and the blue div to have a set width of 200px for screens 768px and larger. For the blue div, we set flex-grow: 0 and width: 200px, and for the wrapper div, we set flex-grow: 1 and add a padding-right transition effect.

In order for the wrapper div to occupy all available space, we additionally add a:after pseudo-element to the wrapper div with a huge flex-grow value and a padding-right value equal to the width of the yellow div. We also make the right bar div shrinkable. This frees up space so that the yellow div may be seen.
In order to prevent the yellow div from changing size, we add it last and give it a constant width of 100px. We also set its flex-grow and flex-shrink values to 0.

/* Media query for screens smaller than 768px */
@media (max-width: 767px) {
  .blue {
    flex-grow: 1; /* Take up all available space */
    transition: width 0.5s ease-out; /* Add transition effect */
  }
}

/* Media query for screens 768px and larger */
@media (min-width: 768px) {
  .blue {
    flex-grow: 0; /* Don&#39;t grow or shrink */
    width: 200px; /* Set width to max-width */
  }
  
  .wrapper {
    flex-grow: 1; /* Take up all available space */
    transition: padding-right 0.5s ease-out; /* Add transition effect */
  }
  
  /* Make the right bar flex item shrinkable */
  .right-bar {
    flex-shrink: 1;
  }
  
  /* Add some padding to the right of the wrapper to make space for the yellow bar */
  .wrapper:after {
    content: &#39;&#39;;
    flex-grow: 9999;
    padding-right: 100px; /* Width of the yellow bar */
  }
  
  /* Add the yellow bar */
  .yellow {
    flex-grow: 0;
    flex-shrink: 0;
    width: 100px;
    background-color: yellow;
  }
}

答案2

得分: 0

是的,在CSS中有方法可以实现这个。通过使用min-width:800px;max-width:800px;,或者你可以使用Bootstrap列来实现,这是创建页面最佳响应式分区的更好方法。

英文:

Yes, there is a way to do it in CSS.
by using min-width:800px; or max-width:800px; or you can do it by using Bootstrap Columns, It is a better way to make the best possible responsive divisions of the page

答案3

得分: 0

经过数小时的努力,我成功找到了解决方案。我过于专注于 blue <div> 本身,没有注意到整体的HTML结构。要实现所需的行为,可以通过将 right-bar <div>width 设置为 100%,使其占据整个空间,直到达到 left-bar <div> 为止。子元素可以右对齐。wrapper <div> 同样也是如此。blue <div> 被分配了 width100%min-width100pxmax-width200px

最终的CSS如下所示,HTML保持不变。

.bar-container {
  background-color: black;
  height: 50px;
  display: flex;
  justify-content: space between;
}

.left-bar {
  display: flex;
}

.right-bar {
  display: flex;
  width: 100%;
  justify-content: flex-end;
}

grey {
  width: 100px;
  background-color: grey;
}

.wrapper {
  display: flex;
  width: 100%;
  justify-content: flex-end;
}

.red {
  width: 100px;
  background-color: red;
}

blue {
  max-width: 200px;
  min-width: 100px;
  width: 100%;
  background-color: blue;
}

yellow {
  width: 100px;
  background-color: yellow;
}
英文:

After several hours spent on this, I have managed to find a solution. I was focusing too much on the blue &lt;div&gt; itself, I did not pay attention to the overall HTML structure.
To obtain the desired behaviour,the right-bar &lt;div&gt; could be made to occupy the whole space until it reaches the left-bar &lt;div&gt; by setting its width to 100%. The children elements can be aligned to the right. Same for the wrapper &lt;div&gt;. The blue &lt;div&gt; is assigned a width of 100%, min-width of 100px and max-width of 200px.

The final CSS is something like below and the HTML is unchanged.

.bar-container {
  background-color: black;
  height: 50px;
  display: flex;
  justify-content: space-between;
}

.left-bar {
  display: flex;
}

.right-bar {
  display: flex;
  width: 100%;
  justify-content: flex-end;
}

.grey {
  width: 100px;
  background-color: grey;
}

.wrapper {
  display: flex;
  width: 100%;
  justify-content: flex-end;
}

.red {
  width: 100px;
  background-color: red;
}

.blue {
  max-width: 200px;
  min-width: 100px;
  width: 100%;
  background-color: blue;
}

.yellow {
  width: 100px;
  background-color: yellow;
}

huangapple
  • 本文由 发表于 2023年3月7日 01:05:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653757.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定