2015-01-07 23 views
4

我有一个包含标题/内容/页脚的HTML页面,它使用了flexbox模型并包含<details>标记。 我需要使细节内容使用最大可用高度,这意味着当处于打开状态时,它的内容应该占据其容器中的所有空间(当然是概要除外)。制作HTML5详细信息标记以获取最大可用高度

这里是我的HTML/CSS代码(http://jsfiddle.net/rtojycvk/2/):

HTML:

<div class="wrapper"> 
    <div class="header">Header</div> 
    <div class="main"> 
     Some text before details 
     <details class="details" open> 
      <summary>Details summary</summary> 
      <div class="content">Details content</div> 
     </details> 
    </div> 
    <div class="footer">Footer</div> 
</div> 

CSS:

html, body { 
    height: 100%; 
    margin: 0; padding: 0; 
} 

.wrapper { 
    display: flex; 
    flex-direction: column; 
    width: 100%; 
    min-height: 100%; 
} 

.header { 
    height: 50px; 
    background-color: yellow; 
    flex: 0 0 auto; 
} 
.main { 
    background-color: cyan; 
    flex: 1; 
    display: flex; 
    flex-direction: column; 
} 
.footer { 
    height: 50px; 
    background-color: green; 
    flex: 0 0 auto; 
} 
.content { 
    background-color: blue; 
    color: white; 
    flex: 1; 
} 
.details { 
    background-color: red; 
    flex: 1; 
    display: flex; 
    flex-direction: column; 
} 

正如你所看到的,细节标签本身通吃可用空间,但不包含其内容。

P.S.我需要这个只在Chrome中工作。

+0

尝试添加.content {height:100vh;} –

+0

它使内容高度等于窗口高度,而不是我想要的 – Norfolc

+0

“我需要使细节内容使用最大可用高度。”它会使内容细节填充详细信息摘要容器。 –

回答

3

http://jsfiddle.net/rtojycvk/16/

使用位置上的内容绝对,相对细节位置,并计算()的CSS(以抵消摘要高度)

.content { 
    background-color: lightgray; 
    color: black; 
    flex: 1; 
    display:flex; 
    position:absolute; 
    height: calc(100% - 18px); 
    width: 100%; 
} 
.details { 
    background-color: gray; 
    flex: 1; 
    display: flex; 
    flex-direction: column; 
    position:relative; 
} 

希望这有助于! (我改变了颜色,因为它们对我来说有点亮:p)

+0

它看起来更好,然后以前的答案(http://stackoverflow.com/a/27823886/3079960),但它有一个问题 - 你应该知道总结 – Norfolc

+0

的大小此外,需要添加'.content {overflow:hidden; }'当窗口太小时,防止内容和页脚重叠 – Norfolc

+0

@Norfolc啊,我明白你对这两个意见的含义。至于第一,你打开使用jQuery或纯粹是寻找一个CSS的答案?如果只是CSS,目前的答案是我的知识可以帮助你的程度。如果你对jQuery感兴趣,我可以解决需要知道摘要大小的问题。 LMK – indubitablee

1

绝对定位。内容和细节相对。 fiddle

html, body { 
    height: 100%; 
    margin: 0; padding: 0; 
} 

.wrapper { 
    display: flex; 
    flex-direction: column; 
    width: 100%; 
    min-height: 100%; 
} 

.header { 
    height: 50px; 
    background-color: yellow; 
    flex: 0 0 auto; 
} 
.main { 
    background-color: cyan; 
    flex: 1; 
    display: flex; 
    flex-direction: column; 
} 
.footer { 
    height: 50px; 
    background-color: green; 
    flex: 0 0 auto; 
} 
.content { 
    background-color: blue; 
    color: white; 
    flex: 1; 
    position: absolute; 
    top: 3%; 
    bottom: 0; 
    height: 97%; 
    width: 100%; 
} 
details { 
    position: relative; 
} 
summary{ 
    height: 3%; 
} 
.details { 
    background-color: red; 
    flex: 1; 
    display: flex; 
    flex-direction: column; 
} 
+0

尝试调整窗口大小,它看起来不错 –

+0

您可以调整:详细信息为100%,摘要在x%。内容为100%-x%满足您的需求? –

1

对于那些没有设置绝对位置或不能这样做的人,有另一种方法来实现它:使用vh.content

html, 
 
body { 
 
    margin: 0; 
 
    padding: 0; 
 
    height:100vh; 
 
    background: orange; 
 
} 
 

 
.wrapper { 
 
    display: flex; 
 
    flex-direction: column; 
 
    width: 100%; 
 
    height:100vh; 
 
    background: pink; 
 
} 
 

 
.header, 
 
.footer { 
 
    height: 10vh; 
 
    min-height: 25px; /* (or max-height, or both!) */ 
 
    max-height: 50px; 
 
    background-color: yellow; 
 
    flex: 0 0 auto; 
 
} 
 

 
.footer { 
 
    background-color: green; 
 
} 
 

 
.main { 
 
    background-color: cyan; 
 
    flex: 1; 
 
    display: flex; 
 
    flex-direction: column; 
 
    height: calc(100vh - 20vh); /* 10vh * 2 (.header + .footer sizes) */ 
 
} 
 

 
.content { 
 
    background-color: blue; 
 
    color: white; 
 
    flex: 1; 
 
    display: flex; 
 
    flex-direction: column; 
 
    height: calc(100vh - 20vh); /* 10vh * 2 (.header + .footer sizes) */ 
 
} 
 

 
.details { 
 
    background-color: red; 
 
    flex: 1; 
 
    display: flex; 
 
    flex-direction: column; 
 
}
<div class="wrapper"> 
 
    <header class="header">Header</header> 
 
    <main class="main"> 
 
     Some text before details 
 
     <details class="details" open> 
 
      <summary>Details summary</summary> 
 
      <div class="content">Details content</div> 
 
     </details> 
 
    </main> 
 
    <footer class="footer">Footer</footer> 
 
</div>

小提琴是在这里:http://jsfiddle.net/ALXWebDev/wxm0v49c/

希望它能帮助!