英文:
How to align the first container based on second container?
问题
我想要使路径"所有产品/巧克力"与其下方的照片对齐,但我需要保持所有元素在.product-detail-container
内居中。如何实现?
您可以通过以下方式实现所需的布局:
.product-detail-container {
display: flex;
flex-direction: column;
max-width: 1200px;
align-self: center;
justify-content: center;
margin: 2.5rem 5%;
}
.product-detail-container .path {
font-size: 1.1rem;
font-family: var(--fontCommon);
display: flex;
color: black;
align-items: center; /* 添加此行以垂直居中路径文本 */
}
.product-detail-container .path a:hover {
text-decoration: underline;
color: black;
}
.product-detail-container .photo {
width: 45%;
margin: 0; /* 添加此行以移除默认的外边距 */
}
.product-detail-container .photo img {
width: 100%;
border: 1px black solid;
}
.description {
display: flex;
margin-left: 2rem;
flex-direction: column;
}
这将使路径文本与下方的照片水平对齐,并保持所有元素在.product-detail-container
内居中。
英文:
Below is my code in html and CSS. Current result and my desired result with red.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.product-detail-container {
display: flex;
flex-direction: column;
/* width: 97%; */
max-width: 1200px;
align-self: center;
justify-content: center;
margin: 2.5rem 5%;
}
.product-detail-container .path {
font-size: 1.1rem;
font-family: var(--fontCommon);
display: flex;
color: black;
}
.product-detail-container .path a:hover {
text-decoration: underline;
color: black;
}
.product-detail-container .photo {
width: 45%;
}
.product-detail-container .photo img {
width: 100%;
border: 1px black solid;
}
.description {
display: flex;
margin-left: 2rem;
flex-direction: column;
}
<!-- language: lang-html -->
<div class="product-detail-container">
<div class="path">
<a class="path" href="/index.html">All products/</a>
<p class="path" id="path-product-title">product-title</p>
</div>
<div style="display: flex; margin-top: 30px; justify-content: center">
<div class="photo" id="product-photo">
<img src="/assets/products/1.jpg" class="photo" />
</div>
<div class="description">
<p class="product-title" id="product-title">product-title</p>
<p class="product-id" id="product-id">product-id</p>
<p
class="product-price"
id="product-price"
style="margin-top: 2vh">
product-price
</p>
</div>
</div>
</div>
<!-- end snippet -->
I want to make the path which is "All products/chocolate" align with the photo below it but I need to keep the all element inside .product-detail-container CENTER. How to achieve?
答案1
得分: 1
移除内联样式中的 justify-content: center
可以修复 path
和 product-detail
的对齐。一旦您完成这一步,您可以继续对齐 <div class="product-detail-container">
到您想要的位置。请参见下面的代码片段。我还在元素中添加了边框,以便更容易查看。
.product-detail-container {
display: flex;
flex-direction: column;
/* width: 97%; */
max-width: 1200px;
align-self: center;
/* justify-content: center; */
margin: 2.5rem 5%;
border: 1px solid black;
}
.product-detail-container .path {
font-size: 1.1rem;
font-family: var(--fontCommon);
display: flex;
color: black;
border: 1px solid green;
}
/* 为了修复垂直对齐 */
.product-detail-container .path-item {
margin: 5px 0;
}
.product-detail-container .path-item a:hover {
text-decoration: underline;
color: black;
}
/* 将内联样式移到这个类中 */
.product-detail {
display: flex;
margin-top: 30px;
border: 1px solid red;
/* justify-content: center; */
}
.product-detail-container .photo {
border: 1px solid cyan;
width: 45%;
}
.product-detail-container .photo img {
width: 100%;
border: 1px black solid;
}
.description {
display: flex;
margin-left: 2rem;
flex-direction: column;
flex-grow: 1; /* 添加了这个 */
border: 1px solid blue;
}
<div class="product-detail-container">
<div class="path">
<a class="path-item" href="/index.html">All products/</a>
<p class="path-item" id="path-product-title">product-title</p>
</div>
<div class="product-detail">
<div class="photo" id="product-photo">
<img src="/assets/products/1.jpg" class="photo" />
</div>
<div class="description">
<p class="product-title" id="product-title">product-title</p>
<p class="product-id" id="product-id">product-id</p>
<p
class="product-price"
id="product-price"
style="margin-top: 2vh"
>
product-price
</p>
</div>
</div>
</div>
希望这有所帮助!
英文:
Removing justify-content: center
from the inline style fixes the path
and product-detail
alignment. Once you have that, you can work on aligning <div class="product-detail-container">
where you want. See the snippet bellow. I also added borders in the elements to make it easier to see.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.product-detail-container {
display: flex;
flex-direction: column;
/* width: 97%; */
max-width: 1200px;
align-self: center;
justify-content: center;
margin: 2.5rem 5%;
border: 1px solid black;
}
.product-detail-container .path {
font-size: 1.1rem;
font-family: var(--fontCommon);
display: flex;
color: black;
border: 1px solid green;
}
/* to fix vertical alignment */
.product-detail-container .path-item {
margin: 5px 0;
}
.product-detail-container .path-item a:hover {
text-decoration: underline;
color: black;
}
/* moved inline style to this class */
.product-detail {
display: flex;
margin-top: 30px;
border: 1px solid red;
/* justify-content: center */
}
.product-detail-container .photo {
border: 1px solid cyan;
width: 45%;
}
.product-detail-container .photo img {
width: 100%;
border: 1px black solid;
}
.description {
display: flex;
margin-left: 2rem;
flex-direction: column;
flex-grow: 1; /* added this */
border: 1px solid blue;
}
<!-- language: lang-html -->
<div class="product-detail-container">
<div class="path">
<a class="path-item" href="/index.html">All products/</a>
<p class="path-item" id="path-product-title">product-title</p>
</div>
<div class="product-detail">
<div class="photo" id="product-photo">
<img src="/assets/products/1.jpg" class="photo" />
</div>
<div class="description">
<p class="product-title" id="product-title">product-title</p>
<p class="product-id" id="product-id">product-id</p>
<p
class="product-price"
id="product-price"
style="margin-top: 2vh">
product-price
</p>
</div>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论