如何在纯CSS中将div放在左边或右边?

时间:2015-12-29 12:14:58

标签: html css yui-pure-css

我是纯css 的新手,非常好用!

我的HTML:

<header class="pure-u-1 pure-u-md-1-1">
<div class="pure-u-4-5">

    <div class="left-in-header pure-u-3-8">This should be in left</div>

</div>

</header>

我的CSS:

@font-face{
    font-family: 'myfont';
    src: url('myfont.ttf');
}


html, button, input, select, textarea, *, * *,
.pure-g [class *= "pure-u"] {
    /* Set your content font stack here: */
    font-family: 'myfont', Times, "Times New Roman", serif !important;
}

html{
    direction:rtl;
    box-sizing: border-box;

}


*, *:after, *:before{
    box-sizing: inherit;
}

*{
    margin:0;
    padding:0;
}

header{
    color:white;
    background:#b90101;
    background: rgba(200,1,1,0.8);
    margin:0 !important;
    padding:2px 0!important;
}

header > div{
    margin:auto;
    display:block !important;
    background:red;

}

.left-in-header{
    background:green;

}

现在请查看课程.left-in-header的绿色div,我想把它放在父母的左边! (其大小为pure-u-3-8)。 (我的网站语言是波斯语,波斯语是从右到左,然后我的方向是rtl)

如何通过纯类将div放在它的父级中心?

2 个答案:

答案 0 :(得分:3)

将以下css属性提供给:

.pure-u-3-8{
 float:left;
text-align:left;
} 

并从div itslef中移除margin:auto,如果给出

,则移除其父级

答案 1 :(得分:1)

使用flexbox可能是一个好主意,它在定义时尊重文本方向,例如: <html dir="rtl" ...>

如果您希望以不同方式对齐更多项目,可以通过设置display: flexjustify-content: flex-end(或不同,例如space-betweenspace-around来实现对齐)给父母。有关更多选项,请参阅本指南:https://css-tricks.com/snippets/css/a-guide-to-flexbox/