css让浮动元素水平居中_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:36:56
Original
993 people have browsed it

对于定宽的非浮动元素我们可以用 margin:0 auto; 进行水平居中。

对于不定宽的浮动元素我们也有一个常用的技巧解决它的水平居中问题。如下:

HTML 代码:

<div class="box">    <p>我是浮动的</p>    <p>我也是居中的</p></div>
Copy after login

CSS 代码:

.box{    float:left;    position:relative;    left:50%;}p{    float:left;    position:relative;    right:50%;}
Copy after login

 

这样就解决了浮动元素水平居中了;

父元素和子元素同时左浮动,然后父元素相对左移动50%,再然后子元素相对右移动50%或者子元素相对左移动-50%也就可以了

 

Related labels:
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