破晓 发表于 2016-8-11 14:31:10

还记得我们折腾过的居中么?DIV垂直居中

<p style="margin-top: 18px; margin-bottom: 18px; color: rgb(51, 51, 51); font-family: &quot;helvetica neue&quot;, tahoma, &quot;hiragino sans gb&quot;, stheiti, &quot;wenquanyi micro hei&quot;, 微软雅黑, 宋体, sans-serif; font-size: 16px; line-height: 27.2px;">虽然div中内容上下居中的问题已经是一个比较古老的话题,但是最近发现还是有很多前端开发者在询问如何实现。其实网络上已经有很多资料和案例了,我这里再总结一下几个比较常见的处理方式。</p><h2 style="font-size: 26px; margin-top: 20px; margin-bottom: 20px; counter-reset: section 0; font-weight: normal; color: rgb(51, 51, 51); font-family: &quot;helvetica neue&quot;, tahoma, &quot;hiragino sans gb&quot;, stheiti, &quot;wenquanyi micro hei&quot;, 微软雅黑, 宋体, sans-serif;">情形一:div限高,内容长度限一行</h2><pre style="font-family: consolas, monaco, menlo, &quot;courier new&quot;, courier, monospace; border: 1px solid rgb(221, 221, 221); font-size: 12px; line-height: 18px; overflow: auto; padding: 6px 10px; border-radius: 3px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248);"><code style="font-family: consolas, monaco, menlo, &quot;courier new&quot;, courier, monospace; display: inline-block; line-height: 1.5; border: 0px none; border-radius: 0px; font-size: inherit; background: none;">&lt;style&gt;
.v-align {
    margin: 0 auto;
    width: 200px;
    height: 80px;
    text-align: center;
    line-height: 80px;
    border: 1px solid #ddd;
}
&lt;/style&gt;
&lt;!-- html --&gt;
&lt;div class="v-align"&gt;我的内容只能有一行。&lt;/div&gt;
</code></pre><p style="margin-top: 18px; margin-bottom: 18px; color: rgb(51, 51, 51); font-family: &quot;helvetica neue&quot;, tahoma, &quot;hiragino sans gb&quot;, stheiti, &quot;wenquanyi micro hei&quot;, 微软雅黑, 宋体, sans-serif; font-size: 16px; line-height: 27.2px;"><img src="http://divio.qiniudn.com/Fus4_kst_xBp2KF9CFcUIUjiRiIW" alt="valign_1" style="max-width: 50em; margin-top: 1em; margin-bottom: 1em;"></p><h2 style="font-size: 26px; margin-top: 20px; margin-bottom: 20px; counter-reset: section 0; font-weight: normal; color: rgb(51, 51, 51); font-family: &quot;helvetica neue&quot;, tahoma, &quot;hiragino sans gb&quot;, stheiti, &quot;wenquanyi micro hei&quot;, 微软雅黑, 宋体, sans-serif;">情形二:div限高,内容不限</h2><pre style="font-family: consolas, monaco, menlo, &quot;courier new&quot;, courier, monospace; border: 1px solid rgb(221, 221, 221); font-size: 12px; line-height: 18px; overflow: auto; padding: 6px 10px; border-radius: 3px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248);"><code style="font-family: consolas, monaco, menlo, &quot;courier new&quot;, courier, monospace; display: inline-block; line-height: 1.5; border: 0px none; border-radius: 0px; font-size: inherit; background: none;">.v-mult {
    margin: 0 auto;
    width: 200px;
    height: 100px;
    border: 1px solid #ddd;
    overflow: hidden;
}
.v-mult .empty,
.v-mult .text {
    display: inline-block;
    *display: inline;
    *zoom: 1;
    vertical-align: middle;
}
.v-mult .empty {
    height: 100%;
}
&lt;!-- html --&gt;
&lt;div class="v-mult"&gt;
    &lt;span class="empty"&gt;&lt;/span&gt;
    &lt;span class="text"&gt;我的内容不限,多高都行&lt;br&gt;换行照常&lt;/span&gt;
