【问题标题】:Form elements alignment through generic css通过通用 css 对齐表单元素
【发布时间】:2011-08-10 05:14:29
【问题描述】:

我正在尝试使表单水平对齐,就像我使用表格一样:

<form>
<table>
  <tr>
    <td style="width:30%"><label for="field1">Field1 Title:</label></td>
    <td><input type="text" id="field1" /></td>
  </tr>
  <tr>
    <td>Are you sure?</td>
    <td>
      <label for="sure_yes"> Yes </label><input type="radio" id="sure_yes" />&nbsp;
      <label for="sure_no"> No </label><input type="radio" id="sure_no" />
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" value="Confirm" /></td>
  </tr>
</table>
</form>

我可以将通用 css 定义为块显示,如下所示:

form label{
display:block;
float:left;
width:200px;
margin:5px 0;
clear:left;
}
form input, form textarea, form select{
display:block;
margin:5px 0;
clear:right;
float:left;
}

在这种情况下,我的 HTML 将如下所示:

<form>
  <label for="field1">Field1 Title:</label>
  <input type="text" id="field1" />
  Are you sure?
  <label for="sure_yes"> Yes </label><input type="radio" id="sure_yes" />&nbsp;
  <label for="sure_no"> No </label><input type="radio" id="sure_no" />
  <input type="submit" value="Confirm" />
</form>

当然,这个系统不适用于我显示收音机和提交的方式。我可以用 javascript 函数解决这个问题,但我想知道是否会有纯 css 解决方案 - 和跨浏览器。

【问题讨论】:

  • 在使用 css 时,在大多数情况下(如 FORM、LABEL、TEXTAREA 等),您肯定希望远离 class HTML 元素。使用更细粒度的 CSS 选择器,这样您就不会对您不想设置样式的其他元素产生意外的副作用。
  • 我不同意。当每个项目都不同时,你不能简单地通过这样的陈述。在编写表单时,99% 的时间我坚持使用标签选择器而不是类。它允许我定义整个网站所有元素的一致感觉,这应该遵循一致的设计。如果出于某种原因,有 5 或 6 种不同的表单样式,则将类或 id 添加到表单元素本身,从而允许您扩展该特定表单的基本样式。
  • @amustill - 让我们同意不同意。 :)
  • 另外,你的建议是更接近 css 规范化的东西,如果你使用这种方法,你可能知道你在做什么,或者不知道你在做什么为了。不管怎样,祝你好运。

标签: html css forms alignment


【解决方案1】:

解决办法在这里:http://jsfiddle.net/gS86P/

您遇到的问题是您试图浮动项目。相反,我使用了 inline-block 和一个行 div 来使它们的行为类似于一个表格,表格的每一行都在一个单独的行 div 中。

【讨论】:

  • 谢谢!我不喜欢每一行都包含在一个 DIV 中的事实,但解决方案应该像你一样为标签设置一个类。我看看能不能去掉这个div。
【解决方案2】:

你可以测试一下:

<form style="width:300px">
  <label for="field1" style="margin-right:20px;">Field1 Title:</label>
  <input type="text" id="field1" style="width:150px"/>
  <textarea style="border:none;background:none;color: #000000;font-size: 15px;resize: none;text-align: left;vertical-align: middle;font-family:'serif';max-width:60px; max-height:50px; overflow-y:hidden" disabled="disabled">Are you sure?</textarea>
  <label for="sure_yes" style="margin-top:12px;margin-left:35px" > Yes </label>
  <input type="radio" id="sure_yes" style="margin-top:17px" />
  <label for="sure_no" style="margin-top:12px" > No </label>
  <input type="radio" id="sure_no" style="margin-top:17px"  />
  <input type="submit" value="Confirm" style="margin-left:100px;"/>
</form>

在这里,我尝试只使用表单元素和 css 来完成这项工作,尽管我也是网络编程的新手。

【讨论】:

  • 谢谢,但不,这不是我想要实现的。我的标签和字段不应单独设置样式。
【解决方案3】:

这是我最后使用的解决方案:

CSS

.appui-form-label{
  display:block !important;
  margin:0.3em 2em 0.3em 0;
  padding-right: 60px;
  float:left !important;
  clear:left !important;
  vertical-align:top;
  position:relative;
  min-height:1.5em;
  white-space:nowrap;
  width: 200px;
}
.appui-form-field{
  display:block !important;
  margin:0.3em 0;
  float:left !important;
  clear:right !important;
  vertical-align:top;
  position:relative;
  width: 500px;
}
.appui-elem-hidden{
  display:none !important;
}

Javascript

(function($){
  $.fn.redraw = function(options){
    return this.each(function(){
      var ele = this,
        w = 0,
        center,
        total,
        cont,
        space,
        $lbl,
        $fld,
        $hiddenEle,
        $fieldsets = $("form", ele),
        $hiddenChildren = {};
      if ( $fieldsets.length > 0 ){
        $fieldsets.each(function(i){
          center = false, $hiddenEle = false, cont = this, $fieldset = $(this);
          $hiddenChildren = $hiddenChildren ?
            $fieldset.children(".appui-elem-hidden").removeClass("appui-elem-hidden") : 
            $hiddenChildren.add($fieldset.children(".appui-elem-hidden").removeClass("appui-elem-hidden"));

          $(".appui-form-label",cont).each(function(){
            $lbl = $(this);
            if ( $lbl.parents(":hidden").length > 0 ){
              $hiddenEle = $hiddenEle ? $hiddenEle.add($lbl.parents(":hidden").show()) : $lbl.parents(":hidden").show();
            }
            $lbl.css("width","auto");
            if ( $lbl.hasClass("appui-c") ){
              center = 1;
            }
            var w2 = $lbl.width();
            if ( w2 > w ){
              w = w2;
            }
            if ( !space ){
              $fld = $lbl.nextAll(".appui-form-field").first();
              space = ( $lbl.outerWidth(true) - $lbl.width() ) + ( $fld.outerWidth(true) - $fld.width() );
            }
          });
        });
        if ( w > 0 ){

          total = false;
          for ( var $par = $lbl.parent(); !total; $par = $par.parent() ){
            total = $par.width();
            if ( $par.width() < total ){
              total = $par.width();
            }
          }

          if ( total > 0 ){
            total -= ( 20 + space );
            if ( center ){
              var w1 = Math.round(total/2);
              w = w1;
            }
            else{
              var w1 = total - w;
            }
            $fieldsets.each(function(i){
              cont = this, $fieldset = $(this);
                $(".appui-form-label",cont).each(function(){
                  var $l = $(this);
                  $l.width(w);
                  if ( center ){
                    $l.css("text-align","right");
                  }
                  if ( $l.parent().prop("tagName").toLowerCase() === 'a' ){
                    $l = $l.parent();
                  }
                  var $f = $l.nextAll(".appui-form-field").first();
                  if ( $f.length === 0 ){
                    $f = $l.nextAll("a:first").find(".appui-form-field");
                  }
                  if ( $f.length === 0 ){
                    appui.f.log('Unable to find a corresponding field (' + $l.text() + ')');
                  }
                  else{
                    var t = $f.prop("tagName");
                    if ( t.toLowerCase() === 'div' || t.toLowerCase() === 'textarea' ){
                      $f.css("width",w1+"px");
                    }
                    else{
                      $f.css("max-width",w1+"px");
                    }
                  }
                });
            });
          }
          $hiddenChildren.addClass("appui-elem-hidden");
          if ( $hiddenEle ){
            $hiddenEle.hide();
          }
        }
      }
    });
  };
})(jQuery);

【讨论】:

    猜你喜欢
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多