CSS元素的样式失败once float:right被添加到div

时间:2022-10-25 00:03:02

The following CSS element from this answer works ok in this JS fiddle example, however once I place the elements in my website, the standard drop down arrow becomes visible again due to my div having a float:right.

这个答案中的以下CSS元素在这个JS小提琴示例中运行正常,但是一旦我将元素放在我的网站中,标准下拉箭头再次可见,因为我的div有一个浮点数:右。

So: No float:right: Everything works, except that my div is in the wrong location.

所以:没有浮动:正确:一切正常,除了我的div在错误的位置。

Add float:right: Drop down becomes visible again, but then my div is in the correct location.

添加浮动:右:下拉再次可见,但然后我的div位于正确的位置。

Sort of: Can't have your cake and eat it.

排序:不能吃你的蛋糕。

Browser: Firefox, latest.

浏览器:Firefox,最新。

Using this, I created a new fiddle with the issue:

使用这个,我创造了一个问题的新小提琴:

<form id="bookingForm">
    <div class="col-md-6" style="float:right">
        Not working due to float:right
        <div class="input-group">
            <select style="width:200px;" class="form-control">
                <option>
                    hr
                </option>
            </select>
        </div>
    </div>
    <div class="col-md-6">
        Working but not in the right place (no float:right)<br/>
        <div class="input-group">
            <select style="width:200px;" class="form-control">
                <option>
                    hr
                </option>
            </select>
        </div>
    </div>
</form>

and the CSS

和CSS

.input-group {
    width:100%;
    display: inline-block;
}

.input-group select {
    padding: 0% 20% 0% 20%;

    -webkit-appearance:none;
    border: 1px solid #cccccc;
    -webkit-border-radius: 4px;
    border-radius: 20px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    height: 30px;

    background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAeCAYAAADZ7LXbAAAACXBIWXMAAAsTAAALEwEAmpwYAAAKT2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AUkSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja9a89+bN/rXXPues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHAAEAizZCFz/SMBAPh+PDwrI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fyl5O/bXyl/erA6xmv28bCxh6+yXgzMV70VvvtwXfcdx3vo98PT+R8IH8o/2j5sfVT0Kf7kxmTk/8EA5jz/GMzLdsAAAAgY0hSTQAAeiUAAICDAAD5/wAAgOkAAHUwAADqYAAAOpgAABdvkl/FRgAAAKRJREFUeNrs1TEKwkAQheEvIoI2nsk7qFdIq1hoJ3gCC5sUVpY23sDKXnvrYOUBbGITG0kQjQriPlgYhmF/3ryFjbIs82nVfEEBEiAB8k+Q+q1IkqSDNVq4lMy3scIkjuP0FSdbjNHMLys6OwyQVlnXEsOS2QP6OL8jkzlmd70jus86eBT8FIu8PqGXg6oFX6ARGthgX+V1ReFnDJAACZAfhFwHAJI7HF2lZGQaAAAAAElFTkSuQmCC) no-repeat right 0% bottom 0% #ffffff;    

}

For the https://jsfiddle.net/norbertnobel/jtx52dd7/

对于https://jsfiddle.net/norbertnobel/jtx52dd7/

2 个解决方案

#1


1  

(This answer is based on the latest version of FF)

(这个答案是基于最新版本的FF)


Explanation:

The root of the problem is the percentage based padding on the left/right of the select element.

问题的根源是select元素左/右的填充百分比。

When the parent element is floated, it basically has a "shrink-to-fit" based width which is reducing the width of the element in your case. Since the percentage based padding on the select element is calculated relative to the parent element's width, the amount of padding differs between both of the elements in your example.

当父元素浮动时,它基本上具有“收缩到适合”的宽度,这会减小您的情况下元素的宽度。由于select元素上基于百分比的填充是相对于父元素的宽度计算的,因此填充量在示例中的两个元素之间不同。

Here is a visual demonstrating how the amount of padding controls whether or not the dropdown arrow appears:

下面是一个视觉演示,显示填充量如何控制是否显示下拉箭头:

This element's parent is floated:

这个元素的父元素是浮动的:

