CSS Float元素旁边的元素

时间:2012-10-04 07:40:55

标签: css css-float center

我的页面中心有一张桌子

http://jsfiddle.net/YrBnd/3/

是否可以将按钮放在示例中,就在表格的左侧?

这个技术高超的绘画应该会给你一个更好的画面:

|                  |
|    $$center      |
|                  |

其中$$是我想放在中心的按钮。

3 个答案:

答案 0 :(得分:9)

尝试这种方法,基于你的jsfiddle:

HTML

<span>Center body text<button>Click</button></span>

CSS

span {
    display: block;
    width: 200px;
    margin: 0 auto;
    position: relative;
}
button {
    position: absolute;
    right: 100%;
}

http://jsfiddle.net/YrBnd/4/

按钮从右边缘绝对放置到量程元素宽度的100%。

答案 1 :(得分:-1)

.center
{
   width:250px;
   margin:0px auto 0px auto;
}
.center input[type="button"], div
{
   float:left;
}

<div class="center">
   <input type="button" name="submit" value="$$" />
   <div> center text </div>
</div>

答案 2 :(得分:-2)

你可以用jquery解决这个问题。

在页面加载时,您可以检查表的开始位置,然后添加该表左侧的按钮

例如

$(function(){
  //Get the left position of the table
  var leftPosition =  $("table").offset().left;
  //Get the top position of the table
  var topPosition=  $("table").offset().top;

  //Now add the left position of the table minus the button width and a spacing of 5
  var leftButtonPosition = leftPosition -  $("myButton).width() + 5;

 $("myButton).offset({ top: topPosition + 5 , left: leftButtonPosition });
});

et voila;)

相关问题