【问题标题】:Position element according to body instead of the relative parent根据body而不是相对父元素定位元素
【发布时间】:2020-06-03 00:44:05
【问题描述】:

我有一个具有相对位置的 div 和两个使用它的孩子,并且都具有绝对位置。我需要它们。但我想在相对 div 中创建另一个孩子并给它绝对位置。但现在的问题是我希望它的某些部分出现在元素之外。那么如何在 CSS 中做到这一点呢?提前致谢。

参考我需要的是:Example of what I need, check the search icon. How it is placed. I want that kind of thing.

div {
   //Parent div
    position:relative;
}
div {
   //Child div
    position:absolute;
}
div {
   //Child div
    position:absolute;
   //This is an emoji
   //But I want this to appear half outside of the parent div,
   //i.e. on the border of the parent div
}

【问题讨论】:

标签: html css sass


【解决方案1】:

在给定的示例中,section 是父 div,它是相对的,其余 2 个子 div,或者您可以拥有任意多个,全部都是绝对的,并且图像按照您的要求在边框外

* {margin: 0; padding: 0; box-sizing: border-box;}
img {max-width: 30px;}
section {border:15px solid #ccc; border-radius: 20px; height: 100px; position: relative;}
.child {position: absolute;}
.child1 {top: 50%; left: 50%; transform: translate(-50%,-50%);}
.childEmoji {top: 0; left:0; transform: translate(-50%,-50%);}
<section>
  <div class="child child1">Child 1</div>
  <div class="child childEmoji"><img src="https://images.macrumors.com/t/45wRHEDrpfO6dWOSqibLkzcZG-s=/800x0/filters:quality(90)/article-new/2017/11/crying-tears-of-joy-emoji-250x248.jpg"></div>
</section>

【讨论】:

  • 根据需要调整左上右下属性
【解决方案2】:

在您的定位中使用 vw 或 vh。例如,如果您使用:50%,它将获得父级的宽度,然后将其除以 2。如果您使用 vw(页面宽度)或 vh(页面高度),它将获取整个页面的宽度和高度。应该为你工作。请记住,您的定位也很重要。

【讨论】:

  • 感谢您的回复。嘿,但我要求将元素定位在父 div 的边框上。我想与元素的宽度和高度没有任何关系。我想在边框内显示一半,在边框外显示一半。我在问题中添加了我需要的图像。所以,如果你能在这方面帮助我,那就太好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-15
相关资源
最近更新 更多