align-multiple-divs-in-one-line-and-center-text-veticaly-and-horizontally

reference : https://stackoverflow.com/questions/21430203/align-multiple-divs-in-one-line-and-center-text-veticaly-and-horizontally

 

 

To show n-number of divs in one line, there are 3 approaches

  • use display:table; 
    This method is supported IE8 and above and comes in pretty handy if you have large amount of css and text and divs to align

  • use float:left;
    All time favorite, the old school approach, this method is most recommended when old browser support has to be considered, requires clearing of the float at the end

  • use display:inline-block;
    never used this method personally float method was considered instead of using this by me

Base CSS

/************Supported by IE8 and above *******************/
 div.table {
    width:100%;        /* important */
    display:table;
    text-align:center;
}
.table-cell {
    display:table-cell;
    vertical-align:middle;
}
/************ Method 2 *******************/
 div.inline {
    width:100%;
    display:inline-block;
    text-align:center;
}
div.inline-div {
    width:32%;
    display:inline-block;
}
/************ Method 3 *******************/
 .float-class {
    width:100%;
    text-align:center;
}
div.floatdiv {
    float:left;
    width:32%;
    border:1px solid black;
}

.clearfloat {
    clear:both;
}

fiddle showing all three methods in 1 place

To vertically center one line in a div

again 3 approaches :
keep in mind, solution has to be responive, so margin-top:25% or 50% is not gonna work!!!

  • line-height
    this approach is usefull when the dimesnion of the parent div is known, then you can simply use the trick line-height:equal to height of parent div

  • position
    idea is to make the parent positioned relative and the text span class an absolute, then center the absolute text using positioning like top/bottom

  • display:table-cell
    highly recommended if IE7 and older support is not required, simply use vertical-align:middle;

Base css

div.fixed-div-height {

    height:200px;
    width:200px;
    text-align:center;
}
div.fixed-div-height span {
    line-height:200px; /* equal to height of the fixed div*/
}


div.unknown-div-height {
    height:100%;
    text-align:center;
    position: relative;
}
div.unknown-div-height > span.unknown-div-margin {
    display:inline-block;
    height:20px;
    position: absolute;
    top: 50%;
    left:0;
    right:0;
}


div.for-ie8-and-above{
    width:100%;
    display:table;
    height:400px;
    text-align:center;
}
div.for-ie8-and-above > div{
    height:400px;
    width:100%;
    display:table-cell;
    vertical-align:middle; /* key here */
}

fiddle showing all three methods

To center a paragraph vertically in center 
this is the tricky part

Practically there is no possible way to center a parapgraph whose height and the containers height is unknown unless you gor for some hacks....one such hack has been quoted at the end of this answer from css tricks!!

Simplest, use :

div.table-cell {
    height:400px; /* can be anything, even in percentage*/
    width:100%;
    display:table-cell;
    vertical-align:middle;   /* key here */
}

fiddle showing remaining 2 possible cases

Another solution posted here : How do I vertically center text with CSS?

IE hack for display:tables : CSS Tricks

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值