我似乎无法弄清楚如何在每个设备上将此div居中

时间:2019-11-04 18:27:10

标签: html css

我是一名css业余爱好者,我一直在尝试将这些div集中在每个设备的页面中间,是的,我已经在互联网上进行了搜索,并尝试了无结果的解决方案居中。

这是我的代码:

摘要:

:after,
:before {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

h3 {
    font-family: inherit;
    font-weight: 500;
    line-height: 1.1;
    color: inherit;
}

h3 {
    margin-top: 20px;
    margin-bottom: 10px;
}

h3 {
    font-size: 24px;
}

p {
    margin: 0 0 10px;
}

.djfl-box-col-2,
.djfl-box-col {
    position: relative;
    min-height: 1px;
    padding-right: 15px;
    padding-left: 15px;
}

.djfl-box-col {
    float: left;
}

.djfl-box-col {
    width: 50%;
}

h3 {
    font-family: 'Source Sans Pro', sans-serif;
}

.djfl-box {
    border-radius: 2px;
    position: relative;
    display: block;
    margin-bottom: 20px;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
}

.djfl-box>.djfl-box-inner {
    padding: 10px;
}

.djfl-box>.djfl-box-header {
    position: relative;
    text-align: center;
    padding: 3px 0;
    color: #fff;
    color: rgba(255, 255, 255, 0.8);
    display: block;
    z-index: 10;
    background: rgba(0, 0, 0, 0.1);
    text-decoration: none;
}

.djfl-box h3 {
    font-size: 38px;
    font-weight: bold;
    margin: 0 0 10px 0;
    white-space: nowrap;
    padding: 0;
}

.djfl-box h3,
.djfl-box p {
    z-index: 5;
}

.djfl-box:hover {
    text-decoration: none;
    color: #f9f9f9;
}

.djfl-box-col {
    padding: 0px 85px;
}

.djfl-box {
    text-align: center;
}

.djfl-box p {
    font-size: 12px;
}

.bg-green {
    color: #fff !important;
}

.bg-green {
    background-color: #00a65a !important;
}
<div class="djfl-box-col-2 djfl-box-col">
  <div class="djfl-box bg-green">
      <div class="djfl-box-header">Trees Planted</div>
      <div class="djfl-box-inner">
          <h3>12,690,517</h3>
          <p>Trees</p>
      </div>
  </div>
</div>

感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

删除类“ djfl-box-col”中的左浮点,并添加自动边距:

p = head
:after,
:before {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

h3 {
    font-family: inherit;
    font-weight: 500;
    line-height: 1.1;
    color: inherit;
}

h3 {
    margin-top: 20px;
    margin-bottom: 10px;
}

h3 {
    font-size: 24px;
}

p {
    margin: 0 0 10px;
}

.djfl-box-col-2,
.djfl-box-col {
    position: relative;
    min-height: 1px;
    padding-right: 15px;
    padding-left: 15px;
}

.djfl-box-col {
    margin: 0 auto;
}

.djfl-box-col {
    width: 50%;
}

h3 {
    font-family: 'Source Sans Pro', sans-serif;
}

.djfl-box {
    border-radius: 2px;
    position: relative;
    display: block;
    margin-bottom: 20px;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
}

.djfl-box>.djfl-box-inner {
    padding: 10px;
}

.djfl-box>.djfl-box-header {
    position: relative;
    text-align: center;
    padding: 3px 0;
    color: #fff;
    color: rgba(255, 255, 255, 0.8);
    display: block;
    z-index: 10;
    background: rgba(0, 0, 0, 0.1);
    text-decoration: none;
}

.djfl-box h3 {
    font-size: 38px;
    font-weight: bold;
    margin: 0 0 10px 0;
    white-space: nowrap;
    padding: 0;
}

.djfl-box h3,
.djfl-box p {
    z-index: 5;
}

.djfl-box:hover {
    text-decoration: none;
    color: #f9f9f9;
}

.djfl-box-col {
    padding: 0px 85px;
}

.djfl-box {
    text-align: center;
}

.djfl-box p {
    font-size: 12px;
}

.bg-green {
    color: #fff !important;
}

.bg-green {
    background-color: #00a65a !important;
}

  

文档:https://www.w3schools.com/css/css_align.asp

答案 1 :(得分:1)

尝试摆脱所有填充,然后将所有内容包装在外部div中。您也可以删除所有头寸属性。然后为外部div设置任意宽度(以百分比为单位)和margin自动属性。

.outer-wrapper{
      width: 50%;
      margin: auto;
   }

您有很多未使用的样式。 I cleaned them up in this code snippet。希望能帮助到你。