边距和填充为0时居中DIV?

时间:2018-11-01 02:21:07

标签: html css

我有一个带类,图像的div,并且我的整个身体的边距和填充设置为0。但是,当我尝试将图像div居中时,它有点偏离,并且右侧还有一些额外的空间。我该如何解决?

我试图使页边空白:0自动;但此处的图像显示在右侧稍有偏斜的位置,从而使其居中且视觉上不美观。非常感谢您的帮助。

代码:

* {
  margin: 0;
  padding: 0;
  max-width: 100%;
  overflow-x: hidden;
  font-family: Georgia, 'Times New Roman', Times, serif;
}

nav {
  width: 100%;
  text-align: center;
  font-size: 1.5em;
  padding: 1em;
  background-color: #2F2F2F;
}

nav li {
  display: inline;
}

nav li a {
  margin: 1.5em;
  color: #878BCD;
  text-decoration: none;
}

.title {
  height: 200px;
  background-color: #2F2F2F;
}

.title h1 {
  text-align: center;
  font-size: 5em;
  color: #878BCD;
  margin-top: .5em;
}

.images {
  width: 90%;
  margin: 0 auto;
}

footer {
  width: 100%;
  text-align: center;
  font-size: 1.5em;
  padding: 1em;
  margin-top: 2em;
  background-color: #2F2F2F;
}

footer li {
  display: inline;
}

footer li a {
  margin: 1.5em;
  color: #878BCD;
  text-decoration: none;
}
<!DOCTYPE html>

<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="stylesheet.css" type="text/css">
</head>

