简体   繁体   English

标题在固定的地方

[英]Header in fixed place

I have HTML like this: 我有这样的HTML:

<article>
    <header>
        <hgroup>
            <h1>Wk 25 </h1>
            <h2>(18-06 tot 24-06)</h2>          
        </hgroup>
        <p>Some info</p>
    </header>
    <table>
        <thead>
            <tr>
                <th class="legenda">Title</th>
                <th class="legenda">Title</th>
                <th class="legenda">Title</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>content</td>
                <td>content</td>
                <td>content</td>
            </tr>
        </tbody>
    </table>
</article>

And CSS (excerpt) like this: 和CSS(摘录)是这样的:

article {
    padding: 0;
    margin-bottom: 15px;
    overflow: auto;
}
article header {
    background: url('header.png') repeat-x bottom center;
    padding: 4px 10px;
    cursor: pointer;
}

The article-element resizes with the browser window. article-element使用浏览器窗口调整大小。 The table inside can be wider then the article-element, hence the overflow: auto. 里面的表可以比article-element更宽,因此溢出:auto。 But: the header element has a 100% width, so if you scroll to right, the header becomes invisible. 但是:标题元素的宽度为100%,因此如果向右滚动,标题将变为不可见。

If I give the header a position: absolute, I need to give it a fixed height too: doesn't work, as the height there is dependent on content (which varies). 如果我给标题一个位置:绝对,我也需要给它一个固定的高度:不起作用,因为那里的高度取决于内容(变化)。

So the thing is, I want it to render just as it does now, except that the header stays visible when I scroll to the right. 所以问题是,我希望它像现在一样呈现,除了当我向右滚动时标题保持可见。

One solution would be to wrap your table in its own <div> , separate from the <header> , with its own set of scrollbars: 一种解决方案是将表包装在自己的<div> ,与<header>分开,并使用自己的一组滚动条:

<article>
  <header><!-- ... --></header>

  <div style="overflow:auto">
    <table><!-- ... --></table>
  </div>
</article>

That way, the header appears to stay fixed, but the table is still scrollable left and right. 这样,标题似乎保持固定,但表仍然可以左右滚动。

If I understand you correctly, the solution should be to put float:right in the header. 如果我理解正确,解决方案应该是将float:right放在标题中。

Another thing: you can't simply give a relative (%) value to a div's height. 另一件事:您不能简单地将相对(%)值赋予div的高度。 There is a way, but I believe that is when it inherits that property from its parent div. 有一种方法,但我相信这是从它的父div继承该属性的时候。

If you want the header to stay in same place left and right and up/down, but don't want to hardcode in where it appears, you could try having a dive with relative positioning, place that where you want, and then place a fixed div in that. 如果您希望标题左右和上/下保持在同一位置,但不想在其出现的位置进行硬编码,您可以尝试进行相对定位的潜水,将其放置在您想要的位置,然后放置固定div。 As long as you don't set it's top/left/bottom/right, the div should stay at the 0,0 of it's parent. 只要你没有设置它的上/左/下/右,div应保持在它的父级的0,0。

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

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