&lt;/div&gt;
</code></pre><p style="margin-top: 18px; margin-bottom: 18px; color: rgb(51, 51, 51); font-family: &quot;helvetica neue&quot;, tahoma, &quot;hiragino sans gb&quot;, stheiti, &quot;wenquanyi micro hei&quot;, 微软雅黑, 宋体, sans-serif; font-size: 16px; line-height: 27.2px;"><img src="http://divio.qiniudn.com/Fqvd3m5TjkCzBpeIwTBiK2NjhjvK" alt="valign_2" style="max-width: 50em; margin-top: 1em; margin-bottom: 1em;"></p><h2 style="font-size: 26px; margin-top: 20px; margin-bottom: 20px; counter-reset: section 0; font-weight: normal; color: rgb(51, 51, 51); font-family: &quot;helvetica neue&quot;, tahoma, &quot;hiragino sans gb&quot;, stheiti, &quot;wenquanyi micro hei&quot;, 微软雅黑, 宋体, sans-serif;">情形三:div高度不定,内容高度一定</h2><pre style="font-family: consolas, monaco, menlo, &quot;courier new&quot;, courier, monospace; border: 1px solid rgb(221, 221, 221); font-size: 12px; line-height: 18px; overflow: auto; padding: 6px 10px; border-radius: 3px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248);"><code style="font-family: consolas, monaco, menlo, &quot;courier new&quot;, courier, monospace; display: inline-block; line-height: 1.5; border: 0px none; border-radius: 0px; font-size: inherit; background: none;">.v-auto {
    position: relative;
    margin: 0 auto;
    width: 200px;
    border: 1px solid #ddd;
}
.v-auto .text {
    position: absolute;
    top: 50%;
    margin-top: -50px;
    height: 100px;
    border: 1px dashed #ddd;
}
&lt;!-- html --&gt;
&lt;div class="v-auto"&gt;
    &lt;div class="text"&gt;
      我的高度是固定的,只有100px高,但是我的父及高度不定,我怎么垂直居中呢?
    &lt;/div&gt;
    &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;/div&gt;
</code></pre><p style="margin-top: 18px; margin-bottom: 18px; color: rgb(51, 51, 51); font-family: &quot;helvetica neue&quot;, tahoma, &quot;hiragino sans gb&quot;, stheiti, &quot;wenquanyi micro hei&quot;, 微软雅黑, 宋体, sans-serif; font-size: 16px; line-height: 27.2px;"><img src="http://divio.qiniudn.com/FvN2irlptvo5ARIBbk2l7XLlGDM8" alt="valign_3" style="max-width: 50em; margin-top: 1em; margin-bottom: 1em;"></p><h2 style="font-size: 26px; margin-top: 20px; margin-bottom: 20px; counter-reset: section 0; font-weight: normal; color: rgb(51, 51, 51); font-family: &quot;helvetica neue&quot;, tahoma, &quot;hiragino sans gb&quot;, stheiti, &quot;wenquanyi micro hei&quot;, 微软雅黑, 宋体, sans-serif;">情形四:div高度不定,内容高度不定</h2><pre style="font-family: consolas, monaco, menlo, &quot;courier new&quot;, courier, monospace; border: 1px solid rgb(221, 221, 221); font-size: 12px; line-height: 18px; overflow: auto; padding: 6px 10px; border-radius: 3px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248);"><code style="font-family: consolas, monaco, menlo, &quot;courier new&quot;, courier, monospace; display: inline-block; line-height: 1.5; border: 0px none; border-radius: 0px; font-size: inherit; background: none;">.v-auto-out {
    position: relative;
    margin: 0 auto;
    width: 200px;
    border: 1px solid #ddd;
}
.v-auto-out .auto-in {
    position: absolute;
    top: 50%;
    border: 1px dashed #ddd;
    /* 这里有兼容性问题 */
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    -o-transform: translateY(-50%);
    transform: translateY(-50%);
}
&lt;!-- html --&gt;
&lt;div class="v-auto-out"&gt;
    &lt;div class="auto-in"&gt;我的高度不定,我的父及高度也不定,这下要上下居中,该如何是好?我们一起来瞧瞧吧。&lt;/div&gt;
    &lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
&lt;/div&gt;
</code></pre><p style="margin-top: 18px; margin-bottom: 18px; color: rgb(51, 51, 51); font-family: &quot;helvetica neue&quot;, tahoma, &quot;hiragino sans gb&quot;, stheiti, &quot;wenquanyi micro hei&quot;, 微软雅黑, 宋体, sans-serif; font-size: 16px; line-height: 27.2px;"><img src="http://divio.qiniudn.com/Fl6U9BDnxJbcx3n4VDY26udVFPtc" alt="valign_4" style="max-width: 50em; margin-top: 1em; margin-bottom: 1em;"></p><p style="margin-top: 18px; margin-bottom: 18px; color: rgb(51, 51, 51); font-family: &quot;helvetica neue&quot;, tahoma, &quot;hiragino sans gb&quot;, stheiti, &quot;wenquanyi micro hei&quot;, 微软雅黑, 宋体, sans-serif; font-size: 16px; line-height: 27.2px;">好了,知道这四种方式,我相信足以应对日常工作中的各种垂直居中问题。代码很简单,不再做多余阐述。总之一句话,CSS的各个属性样式,就好像人肢体的各个器官,了解了各个器官的功能,才能相互配合完成各种任务。相反,个体的能力是有限的。</p><p style="margin-top: 18px; margin-bottom: 18px; color: rgb(51, 51, 51); font-family: &quot;helvetica neue&quot;, tahoma, &quot;hiragino sans gb&quot;, stheiti, &quot;wenquanyi micro hei&quot;, 微软雅黑, 宋体, sans-serif; font-size: 16px; line-height: 27.2px;">作者博客:<a href="http://www.seejs.com/" target="_blank" style="text-decoration: none; color: rgb(49, 135, 219);">百码山庄</a></p><p></p>
页: [1]
查看完整版本: 还记得我们折腾过的居中么?DIV垂直居中