更改选择宽度以适合 div 容器

Mhd*_*Mhd 5 html css twitter-bootstrap

我已将 dropdwon 定义为 Fellow:

    <div class="form-group">
        <select class="form-control">
            <option>small option</option>
            <option>mediummmmmmmmmmmmmmmmmmmm option</option>                
            <option>large eeeeeeeeeeeeeeeeeeeeee eeeeeeeeeeeeeee eeeeeeeeeeeee eeee option</option>
        </select>
    </div>
Run Code Online (Sandbox Code Playgroud)

我想将select宽度更改为容器宽度和option文本宽度的最小值。如果option文本大于div容器,则截断选项的文本并将省略号放在末尾。所以option留在容器内。还select必须是响应式的,调整窗口大小时,select应更新宽度以适应新的窗口宽度。

如何使用 bootstrap CSS 实现这一点?

小智 5

尝试 width:100%;

它适用于我 ListView 中的每个元素


Ste*_*hen -1

我从这个stackoverflow 答案中得到了这个答案

.dropdown{
	width:150px;
	border: 1px solid #cfcfcf;
	height: auto;
	border-radius: 5px;
	padding:3px 4px 4px;
	position: relative;
	z-index: 0;
	overflow:hidden;
	}

.dropdown select{
	border: none;
	background-color: transparent;
	outline: none;
	padding: 0;
	width:150px;
    /* background: url(http://i57.tinypic.com/nnmgpy_th.png) no-repeat right; */
    /* background-position: 55%; */
	}

/* not 100% sure this next option rule is needed */
.dropdown option {
    width:150px
}

/* this places an arbitrary element inside the dropdown but before it's children elements */
/* We use it to cover half the select box as well as display the custom arrow */
Run Code Online (Sandbox Code Playgroud)
<div class="dropdown">
    <select>
       <option value="">Some Text</option>
        <option value="">Some More Text2</option>
        <option value="">Some More More More Longer Text</option>
    </select>
                                
    </div>
Run Code Online (Sandbox Code Playgroud)

  • 我不喜欢将宽度硬编码为 150px 的想法。这也是没有响应的。如果更改容器宽度,“select”宽度保持不变。 (2认同)