简体   繁体   English

CSS - 父 div 宽度应取决于第二个孩子

[英]CSS - Parent div width should depend on second child

I am new to HTML/CSS, I am trying to achieve a particular scenario:我是 HTML/CSS 的新手,我正在尝试实现一个特定的场景:

  1. A container div (parent) has two child divs, first for label and second for content.一个容器 div(父)有两个子 div,第一个用于标签,第二个用于内容。
  2. Parent div (container) width should depend of second child (content) width, ie if the second child width increased due to its content, Parent div width should also be increased.父div(容器)宽度应取决于第二个子(内容)宽度,即如果第二个子宽度因其内容而增加,则父div宽度也应增加。 (Parent div width = second child width). (父 div 宽度 = 第二个子宽度)。
  3. First child is used for label, so it should not exceed Parent div width.第一个孩子用于标签,因此它不应超过父 div 宽度。 If First child is larger than Parent div width, it should show text as ellipsis.如果第一个孩子大于父 div 宽度,它应该将文本显示为省略号。 Parent div width should not depend on first child width.父 div 宽度不应依赖于第一个子宽度。

I am not sure if it can be achieved using only CSS.我不确定是否可以仅使用 CSS 来实现。 Would really appreciate your help with this.非常感谢您的帮助。

Thanks in advance.提前致谢。

Image for visual representation用于视觉表示的图像

For the container to use the content as a width reference it's possible and it's called "shrinkwrapping" visit this thread for more detail.对于使用内容作为宽度参考的容器,它被称为“收缩包装”,请访问此线程以获取更多详细信息。
For the label div, you can use this CSS property .对于标签 div,您可以使用此 CSS 属性
So the answer to your question, yes it's possible.所以你的问题的答案是,是的,这是可能的。

Implemented using flex in CSS.在 CSS 中使用 flex 实现。 Working demo can be found here.可以在此处找到工作演示。 'https://codepen.io/shrdz_yadav/pen/rNLNPQE' 'https://codepen.io/shrdz_yadav/pen/rNLNPQE'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM