简体   繁体   English

相对于div右侧的CSS位置图片

[英]CSS position image relative to right hand side of div

There is a <div></div> in my Angular project, that's displaying a dialog when the user click an 'Edit' button to edit field in a table. 我的Angular项目中有一个<div></div> ,当用户单击“编辑”按钮以编辑表中的字段时,该对话框显示一个对话框。

I have just added a 'close' button to the dialog, but am having some trouble positioning that button correctly... 我刚刚在对话框中添加了“关闭”按钮,但是在正确放置该按钮时遇到了一些麻烦...

The <div></div> is defined with: <div></div>定义为:

<div class="provContactSelector" *ngIf="payer.showProvContactSelector">
    ...
    <button class= "icon icon-close-selected" ...></button>
    ...
</div>

In the .scss file, I've added the block for this <div> , and added some styling to the icon: 在.scss文件中,我为此<div>添加了代码块,并为图标添加了一些样式:

.provContactSelector {
    .icon {
        appearance: none;
        background-color: transparent;
        position: relative;
        border: none;
        right: 50px;
    }
}

I want the close button to be displayed just slightly in from the right hand side of the dialog, but as it stands, it's currently displayed just over half way across the width of the box, and so is displayed on top of the dialog title. 我希望关闭按钮仅在对话框的右侧显示,但就目前而言,它当前显示在框宽度的一半以上,因此显示在对话框标题的顶部。

If I change the positioning to right: 5px; 如果我将位置更改为right: 5px; , recompile the CSS, and view the page in the browser again, I can see that the close icon has moved further to the right, but is now just right at the end of the dialog title, and there is still a lot more space to its right, before the edge of the dialog... ,重新编译CSS,然后再次在浏览器中查看页面,我可以看到关闭图标已经向右移动了很多,但现在恰好位于对话框标题的末尾,还有更多空间可以它的右边,在对话框边缘之前...

How can I anchor the close icon to the right hand side of the dialog, so that it's always displayed relative to where that is? 如何将关闭图标锚定在对话框的右侧,以使其始终相对于其所在位置显示?

you defined the icon as position: relative. 您将图标定义为位置:相对。 For what you descrived, I understood that you want to position the icon in absolute way, taking provContactSelector as the reference. 对于您所描述的内容,我了解到您想以provContactSelector作为参考以绝对的方式放置图标。 In this case you should change the css to the following: 在这种情况下,您应该将css更改为以下内容:

.provContactSelector {
    position: relative;

    .icon {
        appearance: none;
        background-color: transparent;
        position: absolute;
        border: none;
        right: 50px;
        top: 50px; // or whatever the value you need
    }
}

Explanation: 说明:

position css instruction can be a bit tricky, and I have a lot of people having some confusion with how it works. 位置css指令可能有点棘手,而且我有很多人对其工作原理有些困惑。 So I will try to briefly explain what is happening: 因此,我将尝试简要解释正在发生的事情:

  • position: static is the default value of a normal html block, and it positions itself depending of the other blocks that are around it. position:static是普通html块的默认值,它根据周围的其他块来定位自己。 css like "top, left, right, bottom, z-index" won't work on them. 像“上,左,右,下,z-index”这样的css对它们不起作用。

  • position: fixed an element defined as fixed will ignore all the blocks defined in the page and will position itself using the windows element (the whole document) as reference. 位置:固定定义为固定的元素将忽略页面中定义的所有块,并使用windows元素(整个文档)作为参考来定位自身。 you can position it using css like "top, left, right, bottom". 您可以使用诸如“上,左,右,下”之类的CSS进行定位。 You can define if other elements are on top of it or under it using "z-index". 您可以使用“ z-index”定义其他元素在其上方还是下方。

  • position: absolute an element defined as absolute will ignore all the blocks defined in the page and will position itself using its nearest parent that IS NOT position: static as a reference. position:absolute定义为absolute的元素将忽略页面中定义的所有块,并将使用其最近的父对象(NOT NOT)将自身定位:position作为参考。 You can position it using css like "top, left, right, bottom". 您可以使用诸如“上,左,右,下”之类的css对其进行定位。 You can define if other elements are on top of it or under it using "z-index". 您可以使用“ z-index”定义其他元素在其上方还是下方。

  • position: relative can be defined as an hybrid between absolute and static. 位置:相对可以定义为绝对与静态的混合体。 The element will take in account the blocks that are near itself to find its position in the document. 该元素将考虑其自身附近的块以找到其在文档中的位置。 however, you can modify that position using "top, left, right, bottom", but that position will use as a reference the original place the element was located. 但是,您可以使用“上,左,右,下”来修改该位置,但是该位置将用作元素所位于的原始位置的参考。 This type of elements can also use "z-index". 这种类型的元素也可以使用“ z-index”。

Overall, position relative has properties from "absolute" and "static". 总的来说,相对位置具有“绝对”和“静态”的特性。 I have yet to see a "position: relative" element in where using "top, bottom, left, right" is justified, because makes the element to be less predictable, and you can displace it using padding or margins instead. 我还没有看到“位置:相对”元素在哪里使用“上,下,左,右”是合理的,因为这使得该元素难以预测,您可以使用填充或边距来代替它。

Usually, position relative elements are defined not because you can position them with "top, left, right bottom" but because making them relative will let you position elements inside of them with "position: absolute" taking the relative element as reference. 通常,位置相对元素的定义不是因为您可以使用“顶部,左侧,右侧底部”来定位它们,而是因为将它们设置为相对位置将使您可以使用“ position:absolute”将它们内部定位,并将相对元素作为参考。

Most of the problems I have found that confuse people is due the name they have: "absolute" looks like you will position the element taking in account only the windows, and "relative" sounds like you are using other element as base. 我发现大多数使人感到困惑的问题是由于他们的名字引起的:“绝对”看起来您将只考虑窗口而定位元素,而“相对”听起来就像您正在使用其他元素作为基础。 However, the truth is that "absolute" is not absolute at all, it takes is position in relation of other element. 但是,事实是“绝对”根本不是绝对的,它是与其他元素有关的位置。

edit: as geeksamu mentions, the "icon" is a class, so it should have a dot before. 编辑:正如geeksamu所提到的,“图标”是一个类,因此它之前应该有一个点。

I think the problem with your code at 我认为您的代码存在问题

.provContactSelector {
    icon {

icon is a class so it should be .icon not just icon icon是一个类,因此它应该是.icon而不只是icon

With the settings you use, the element will be moved 50px left of its original position, because you use position: relative; 使用您的设置后,元素将向其原始位置左移50px,因为您使用的是position: relative; and right: 50px (ie right border 50px away from original right border). right: 50px (即右边界距原始右边界50px)。 To achieve what you describe, you should use position: absolute; 要实现您所描述的内容,您应该使用position: absolute; . But note that for the absolute position to relate to the parent element , the parent element needs to have position: relative; 但是请注意,要使绝对位置与父元素相关联,父元素需要具有position: relative; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM