gpt4 book ai didi

html - 将一个 div 居中,但将其保持在类似网格的布局中

转载 作者:太空宇宙 更新时间:2023-11-04 08:35:03 28 4
gpt4 key购买 nike

我用 9 张图片制作了一个类似网格的图案,并希望将这 9 张图片作为一个整体居中。我实现了带有边距的网格状结构。我遇到的问题是,当我执行 margin: autowidth: 100% 时,它们又变成了 block 元素,而不是 inline-block 元素和以前一样。

这是我想要居中的代码:(单击“整页”以查看类似网格的布局)

.content {
width: 100%;
}

.titletext {
margin: 0px;
padding: 0px;
}

.title {
font-family: "Fjalla One";
text-align: center;
color: #560606;
font-size: 100px;
margin: 10px 50px;
}

.subtitle {
font-family: "Fjalla One";
text-align: center;
color: #BF1C1C;
font-size: 40px;
margin: 5px 50px
}

.leftcorner {
width: 180px;
height: 55px;
position: relative;
left: 10px;
font-family: lato;
font-size: 20px;
color: #A81C1C;
top: -10px;
}

.title,
.subtitle {
position: relative;
top: -80px;
}

.rightcorner {
width: 400px;
height: 55px;
position: absolute;
right: 10px;
font-family: lato;
font-size: 20px;
color: #A81C1C;
top: 355px;
float: right;
text-align: right;
}

.products {
position: absolute;
}

.images {
width: 225px;
height: 225px;
margin: 0px 150px;
border: 13px solid #390404;
border-radius: 20px;
}

.strawberrycream,
.vanilladelight,
.greenlight,
.originalchoc,
.unicorn,
.americafresh,
.thegoat,
.buttermilk,
.oreo {
display: inline-block;
text-align: center;
}

.greenlight,
.americafresh,
.oreo {
margin-right: 0px;
}

.strawberrycream,
.thegoat,
.originalchoc {
margin-left: 0px;
}

.subtext {
font-size: 30px;
font-family: "Lato";
color: #560606;
margin-bottom: -15px;
margin-top: 5px;
}

.price {
font-size: 20px;
font-family: "Lato";
color: #900B0B;
margin-bottom: 80px;
}
<html>

<head>
<title>Cupcakes</title>
<link href="https://fonts.googleapis.com/css?family=Fjalla+One|Gloria+Hallelujah|Lato" rel="stylesheet">
</head>

<body>
<div class="content">
<div class="titletext">
<p class="leftcorner">Great for home snacks and parties!</p>
<p class="rightcorner">*All prices listed in dozens.</p>
<h1 class="title">Cupcakes</h1>
<h2 class="subtitle">Our great selection of hand-crafted cupcakes!</h2>
</div>
<div class="products">
<div class="strawberrycream">
<img class="images" src="Pictures/strawberrycream.jpg">
<p class="subtext">Strawberry Cream</p>
<p class="price">$24.99</p>
</div>
<div class="vanilladelight">
<img class="images" src="Pictures/vanilladelight.jpeg">
<p class="subtext">Vanilla Delight</p>
<p class="price">$16.99</p>
</div>
<div class="greenlight">
<img class="images" src="Pictures/greenlight.jpg">
<p class="subtext">Green Light</p>
<p class="price">$27.99</p>
</div>
<div class="originalchoc">
<img class="images" src="Pictures/OriginalChoc.jpg">
<p class="subtext">Original Choc'</p>
<p class="price">$16.99</p>
</div>
<div class="unicorn">
<img class="images" src="Pictures/unicorn.jpg">
<p class="subtext">Unicorn</p>
<p class="price">$27.99</p>
</div>
<div class="americafresh">
<img class="images" src="Pictures/americafresh.jpg">
<p class="subtext">America Fresh</p>
<p class="price">$24.99</p>
</div>
<div class="thegoat">
<img class="images" src="Pictures/TheGOAT.jpg">
<p class="subtext">The G.O.A.T.</p>
<p class="price">$29.99</p>
</div>
<div class="buttermilk">
<img class="images" src="Pictures/Buttermilk.jpg">
<p class="subtext">Buttermilk</p>
<p class="price">$19.99</p>
</div>
<div class="oreo">
<img class="images" src="Pictures/oreo.jpg">
<p class="subtext">Oreo</p>
<p class="price">$26.99</p>
</div>
</div>
</div>
</body>

