Position fixed in css fixed specified position

Source: Internet
Author: User


We all know the position value of the position attribute in CSS. Besides the default value, there are absolute, relative, and fixed. I usually use absolute and relative, but I don't pay much attention to position: fixed. It may be because the position: fixed mentioned in the CSS Chinese manual shows that "IE5.5 and NS6 do not support this attribute.

Some time ago, when creating a project, a layer should be fixed relative to the browser border. At that time, I tried using position: absolute and found that absolute is for the webpage border. Later, I checked some JavaScript statements that dynamically changed the left and top values based on the movement of the scroll bar. Although they can achieve similar results, when the scroll bar moves, the layer is shaking, and it doesn't look good. I want a way to fix the layer.

See the following code:

The code is as follows: Copy code
<Style type = "text/css">
<! --
# Help {
Width: 30px;
Height: 20px;
Background-color: green;
Position: fixed;
Left: 60px;
Top: 100px;
}
-->
</Style>

 

We use the above code to define a layer "help" (id = "help") on the page "). In this way, we can achieve the desired effect.

In IE 8, Firefox, Opera, Google and other browsers, there is no problem, but in earlier versions of IE, this attribute is invalid.

Example

The code is as follows: Copy code

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/>
<Title> demo </title>
<Style>
# Top {
Border: 1px solid blue;
Background: # ccc;
Width: 200px;
Height: 150px;
Position: fixed;
_ Position: absolute;
Bottom: 0;
Right: 0;
_ Top: expression(eval(document.documentElement.scrollTop+document.doc umentElement. clientHeight-this.offsetHeight-(parseInt (this. currentStyle. marginTop, 10) | 0)-(parseInt (this. currentStyle. marginBottom, 10) | 0 )));
    }

* Html {
Background-image: url (about: blank );
Background-attachment: fixed;
    }
</Style>
</Head>
<Body>
<Br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/>

<Div id = "top">
Test results
</Div>
</Body>
</Html>

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.