gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 00:53:34 25 4
gpt4 key购买 nike

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

我试图让 margin: 0 auto;但是那里的图像显示略微偏右,使它​​们不居中并且在视觉上没有吸引力。感谢您的帮助,非常感谢。

代码:

* {
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>

最佳答案

你的 .images元素是完全集中的。如果您在右侧看到填充,那是因为您的图像具有硬编码的 width。 (200px ),所以空间在最终图像和 .images 的边缘之间.例如,如果您的屏幕分辨率为 .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>标记你的问题;这在我的示例代码中已修复。

关于html - 当边距和填充为 0 时居中 DIV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53094406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com