简体   繁体   English

CSS上下左右属性如何工作?

[英]How do the CSS top bottom left and right attributes work?

I was wondering what the logic was behind the CSS attributes for top , left bottom and right . 我想知道topleft bottomrightCSS属性背后的逻辑是什么。

for example if i was to do the following: 例如,如果我要执行以下操作:

img {
    position:absolute;
    right:5px;
}

this would set the right edge of the image 5px to the left of the right edge of its containing element. 这会将图片的右侧边缘设置为其包含元素的右侧边缘的左侧5px。

which i understand fine and the syntax makes sense, however where i'm slightly confused is if i was to set it like this: 我理解得很好并且语法很有意义,但是如果我将其设置为这样,我有点困惑的地方是:

img {
    position:absolute;
    right:0px;
}

It should set the right edge of the image to the left of the right edge of its containing element, but the syntax seems odd to me, that i have to specify right:0px; 它应该将图像的右边缘设置为其包含元素的右边缘的左侧,但是语法对我来说似乎很奇怪,我必须指定right:0px; .

I was just curious as to why it works this way, and is not in a similar format to float:right; 我只是想知道为什么它会以这种方式工作,并且没有与float:right;类似的格式float:right; or position:right; position:right;

The float properties are relative to other elements in your document. float属性是相对于文档中其他元素的。 The top , right , left, and bottom properties are relative to the edge of that element . toprightleft,bottom属性相对于该元素的边缘。 They add margin of that amount to the specified side of the element. 它们会将该数量的边距添加到元素的指定面。

float: left; says "I'm going to float to the left of other elements next to me. 说:“我将漂浮在我旁边其他元素左侧

right: 5px; says "The closest element has to be 5px away from me when on the right side. 说:“最右边的元素在右侧时必须离我5像素。

It's a matter of selflessness vs. selfishness. 这是无私与自私的问题。 See the W3C Wiki for more information. 有关更多信息,请参见W3C Wiki

Also, keep in mind that top , right , left , and bottom properties only work when position is set. 另外,请记住, toprightleftbottom属性仅在position设置时有效。

Well, you need to understand the concept of positioning for this. 好吧,您需要了解定位的概念。 Let us try to understand this. 让我们尝试理解这一点。 Consider you have the following structure: 考虑您具有以下结构:

HTML: HTML:

<div id = "divContainer">
    <div id = "divElement"></div>
</div>

CSS: CSS:

#divContainer {
    height:300px;
    position:relative;
    width:300px;
    background-color:Red;
}

#divElement {
    height:150px;
    position:absolute;
    width:150px;
    background-color: Orange;
    right:0px;
}

See this here: http://jsfiddle.net/3mQk2/ 在这里查看: http : //jsfiddle.net/3mQk2/

As you can see, the divElement sticks to the right end corner of the divContainer. 如您所见,divElement粘贴在divContainer的右端角。 This is because position:absolute takes the element out of the normal 'flow of the document'. 这是因为position:absolute将元素从正常的“文档流”中删除。 This in itself does not move the element (remove the right:0; and see). 这本身不会移动元素(删除right:0;然后看)。 When you provide the right:0 value it understands 'make the distance between the current element (divElement) and its container 0px on the inside right side of the container'. 当您提供right:0值时,它将理解为“使当前元素(divElement)与它的容器之间的距离在容器的内部右侧为0px”。

This also happens because the container has position:relative attached to it. 这也发生是因为容器具有附加的position:relative If instead, the position property of the container was set to static (which is the default by the way) in this manner: 如果相反,则以这种方式将容器的position属性设置为static(顺便说一下,这是默认值):

#divContainer {
    height:300px;
    position:static;
    width:300px;
    background-color:Red;
}

Then divElement's container would have been the window element and not the divContainer(yes, this is a aha!moment to many). 然后,divElement的容器将是window元素,而不是divContainer(是的,这对许多人来说都是一个时刻)。 In that case, the divElement would have 'stuck' to the corner of its container (ie the window). 在这种情况下,divElement将“粘在”其容器(即窗口)的角落。

You can see this here: http://jsfiddle.net/35qc2/ 您可以在这里看到: http : //jsfiddle.net/35qc2/

Hope this helps. 希望这可以帮助。

EDIT: i know what it does, i was more wondering why the syntax of right:0, i know that position:right; 编辑:我知道它的作用,我更想知道为什么right:0的语法,我知道position:right; doesn't exist but it would make more sense syntactically than right:0px. 不存在,但在语法上比right:0px更有意义。

This is the comment you have made above. 这是您在上面所做的评论。 Let us try to break this down and examine it. 让我们尝试分解并检查它。

"I know that position:right; doesn't exist" “我知道那个位置:正确;不存在”

Yes, you are right. 是的,你是对的。 position:right does not exist. position:right不存在。

But it would make more sense syntactically than right:0px. 但这在语法上比right:0px更有意义。

Nope. 不。 It wouldn't. 不会的。 For this, you have to understand the fundamental responsibility of the properties. 为此,您必须了解属性的基本责任。 ie: 即:

position property : HOW the element is to be positioned. position property :如何position property元素。

top, right, bottom, left : WHERE the element is to be positioned. top, right, bottom, left :元素要放置的位置。

That is these properties (top, right, bottom, left) are dependent on the position property. 也就是说,这些属性(顶部,右侧,底部,左侧)取决于position属性。

That's why these properties have no effect when the position property is set to static. 这就是为什么将position属性设置为static时这些属性无效的原因。 They only come into picture when the position is NOT static. 它们仅在position不固定时才显示。

top, right, left, and right; 顶部,右侧,左侧和右侧; refer to specific coordinates in the screen (or container) whereas float:right, and float:left respect the padding the container may have set previously. 请参考屏幕(或容器)中的特定坐标,而float:right和float:left尊重容器先前可能设置的填充。

For instabnce in the DEMO I include here. 对于在instabnce DEMO我这里有。 You will see that both elements are inside the same wrapper. 您将看到两个元素都在同一个包装器中。 The float is restrained to the wrapper width and padding. 浮子被限制在包装纸的宽度和内衬上。 The other element is following specific instructions to be located where I want it. 另一个要素是按照特定的说明放置在我想要的位置。 In this case 100px down from the top and 0px from the right (of the screen) 在这种情况下,(屏幕)从顶部向下100px,从右侧0px

There is a misconception where some developers think it follows the top-left coordinate of the screen as (0,0). 有一个误解,一些开发人员认为它遵循屏幕的左上角坐标为(0,0)。 Not exactly. 不完全是。 As far as position absolute goes every edge is a 0. So when you say left:10px. 至于绝对位置,每个边缘都是0。因此,当您说left:10px时。 It means 10px towards the center of the screen. 这意味着朝向屏幕中心10px。 Same for right:0px. 正确:0px。 It means 10px towards the center of the screen from the right edge. 这意味着从右边缘到屏幕中心的距离为10px。

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

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