简体   繁体   English

垂直对齐CSS

[英]Vertical alignment CSS

First of all, I have tried many other thing I found out here, and nothing seems to really work for me. 首先,我尝试了在这里发现的许多其他事情,但似乎没有什么对我真正有用。
When I resize the browser everything works perfect horizontally, but vertically, element hidden at the top. 当我调整浏览器的大小时,所有元素在水平方向都可以正常工作,但在垂直方向上,元素隐藏在顶部。
If I remove display:flex some of the divs in Content move to the left side of the screen. 如果删除display:flex ,则Content中的某些div会移到屏幕的左侧。 One of those div is this: 这些div之一是:

 html,body { height:100%; width:100%; min-width: 320px; margin:0; padding:0; display: table; background-image: url('background.png'); background-attachment: fixed; } .content{ width: 100%; height:100%; overflow:auto; align-items: center; justify-content: center; display: table-cell; flex-direction: column; display: flex; text-align: center; vertical-align: middle; position: fixed; top: 50%; left: 50%; transform: translateX(-50%) translateY(-50%); color:white; background-color: rgba(0,0,0,0.4); } .window{ width:40%; color:white; border-radius: 20px; background-color: rgba(255,255,255,0.1); border: 3px solid #03A0D3; margin:0; } 
  <div class="content"> <h1>Title</h1> <p>Text..</p> <div class="window"> <div> <p>Text</p> </div> </div> <div> <?php @include_once ('modules/file.php') ?> </div> <p>Some text here</p> <p>Other text here</p> </div> 

Use margin 0 auto to center the div horizontally. 使用margin 0 auto将div水平居中。
Remove position: fixed , it's causing the divs to overflow out of view on small resolution. 移除position: fixed ,这会导致div在小分辨率下溢出视线。
You can remove the flexbox, you are already centering it with the table display. 您可以删除flexbox,您已经将其与表格显示居中放置了。

 html,body { height:100%; width:100%; min-width: 320px; margin:0; padding:0; display: table; background-image: url('background.png'); background-attachment: fixed; } .content{ width: 100%; height:100%; overflow:auto; /*align-items: center; justify-content: center;*/ display: table-cell; /* flex-direction: column; display: flex;*/ text-align: center; vertical-align: middle; /*position: fixed; top: 50%; left: 50%; transform: translateX(-50%) translateY(-50%);*/ color:white; background-color: rgba(0,0,0,0.4); } .window{ width:40%; color:white; border-radius: 20px; background-color: rgba(255,255,255,0.1); border: 3px solid #03A0D3; margin:0 auto;/*add*/ } 
  <div class="content"> <h1>Title</h1> <p>Text..</p> <div class="window"> <div> <p>Text</p> </div> </div> <div> <?php @include_once ('modules/file.php') ?> </div> <p>Some text here</p> <p>Other text here</p> </div> 

Table display browser support table. 该表显示浏览器支持的表。
Flexbox support table. Flexbox支持表。

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

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