代码之家  ›  专栏  ›  技术社区  ›  MK-DK

使用媒体查询时两边的填充相同

  •  1
  • MK-DK  · 技术社区  · 7 年前

    当我将浏览器窗口的大小调整到768像素以下时,这些框在列框的两侧会得到不同的填充: See the example here 是的。

    我试图用这样的方法设置媒体查询,但我知道这根本不是正确的方法:

    @media only screen and (max-width: 768px) {
        .small-padding-left {
                padding-left: 7.5px;
            }
    
        .col-sm-8 {
            padding-right: 7.5px;
        }
    
    }
    

    Example picture

    有人知道我怎样才能做到这一点吗?到目前为止这是我的代码:

    /* Global CSS*/
        .row > div{
            margin-bottom: 15px;
        }
        .content{
            background: #b4bac0;
            min-height: 300px;
        }
        /* When there is 2 columns on top of eachother */
        .sidebar{
            background: #b4bac0;
            min-height: 300px;
        }
        .sidebar-top{
            min-height: 140px;
            background: #dbdfe7;
        }
        .sidebar-bottom{
            min-height: 145px;
            background: #7e8aa0;
            margin-bottom: -15px;
        }
        /* Padding used for the core CSS */
        .small-padding.top {
           padding-top:5px;
        }
    
        .small-padding.bottom {
            padding-bottom:5px;
        } 
        .small-padding.left {
            padding-left:7.5px;
        }
    
        .small-padding.right {
            padding-right:7.5px;
        }
        /* Media quries */
        @media only screen and (max-width: 768px) {
            .small-padding-left {
                    padding-left: 7.5px;
                }
    
            .col-sm-8 {
                padding-right: 7.5px;
            }
    
        }
    
    <body style="background-color: #4940fb;">
        <div class="container">
            <div class="row">
                <div class="col-sm-4 small-padding right">
                    <div class="content"></div>
                </div>
                <div class="col-sm-8 small-padding left">
                    <div class="content"></div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-7 small-padding right">
                    <div class="content"></div>
                </div>
                <div class="col-sm-5">
                    <div class="row">
                        <div class="col-sm-12 small-padding left">
                            <div class="sidebar-top"></div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-12 small-padding left">
                            <div class="sidebar-bottom"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Aryan Twanju    7 年前

    您需要添加一些自定义css以使其以正确的方式工作。尝试将此代码添加到css文件中。

    .row {
      margin-left: -7.5px;
      margin-right: -7.5px;
    }
    
    .row>[class*="col-"] {
      padding-left: 7.5px;
      padding-right: 7.5px;
    }
    
        2
  •  0
  •   Alfin Paul    7 年前

    在您的示例中,问题发生在575宽度以下。

    请更新css文件中的媒体查询

    @media only screen and (max-width: 575px) {
    .small-padding.right
    {
    padding-right: 15px;
    }
    .small-padding.left
    {
    padding-left: 15px;
    
    }
    }