I try to design some kind of html , but I want to use images instead of text . So I've build some very basic html outline, with two divs for every option and two divs around them:
<div class="dungeon-action-select-container">
<div class="dungeon-action-select">
<div class="border"><div></div></div>
<div class="border"><div></div></div>
<div class="border selected"><div></div></div>
<div class="border"><div></div></div>
<div class="border"><div></div></div>
</div>
</div>
The point of the most inner divs is to contain images later. The .border divs are there to allow different visual effects for selected options. The .dungeon-action-select should contain a vertical scrollbar, when there are more options to select from.
The container div is there, to allow the whole "thing" to be placed fixed to the top left of the screen.
My Problem: When the .dungeon-action-select div is to small, to display all options, there will be a vertical scrollbar. But the vertical scrollbar is displayed within the div, not attached at the right. One solution, I came up with, is to make the .dungeon-action-select 15px greater, than the inner divs. But that is just a hack because the scrollbar could (of cause) be bigger than 15px.
Here is my "solution" so far:
.dungeon-action-select-container {
position: fixed;
width: 125px; /* 80px + 2 * 15px + width of scrollbar */
top: 60px;
left: 25px;
}
.dungeon-action-select-container .dungeon-action-select {
height: 320px;
overflow: auto;
position: relative;
}
.dungeon-action-select-container .dungeon-action-select .border {
background-color: #333;
padding: 7px 15px;
}
.dungeon-action-select-container .dungeon-action-select .border div {
width: 80px;
height: 80px;
background-color: black;
}
So I'm looking for a way to automatically make the scrollbar containing div big enough to have place for the option divs and the scrollbar (when needed). Or a way to attach the scrollbar from outside of the .dungeon-action-select div. Or as a last resort, some trick to determine, portable the size of the scrollbar.
Any pointers are welcome; here is a fiddle to fiddle with: http://jsfiddle.net/b8kUr/2/
Solution: I wish Sharkys idea would have worked, but the only working solution, I've found, is to use JavaScript (as Marcelo suggested) to calculate the width of the scrollbar and to resize the containing div accordingly (the solution is based on an other question, I've found here on SO):
scrollBarHeightAndWidth = null
calcScrollBarHeightAndWidth = ->
scrollDiv = document.createElement("div")
scrollDiv.className = 'tools-scrollbar-measure'
document.body.appendChild(scrollDiv)
# Get the scrollbar width
result = {
width: scrollDiv.offsetWidth - scrollDiv.clientWidth,
height: scrollDiv.offsetHeight - scrollDiv.clientHeight
}
# Delete the DIV
document.body.removeChild(scrollDiv)
result
Tools.scrollBarWidth = ->
scrollBarHeightAndWidth = scrollBarHeightAndWidth || calcScrollBarHeightAndWidth()
scrollBarHeightAndWidth.width
and some little css to hide the test div and configure it to have a scrollbar:
.tools-scrollbar-measure {
width: 100px;
height: 100px;
overflow: scroll;
position: absolute;
top: -9999px;
}