简体   繁体   English

使子div文本内容不扩展其父级固定位置的div宽度

[英]Make child div text content NOT expand its parent fixed-positioned div's width

I am working on kind of a popup. 我正在某种弹出窗口上工作。 Its structure is very simple and is as follows: 其结构非常简单,如下所示:

<div class = "popup">
    <div class = "upper">
        <img src = "http://www.tapeta-mis-galazki-koala.na-pulpit.com/pokaz_obrazek.php?adres=mis-galazki-koala&rozdzielczosc=128x128" />
    </div>
    <div class = "description">This is a very interesting description of what you can see above.</div>
</div>

with styles of 与风格

.popup
{
    position: fixed;
    left: 50px; 
    top: 50px;
    border: 1px solid;
    background: #fff;
    padding: 10px;
    box-shadow: 0 0 5px #000;
}
.popup .upper {
    min-width: 100px;
    min-height: 100px;
    border: 1px solid;
    padding: 5px;
    display: inline-block;
}
.popup .upper img {
    display: block;
}

and here is a fiddle with the code applied. 这是应用代码的摆弄

As you can see, the div.popup is positioned as fixed to the body . 如您所见, div.popup被固定为body

What I want to achieve is to make the div.description NOT extend its parent div.popup width when it contains much text, instead it should wrap the text to be multilined and be of width of the div.popup . 我要实现的是使div.description在包含大量文本时扩展其父div.popup宽度,而是应将文本包装成多行并具有div.popup的宽度。 The div.popup width should be determined by the div.upper width and its content. div.popup宽度应由div.upper宽度及其内容确定。 In other words I mean to have div.description 's width AT MOST of the div.upper 's width, regardless to its ( div.description text content). 换句话说,我的意思是有div.description的宽度至多div.upper的宽度,不管它( div.description文本内容)。

EDIT 编辑

There's this little difficulty: the image content is not static and may be dynamically changed so the width is not constant. 这有点困难: 图像内容不是静态的,可以动态更改,因此宽度不是恒定的。

Is that even possible to achieve that with CSS? 使用CSS甚至可以实现这一目标吗?

http://jsfiddle.net/de6fr/1/ - a basic example of how to fix http://jsfiddle.net/de6fr/1/-如何修复的基本示例


You're basically using popup as a container, which means that if you want to retain its width, that's what you have to work on. 基本上,您将弹出窗口用作容器,这意味着如果要保留其宽度,则必须进行此操作。 I used the max-width property with .popup like this: 我将max-width属性与.popup一起.popup如下所示:

.popup {
    position: fixed;
    left: 50px; 
    top: 50px;
    border: 1px solid;
    background: #fff;
    padding: 10px;
    box-shadow: 0 0 5px #000;
    display: table;
    width: 1px;
}
.popup > div { 
    display: table-row;
}

.popup .upper {
    min-width: 100px;
    min-height: 100px;
    border: 1px solid;
    padding: 5px;
}
.popup .upper img {
    display: block;
}

Update - Flexible 更新-灵活

http://jsfiddle.net/de6fr/4/ http://jsfiddle.net/de6fr/4/

The fix for making it flexible is to use a CSS hack , which basically changes the nature of the element to a table 使其灵活的解决方法是使用CSS hack ,该CSS hack基本上将元素的性质更改为table

The nature of CSS ( cascading style sheets) means that it's pretty hard to get a parent DIV to take the size of a child div without some crazy ideas involved. CSS( 级联样式表)的性质意味着,在没有涉及任何疯狂想法的情况下,很难让父DIV达到子div的大小。 However, there's nothing preventing a "table" with a really small width doing that, as per this code: 但是,按照下面的代码,没有什么可以阻止宽度很小的“表”这样做的:

.popup
{
    position: fixed;
    left: 50px; 
    top: 50px;
    border: 1px solid;
    background: #fff;
    padding: 10px;
    box-shadow: 0 0 5px #000;
    display: table;
    width: 1px;
}
.popup .upper {
    min-width: 100px;
    min-height: 100px;
    border: 1px solid;
    padding: 5px;
    display: table-row;
}
.popup .upper img {
    display: block;
}
.popup .description {
    display: table-row;
}

Add a CSS property to your popup class and Give it a width 将CSS属性添加到您的弹出类并为其设置宽度

.popup
{
    position: fixed;
    left: 50px; 
    top: 50px;
    border: 1px solid;
    background: #fff;
    padding: 10px;
    box-shadow: 0 0 5px #000;
    overflow:scroll;
    width:400px;
}

You have not defined the width for fixed element so add some width to your fiexed element 您尚未为固定元素定义宽度,因此请为固定元素添加一些宽度

.popup
{
    position: fixed;
    left: 50px; 
    top: 50px;
    border: 1px solid;
    background: #fff;
    padding: 10px;
    box-shadow: 0 0 5px #000;
    width: 100%;
}

here is the demo 这是演示

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

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