居中浮动DIV [英] Center floating DIVs

查看:127
本文介绍了居中浮动DIV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图集中3个浮动DIV。如果我给父DIV display:table; 和子DIV display:cell; ,它就会起作用,就像表格一样。还有另一种方法吗?

 <!DOCTYPE html> 
< html lang =en>
< head>
< meta charset =UTF-8/>
< title>中心Div< /标题>
< style type =text / css>
#container
{
text-align:center;
margin:0 auto;
display:table;
}
#container div
{
float:left;
padding:5px;
display:cell;
}
< / style>
< / head>
< body>
< div id =container>
< div style =background-color:yellow> Text 1< / div>
< div style =background-color:lightgreen> Text 2< / div>
< div style =background-color:lightblue> Text 3< / div>
< / div>
< / body>
< / html>


解决方案



$ $ p $ $ $ $ $ $ $ $ $ $ -居中对齐;
margin:0 auto;
width:150px;
}

My Fiddle


Im trying to center 3 floating DIVs. It works if I give the parent DIV display:table; and the child DIVs display:cell; which will then act like a table. Is there another way ?

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Center Div</title>
        <style type="text/css">
        #container
        {               
            text-align:center;
            margin:0 auto;          
            display:table;
        }
        #container div
        {           
            float:left;
            padding:5px;
            display:cell;
        }
        </style>
    </head>
    <body>  
        <div id="container">
            <div style="background-color:yellow">Text 1</div>
            <div style="background-color:lightgreen">Text 2</div>
            <div style="background-color:lightblue">Text 3</div>            
        </div>
    </body>
</html>

解决方案

You need to assign width to the parent div like this :

#container
  {               
    text-align:center;
    margin:0 auto;   
    width:150px;    
  }

My Fiddle

这篇关于居中浮动DIV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