垂直居中显示(文字&块级元素)

文字在div中垂直居中显示

-方法1:将div改为table格式
css如下:

<style>
    .table{
        display: table;
        width:100px;
        height:100px;
        background: pink;
    }
    .table .box{
        display: table-cell;
        vertical-align: middle;
        text-align: center;
    }
</style>

html如下:

<div class="table">
    <div class="box">我是table方法写出的文字</div>
</div>

-方法2:将div改为flex格式
css如下:

.flex{
  display: flex;
  display:-webkit-flex;
  width:100px;
  height:100px;
  background: yellow;
  -webkit-align-items:center;
  justify-content: center;
  align-items: center;
}

html如下:

<div class="flex">
    我是flex方法写出的文字
</div>

块级元素始终在屏幕居中显示

css如下:

.container{
  position:absolute;
  top:0;
  left:0;
  width:100%;
  height:100%;
}
.box{
  position:absolute;
  left:50%;
  top:50%;
  height:auto;
  -webkit-transform:translate(-50%,-50%);
  -moz-transform:translate(-50%,-50%);
  -ms-transform:translate(-50%,-50%);
  -o-transform:translate(-50%,-50%);
  transform:translate(-50%,-50%);
}

html如下:

<div class="container">
  <div class="box">
    ...
  </div>
</div>

图片始终在屏幕居中显示

#viewer{
    position: relative;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    text-align: center;
    display: flex;
    display:-webkit-flex;
    -webkit-align-items:center;
    justify-content: center;
    align-items: center;
}
#viewer img{
    max-width:100%;
    max-height:100%;
    margin:auto;
}

html如下:

<div id="viewer">
  <img>
</div>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,065评论 1 92
  • 收听音频,戳链接,旧号itclan已暂停使用,欢迎关注微信itclanCoder公众号可收听更多音频 前言 关于网...
    itclanCoder阅读 8,324评论 3 30
  • 移动开发基本知识点 一.使用rem作为单位 html { font-size: 100px; } @media(m...
    横冲直撞666阅读 3,698评论 0 6
  • H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解dis...
    Mx勇阅读 4,803评论 0 26
  • 当明天变成了今天成为了昨天,最后成为记忆里不再重要的某一天。 我突然发现自己在不知不觉中已被时间推着向前走,这不是...
    看车侃车阅读 372评论 0 0

友情链接更多精彩内容