如何基于第二个容器来对齐第一个容器?

huangapple go评论53阅读模式
英文:

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

&lt;div class=&quot;product-detail-container&quot;&gt;
  &lt;div class=&quot;path&quot;&gt;
    &lt;a class=&quot;path&quot; href=&quot;/index.html&quot;&gt;All products/&lt;/a&gt;
    &lt;p class=&quot;path&quot; id=&quot;path-product-title&quot;&gt;product-title&lt;/p&gt;
  &lt;/div&gt;
  &lt;div style=&quot;display: flex; margin-top: 30px; justify-content: center&quot;&gt;
    &lt;div class=&quot;photo&quot; id=&quot;product-photo&quot;&gt;
      &lt;img src=&quot;/assets/products/1.jpg&quot; class=&quot;photo&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&quot;description&quot;&gt;
      &lt;p class=&quot;product-title&quot; id=&quot;product-title&quot;&gt;product-title&lt;/p&gt;
      &lt;p class=&quot;product-id&quot; id=&quot;product-id&quot;&gt;product-id&lt;/p&gt;
      &lt;p
        class=&quot;product-price&quot;
        id=&quot;product-price&quot;
        style=&quot;margin-top: 2vh&quot;&gt;
        product-price
      &lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- 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 可以修复 pathproduct-detail 的对齐。一旦您完成这一步,您可以继续对齐 &lt;div class=&quot;product-detail-container&quot;&gt; 到您想要的位置。请参见下面的代码片段。我还在元素中添加了边框,以便更容易查看。

.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 &lt;div class=&quot;product-detail-container&quot;&gt; 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 -->

&lt;div class=&quot;product-detail-container&quot;&gt;
  &lt;div class=&quot;path&quot;&gt;
    &lt;a class=&quot;path-item&quot; href=&quot;/index.html&quot;&gt;All products/&lt;/a&gt;
    &lt;p class=&quot;path-item&quot; id=&quot;path-product-title&quot;&gt;product-title&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;product-detail&quot;&gt;
    &lt;div class=&quot;photo&quot; id=&quot;product-photo&quot;&gt;
      &lt;img src=&quot;/assets/products/1.jpg&quot; class=&quot;photo&quot; /&gt;
    &lt;/div&gt;
    &lt;div class=&quot;description&quot;&gt;
      &lt;p class=&quot;product-title&quot; id=&quot;product-title&quot;&gt;product-title&lt;/p&gt;
      &lt;p class=&quot;product-id&quot; id=&quot;product-id&quot;&gt;product-id&lt;/p&gt;
      &lt;p
        class=&quot;product-price&quot;
        id=&quot;product-price&quot;
        style=&quot;margin-top: 2vh&quot;&gt;
        product-price
      &lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 17:26:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定