简体   繁体   English

父div中的css html居中div

[英]css html centering div within parent div

Why won't the green button center within the red parent div? 为什么绿色按钮不在红色父级div的中心?

CSS CSS

.button_green {
    background-image: url('../Images/btn_bg.jpg');
    width: 202px;
    height: 30px;
    text-align: center;
    text-transform: capitalize;
    color: white;
    text-shadow: 2px 2px #000;
    font-family: Arial;
    vertical-align: text-bottom;
    display: inline-block;
    margin: 0 auto;
   }

#col2_rightbuttons span { 
    display:inline-block;
    vertical-align:middle;
    margin:5 auto 0 auto;
}

HTML: HTML:

<div id="col2_rightbuttons" style="background-color:red;">
   <div class="button_green">
        <span>test2</span>
    </div>
</div>

在此处输入图片说明

Try display: block instead of inline-block . 尝试使用display: block而不是inline-block Plus, no need for span inside div - just combine the two and use a single element, preferably the more semantically correct button . 另外,不需要在div内跨度-只需将两者结合并使用单个元素,最好是语义上更正确的button

The shorthand declaration of margin takes 4 arguments moving from the top and going around clockwise. margin的简写声明有4个自上而下顺时针旋转的参数。 Use 采用

margin: 0 auto 0 auto;

this is the same as 这和

margin-top: 0;
margin-right: auto;
margin-bottom: 0;
margin-left: auto;

which should centre your green button. 这应该使您的绿色按钮居中。

Using CSS3 (popular animation platform http://famo.us uses CSS3) 使用CSS3(流行的动画平台http://famo.us使用CSS3)

#div {
   position: absolute
   left: 50%
   top: 50%
   transform: translate(-50%, -50%)
}

translate uses the child's own sizing translate使用孩子自己的尺寸

Remember to add prefixes -webkit, -moz, -o, etc. and remember that any other transform property will overwrite existing transform property 请记住添加前缀-webkit,-moz,-o等,并记住任何其他transform属性都将覆盖现有的transform属性。

Use JS to query select / concatenate an inline-css string, or you can just use a seperate container "shell" for other positional changes 使用JS查询选择/连接inline-css字符串,或者您可以仅使用单独的容器“外壳”进行其他位置更改

您可能想尝试margin-left: automargin-right: auto ,但我不确定您是否需要display: inline-block作为父div,它可以只是display: block

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM