让CSS实现居中显示的5种方法
时间:20-02-21 10:57 责任编辑: admin 来源:电脑吧 点击: 次
使用 CSS 实现垂直居中并不容易。有些方法在一些浏览器中无效。下面我们看一下使对象垂直集中的5种不同方法,以及它们各自的优缺点。 ![]() 方法一 这个方法把一些 div 的显示方式设置为表格,因此我们可以使用表格的 vertical-align property 属性。 Content goes here #wrapper {display:table;}#cell {display:table-cell; vertical-align:middle;} 优点: content 可以动态改变高度(不需在 CSS 中定义)。当 wrapper 里没有足够空间时, content 不会被截断 缺点: Internet Explorer(甚至 IE8 beta)中无效,许多嵌套标签(其实没那么糟糕,另一个专题) 方法二: 这个方法使用绝对定位的 div,把它的 top 设置为 50%,top margin 设置为负的 content 高度。这意味着对象必须在 CSS 中指定固定的高度。 因为有固定高度,或许你想给 content 指定 overflow:auto,这样如果 content 太多的话,就会出现滚动条,以免content 溢出。 Content goes here #content { position:absolute; top:50%; height:240px; margin-top:-120px; /* negative half of the height */} 优点: 适用于所有浏览器 不需要嵌套标签 缺点: 没有足够空间时,content 会消失(类似div 在 body 内,当用户缩小浏览器窗口,滚动条不出现的情况) 方法三 这种方法,在 content 元素外插入一个 div。设置此 div height:50%; margin-bottom:-contentheight;。 content 清除浮动,并显示在中间。 Content here #floater {float:left; height:50%; margin-bottom:-120px;}#content {clear:both; height:240px; position:relative;} 优点: 适用于所有浏览器 没有足够空间时(例如:窗口缩小) content 不会被截断,滚动条出现 缺点: 唯一我能想到的就是需要额外的空元素了(也没那么糟,又是另外一个话题) 方法四 这个方法使用了一个 position:absolute,有固定宽度和高度的 div。这个 div 被设置为 top:0; bottom:0;。但是因为它有固定高度,其实并不能和上下都间距为 0,因此 margin:auto; 会使它居中。使用 margin:auto;使块级元素垂直居中是很简单的。 Content here #content { position:absolute; top:0; bottom:0; left:0; right:0; margin:auto; height:240px; width:70%;} 优点: 简单 缺点: IE(IE8 beta)中无效 无足够空间时,content 被截断,但是不会有滚动条出现 方法五 这个方法只能将单行文本置中。只需要简单地把 line-height 设置为那个对象的 height 值就可以使文本居中了。 Content here #content {height:100px; line-height:100px;} 优点: 适用于所有浏览器 无足够空间时不会被截断 缺点: 只对文本有效(块级元素无效) 多行时,断词比较糟糕 这个方法在小元素上非常有用,例如使按钮文本或者单行文本居中 哪个方法? 我最喜欢的是方法三,缺点不多。因为 content 会清除浮动,所以可以在它上面放置别的元素,并且当窗口缩放时, 居中的 content 不会把另外的元素盖住。 Title Content Here #floater {float:left; height:50%; margin-bottom:-120px;}#top {float:right; width:100%; text-align:center;}#content {clear:both; height:240px; position:relative;} 现在你知道是怎么回事了,现在我们开始创建一个简单但是有趣的网站。最终的样子是这样的: ![]() 步骤一 以语义化标签开始是很好的。下面是我们的页面构成: #floater/*把 content 置中*/#contred/*centre 盒*/#side#logo#nav/*无序列表*/#content#bottom/*放置版权等*/ 这是我用到的 xhtml 代码: A Centred Company A Company Home Products Blog Contact About Page Title Holisticly re-engineer value-added outsourcing after process-centric collaboration and idea-sharing. Energistically simplify impactful niche markets via enabled imperatives. Holisticly predominate premium innovation after compelling scenarios. Seamlessly recaptiualize high standards in human capital with leading-edge manufactured products. Distinctively syndicate standards compliant schemas before robust vortals. Uniquely recaptiualize leveraged web-readiness vis-a-vis out-of-the-box information. Heading 2 Efficiently embrace customized web-readiness rather than customer directed processes. Assertively grow cross-platform imperatives vis-a-vis proactive technologies. Conveniently empower multidisciplinary meta-services without enterprise-wide interfaces. Conveniently streamline competitive strategic theme areas with focused e-markets. Phosfluorescently syndicate world-class communities vis-a-vis value-added markets. Appropriately reinvent holistic services before robust e-services. Copyright notice goes here |