<body>
  <nav>
    <li><a href="">Home</a></li>
    <li><a href="">Shows</a></li>
    <li><a href="">Gallery</a></li>
    <li><a href="">Contact</a></li>
    <li><a href="">Resources</a></li>
  </nav>
  <div class="title">
    <h1>Gallery</h1>
  </div>
  <div class="images">
    <img src="Photos/IMG_01.JPG" alt="IMG_01" width="200" height="200">
    <img src="Photos/IMG_02.JPG" alt="IMG_02" width="200" height="200">
    <img src="Photos/IMG_03.JPG" alt="IMG_03" width="200" height="200">
    <img src="Photos/IMG_04.JPG" alt="IMG_04" width="200" height="200">
    <img src="Photos/IMG_05.JPG" alt="IMG_05" width="200" height="200">
    <img src="Photos/IMG_06.JPG" alt="IMG_06" width="200" height="200">
    <img src="Photos/IMG_07.JPG" alt="IMG_07" width="200" height="200">
    <img src="Photos/IMG_08.JPG" alt="IMG_08" width="200" height="200">
    <img src="Photos/IMG_09.JPG" alt="IMG_09" width="200" height="200">
    <img src="Photos/IMG_10.PNG" alt="IMG_10" width="200" height="200">
    <img src="Photos/IMG_11.PNG" alt="IMG_11" width="200" height="200">
    <img src="Photos/IMG_12.PNG" alt="IMG_12" width="200" height="200">
    <img src="Photos/IMG_13.PNG" alt="IMG_13" width="200" height="200">
    <img src="Photos/IMG_14.JPG" alt="IMG_14" width="200" height="200">
    <img src="Photos/IMG_15.JPG" alt="IMG_15" width="200" height="200">
    <img src="Photos/IMG_16.JPG" alt="IMG_16" width="200" height="200">
    <img src="Photos/IMG_17.JPG" alt="IMG_17" width="200" height="200">
    <img src="Photos/IMG_18.JPG" alt="IMG_18" width="200" height="200">
    <img src="Photos/IMG_19.JPG" alt="IMG_19" width="200" height="200">
    <img src="Photos/IMG_20.JPG" alt="IMG_20" width="200" height="200">
    <img src="Photos/IMG_21.JPG" alt="IMG_21" width="200" height="200">
    <img src="Photos/IMG_22.JPG" alt="IMG_22" width="200" height="200">
    <img src="Photos/IMG_23.JPG" alt="IMG_23" width="200" height="200">
    <img src="Photos/IMG_24.JPG" alt="IMG_24" width="200" height="200">
    <!-- <img src="Photos/IMG_25.MOV" alt="IMG_25" width="200" height="200"> -->
    <img src="Photos/IMG_26.PNG" alt="IMG_26" width="200" height="200">
    <img src="Photos/IMG_27.JPG" alt="IMG_27" width="200" height="200">
    <img src="Photos/IMG_28.JPG" alt="IMG_28" width="200" height="200">
    <img src="Photos/IMG_29.JPG" alt="IMG_29" width="200" height="200">
    <img src="Photos/IMG_30.JPG" alt="IMG_30" width="200" height="200">
    <img src="Photos/IMG_31.JPG" alt="IMG_31" width="200" height="200">
    <img src="Photos/IMG_32.JPG" alt="IMG_32" width="200" height="200">
    <img src="Photos/IMG_33.JPG" alt="IMG_33" width="200" height="200">
    <img src="Photos/IMG_34.JPG" alt="IMG_34" width="200" height="200">
    <img src="Photos/IMG_35.JPG" alt="IMG_35" width="200" height="200">
    <img src="Photos/IMG_36.JPG" alt="IMG_36" width="200" height="200">
    <img src="Photos/IMG_37.JPG" alt="IMG_37" width="200" height="200">
    <img src="Photos/IMG_38.JPG" alt="IMG_38" width="200" height="200">
    <img src="Photos/IMG_39.JPG" alt="IMG_39" width="200" height="200">
    <img src="Photos/IMG_40.JPG" alt="IMG_40" width="200" height="200">
    <img src="Photos/IMG_40.JPG" alt="IMG_40" width="200" height="200">
    <img src="Photos/IMG_41.JPG" alt="IMG_41" width="200" height="200">
    <img src="Photos/IMG_42.JPG" alt="IMG_42" width="200" height="200">
    <img src="Photos/IMG_43.JPG" alt="IMG_43" width="200" height="200">
    <img src="Photos/IMG_44.JPG" alt="IMG_44" width="200" height="200">
    <img src="Photos/IMG_45.JPG" alt="IMG_45" width="200" height="200">
    <img src="Photos/IMG_46.JPG" alt="IMG_46" width="200" height="200">
    <img src="Photos/IMG_47.JPG" alt="IMG_47" width="200" height="200">
    <img src="Photos/IMG_48.JPG" alt="IMG_48" width="200" height="200">
    <img src="Photos/IMG_49.JPG" alt="IMG_49" width="200" height="200">
    <img src="Photos/IMG_50.JPG" alt="IMG_50" width="200" height="200">
    <img src="Photos/IMG_51.JPG" alt="IMG_51" width="200" height="200">
    <img src="Photos/IMG_52.JPG" alt="IMG_52" width="200" height="200">
    <img src="Photos/IMG_53.JPG" alt="IMG_53" width="200" height="200">
    <img src="Photos/IMG_54.JPG" alt="IMG_54" width="200" height="200">
    <img src="Photos/IMG_55.JPG" alt="IMG_55" width="200" height="200">
    <img src="Photos/IMG_56.JPG" alt="IMG_56" width="200" height="200">
    <img src="Photos/IMG_57.JPG" alt="IMG_57" width="200" height="200">
    <img src="Photos/IMG_58.JPG" alt="IMG_58" width="200" height="200">
    <img src="Photos/IMG_59.JPG" alt="IMG_59" width="200" height="200">
    <img src="Photos/IMG_60.JPG" alt="IMG_60" width="200" height="200">
    <img src="Photos/IMG_60.JPG" alt="IMG_60" width="200" height="200">
    <img src="Photos/IMG_61.JPG" alt="IMG_61" width="200" height="200">
    <img src="Photos/IMG_62.JPG" alt="IMG_62" width="200" height="200">
    <img src="Photos/IMG_63.JPG" alt="IMG_63" width="200" height="200">
    <img src="Photos/IMG_64.JPG" alt="IMG_64" width="200" height="200">
    <img src="Photos/IMG_65.JPG" alt="IMG_65" width="200" height="200">
    <img src="Photos/IMG_66.PNG" alt="IMG_66" width="200" height="200">
    <img src="Photos/IMG_67.JPG" alt="IMG_67" width="200" height="200">
    <img src="Photos/IMG_68.JPG" alt="IMG_68" width="200" height="200">
    <img src="Photos/IMG_69.PNG" alt="IMG_69" width="200" height="200">
    <img src="Photos/IMG_70.JPG" alt="IMG_70" width="200" height="200">
    <img src="Photos/IMG_71.JPG" alt="IMG_71" width="200" height="200">
    <img src="Photos/IMG_72.JPG" alt="IMG_72" width="200" height="200">
  </div>
  <footer>
    <li><a href="">Home</a></li>
    <li><a href="">Shows</a></li>
    <li><a href="">Gallery</a></li>
    <li><a href="">Contact</a></li>
    <li><a href="">Resources</a></li>
  </footer>
</body>

</html>

2 个答案:

答案 0 :(得分:0)

您的.images元素完全集中。如果您在右边看到填充,这是由于您的图片具有width200px)的硬编码.images,所以空格位于最终图片和{{1 }}。例如,如果您的屏幕分辨率为.images1100px宽,则您将有5张200px图像彼此相邻(总计1000px)加上一个最终图像右侧的100px间距。第六张200px图像太小,无法容纳此间隙。

要解决此问题,我建议对图像使用基于百分比的相对 width以及min-width

img {
  display: block; /* Remove the gap between the images */
  width: 25%; /* If you want four side-by-side */
  min-width: 200px;
}

这是一个例子:

* {
  margin: 0;
  padding: 0;
  max-width: 100%;
  overflow-x: hidden;
  font-family: Georgia, 'Times New Roman', Times, serif;
}

