I have the following DOM elements on one horizontal line:
- A div containing a line of text; i.e. "Hello world". The width of this div is the same as the containing text.
- A button to swap the two divs.
- A div containing a listbox. The width of this div is the same as the containing listbox.
With float
or position:absolute
I can align these elements correctly on one line. However, when the user clicks the swap button, the divs should swap places. I would prefer not to remove the divs from the DOM and adding them again in each other's place. That's because the elements are actually Javascript objects (Google Closure Library components to be more specific) that hold variables and have event handlers attached to them. Is it possible to swap the divs using CSS, without assigning a static width to the divs?
Simplified example:
<div style="position:relative; height:60px;">
<div style="float:left;">Our Team</div>
<div style="float:left;"><button onclick="swap();">swap home/away</button></div>
<div style="float:left;"><select><option>Opponent A</option><option>Opponent B</option></select></div>
</div>
<div style="clear:both;"></div>
When the user clicks the button, the first (our team) and the third (opponent) div should be visually swapped in the browser, preferably without removing the elements from the DOM.