How to horizontally center a floating element of a variable width?

Assuming the element which is floated and will be centered is a div with an id=”content” … <body> <div id=”wrap”> <div id=”content”> This will be centered </div> </div> </body> And apply the following CSS: #wrap { float: left; position: relative; left: 50%; } #content { float: left; position: relative; left: -50%; } Here is a … Read more

random position of divs in javascript

Here’s one way to do it. I’m randomly varying the size of the div within a fixed range, then setting the position so the object is always placed within the current window boundaries. (function makeDiv(){ // vary size for fun var divsize = ((Math.random()*100) + 50).toFixed(); var color=”#”+ Math.round(0xffffff * Math.random()).toString(16); $newdiv = $(‘<div/>’).css({ ‘width’:divsize+’px’, … Read more

jQuery position DIV fixed at top on scroll

instead of doing it like that, why not just make the flyout position:fixed, top:0; left:0; once your window has scrolled pass a certain height: jQuery $(window).scroll(function(){ if ($(this).scrollTop() > 135) { $(‘#task_flyout’).addClass(‘fixed’); } else { $(‘#task_flyout’).removeClass(‘fixed’); } }); css .fixed {position:fixed; top:0; left:0;} Example

What is the simplest jQuery way to have a ‘position:fixed’ (always at top) div?

Using this HTML: <div id=”myElement” style=”position: absolute”>This stays at the top</div> This is the javascript you want to use. It attaches an event to the window’s scroll and moves the element down as far as you’ve scrolled. $(window).scroll(function() { $(‘#myElement’).css(‘top’, $(this).scrollTop() + “px”); }); As pointed out in the comments below, it’s not recommended to … Read more

Z-Index Relative or Absolute?

z-index is relative. See this detailed answer, which I wrote for a similar question. If none of the other elements have a defined z-index, using z-index: 1 will be fine. Model: How is the z-index determined? z-index <div id=A> Auto 1 <div id=B> Auto 1.1 <div id=C style=”z-index:1″></div> Manual 1 <div id=D></div> Auto 1.1.2 </div> … Read more