html - 如何在一个div中居中一个div? ( margin : 0 auto; not working)

标签 html css

我有一个要用作按钮的 div,但它不想在较大的 div 中居中!

这是我为较大的 div 设计的 CSS:

.news-quarters{
    width:189px;
    height:300px;
    position:relative;
    float:left;
    padding:10px;
    padding-top:0px;
    margin:10px;
    text-align:left;

}

对于按钮 div:

.green-button{
    width:auto;
    height:auto;
    position: relative;
    float:none;
    padding:0px;
    margin: 0 auto;
    background: #0A9C00;
    background-size:auto;
    border:1px outset #0A9C00;
    border-radius: 3px;
    -webkit-box-shadow: 0px 0px 9px rgba(0,0,0,0.15);
    -moz-box-shadow: 0px 0px 9px rgba(0,0,0,0.15);
    box-shadow: 0px 0px 9px rgba(0,0,0,0.15);
    overflow:auto;
    display:inline-block;

}

.green-button h4{
    text-align:center;
    color:white;
    line-height:1;
    margin:0px;
    font-size:12px;
    padding-bottom: 5px;
    padding-left: 10px;
    padding-right: 10px;
    padding-top: 5px;    
    width:auto;
}

我的 html 是这样的:

<div class="news-quarters">
     <div class="green-button">
         <h4>Insert button text here</h4>
     </div>
</div>

有人可以帮忙吗?这让我很生气 :(

谢谢!

最佳答案

在 .news-quarters 样式中将 text-align:left 更改为 text-align:center;

这个问题已经有很多现成的答案见How to horizontally center a <div> in another <div>?

关于html - 如何在一个div中居中一个div? ( margin : 0 auto; not working),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26153725/

相关文章:

asp.net - 分页后应用 css media=print 不使用母版页

html - 变换 : scale offsets element

javascript - 如何使用 Css 或 javascript 创建圆 Angular

html - 当高度和宽度是百分比时,如何用CSS绘制半圆?

CSS3 显示 :table, 溢出-y:滚动不起作用

android - 如何使用 HTML 标签在 Android 的 EditText 中格式化文本

html - 内联选择 selectedIndex

node.js - 为什么我的下载链接需要控制点击才能下载?

css - 覆盖 vuetify 对话框的溢出属性

css - 这个媒体查询到底做了什么