博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【CSS】CSS垂直居中:父层宽高不固定子层固定 和 父层宽高固定子层不固定
阅读量:7018 次
发布时间:2019-06-28

本文共 1618 字,大约阅读时间需要 5 分钟。

  hot3.png

垂直居中:父层高宽不固定,子层高宽固定

//<body><div class="app">*********</div></body>

/* Portrait layout (default) */

.app {

    background:url(../img/logo.png) no-repeat center top; /* 170px x 200px */
    position:absolute;             /* position in the center of the screen */
    left:50%;
    top:50%;
    height:50px;                   /* text area height */
    width:225px;                   /* text area width */
    text-align:center;
    padding:180px 0px 0px 0px;     /* image height is 200px (bottom 20px are overlapped with text) */
    margin:-115px 0px 0px -112px;  /* offset vertical: half of image height and text area height */
    /* offset horizontal: half of text area width */
}
/* Landscape layout (with min-width) */
screen and (min-aspect-ratio: 1/1) and (min-width:400px) {
    .app {
        background-position:left center;
        padding:75px 0px 75px 170px;  /* padding-top + padding-bottom + text area = image height */
        margin:-90px 0px 0px -198px;  /* offset vertical: half of image height */
        /* offset horizontal: half of image width and text area width */
    }

}

垂直居中:父层高宽固定,子层高度固定

//<a href="product.html"><b>*********</b></a>

a{

    display:table-cell;
    position: relative;
    z-index: 1;
    color:#ffffff;
    vertical-align:middle;
    font-size:160px;
    height:158px;
    width: 158px;
}
a b{
    display:inline-block;
    vertical-align:middle;
    font-size:14px;
    line-height: 30px;
    padding: 0 10px;
    cursor: pointer;
}

如何让未知尺寸的图片在已知宽高的容器内水平垂直居中?

#test{display:table-cell;*display:block;*position:relative;width:200px;height:200px;text-align:center;vertical-align:middle;}#test p{*position:absolute;*top:50%;*left:50%;margin:0;}#test p img{*position:relative;*top:-50%;*left:-50%;vertical-align:middle;}

转载于:https://my.oschina.net/maomi/blog/117693

你可能感兴趣的文章
body元素对象的clientWidth、offsetWidth、scrollWidth、clientLeft、offsetLeft、scrollLeft
查看>>
[转]用Excel制作甘特图并管理项目
查看>>
滚动加载事件和禁止滚动条滚动
查看>>
Halcon算子翻译——convert_vector_to_tuple
查看>>
react-native-vector-icons的使用方法
查看>>
《Effective C++》笔记:I
查看>>
C语言 指针和指针变量
查看>>
Windows Mobile 消除安装程序时显示“此程序来自未知发行者…“提示的方法
查看>>
机器学习各类工具weka、scikit-learn等各项指标的对比
查看>>
Java Hibernate 二级缓存配置及缓存的统计策略
查看>>
Openfire开发配置,Openfire源代码配置,OpenFire二次开发配置
查看>>
System.Types.hpp(77): E2029 'TObject' must be a previously defined class or struct
查看>>
安卓图表引擎AChartEngine(三) - 示例源码折线图、饼图和柱状图
查看>>
微信流量变现有哪些方式?
查看>>
MySQL for Windows 解压缩版配置安装
查看>>
主机和虚拟机能相互ping通但是不能复制
查看>>
angularjs自动化测试系列之jasmine
查看>>
linux 用户打开进程数和文件数调整
查看>>
【小白的CFD之旅】19 来自计算网格的困惑
查看>>
Tcl与Design Compiler (八)——DC的逻辑综合与优化
查看>>