【问题标题】:Center child divs with tables inside a parent div中心子 div 与父 div 内的表
【发布时间】:2021-07-25 02:13:58
【问题描述】:

嗨,这是我的代码 sn-p 我正在尝试将父容器下方的容器居中对齐,但我的 css 没有按预期工作。谁能帮助我的表格为什么没有居中对齐。我已经通过堆栈溢出的各种解决方案,但似乎没有任何工作。 在 sn-p 中,我将表数显示为 3,但也可以是最多 8 个。它是动态的。

 <style>
         .parent_container {

            width: 100%;
            text-align: center;
        }
        .table_container {
            float: left;
            width: 30rem;
            margin-bottom: 3rem;
        }

        .table_container2 {
            float: left;
            width: 30rem;
            margin-bottom: 3rem;
        }

        .container {
            width: 100%;
            margin-top: 30px;
            display: inline-block
        }

        .container::after {
            content: "";
            clear: both;
            display: table;
        }

        table {
            margin: 2rem auto;
            border-radius: 10px;
        }

        tr {
            padding: 15px;
            text-align: center;
        }

        td {
            color: black;
            text-align: center;
            vertical-align: middle;
            height: 60px;
            background-color: #e1edf9;
            width: 272px;
            border-top: 1px solid white;
        }

        td: first-of-type {
            border-top: none;
        }

        .sub_text {
            font-size: 12px;
            font-style: italic;
            color: #0071ce;
            font-weight: 100;
        }
        .wrapper {
            text-align: center;
            margin-top: 20px;
        }
        @media screen and (min-width: 1400px) {
            .table_container {
                float: left;
                width: 15rem;
            }

            .table_container:first-of-type {
                width: 30rem;
            }
        }

        @media screen and (min-width: 1400px) {
            .table_container2 {
                float: left;
                width: 15rem;
            }

            .table_container2:first-of-type {
                width: 30rem;
            }
        }

        @media only screen and (min-width: 1000) and (max-device-width: 1400px) {
            .table_container2 {
                float: left;
                width: 14rem;
            }

            .table_container2:first-of-type {
                width: 27rem;
            }
        }

        @media only screen and (min-width: 1000) and (max-device-width: 1400px) {
            .table_container {
                float: left;
                width: 14rem;
            }

            .table_container:first-of-type {
                width: 27rem;
            }
        }

    </style>
<div class="parent_container">
    <div class="wrapper">
        <a type="button" id="modalButton" class="btn btn-primary" data-target="#myModal" onclick="showmodal ();">
            Compare
        </a>
    </div>
    <div class="container">
        <div class="table_container">
            <table>
                <tr>
                    <td></td>
                </tr>
            </table>
        </div>
        <div class="table_container">
            <table>
                <tr>
                    <td></td>
                </tr>
            </table>
        </div>
        <div class="table_container">
            <table>
                <tr>
                    <td></td>
                </tr>
            </table>
        </div>
    </div>
    <div class="container2">
        <div class="table_container2">
            <table>
                <tr>
                    <td></td>
                </tr>
            </table>
        </div>
        <div class="table_container2">
            <table>
                <tr>
                    <td></td>
                </tr>
            </table>
        </div>
        <div class="table_container2">
            <table>
                <tr>
                    <td></td>
                </tr>
            </table>
        </div>
    </div>
</div>

【问题讨论】:

    标签: html css alignment media-queries center


    【解决方案1】:

    尝试添加

    display: flex;
    justify-content: center;
    

    到你的父容器

    .parent_container {
      width: 100%;
      text-align: center;
    }
    
    .table_container {
      float: left;
      width: 30rem;
      margin-bottom: 3rem;
    }
    
    .table_container2 {
      float: left;
      width: 30rem;
      margin-bottom: 3rem;
    }
    
    .container {
      width: 100%;
      margin-top: 30px;
      display: flex;
      justify-content: center;
    }
    
    .container::after {
      content: "";
      clear: both;
      display: table;
    }
    
    table {
      margin: 2rem auto;
      border-radius: 10px;
    }
    
    tr {
      padding: 15px;
      text-align: center;
    }
    
    td {
      color: black;
      text-align: center;
      vertical-align: middle;
      height: 60px;
      background-color: #e1edf9;
      width: 272px;
      border-top: 1px solid white;
    }
    
    td: first-of-type {
      border-top: none;
    }
    
    .sub_text {
      font-size: 12px;
      font-style: italic;
      color: #0071ce;
      font-weight: 100;
    }
    
    .wrapper {
      text-align: center;
      margin-top: 20px;
    }
    
    @media screen and (min-width: 1400px) {
      .table_container {
        float: left;
        width: 15rem;
      }
      .table_container:first-of-type {
        width: 30rem;
      }
    }
    
    @media screen and (min-width: 1400px) {
      .table_container2 {
        float: left;
        width: 15rem;
      }
      .table_container2:first-of-type {
        width: 30rem;
      }
    }
    
    @media only screen and (min-width: 1000) and (max-device-width: 1400px) {
      .table_container2 {
        float: left;
        width: 14rem;
      }
      .table_container2:first-of-type {
        width: 27rem;
      }
    }
    
    @media only screen and (min-width: 1000) and (max-device-width: 1400px) {
      .table_container {
        float: left;
        width: 14rem;
      }
      .table_container:first-of-type {
        width: 27rem;
      }
    }
    <div class="parent_container">
      <div class="wrapper">
        <a type="button" id="modalButton" class="btn btn-primary" data-target="#myModal" onclick="showmodal ();">
                Compare
            </a>
      </div>
      <div class="container">
        <div class="table_container">
          <table>
            <tr>
              <td></td>
            </tr>
          </table>
        </div>
        <div class="table_container">
          <table>
            <tr>
              <td></td>
            </tr>
          </table>
        </div>
        <div class="table_container">
          <table>
            <tr>
              <td></td>
            </tr>
          </table>
        </div>
      </div>
      <div class="container container2">
        <div class="table_container2">
          <table>
            <tr>
              <td></td>
            </tr>
          </table>
        </div>
        <div class="table_container2">
          <table>
            <tr>
              <td></td>
            </tr>
          </table>
        </div>
        <div class="table_container2">
          <table>
            <tr>
              <td></td>
            </tr>
          </table>
        </div>
      </div>
    </div>

    【讨论】:

    • 试过了,当我们有很多桌子时它似乎不起作用
    • @ShilpiDhiman 在上面有很多表的演示中不起作用吗?
    • 对不起,我再次尝试,它的工作谢谢
    • @ShilpiDhiman 如果你没有注意到我已经添加了&lt;div class="container container2"&gt; 以使第二个容器具有相同的样式
    猜你喜欢
    • 1970-01-01
    • 2020-09-12
    • 2018-05-02
    • 2016-08-31
    • 2017-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多