Css左侧属性问题

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

Css Left property issue

问题

#image-list>.image-container>p中使用left属性似乎将<p>元素居中在具有id #image-list的div中,而不是在其父元素中。我不明白我做错了什么。

#image-list {
  display: flex;
  gap: 4vmin;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(0%, -50%);
}

#image-list>.image-container>.image {
  width: 40vmin;
  height: 56vmin;
  object-fit: cover;
  object-position: 100% center;
}

#image-list>.image-container>p {
  position: absolute;
  color: white;
  top: 25%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: block;
  font-family: Times New Roman;
  font-size: 2rem;
  -webkit-text-stroke: 0.5px black;
}

#image-list>.image-container {
  text-align: center;
  position: relative;
}

这是你提供的CSS代码部分的翻译。

英文:

Using the left property in #image-list&gt;.image-container&gt;p seems to center the &lt;p&gt; element in the div with id #image-list and not its parent. I do not understand what I did wrong.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

btn.onclick = e =&gt; {
  list = document.getElementById(&quot;image-list&quot;);
  item = list.children[0];
  item = item.cloneNode(true);
  document.getElementById(&quot;image-list&quot;).appendChild(item);
}

<!-- language: lang-css -->

#image-list {
  display: flex;
  gap: 4vmin;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(0%, -50%);
}

#image-list&gt;.image-container&gt;.image {
  width: 40vmin;
  height: 56vmin;
  object-fit: cover;
  object-position: 100% center;
}

#image-list&gt;.image-container&gt;p {
  position: absolute;
  color: white;
  top: 25%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: block;
  font-family: Times New Roman;
  font-size: 2rem;
  -webkit-text-stroke: 0.5px black;
}

#image-list&gt;.image-container {
  text-align: center;
  position:relative;
}

<!-- language: lang-html -->

&lt;div id=&quot;image-list&quot;&gt;
  &lt;div class=&quot;image-container&quot;&gt;
    &lt;p&gt;hello world&lt;/p&gt;
    &lt;img class=&quot;image&quot; src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/800px-Image_created_with_a_mobile_phone.png&quot; draggable=&quot;false&quot;&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;button id=&quot;btn&quot;&gt;add image&lt;/button&gt;

<!-- end snippet -->

答案1

得分: 2

为了使&lt;p&gt;元素在其父元素内居中,即.image-container,您可以在.image-container上添加position: relative,并在#image-list > .image-container > p上设置left: 50%transform: translateX(-50%)

尝试这个示例:

#image-list > .image-container {
  text-align: center;
  position: relative;
}

#image-list > .image-container > p {
  position: absolute;
  color: white;
  top: 25%;
  left: 50%;
  transform: translateX(-50%);
  display: block;
  font-family: Times New Roman;
  font-size: 2rem;
  -webkit-text-stroke: 0.5px black;
}
英文:

To center the &lt;p&gt; element within its parent, which is .image-container, you can add position: relative to .image-container and set left: 50% and transform: translateX(-50%) on #image-list &gt; .image-container &gt; p.

try this example :

#image-list&gt;.image-container {
  text-align: center;
  position: relative;
}

#image-list&gt;.image-container&gt;p {
  position: absolute;
  color: white;
  top: 25%;
  left: 50%;
  transform: translateX(-50%);
  display: block;
  font-family: Times New Roman;
  font-size: 2rem;
  -webkit-text-stroke: 0.5px black;
}

答案2

得分: 1

绝对定位是相对于最近的已定位祖先元素的边缘。

"已定位" 意味着具有非 "static" 的 position 属性。

您尚未设置 .image-containerposition 属性,因此它具有默认值,即 static

#image-list 具有 position: absolute,因此它是最近的已定位祖先,因此您的定位是相对于该元素的边缘完成的。

英文:

Absolute positioning is with respect to the edges of the closest positioned ancestor.

"positioned" means "has a position property that is not static.

You haven't set the position property of .image-container, so it has the default value, which is static.


#image-list has position: absolute so it is the closest positioned ancestor so your positioning is done with respect to that element's edges.

huangapple
  • 本文由 发表于 2023年2月16日 19:25:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471579.html
匿名

发表评论

匿名网友

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

确定