nav {
  width: 100%;
  text-align: center;
  font-size: 1.5em;
  padding: 1em;
  background-color: #2F2F2F;
}

nav li {
  display: inline;
}

nav li a {
  margin: 1.5em;
  color: #878BCD;
  text-decoration: none;
}

.title {
  height: 200px;
  background-color: #2F2F2F;
}

.title h1 {
  text-align: center;
  font-size: 5em;
  color: #878BCD;
  margin-top: .5em;
}

.images {
  width: 90%;
  margin: 0 auto;
}

footer {
  width: 100%;
  text-align: center;
  font-size: 1.5em;
  padding: 1em;
  margin-top: 2em;
  background-color: #2F2F2F;
}

footer li {
  display: inline;
}

footer li a {
  margin: 1.5em;
  color: #878BCD;
  text-decoration: none;
}

img {
  display: block;
  width: 25%;
  min-width: 200px;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="stylesheet.css" type="text/css">
</head>

<body>
  <nav>
    <li><a href="">Home</a></li>
    <li><a href="">Shows</a></li>
    <li><a href="">Gallery</a></li>
    <li><a href="">Contact</a></li>
    <li><a href="">Resources</a></li>
  </nav>
  <div class="title">
    <h1>Gallery</h1>
  </div>
  <div class="images">
    <img src="http://placehold.it/200" alt="IMG_01">
    <img src="http://placehold.it/200" alt="IMG_02">
    <img src="http://placehold.it/200" alt="IMG_03">
    <img src="http://placehold.it/200" alt="IMG_04">
    <img src="http://placehold.it/200" alt="IMG_05">
  </div>
  <footer>
    <li><a href="">Home</a></li>
    <li><a href="">Shows</a></li>
    <li><a href="">Gallery</a></li>
    <li><a href="">Contact</a></li>
    <li><a href="">Resources</a></li>
  </footer>
</body>

</html>

还要注意,您在问题中遗漏了<html>标签;在我的示例代码中已解决。

答案 1 :(得分:0)

您需要修改.images类,并添加其他.images> img类以使图像在任何屏幕宽度上居中。请检查代码。

 * {
      margin: 0;
      padding: 0;
      max-width: 100%;
      overflow-x: hidden;
      font-family: Georgia, 'Times New Roman', Times, serif;
    }

    nav {
      width: 100%;
      text-align: center;
      font-size: 1.5em;
      padding: 1em;
      background-color: #2F2F2F;
    }

      nav li {
        display: inline;
      }

        nav li a {
          margin: 1.5em;
          color: #878BCD;
          text-decoration: none;
        }

    .title {
      height: 200px;
      background-color: #2F2F2F;
    }

      .title h1 {
        text-align: center;
        font-size: 5em;
        color: #878BCD;
        margin-top: .5em;
      }

    .images {
      width: 100%;
      margin: 0 12% 15% !important;
    }

    .images>img {
      width: 25%;
      
    }

    footer {
      width: 100%;
      text-align: center;
      font-size: 1.5em;
      padding: 1em;
      margin-top: 2em;
      background-color: #2F2F2F;
    }

      footer li {
        display: inline;
      }

        footer li a {
          margin: 1.5em;
          color: #878BCD;
          text-decoration: none;
        }
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="stylesheet.css" type="text/css">

</head>

<body>
  <nav>
    <li><a href="">Home</a></li>
    <li><a href="">Shows</a></li>
    <li><a href="">Gallery</a></li>
    <li><a href="">Contact</a></li>
    <li><a href="">Resources</a></li>
  </nav>
  <div class="title">
    <h1>Gallery</h1>
  </div>
  <div class="images">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_01" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_02" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_03" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_04" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_05" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_01" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_02" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_03" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_04" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_05" width="200" height="200">

    <img src="../LogoMakr_2L8GJr.png" alt="IMG_01" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_02" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_03" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_04" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_05" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_01" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_02" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_03" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_04" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_05" width="200" height="200">
    <!-- <img src="Photos/IMG_25.MOV" alt="IMG_25" width="200" height="200"> -->
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_01" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_02" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_03" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_04" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_05" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_01" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_02" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_03" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_04" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_05" width="200" height="200">

    <img src="../LogoMakr_2L8GJr.png" alt="IMG_01" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_02" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_03" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_04" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_05" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_01" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_02" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_03" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_04" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_05" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_01" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_02" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_03" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_04" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_05" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_01" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_02" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_03" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_04" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_05" width="200" height="200">

    <img src="../LogoMakr_2L8GJr.png" alt="IMG_01" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_02" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_03" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_04" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_05" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_01" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_02" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_03" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_04" width="200" height="200">
    <img src="../LogoMakr_2L8GJr.png" alt="IMG_05" width="200" height="200">
  </div>
  <footer>
    <li><a href="">Home</a></li>
    <li><a href="">Shows</a></li>
    <li><a href="">Gallery</a></li>
    <li><a href="">Contact</a></li>
    <li><a href="">Resources</a></li>
  </footer>
</body>

</html>