HTML/CSS 关闭按钮重叠右上角/上角

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33336396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 18:24:44  来源:igfitidea点击:

HTML/CSS close button overlapping right/upper corner

htmlcss

提问by cycle4passion

Looking to place a button overlying, and partially outside parent div such as in this picture. MY attempts have it within the parent DIV. The picture below is what I am after.

想要放置一个按钮覆盖在父 div 之外,例如在这张图片中。我的尝试将它放在父 DIV 中。下面的图片是我所追求的。

enter image description here

在此处输入图片说明

#container {
  width: 80%;
  border-radius: 25px;
  border: 2px solid Black;
  padding: 15px 15px 15px 15px;
  margin: 20px 20px 20px 20px;
  background: #A4D3EE;
  overflow: auto;
  box-shadow: 5px 5px 2px #888888;
  position: relative;
}

#x {
    position: relative;
    float: right;
    background: red;
 color: white;
    top: -10px;
    right: -10px;
}
<div id="container">
        <button id = "x">
            X
        </button>
    Text Text Text
    Text Text Text
    Text Text Text
</div>

回答by divy3993

I guess this is what you looking for:

我想这就是你要找的:

Just add position:absolute;instead relative

只需添加position:absolute;代替relative

#container {
  width: 80%;
  /*border-radius: 25px;*/
  border: 2px solid Black;
  padding: 15px 15px 15px 15px;
  margin: 20px 20px 20px 20px;
  background: #A4D3EE;
  overflow: visible;
  box-shadow: 5px 5px 2px #888888;
  position: relative;
}

#x {
    position: absolute;
    background: red;
    color: white;
    top: -10px;
    right: -10px;
}
<div id="container">
        <button id = "x">
            X
        </button>
    Text Text Text
    Text Text Text
    Text Text Text
</div>