CSS实现垂直居中的几种方法
建站知识 2021-07-03 08:43www.168986.cn长沙网站建设
有些方法在一些浏览器中无效,,到底选择哪种方法好,还要看具体情况。
方法一
这个方法把一些 div 的显示方式设置为表格,我们可以使用表格的 vertical-align property 属性。
<div id="wrapper">
<div id="cell">
<div class="content">
Content goes here</div>
</div>
</div>
#wrapper {display:table;}
#cell {display:table-cell; vertical-align:middle;}
优点content 可以动态改变高度(不需在 CSS 中定义)。当 wrapper 里没有足够空间时, content 不会被截断;
缺点Inter Explorer(甚至 IE8 beta)中无效,许多嵌套标签。
方法二
这个方法使用绝对定位的 div,把它的 设置为 50%, margin 设置为负的 content 高度。这意味着对象必须在 CSS 中指定固定的高度。
因为有固定高度,或许你想给 content 指定 overflow:auto,这样如果 content 太多的话,就会出现滚动条,以免content 溢出。
<div class="content">
Content goes here
</div>
#content {
position:absolute;
:50%;
height:240px;
margin-:-120px; / negative half of the height /
}
优点适用于所有浏览器,不需要嵌套标签
缺点没有足够空间时,content 会消失(类似div 在 body 内,当用户缩小浏览器窗口,滚动条不出现的情况)
方法三
这种方法,在 content 元素外插入一个 div。设置此 div height:50%; margin-bottom:-contentheight;。
content 清除浮动,并显示在中间。
<div id="floater">
<div id="content">
Content here
</div>
</div>
#floater {float:left; height:50%; margin-bottom:-120px;}
#content {clear:both; height:240px; position:relative;}
优点适用于所有浏览器,没有足够空间时(例如窗口缩小) content 不会被截断,滚动条出现
缺点唯一我能想到的就是需要额外的空元素了
方法四
这个方法使用了一个 position:absolute,有固定宽度和高度的 div。这个 div 被设置为 :0; bottom:0;。因为它有固定高度,其实并不能和上下都间距为 0, margin:auto; 会使它居中。使用 margin:auto;使块级元素垂直居中是很简单的。
<div id="content">
Content here
</div>
#content {
position:absolute;
:0;
bottom:0;
left:0;
right:0;
margin:auto;
height:240px;
width:70%;
}
优点简单;
缺点IE(IE8 beta)中无效,无足够空间时,content 被截断,不会有滚动条出现;
方法五
这个方法只能将单行文本置中。只需要简单地把 line-height 设置为那个对象的 height 值就可以使文本居中了。
<div id="content"> Content here</div>
#content {height:100px; line-height:100px;}
优点适用于所有浏览器,无足够空间时不会被截断
缺点只对文本有效(块级元素无效),多行时,断词比较糟糕
这个方法在小元素上非常有用,例如使按钮文本或者单行文本居中。
方法一
这个方法把一些 div 的显示方式设置为表格,我们可以使用表格的 vertical-align property 属性。
复制代码
代码如下:<div id="wrapper">
<div id="cell">
<div class="content">
Content goes here</div>
</div>
</div>
#wrapper {display:table;}
#cell {display:table-cell; vertical-align:middle;}
优点content 可以动态改变高度(不需在 CSS 中定义)。当 wrapper 里没有足够空间时, content 不会被截断;
缺点Inter Explorer(甚至 IE8 beta)中无效,许多嵌套标签。
方法二
这个方法使用绝对定位的 div,把它的 设置为 50%, margin 设置为负的 content 高度。这意味着对象必须在 CSS 中指定固定的高度。
因为有固定高度,或许你想给 content 指定 overflow:auto,这样如果 content 太多的话,就会出现滚动条,以免content 溢出。
复制代码
代码如下:<div class="content">
Content goes here
</div>
#content {
position:absolute;
:50%;
height:240px;
margin-:-120px; / negative half of the height /
}
优点适用于所有浏览器,不需要嵌套标签
缺点没有足够空间时,content 会消失(类似div 在 body 内,当用户缩小浏览器窗口,滚动条不出现的情况)
方法三
这种方法,在 content 元素外插入一个 div。设置此 div height:50%; margin-bottom:-contentheight;。
content 清除浮动,并显示在中间。
复制代码
代码如下:<div id="floater">
<div id="content">
Content here
</div>
</div>
#floater {float:left; height:50%; margin-bottom:-120px;}
#content {clear:both; height:240px; position:relative;}
优点适用于所有浏览器,没有足够空间时(例如窗口缩小) content 不会被截断,滚动条出现
缺点唯一我能想到的就是需要额外的空元素了
方法四
这个方法使用了一个 position:absolute,有固定宽度和高度的 div。这个 div 被设置为 :0; bottom:0;。因为它有固定高度,其实并不能和上下都间距为 0, margin:auto; 会使它居中。使用 margin:auto;使块级元素垂直居中是很简单的。
复制代码
代码如下:<div id="content">
Content here
</div>
#content {
position:absolute;
:0;
bottom:0;
left:0;
right:0;
margin:auto;
height:240px;
width:70%;
}
优点简单;
缺点IE(IE8 beta)中无效,无足够空间时,content 被截断,不会有滚动条出现;
方法五
这个方法只能将单行文本置中。只需要简单地把 line-height 设置为那个对象的 height 值就可以使文本居中了。
复制代码
代码如下:<div id="content"> Content here</div>
#content {height:100px; line-height:100px;}
优点适用于所有浏览器,无足够空间时不会被截断
缺点只对文本有效(块级元素无效),多行时,断词比较糟糕
这个方法在小元素上非常有用,例如使按钮文本或者单行文本居中。
长沙网站设计
- 如何进行东阳SEO关键词优化?
- 边坝哪有关键词排名优化:提升你的网站流量与
- 安国百度优化服务:提升您的在线可见性
- 阜康新手做SEO怎么做
- 山西seo网站排名关键词优化:提升您网站曝光率
- 临沂seo网站排名关键词优化:提高你的网站可见
- 广西SEO网站推广怎样付费比较合理
- 双辽SEO网站推广:提升你的网站可见性与流量
- 辽宁企业网站优化购买方式有哪些
- 提升宝清百度SEO排名的实用技巧与策略
- 静宁百度SEO排名:提升您网站曝光率的关键策略
- 彭州百度SEO排名的提升策略和实施指南
- 广南百度关键词SEO:提升网站排名的关键策略
- 辽宁关键词优化怎么做论坛营销
- 吉林百度seo排名如何做到让用户满意
- 内黄百度优化服务:提升在线可见性的关键