The affix plugin sets different classes for your element based on scroll position (and offset). Before the offset this class will be affix-top
, between affix-offset-top and affix-offset-bottom this class will be affix
. The position defined by css for affix-top
will be static (default, also see: http://www.w3schools.com/cssref/pr_class_position.asp) while the position of the affix
class is set to fixed.
When a element has position:absolute it's width is max the width of it's parent. So in your case your panel get 100% width of the col-lg-3 (due to the box-sizing: border-box, see Why did Bootstrap 3 switch to box-sizing: border-box? ). The width of this class will 25% (3/12*100) parent .container div. The (max-)width of the .container depends on the screen / viewport's width.
The affix class with position:fixed don't have a parent container. So 100% width of this element will be 100% of the viewport minus the left position of the element (100% of the available space).
When changing the viewport, the width of the .container is fixed between the boundaries of the grid (see: http://getbootstrap.com/css/#overview-container). So the size of your affix-top will be fixed between this boundaries. Always 25% of the .container. Set the .affix class also to 25% it's size will varies fluid. Cause 25% of the viewport (minus the left) mostly not equals 25% of the container your affixed element will have a different width with class .affix.
For the reason mentioned above the width of the affixed element is mostly set fixed to a value in pixels. For an example see the docs on http://getbootstrap.com/.
The best way to varies the width of your side panel depending on screen width (make it responsive) will be widths in pixels in combination with media queries.
Example:
/* Extra small devices (phones, up to 480px) */
/* No media query there is no affix */
/* Small devices (tablets, 768px and up) */
@media (min-width: 768px) { #side-panel { width: 152px; } }
/* Medium devices (desktops, 992px and up) */
@media (min-width: 992px) { #side-panel { width: 208px; } }
/* Large devices (large desktops, 1200px and up) */
@media (min-width: 1200px) { #side-panel { width: 270px; } }
See: http://bootply.com/82661 (note i adopt your code for Bootstrap 3.0.0.)
Alternative set the width with jQuery:
This shows the width depends on the .container, but prefer the css solution above.
$(document).scroll(function(){
$('.panel.affix').width(((($('.container').width()+30)/4)-30));
})