表单不会在CSS中居中

时间:2012-04-17 21:05:07

标签: html css forms center

编辑:Here's a jsfiddle demo.

编辑2:我认为它正被我正在使用的Twitter Bootstrap样式表所覆盖。我会弄清楚那里有什么问题。谢谢你的帮助,伙计。

以下是我的表单现在的样子:

enter image description here

理想情况下,我希望搜索栏和提交按钮居中(搜索栏左侧和按钮右侧的边距相等)。

我的CSS似乎不起作用:

body {
    text-align:center;
    padding-top:5%;
    background:url(/img/bg.png);
    background-position:center;
    background-position:top;
    background-repeat:no-repeat;
    background-color:0b334c;
}

.searchContainer {
    width:440px;
    margin-left:auto;
    margin-right:auto;
    margin-top: 5%;
    zoom:1;
}

.searchBox {
    border: 1px solid #000000;
    padding:10px;
    margin: 0 auto;
    height:auto;
    width:auto;
    -webkit-border-radius:7px;
    -moz-border-radius:7px;
    border-radius:7px;
    background-color:#6eb0d9;
    background-color:#6eb0d9;
    background-repeat:repeat-x;
    background-image:-khtml-gradient(linear, left top, left bottom, from(#9cd1e9), to(#6eb0d9));
    background-image:-moz-linear-gradient(top, #9cd1e9, #6eb0d9);
    background-image:-ms-linear-gradient(top, #9cd1e9, #6eb0d9);
    background-image:-webkit-gradient(linear, left top, left bottom, color-stop(0%, #9cd1e9), color-stop(100%, #6eb0d9));
    background-image:-webkit-linear-gradient(top, #9cd1e9, #6eb0d9);
    background-image:-o-linear-gradient(top, #9cd1e9, #6eb0d9);
    background-image:linear-gradient(top, #9cd1e9, #6eb0d9);
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#9cd1e9', endColorstr='#6eb0d9', GradientType=0);
}

.searchBox form {
    display:inline-block;
}

.searchBox h2 {
    letter-spacing:-0.05em;
    padding-bottom:8px;
}

.searchBar {
    border: double;
    height:31px;
    width:357px;
    background-color:#fff;
    background-color:#fff;
    background-repeat:repeat-x;
    background-image:-khtml-gradient(linear, left top, left bottom, from(#eee), to(#fff));
    background-image:-moz-linear-gradient(top, #eee, #fff);
    background-image:-ms-linear-gradient(top, #eee, #fff);
    background-image:-webkit-gradient(linear, left top, left bottom, color-stop(0%, #eee), color-stop(100%, #fff));
    background-image:-webkit-linear-gradient(top, #eee, #fff);
    background-image:-o-linear-gradient(top, #eee, #fff);
    background-image:linear-gradient(top, #eee, #fff);
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#eee', endColorstr='#fff', GradientType=0);
}

input.searchButton {
    position:fixed;
    height: 31px;
    width: 33px;
    cursor: pointer;
    border: none;
    background:url(../img/searchButton-Off.png) no-repeat;
} 

input.searchButton:hover { 
    position:fixed;
    height: 31px;
    width: 33px;
    cursor: pointer;
    border: none;
    background:url(../img/searchButton-On.png) no-repeat;
} 

  <div class="searchBox">
    <h2 style="color:#000000;">Search</h2>
    <form id="form_297586" class="appnitro"  method="get" action="results.php">
      <input id="keywords" name="keywords" class="searchBar" title="What do you like...?" type="text" maxlength="255" value=""/>
      &nbsp;<input type="submit" class="searchButton" value="" />
      <input type="hidden" name="form_id" value="297586" />
      <input type="hidden" name="src" value="n" />
      <input type="hidden" name="fancy" value="y" />
    </form>
  </div>

我在这里做错了什么?

6 个答案:

答案 0 :(得分:1)

我能让这个工作的唯一方法是从搜索按钮和搜索栏(position: fixed s)中移除input,然后将position: relative; top: -9px;添加到.searchButton元素。

说实话,哪个感觉很脏;我不知道为什么应该是必需的。

无论如何,JS Fiddle demo

答案 1 :(得分:0)

尝试将此类添加到您的css:

.appnitro { margin: 0 auto; }

您可能需要在表单上设置宽度。

刷新时按Ctrl + F5 =)

答案 2 :(得分:0)

你可以扭曲div中的形式,给出一个大小并设置左右边距到自动

http://jsfiddle.net/chepe263/WbTLr/

答案 3 :(得分:0)

我在jsfiddle上运行你的代码,我发现它实际上是你的搜索栏完全居中。提交按钮就在它的右侧。 从position:fixed课程中删除input.searchButton并添加:

position: relative;
bottom: 8px;

这是因为如果删除position:fixed整个中心,但提交按钮会向下移动一点。所以这是一个很小的黑客推动它。

jsFiddle working demo

答案 4 :(得分:0)

当我需要集中注意力时,我就是这样做的:

样式:

div.centera {
    float: right;
    left: -50%;
    position: relative;
}
div.centerb {
    float: right; 
    left: 50%;  
    position: relative;
}

并集中内容:

<div class="centera">
    <div class="centerb">
        <div>Should be in center</div>
    </div>
</div>

编辑:

在为float: left;

添加<input id="keywords"...后尝试了它并且在Chrome中运行良好

无论如何,我认为float: left也应该为按钮添加......

答案 5 :(得分:-1)

您可以将表单包装在中心标签中。