英文:
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>.image-container>p seems to center the <p> 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 => {
  list = document.getElementById("image-list");
  item = list.children[0];
  item = item.cloneNode(true);
  document.getElementById("image-list").appendChild(item);
}
<!-- language: lang-css -->
#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;
}
<!-- language: lang-html -->
<div id="image-list">
  <div class="image-container">
    <p>hello world</p>
    <img class="image" src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/800px-Image_created_with_a_mobile_phone.png" draggable="false">
  </div>
</div>
<button id="btn">add image</button>
<!-- end snippet -->
答案1
得分: 2
为了使<p>元素在其父元素内居中,即.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 <p> 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 > .image-container > p.
try this example :
#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;
}
答案2
得分: 1
绝对定位是相对于最近的已定位祖先元素的边缘。
"已定位" 意味着具有非 "static" 的 position 属性。
您尚未设置 .image-container 的 position 属性,因此它具有默认值,即 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论