CSS 浮动 Div's &结盟

发布于 2024-12-07 08:01:20 字数 2456 浏览 0 评论 0原文

我正在尝试创建一个控件,其中包含两个带有添加/删除按钮的列表框,用于将项目从一个列表移动到另一个列表。通常我会使用表格来完成此操作,但我尝试遵循 css 标准并使用 div。

我的列表框完美对齐,但我不知道如何设置它们之间的按钮。

这是我的 html(已更新以显示渲染的 html):

<div id="dealsummary-ladderlist">    
    <form action="/Reporting/DealSummaryComparison" method="post">    
        <div id="available">
            <div><strong>Available</strong></div>
            <div id="available-items">
                <select id="ItemsToSelect" multiple="multiple" name="ItemsToSelect" size="30">
                    <option value="16">Item 1</option>
                    <option value="17">Item 2</option>
                    <option value="21">Item 3</option>
                    <option value="22">Item 4</option>
                    <option value="23">Item 5</option>
                    <option value="24">Item 6</option>
                    <option value="25">Item 7</option>
                </select>
            </div>
        </div>
        <div id="add-remove">
            <div><input type="button" value=">>" /></div>
            <div><input type="button" value="<<" /></div>
        </div>
        <div id="selected">
            <div><strong>Selected</strong></div>
            <div id="selected-items">
                <select id="ItemsToDeselect" multiple="multiple" name="ItemsToDeselect" size="30"></select>
            </div>
        </div>
        <div style="clear:both;"></div>

        <br /><br />

        <center>
            <p>
                <input type="submit" value="Generate Report" />
            </p>
        </center>
    </form>
</div>

这就是我的 css:

#add-remove {
    /* want to center on page */
    float: left;
    width: 10%;
}

#add-remove div {
   /* want to add even spacing between buttons */
}

#available {
    float: left;
    width: 45%;
}

#selected {
    float: right;
    width: 45%;
}

#available #available-items,
#selected #selected-items {
    margin: 1em 0 0 0;
}

#available #available-items select,
#selected #selected-items select {
    width: 100%;
    font-size: 10pt;
}

如何使用 css 实现箭头按钮的居中和均匀间距?

I am trying to create a control which contains two listboxes with add/remove buttons to move items from one list to the other. Typically I would do this using a table, but I am trying to follow css standards and use divs.

I have the listboxes aligned perfectly, but I can't figure out how to set up the buttons between them.

This is my html (updated to show rendered html):

<div id="dealsummary-ladderlist">    
    <form action="/Reporting/DealSummaryComparison" method="post">    
        <div id="available">
            <div><strong>Available</strong></div>
            <div id="available-items">
                <select id="ItemsToSelect" multiple="multiple" name="ItemsToSelect" size="30">
                    <option value="16">Item 1</option>
                    <option value="17">Item 2</option>
                    <option value="21">Item 3</option>
                    <option value="22">Item 4</option>
                    <option value="23">Item 5</option>
                    <option value="24">Item 6</option>
                    <option value="25">Item 7</option>
                </select>
            </div>
        </div>
        <div id="add-remove">
            <div><input type="button" value=">>" /></div>
            <div><input type="button" value="<<" /></div>
        </div>
        <div id="selected">
            <div><strong>Selected</strong></div>
            <div id="selected-items">
                <select id="ItemsToDeselect" multiple="multiple" name="ItemsToDeselect" size="30"></select>
            </div>
        </div>
        <div style="clear:both;"></div>

        <br /><br />

        <center>
            <p>
                <input type="submit" value="Generate Report" />
            </p>
        </center>
    </form>
</div>

This is what I have for css:

#add-remove {
    /* want to center on page */
    float: left;
    width: 10%;
}

#add-remove div {
   /* want to add even spacing between buttons */
}

#available {
    float: left;
    width: 45%;
}

#selected {
    float: right;
    width: 45%;
}

#available #available-items,
#selected #selected-items {
    margin: 1em 0 0 0;
}

#available #available-items select,
#selected #selected-items select {
    width: 100%;
    font-size: 10pt;
}

How would I achieve the centering and even spacing of the arrow buttons using css?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2024-12-14 08:01:20

如果您知道

元素的精确宽度和高度,则可以将整个元素包装在相对定位的

< 中/code> 并使用带负边距的绝对定位,如下所示:

<div id="relativeWrapper"> <!-- added this -->
    <div id="available">
        <!-- ... snip ... -->
    </div>

    <div id="add-remove">
        <div><input type="button" value=">>" /></div>
        <div><input type="button" value="<<" /></div>
    </div>

    <div id="selected">
        <!-- ... snip ... -->
    </div>

    <div style="clear:both;"></div>
</div>
<!-- ... etc ... -->

使用 CSS:

div#relativeWrapper {
    position: relative;
}

div#add-remove {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 80px;
    margin-left: -40px;
    height: 64px;
    margin-top: -32px;
}

topleft 设置为 50%margin-left 的值的一半widthmargin-topheight 值的一半将使绝对定位元素在其相对父元素中水平和垂直居中。

垂直居中很难实现;您可以使用 display: inline-block; Vertical-align: middle;,但并非所有浏览器都支持 inline-block。或者,使用 display: table-cell; Vertical-align: middle; 往往会起作用。

顺便说一句,

元素已被弃用。使用

或简单地使用

代替。

If you know the precise width and height of the <div id="add-remove"> element, you could wrap the whole thing in a relatively-positioned <div> and use absolute positioning with negative margins like so:

<div id="relativeWrapper"> <!-- added this -->
    <div id="available">
        <!-- ... snip ... -->
    </div>

    <div id="add-remove">
        <div><input type="button" value=">>" /></div>
        <div><input type="button" value="<<" /></div>
    </div>

    <div id="selected">
        <!-- ... snip ... -->
    </div>

    <div style="clear:both;"></div>
</div>
<!-- ... etc ... -->

With the CSS:

div#relativeWrapper {
    position: relative;
}

div#add-remove {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 80px;
    margin-left: -40px;
    height: 64px;
    margin-top: -32px;
}

Setting both top and left to 50% and margin-left to half the value of width and margin-top to half the value of height will horizontally and vertically center an absolutely-positioned element within its relative parent.

Vertical-centering is difficult to achieve; you can use display: inline-block; vertical-align: middle;, but inline-block is not supported by all browsers. Alternatively, using display: table-cell; vertical-align: middle; tends to work.

Incidentally, the <center> element is deprecated. Use <div style="text-align: center;"> or simply <p style="text-align: center;"> instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文