如何正确浮动元素并仍然保持tabindex?

NoB*_*BBy 12 css user-experience tabindex css-float

这是一个小提琴:http://jsfiddle.net/5s7vv/

我想要做的是有2个按钮向左浮动,2向右浮动,就像示例一样,但button_4应该是最右边的元素.但是,我只能通过一个简单的浮动权来实现.我应该在标记中更改它们的顺序,但这会破坏用户体验(按钮之间的标签顺序错误),这实际上导致了我的问题.

是否可以按正确的顺序向右浮动两个按钮并保持其标签索引,当然也没有额外的标记.(将它们包装在div中很容易,但我真的想避免这种情况).

我知道tabindex属性,但据我所知它没有得到很好的支持......


HTML代码:

CSS:

#container{
    width: 200px;    
}

#container a{
    width: 20px;
    height: 20px;
}

.button_1, .button_2{
     float: left;   
}

.button_3, .button_4{
     float: right;  
}

.button_1{background-color: blue;}
.button_2{background-color: red;}
.button_3{background-color: green;}
.button_4{background-color: yellow;}
Run Code Online (Sandbox Code Playgroud)

Kal*_*sin 10

这有点像黑客攻击,但你可以将div中的按钮3和4包装起来并右移浮动div.这将保持按钮顺序完好无损.

  • 这是最干净、最实用的解决方案。接受的答案仅在您知道元素的宽度时才有效。 (2认同)

cla*_*uzy 6

添加这个:

.button_3 {margin-right: 20px;}
.button_4 {margin-right: -40px;}
Run Code Online (Sandbox Code Playgroud)

这使他们(3和4}"交换"的地方

或者position: relative可能也会工作

更新:使用位置相对似乎在IE中更稳定..

.button_3, .button_4{
    float: right; 
    position: relative; 
}

.button_3 {left: -20px;}
.button_4 {left: 20px;}
Run Code Online (Sandbox Code Playgroud)