CSS元素的样式失败once float:right被添加到div

This select element's parent is not floated:

此select元素的父元素未浮动:

CSS元素的样式失败once float:right被添加到div

In other words, the floated element has less padding because its parent element's width is smaller.

换句话说,浮动元素具有较少的填充,因为其父元素的宽度较小。

As you can see, the default arrow is not appearing if the amount of padding exceeds the width of the select element itself. This essentially explains why floating the element causes these visual differences.

如您所见,如果填充量超过select元素本身的宽度,则不会出现默认箭头。这基本上解释了为什么浮动元素会导致这些视觉差异。


Potential solution:

When it comes down to it, you are basically just trying to remove the default dropdown arrow, regardless of the width/padding of the elements. In the latest version of Firefox (v35), you can now simply remove the arrow using -moz-appearance: none:

当它归结为它时,你基本上只是试图删除默认的下拉箭头,无论元素的宽度/填充。在最新版本的Firefox(v35)中,您现在可以使用-moz-appearance:none删除箭头:

Updated Example

更新的示例

.input-group select {
    padding: 0% 20% 0% 20%;
    -moz-appearance: none;
    -webkit-appearance: none;
    appearance: none;
    border: 1px solid #cccccc;
    border-radius: 20px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    height: 30px;
    background: url(...);
}

#2


0  

It seems like it's your padding that's at fault:

看起来这是你的填充错误:

https://jsfiddle.net/jtx52dd7/2/

https://jsfiddle.net/jtx52dd7/2/

It works better with this:

它的效果更好:

.input-group select {
    padding: 0 10px;
}

#1


1  

(This answer is based on the latest version of FF)

(这个答案是基于最新版本的FF)


Explanation:

The root of the problem is the percentage based padding on the left/right of the select element.

问题的根源是select元素左/右的填充百分比。

When the parent element is floated, it basically has a "shrink-to-fit" based width which is reducing the width of the element in your case. Since the percentage based padding on the select element is calculated relative to the parent element's width, the amount of padding differs between both of the elements in your example.

当父元素浮动时,它基本上具有“收缩到适合”的宽度,这会减小您的情况下元素的宽度。由于select元素上基于百分比的填充是相对于父元素的宽度计算的,因此填充量在示例中的两个元素之间不同。

Here is a visual demonstrating how the amount of padding controls whether or not the dropdown arrow appears:

下面是一个视觉演示,显示填充量如何控制是否显示下拉箭头:

This element's parent is floated:

这个元素的父元素是浮动的:

CSS元素的样式失败once float:right被添加到div

This select element's parent is not floated:

此select元素的父元素未浮动:

CSS元素的样式失败once float:right被添加到div

In other words, the floated element has less padding because its parent element's width is smaller.

换句话说,浮动元素具有较少的填充,因为其父元素的宽度较小。

As you can see, the default arrow is not appearing if the amount of padding exceeds the width of the select element itself. This essentially explains why floating the element causes these visual differences.

如您所见,如果填充量超过select元素本身的宽度,则不会出现默认箭头。这基本上解释了为什么浮动元素会导致这些视觉差异。


Potential solution:

When it comes down to it, you are basically just trying to remove the default dropdown arrow, regardless of the width/padding of the elements. In the latest version of Firefox (v35), you can now simply remove the arrow using -moz-appearance: none:

当它归结为它时,你基本上只是试图删除默认的下拉箭头,无论元素的宽度/填充。在最新版本的Firefox(v35)中,您现在可以使用-moz-appearance:none删除箭头:

Updated Example

更新的示例

.input-group select {
    padding: 0% 20% 0% 20%;
    -moz-appearance: none;
    -webkit-appearance: none;
    appearance: none;
    border: 1px solid #cccccc;
    border-radius: 20px;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    height: 30px;
    background: url(...);
}

#2


0  

It seems like it's your padding that's at fault:

看起来这是你的填充错误:

https://jsfiddle.net/jtx52dd7/2/

https://jsfiddle.net/jtx52dd7/2/

It works better with this:

它的效果更好:

.input-group select {
    padding: 0 10px;
}