【问题标题】:float:right behaves differently in chrome and safari when using internal style sheet instead of inline stylefloat:right 在使用内部样式表而不是内联样式时在 chrome 和 safari 中的行为不同
【发布时间】:2013-04-12 06:36:16
【问题描述】:

我正在研究一个关于制作可配置下拉列表的示例,来自here。它仍在进行中,但我试图保持干净,并将我的内联样式声明移动到 html 文件头部的内部样式表中。

Here's a fiddle for it.

奇怪的是 html 的第 9 行,这里显示为第二跨行:

<dl id="sample" class="dropdown">
    <dt><a href="#" class="uiwidgetcontent uiborderall" style="width: 200px;">
        <span>Please select the country</span>
        <span class="ui-icon ui-icon-triangle-1-s" style="float:right;"></span>
    </a></dt>

正如我所料,这在所有浏览器中都可以正常工作。但是在标题的样式中,我把同样的东西放在span:last-child 行中。 (我将整个样式部分留作上下文):

    .uiborderall {
        border-radius: 18px;
    }
    .uiwidgetcontent {
        background-color: #abc;
    }
    .dropdown dd, .dropdown dt, .dropdown ul { margin: 0px; padding: 0px; }
    .dropdown dd { position: relative; }
    .dropdown a, .dropdown a:visited { text-decoration: none; outline: none; display: inline-block;}
    .dropdown dt a:hover, .dropdown dt a:focus { color: #5d4617; border: 1px solid #5d4617;}
    .dropdown dt a {display: inline-block; border: 1px solid #d4ca9a;}
    .dropdown dt a span:first-child {cursor: pointer; padding: 5px; display: inline-block; }
    .dropdown dt a span:last-child {cursor: pointer; float: right; margin-right: 10px; }
    .dropdown dd ul { display:none; left:0px;  position:absolute; top:1px; width:auto; min-width:170px; list-style:none; }
    .dropdown span.value { display:none;}
    .dropdown dd ul li a { padding:5px; display:block;}
    .dropdown dd ul li a:hover {}

我应该能够从&lt;span&gt; 元素中删除内联样式,它应该回退到内部样式表。在 Firefox 和 IE 中,这可以正常工作。但是,在 Chrome 和 Safari 中,当我这样做时,内部样式表中的 float: right 似乎被忽略了。对于它的价值,至少在 chrome 中,float:right 仍然设置在适当的范围内。渲染器似乎忽略了它。

有没有办法让我在四大浏览器的样式表中使用float:right,还是必须将它内联到元素中?

谢谢!

【问题讨论】:

  • 是否可以发布指向功能示例的链接,以便我们对其进行修改?
  • 尝试在您的样式表中添加!important - 也许它已被其他样式覆盖。内联样式的权重高于样式表中的权重。
  • @showdev - 请参阅我的 OP,在“这是一个小提琴”下。 jsfiddle.net/3xbcF
  • @MarkParnell 值得一试,但将 !important 添加到小提琴中然后删除内联样式给了我相同的结果。

标签: css cross-browser


【解决方案1】:

您不需要将float 元素放在您希望它浮动的内容之前吗?

我颠倒了你的 :first-child:last-child css 样式,颠倒了你的跨度的顺序,并删除了内联样式并且它有效:http://jsfiddle.net/3xbcF/1/

<dl id="sample" class="dropdown"> <dt><a href="#" class="uiwidgetcontent uiborderall" style="width: 200px;">
        <span class="ui-icon ui-icon-triangle-1-s"></span>
        <span>Please select the country</span>
    </a></dt>

css:

.dropdown dt a span:last-child {
    cursor: pointer;
    padding: 5px;
    display: inline-block;
}
.dropdown dt a span:first-child {
    cursor: pointer;
    float: right;
    margin-right: 10px;
}

【讨论】:

  • 谢谢,它看起来很棒。我仍然习惯于 CSS 布局,我确信我可以从中得到一个通用规则。是“始终将您想要浮动的元素放在内联元素之前”吗?你知道为什么它在 IE 和 FireFox 中有效吗?再次感谢!
  • 我很确定这是规则。它在 IE/FF 和 Chrome(如果您使用内联样式)中工作的事实可能是由于浏览器在您违反规则时试图帮助您。
猜你喜欢
  • 2012-08-01
  • 2019-07-30
  • 2019-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-07
  • 2018-09-10
相关资源
最近更新 更多