内容垂直对齐时如何修复页脚(how to fixed the footer when content is aligned vertically)

编程入门 行业动态 更新时间:2024-10-27 13:24:18
内容垂直对齐时如何修复页脚(how to fixed the footer when content is aligned vertically)

我有一个非常不典型的网站,其中内容在屏幕中间对齐,我的意思是垂直和水平,以获得此结果使用vertical-align: middle; 对于每个项目和主要容器text-align: center; height: calc(100% - header - footer )) text-align: center; height: calc(100% - header - footer ))

但是当用户改变大小时

对于窗口,页脚也改变了他的位置但不应该这样做

Js小提琴https://jsfiddle.net/hm97o1sa/

有没有办法解决它没有“flex”?

更新:预期的行为

滚动到顶部

滚动到底部

I have a very not typical site where content is aligned by the middle of screen I mean vertically and horizontally, for getting this result used vertical-align: middle; for each item and for the main container text-align: center; height: calc(100% - header - footer ))

but when the user is changing size

for the window the footer is also change his position but should not do it

Js fiddle https://jsfiddle.net/hm97o1sa/

is there any way to fix it without "flex" ?

updated: expected behavior

scrolled to the top

scrolled to the bottom

最满意答案

一种可能的解决方案是将calc与Viewport单位一起使用vh 。

使用calc() ,您可以执行计算以确定CSS属性值。

使用视口单元,您可以使用视口大小,例如在这种情况下100%的视口高度( vh )。

    html,
    body {
      margin: 0;
      padding: 0;
      height: 100%;
    }
    
    #header {
      height: 100px;
      background: blue;
    }
    
    #content {
      height: calc(100vh - 150px);
      min-height: 250px;
      text-align: center;
    }
    
    #vert-align {
      display: inline-block;
      height: 100%;
      vertical-align: middle;
    }
    
    #item_1 {
      background: yellow;
      height: 250px;
      width: 250px;
      display: inline-block;
      vertical-align: middle;
      margin-right: 50px;
    }
    
    #item_2 {
      background: red;
      height: 250px;
      width: 250px;
      display: inline-block;
      vertical-align: middle;
    }
    
    #footer {
      height: 50px;
      background: green;
    } 
  
<div id="header">HEADER</div>
<div id="content">
  <div id="vert-align"></div>
  <div id="item_1"></div>
  <div id="item_2"></div>
</div>
<div id="footer">FOOTER</div> 
  
 

A possible solution would be to use calc together with Viewport units vh.

With calc(), you can perform calculations to determine CSS property values.

With Viewport units, you can get work with Viewport size, for example in this case 100% of the Viewport height (vh).

    html,
    body {
      margin: 0;
      padding: 0;
      height: 100%;
    }
    
    #header {
      height: 100px;
      background: blue;
    }
    
    #content {
      height: calc(100vh - 150px);
      min-height: 250px;
      text-align: center;
    }
    
    #vert-align {
      display: inline-block;
      height: 100%;
      vertical-align: middle;
    }
    
    #item_1 {
      background: yellow;
      height: 250px;
      width: 250px;
      display: inline-block;
      vertical-align: middle;
      margin-right: 50px;
    }
    
    #item_2 {
      background: red;
      height: 250px;
      width: 250px;
      display: inline-block;
      vertical-align: middle;
    }
    
    #footer {
      height: 50px;
      background: green;
    } 
  
<div id="header">HEADER</div>
<div id="content">
  <div id="vert-align"></div>
  <div id="item_1"></div>
  <div id="item_2"></div>
</div>
<div id="footer">FOOTER</div> 
  
 

更多推荐

本文发布于:2023-08-06 11:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450062.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内容   fixed   footer   vertically   aligned

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!