如何将水平css应用于div

时间:2016-04-07 09:27:36

标签: html css center

如何使用css将div设为1,2,3?

text-align: center只会让它们垂直吗?

<div class="container">
   <div class="one">One</div>
   <div class="two">Two</div>
   <div class="three">Three</div>
</div>

Js代码:

$(".container").css({
    "position": "fixed", 
    "margin":"0", 
    "left": "0px", 
    "right": "0px", 
    "height": "auto", 
    "min-height": "50px", 
    "z-index": "255", 
    "color": "white", 
    "line-height": "50px", 
    "padding": "16px", 
    "font-size": "1em",
    "text-align": "center", 
    "bottom": "0px", 
    "opacity": "1", 
    "background": "black"
});

3 个答案:

答案 0 :(得分:1)

设置宽度并在CSS中应用display:inline-block 像这样:

.container div {
    width:100px;
    display:inline-block;
}

答案 1 :(得分:1)

display: inline-block;可以使其有效。

float: left;无效,因为它会将您的所有div元素放到left而不是center(它会删除您放置的text-align: center;属性在.container

See it here

顺便问一下,为什么用jQuery而不是CSS来应用样式?

答案 2 :(得分:0)

我认为你必须尝试'浮动:左'