div+css 水平居中 实现方法_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 12:27:31
Original
1237 people have browsed it

1、  文本、图片等行内元素的水平居中

给父元素设置text-align:center;即可实现居中,例如:

   自由飞翔

若要实现自由飞翔四字的居中,只需在样式中写.top{text-align:certer;}

2、  确定宽的块级元素的水平居中

需设置marin属性,为margin:0 auto;

例如:

内容
若要实现整个div块的水平居中,而定义块宽为70px,需在样式中写入.top{width:70px; margin:0 auto;}

3、  不确定宽度的块级元素的水平居中

有时块的大小是不能确定的,变化的,在这种情况下,用margin属性已经不能实现居中,若要实现,有三种方法可以解决。

(第一种):定义需要居中的块元素为display:table之后,就可以用margin来居中了。例如:

 

        

  • 内容1
  •  

  • 内容2
  • 内容3

li的个数不确定,ul的宽度则不能确定,实现居中,在样式中应写入.footer ul{display:table;margin:0 auto;}

(第二种):将块元素转换为行内元素

在样式中写入.footer ul{display:inline;text-align:center;}

      此时,li也应作出变化 .footer ul li{display:inline;}

 

(第三种):使用相对定位。此种方法在上传图片大小不同,但需要居中时,尤为适用。

按照上例,实现居中,需在样式中写入 .footer {position:relative; left:50%;}

                                   .footer ul {position:relative; left:-50%;}

                    此时li也应转为行内元素。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!