</html>

谢谢。

最佳答案

这是您要找的吗?您可以添加媒体查询以在屏幕达到特定大小时更改框的宽度。

.content {
width: 100%;
}

.titletext {
margin: 0px;
padding: 0px;
display:block;
}

.title {
font-family: "Fjalla One";
text-align: center;
color: #560606;
font-size: 100px;
margin: 10px 50px;
}

.subtitle {
font-family: "Fjalla One";
text-align: center;
color: #BF1C1C;
font-size: 40px;
margin: 5px 50px
}

.leftcorner {
width: 180px;
height: 55px;
position: relative;
left: 10px;
font-family: lato;
font-size: 20px;
color: #A81C1C;
top: -10px;
}

.title,
.subtitle {
position: relative;
top: -80px;
}

.rightcorner {
width: 400px;
height: 55px;
position: absolute;
right: 10px;
font-family: lato;
font-size: 20px;
color: #A81C1C;
top: 200px;
text-align:right;
}

.products {
position: absolute;
}

.images {
width: 225px;
height: 225px;
border: 13px solid #390404;
border-radius: 20px;
}

.strawberrycream,
.vanilladelight,
.greenlight,
.originalchoc,
.unicorn,
.americafresh,
.thegoat,
.buttermilk,
.oreo {
display: inline-block;
text-align: center;
}

.greenlight,
.americafresh,
.oreo {
margin-right: 0px;
}

.strawberrycream,
.thegoat,
.originalchoc {
margin-left: 0px;
}

.subtext {
font-size: 30px;
font-family: "Lato";
color: #560606;
margin-bottom: -15px;
margin-top: 5px;
}

.price {
font-size: 20px;
font-family: "Lato";
color: #900B0B;
margin-bottom: 80px;
}
.box{
width:33%;
text-align:center;
}
<html>

<head>
<title>Cupcakes</title>
<link href="https://fonts.googleapis.com/css?family=Fjalla+One|Gloria+Hallelujah|Lato" rel="stylesheet">
</head>

<body>
<div class="content">
<div class="titletext">
<p class="leftcorner">Great for home snacks and parties!</p>
<p class="rightcorner">*All prices listed in dozens.</p>
<h1 class="title">Cupcakes</h1>
<h2 class="subtitle">Our great selection of hand-crafted cupcakes!</h2>
</div>
<div class="products">
<div class="strawberrycream box">
<img class="images" src="Pictures/strawberrycream.jpg">
<p class="subtext">Strawberry Cream</p>
<p class="price">$24.99</p>
</div>
<div class="vanilladelight box">
<img class="images" src="Pictures/vanilladelight.jpeg">
<p class="subtext">Vanilla Delight</p>
<p class="price">$16.99</p>
</div>
<div class="greenlight box">
<img class="images" src="Pictures/greenlight.jpg">
<p class="subtext">Green Light</p>
<p class="price">$27.99</p>
</div>
<div class="originalchoc box">
<img class="images" src="Pictures/OriginalChoc.jpg">
<p class="subtext">Original Choc'</p>
<p class="price">$16.99</p>
</div>
<div class="unicorn box">
<img class="images" src="Pictures/unicorn.jpg">
<p class="subtext">Unicorn</p>
<p class="price">$27.99</p>
</div>
<div class="americafresh box">
<img class="images" src="Pictures/americafresh.jpg">
<p class="subtext">America Fresh</p>
<p class="price">$24.99</p>
</div>
<div class="thegoat box">
<img class="images" src="Pictures/TheGOAT.jpg">
<p class="subtext">The G.O.A.T.</p>
<p class="price">$29.99</p>
</div>
<div class="buttermilk box">
<img class="images" src="Pictures/Buttermilk.jpg">
<p class="subtext">Buttermilk</p>
<p class="price">$19.99</p>
</div>
<div class="oreo box">
<img class="images" src="Pictures/oreo.jpg">
<p class="subtext">Oreo</p>
<p class="price">$26.99</p>
</div>
</div>
</div>
</body>

</html>

关于html - 将一个 div 居中,但将其保持在类似网格的布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44527236/

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