Case study of position attribute application in CSS style sheets

Source: Internet
Author: User

CSS style sheets have been written for a long time, but someCSSStyle attributes still have some problems. For example, write in the pastCSS styleForPositionAttributes are rarely used. It is also relatively unfamiliar. Different browser parsing results are also a big problem. Write this articleArticleLet's take a deeper look at it.PositionAttribute usage and background-position usage.

PositionThe original English meaning is the position, position, status. It also means placement. In CSS layout, the position attribute plays a very important role. Many containers use the position attribute for positioning.


 The position attribute in a CSS style sheet has the following values:Static, relative, absolute, fixed.

Static: static location. If you have not set the position attribute, the default value is static. Top, left, bottom, right and other attributes are invalid in the case of static. To use these attributes, you must set position to one of the other three values.

Relative: relative positioning. The element will be adjusted according to the position during the static positioning, and the space allocated to the element in the static positioning will still be allocated to it, and the elements on both sides will not fill the space with it, but they will not be squeezed out from the new position of the element.

Absolute: absolute positioning. The element is adjusted according to the position of the element containing it. For example, if it is nested in another absolute positioning element, it is located relative to that element.

Fixed: Fixed positioning. The element will be set to a fixed position on the browser and will not scroll with other elements. The position of the fixed element on the screen remains unchanged when the scroll bar is pulled up or down. Note that IE6 does not support this attribute.Note that IE6 does not support the position: fixed attribute in CSS.

Use a small real column to describe: CodePart: <Style> . Fl {float: Left; width: 60px; Background: #99 CCFF; Border: # cccccc solid 1px; Height: 20px; margin-left: 10px; text-align: Center} </Style> <Body> <Div> <Div class = "FL" style = "position: static; top: 20px; left: 50px;">Baidu </div> <Div class = "FL"> Google </div> <Div class = "FL"> Sina </div> <Div class = "FL"> Firefox </div> </Div> </Body> You can also view the running result by replacing position: static with relative and absolute.

Note: many web pages are centered. In this way, when the elements are absolutely located, there will be deviations in the display at different resolutions. In this case, we can use the following methods to deal with them:

<Div style = "position: relative;">

<Div style = "position: absolute; top: 10px; left: 10px;"> </div>

</Div> Finally, let's talk about the background-position attribute. The background-position attribute is used to determine the position of the background image. Background-position: center 25px; what does this mean? It refers to the center display of the background image, and the distance from the top is 25 pixels. You can also set other values. Then we use a small CSS style example to illustrate: <Style> body {Background: URL (images/bg.png) Repeat-X ;} . Totbg {background-position: center 25px; Background-repeat: No-Repeat; Background-image: URL (http://images.cnblogs.com/baiyjkbg.png);} </style> <Body> <Div class = "totbg"> </div> </Body> Simple Description: Background: URL (images/bg.png) repeat-xlarge is the background image of the entire bodypage, bg.png, and horizontal repetition. . Totbg {background-position: center 25px; Background-repeat: No-Repeat; Background-image: URL (http://images.cnblogs.com/baiyjkbg.png );} indicates the position of the background of the outermost Div without repeating the loop. If you do not want the background to follow the scroll bar, you can add it to the style sheet above. Background-Attachment: fixed; .
For more information, please go to: http://hi.baidu.com/design_bg/blog/category/div%2Bcss%D7%A8%C0%B8

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.