简体   繁体   English

如何执行此操作:可变宽度的父元素,具有固定宽度的子元素和可变宽度的子元素?

[英]How to do this: A fluid-width parent element, with a fixed-width and a fluid-width child element?

Before I explain, consider the following... 在我解释之前,请考虑以下内容...

EXAMPLE HTML CODE: 示例HTML代码:

<div id="Body">
    <div id="Content">...</div>
    <div id="Panel">...</div
</div>

CSS CODE: CSS代码:

#Body {
    min-width: 865px;
    width: 93%;
    max-width: 1785px;
}

#Panel {
    width: 250px;
    float: right;
}

#Content {
    float: left;
    min-width: 595px;
    width: /* This one needs to be fluid. So, what should I do? */;
    max-width: 1510px;
}

As you can see, 'Body' div is the fluid-width parent I was referring to, with fixed-width child element 'Panel' div and fluid-width 'Content' div — and that's exactly where I am stuck. 如您所见,“ Body” div是我所指的宽度可变的父级,具有固定宽度的子元素“ Panel” div和宽度可变的“ Content” div,而这正是我遇到的问题。

How should I define the width of 'Content' div? 如何定义“内容” div的宽度? Any ideas are welcome. 任何想法都欢迎。 Thanks. 谢谢。

Put the #Panel div first, and then simply use this for #Content : 首先放置#Panel div,然后将其简单地用于#Content

#Content {
    margin-right: 250px;
}

jsFiddle Demo jsFiddle演示


Here are some simple guidelines to make this work on your site. 这里有一些简单的准则,可以在您的网站上实现。 I used Chrome Developer Tools to do these and it works fine. 我使用Chrome开发者工具来完成这些工作,并且效果很好。

  • #Body @ style.css #Body @ style.css
    • overflow: hidden;
  • #Content @ custom.css #Content @ custom.css
    • min-width: 595px;
    • width: 68%;
    • max-width: 1510px;
    • margin-right: 270px;
  • #Content @ style.css #Content @ style.css
    • width: 680px;
    • float: left;

The fix for this issue is: 此问题的解决方法是:

#Body {
    min-width: 865px;
    width: 93%;
    max-width: 1785px;
    position:relative; /* Added by me */
}

#Panel {
    width: 250px;
    /*float: right;*/
    position:absolute; /* Added */
    top:0; /* Added */
    right:0; /* Added */
}

#Content {
    float: left;
    min-width: 595px;
    width: 100%; /* Value added */
    max-width: 1510px;
    margin-right:250px; /* Added */
}

It worked fine for me. 对我来说很好。

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

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