南昌做网站的公司多不多,pageadmin做网站,青岛网站制作服务商,wordpress会员无广告css 的居中有水平居中和垂直居中#xff0c;这两种居中又分为行内元素居中和块级元素居中#xff0c;不同的居中用不同方法。 水平居中 1、行内元素水平居中#xff08;文本#xff0c;图片#xff09; 给父层设置 text-align:center; 可以实现行内元素水平居中。 !DO…css 的居中有水平居中和垂直居中这两种居中又分为行内元素居中和块级元素居中不同的居中用不同方法。 水平居中 1、行内元素水平居中文本图片 给父层设置 text-align:center; 可以实现行内元素水平居中。 !DOCTYPE HTML
html langen-US
headmeta charsetUTF-8title/titlestyle typetext/css.center{text-align:center;}/style
/head
bodydiv classcentera hrefhttp://www.google.com.hk/谷歌搜索/abr/br/img srccat.jpg width248 height162 alt//div
/body
/html 2、确定宽度块级元素水平居中 确定宽度的块级元素水平居中常用的有 margin:0 auto; 相信很多开发人员都用的是这个不过本人认为还有更好的写法margin-left:auto;margin-right:auto; 因为 margin-top 和 margin-bottom 在重置 css 时就已经写了为 0 了。 !DOCTYPE HTML
html langen-US
headmeta charsetUTF-8title/titlestyle typetext/css.center{width:100px;height:100px;margin-left:auto;margin-right:auto;background:green;}/style
/head
bodydiv classcenter/div
/body
/html 3、不确定宽度的块级元素水平居中 不确定宽度的块级元素有三种方法实现。 方法一 !DOCTYPE HTML
html langen-US
headmeta charsetUTF-8title/titlestyle typetext/css*{margin:0;padding:0;}ul{list-style:none;}table{margin-left:auto;margin-right:auto;}.demo li{float:left;display:inline;margin-right:5px;}.demo a{float:left;width:20px;height:20px;text-align:center;line-height:20px;background:#316ac5;color:white;border:1px solid #316ac5;text-decoration:none;}.demo a:hover{background:white;color:#316ac5;}/style
/head
bodytabletbodytrtdul classdemolia href#1/a/li/ul/td/tr/tbody/tabletabletbodytrtdul classdemolia href#1/a/lilia href#2/a/lilia href#3/a/li/ul/td/tr/tbody/tabletabletbodytrtdul classdemolia href#1/a/lilia href#2/a/lilia href#3/a/lilia href#4/a/lilia href#5/a/li/ul/td/tr/tbody/table
/body
/html 这里用到了 table 标签来实现不确定宽度的块级元素水平居中。table 本身不是块级元素如果不给它设定宽度的话会由内部元素的宽度“撑开”但即使不设定它的宽度仅设置 margin-left:auto 和 margin-right:auto 就可以实现水平居中。 这种方法很巧妙但是增加了无语义标签加深了标签的嵌套层数。 方法二 !DOCTYPE HTML
html langen-US
headmeta charsetUTF-8title/titlestyle typetext/css*{margin:0;padding:0;}ul{list-style:none;}.wrapper{width:500px;height:100px;background:black;}.demo{text-align:center;padding:5px;}.demo li{display:inline;}.demo a{padding:2px 6px;background:#316ac5;color:white;border:1px solid #316ac5;text-decoration:none;}.demo a:hover{background:white;color:#316ac5;}/style
/head
bodydiv classwrapperul classdemolia href#1/a/li/ulul classdemolia href#1/a/lilia href#2/a/lilia href#3/a/li/ulul classdemolia href#1/a/lilia href#2/a/lilia href#3/a/lilia href#4/a/lilia href#5/a/li/ul/div
/body
/html 方法二是改变元素的 display 值使块级元素变成行内元素然后使用 text-align:center 使其居中。相对于方法一它不用增加无语义标签简化了标签的嵌套深度但它也存在一定的问题它将块级元素变成了行内元素这样就失去了一些块级元素的功能比如设置宽度高度。 方法三 !DOCTYPE HTML
html langen-US
headmeta charsetUTF-8title/titlestyle typetext/css*{margin:0;padding:0;}ul{list-style:none;}.wrapper{width:500px;height:100px;background:black;}.demo{clear:both;padding-top:5px;float:left;position:relative;left:50%;}.demo li{display:inline;float:left;margin-right:5px;position:relative;left:-50%;}.demo a{float:left;width:20px;height:20px;text-align:center;line-height:20px;background:#316ac5;color:white;border:1px solid #316ac5;text-decoration:none;}.demo a:hover{background:white;color:#316ac5;}/style
/head
bodydiv classwrapperul classdemolia href#1/a/li/ulul classdemolia href#1/a/lilia href#2/a/lilia href#3/a/li/ulul classdemolia href#1/a/lilia href#2/a/lilia href#3/a/lilia href#4/a/lilia href#5/a/li/ul/div
/body
/html 方法三通过给父层设置浮动和相对定位以及 left:50%子元素设置相对定位和 left:-50% 来实现水平居中。它可以保留块级元素的功能而且不会添加无语义标签不增加嵌套深度但是设置了相对定位会带来一定的副作用。 这三种方法各有优缺点具体使用哪种方法可以视具体情况而定。 垂直居中 1、父层高度不确定的垂直居中 通过给父层设置相同的上下内边距实现。 !DOCTYPE HTML
html langen-US
headmeta charsetUTF-8title/titlestyle typetext/css*{margin:0;padding:0;}.demo{width:500px;color:white;margin-bottom:10px;padding-top:20px;padding-bottom:20px;background:black;}.content{width:200px;height:50px;background:red;}/style
/head
bodydiv classdemohello world/divdiv classdemoimg srccat.jpg width248 height162 alt//divdiv classdemodiv classcontent/div/div
/body
/html 2、父层高度确定的单行文本垂直居中 通过给父层设置行高来实现行高和父层高度相同。 !DOCTYPE HTML
html langen-US
headmeta charsetUTF-8title/titlestyle typetext/css*{margin:0;padding:0;}.demo{width:500px;color:white;background:black;height:100px;line-height:100px;}/style
/head
bodydiv classdemohello world/div
/body
/html 3、父层高度确定的多行文本、图片、块级元素垂直居中 方法一 说到垂直居中css 中有个用于垂直居中的属性 vertical-align但只有在父层为 td 或者 th 时这个属性才会生效对于其他块级元素例如 div、p 等默认情况是不支持的。在 firefox 和 ie8 下可以设置块级元素的 display 值为 table-cell来激活 vertical-align 属性但 ie6,7 并不支持所以这种方法没有办法跨浏览器兼容。但是我们可以使用 table。 !DOCTYPE HTML
html langen-US
headmeta charsetUTF-8title/titlestyle typetext/css*{margin:0;padding:0;}.wrapper{background:black;width:500px;color:white;height:100px;}.demo{width:200px;background:red;height:50px;}/style
/head
bodytabletrtd classwrapperhellow worldbr/hellow worldbr/hellow worldbr//td/tr/tabletabletrtd classwrapperimg srccat.jpg alt//td/tr/tabletabletrtd classwrapperdiv classdemo/div/td/tr/table
/body
/html table 可以很好的实现垂直居中效果但是它添加了无语义标签增加了嵌套深度。 方法二 对支持 display:table-cell 的 ie8 和 firefox 用 display:table-cell 和 vertical-align:middle 来实现居中对不支持 display:table-cell 的 ie6 和 ie7 使用 hack 写法。 !DOCTYPE HTML
html langen-US
headmeta charsetUTF-8title/titlestyle typetext/css*{margin:0;padding:0;}.mb{margin-bottom:10px;}.wrapper{background:black;width:500px;color:white;height:100px;margin-bottom:10px;display:table-cell;vertical-align:middle;*position:relative;}.demo{width:200px;background:red;height:50px;}.vam{*position:absolute;*top:50%;}.va{*position:relative;*top:-50%;}/style
/head
bodydiv classmb10div classwrapperdiv classvamdiv classvahellow worldbr/hellow worldbr/hellow world/div/div/div/divdiv classmb10div classwrapperdiv classvamimg srccat.jpg alt//div/div/divdiv classmb10div classwrapperdiv classvamdiv classva demo/div/div/div/div
/body
/html 利用 hack 技术区别对待 firefox、ie8 和 ie6、ie7在不支持 display:table-cell 的 ie6 和 ie7 下通过给父子两层元素分别设置 top:50% 和 top:-50% 来实现居中。这种方法的好处是没有增加额外的标签但缺点也很明显一方面它使用了 hack不利于维护另一方面它设置了 position:relative 和 position:absolute带来了副作用。转载于:https://www.cnblogs.com/axl234/p/3867347.html