简体   繁体   中英

Position fixed; left works fine, right doesn't

I have a span called $mySpan .

Calling

$mySpan.css({
    position        : 'fixed',
    left            : '50%',
    top             : '15%',
    backgroundColor : 'red',
    'z-index'       : 1000
});

works just fine. However,

$mySpan.css({
    position        : 'fixed',
    right           : '50%',
    top             : '15%',
    backgroundColor : 'red',
    'z-index'       : 1000
});

doesn't show the span.

As you can see, it's probably not a z-index problem. I'm really confused because my code is quite complex and I haven't been able to build a simple example using jsFiddle. I have no clue where to start looking for what is going wrong.

Does anyone have a hint as to why the second approach is not working? How could I debug such a problem?

Try this:

$mySpan.css({
    'position'        : 'fixed',
    'left'            : 'auto', /* <-- oftentimes, browsers screw up if 'left' is still set */
    'right'           : '50%',
    'top'             : '15%',
    'backgroundColor' : 'red',
    'z-index'         : 1000
});

The unexpected result can be caused by a margin-right CSS property. If this property is set, remove it to fix your code.

在第二个例子中,你可能已经错过了$$mySpan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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