Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

css - Positioning the drop down window of a HTML SELECT

I am using an HTML SELECT tag and want to make sure the window that opens when the user hits the arrow to position below the "textbox" part of the control.

The problem is that IE seems to just open the window arbitrarily depending on the selection but I need to ensure consistent positioning as there are other elements on the page that need to remain visible at all times

Here's the current markup

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <div style="margin-left: 5px; margin-top: 5px">
        <div style="position: relative;">
            <select style="position: absolute; top: 10px; left: 10px;">
                <optgroup label="Choice A">
                    <option value="A-A">Sub choice A</option>
                    <option value="A-B">Sub choice B</option>
                </optgroup>
                <optgroup label="Choice B">
                    <option value="B-A">Sub Choice A</option>
                    <option value="B-B">Sub Choice B</option>
                </optgroup>
            </select>
        </div>
    </div>
</body>
</html>

This is whats happening

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sorry. I'm afraid this is not possible. The problem is <select> dropdowns uses browser's native rendering of dropdowns. This means that there is limited styling capability and differ by browsers. I see the behavior you described on my OSX Chrome as well - not opening below the select box.

The only way you can have complete control the styles completely is to use something completely custom - building the <select> using DOM and javascript:

Check out this: http://gregfranko.com/jquery.selectBoxIt.js/

When selecting a library, make sure to pick one that transforms the native <select> tag into custom - IE the markup shouldn't be custom for your HTML. This is so when browsers don't have JavaScript, your <select> dropdown should still work


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...