简体   繁体   English

表单元素的CSS跨浏览器定位

[英]CSS cross-browser positioning of form elements

Has anyone come up with a strategy for styling form elements that works cross-browser? 有没有人想出一种跨浏览器设计样式元素的策略? For me, textfields and textareas are usually not a problem, but checkboxes , radio buttons , and selects (dropdowns) create havoc. 对我来说, textareas textfieldstextareas通常不是问题,但是checkboxesradio buttonsselects (下拉菜单)会造成严重破坏。

I don't need these things to look identical in the various browsers. 我不需要这些东西在各种浏览器中看起来都一样。 But I'd like to be able to line them up predictably. 但我希望能够将它们按预期排列。 I'd like to be able to put a checkbox below a textfield and have it line up with the left edge of the textfield . 我希望能够在textfield checkbox下方放置一个checkbox ,并使它与textfield checkbox的左边缘textfield If this works in Safari , then it doesn't in Firefox : there the checkbox ends up below the center of the textfield . 如果这在Safari ,则在Firefox中无效: checkbox结束于textfield下方。 Sometimes the vertical placement can differ by 48px or more. 有时,垂直位置可能相差48px或更多。

I have Opera on my Apple iMac , on my PC desktop computer with Windows XP Pro , and on a PC laptop with Windows 7 (in each case, the newest version of Opera ). 我在Apple iMac ,装有Windows XP Pro PC台式计算机和装有Windows 7 (在每种情况下均为Opera的最新版本)的PC笔记本电脑上Windows 7 Opera In order to position the checkboxes I had to individually target each of these machines. 为了checkboxes我必须分别针对这些机器中的每一个。

Select fields (dropdowns) give me the same headaches. Select字段(下拉列表)使我同样头疼。 Often they're okay in some browsers, but on others they end up on top of a checkbox or textfield , or 60px to the left or right. 通常在某些浏览器中它们没问题,但在另一些浏览器中,它们最终会出现在checkboxtextfield顶部,或者位于左侧或右侧的60px

The one thing I really haven't tried is CSS reset, since Eric Meyer has said he doesn't use it for forms. 我真的没有尝试过的一件事是CSS重置,因为Eric Meyer表示他不将其用于表单。 Has anyone else tried it with success? 还有其他人成功尝试过吗? Is there any reliable way to build a form with generally uniform styling? 有没有可靠的方法来构建具有大致统一样式的表单? I don't even mind identifying the user agent and delivering specific CSS . 我什至不介意识别用户代理并提供特定的CSS But when the same browsers render the elements differently on computers of different screen size, the problem becomes overwhelming. 但是,当相同的浏览器在屏幕大小不同的计算机上以不同的方式呈现元素时,问题将变得不堪重负。

Yet clearly there's an answer, because a lot of forms on the web seem to work cross-browser. 但显然有一个答案,因为网络上的许多表格似乎可以跨浏览器工作。 Can anyone give me a hand? 有人可以帮我吗?

Thanks. 谢谢。

Mark 标记

The Basic idea for customizing the form elements for better UI of it is not 100% accurate in css, it is because the form elements styling depends upon the OS in the system, it embeds the default css of the OS for the browser... 自定义表单元素以获得更好的UI的基本思想在css中并不是100%准确的,这是因为表单元素的样式取决于系统中的操作系统,它为浏览器嵌入了操作系统的默认CSS ...

For instance, the radio button, checkboxs, selects, input, textarea they all fetches default css of OS depends upon the browser... 例如, 单选按钮,复选框,选择,输入,文本区域,它们都将获取操作系统的默认CSS取决于浏览器...

Their is no solution but a making a different UI for the form element with jquery. 他们不是解决方案,而是使用jquery为form元素制作不同的UI。 Like i am currently on Jquery mobile framework, this is Touch-Optimized Web for mobile and tablets. 就像我目前在使用Jquery移动框架一样,这是针对移动设备和平板电脑的Touch-Optimized Web。 They have customized the whole UI for the form element with divs, spans, anchors etc with jquery and css3... 他们使用jQuery,css3,div,span,anchor等自定义表单元素的整个UI ...

Refer:- http://view.jquerymobile.com/1.3.1/demos/widgets/forms/ 请参阅: -http : //view.jquerymobile.com/1.3.1/demos/widgets/forms/

Form structure in html:- HTML中的表单结构:-

<fieldset data-role="controlgroup">
    <legend>Radio buttons, vertical controlgroup:</legend>
        <input name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked" type="radio">
        <label for="radio-choice-1">Cat</label>
        <input name="radio-choice-1" id="radio-choice-2" value="choice-2" type="radio">
        <label for="radio-choice-2">Dog</label>
        <input name="radio-choice-1" id="radio-choice-3" value="choice-3" type="radio">
        <label for="radio-choice-3">Hamster</label>
        <input name="radio-choice-1" id="radio-choice-4" value="choice-4" type="radio">
        <label for="radio-choice-4">Lizard</label>
</fieldset>

Form structure showing on browser:- 浏览器上显示的表单结构:-

<div class="ui-controlgroup-controls">
  <div class="ui-radio">
    <input type="radio" checked="checked" value="choice-1" id="radio-choice-1" name="radio-choice-1">
    <label for="radio-choice-1" data-corners="true" data-shadow="false" data-iconshadow="true" data-wrapperels="span" data-icon="radio-on" data-theme="c" data-mini="false" class="ui-radio-on ui-btn ui-btn-corner-all ui-fullsize ui-btn-icon-left ui-first-child ui-btn-up-c"><span class="ui-btn-inner"><span class="ui-btn-text">Cat</span><span class="ui-icon ui-icon-radio-on ui-icon-shadow">&nbsp;</span></span></label>
  </div>
  <div class="ui-radio">
    <input type="radio" value="choice-2" id="radio-choice-2" name="radio-choice-1">
    <label for="radio-choice-2" data-corners="true" data-shadow="false" data-iconshadow="true" data-wrapperels="span" data-icon="radio-off" data-theme="c" data-mini="false" class="ui-radio-off ui-btn ui-btn-corner-all ui-fullsize ui-btn-icon-left ui-btn-up-c"><span class="ui-btn-inner"><span class="ui-btn-text">Dog</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow">&nbsp;</span></span></label>
  </div>
  <div class="ui-radio">
    <input type="radio" value="choice-3" id="radio-choice-3" name="radio-choice-1">
    <label for="radio-choice-3" data-corners="true" data-shadow="false" data-iconshadow="true" data-wrapperels="span" data-icon="radio-off" data-theme="c" data-mini="false" class="ui-radio-off ui-btn ui-btn-corner-all ui-fullsize ui-btn-icon-left ui-btn-up-c"><span class="ui-btn-inner"><span class="ui-btn-text">Hamster</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow">&nbsp;</span></span></label>
  </div>
  <div class="ui-radio">
    <input type="radio" value="choice-4" id="radio-choice-4" name="radio-choice-1">
    <label for="radio-choice-4" data-corners="true" data-shadow="false" data-iconshadow="true" data-wrapperels="span" data-icon="radio-off" data-theme="c" data-mini="false" class="ui-radio-off ui-btn ui-btn-corner-all ui-fullsize ui-btn-icon-left ui-last-child ui-btn-up-c"><span class="ui-btn-inner"><span class="ui-btn-text">Lizard</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow">&nbsp;</span></span></label>
  </div>
</div>

The UI change by jquery and css3 for better looking UI what i am trying to say, simply take this as example to understand whats the idea behind the issue? 通过jquery和css3进行的UI更改使UI看起来更美观,我只是想以此为例,以了解问题背后的想法是什么?

  • Checkbox and radio buttons can't be positioned (vertically) with their labels cross-browser. 复选框和单选按钮不能被定位(垂直方向)与它们的标签跨浏览器。 You've to rely on some proprietary or CSS hack stuff. 您必须依靠一些专有的或CSS hack的东西。
    That's not as bad as it could appear: I despise using CSS hacks for layout but for translating 1px vertically a button on some browser: who seriously cares if another browser is falsely targeted and a radio button is moved 1px? 这并不像看起来的那么糟:我鄙视使用CSS hacks进行布局,而是在某些浏览器上垂直转换1px的按钮:谁在乎另一个浏览器是否被错误地定位并且单选按钮被移动了1px?

  • You still can custom the style of these form elements. 您仍然可以自定义这些表单元素的样式。 The most accessible way I could find is here: http://www.filamentgroup.com/examples/customInput/ 我能找到的最方便的方法是在这里: http : //www.filamentgroup.com/examples/customInput/
    Radio and checkbox are hidden under the background images of the labels. 单选和复选框被隐藏在标签的背景图像下。 With or without CSS and images, it still works as well as in screen readers. 无论有没有CSS和图像,它在屏幕阅读器中仍然可以正常工作。

  • I don't use CSS resets (overkill for my use, just Knacss which is very light on purpose) and for forms I use the very good work of isellsoap forms.css - Consistent looking forms across all major browsers . 我不使用CSS重置(对于我来说使用过大的杀伤力,只是出于故意而已 ),对于表单,我使用了isellsoap form.css的出色工作-在所有主要浏览器中外观一致的表单
    I tried to remove each single line of their CSS and that's how I understood how f**up modern browsers are when it comes to forms. 我试图删除CSS的每一行,这就是我了解现代现代浏览器在表单方面的情况。 The gold medal goes to Chrome, when you understand that nothing else than -webkit-appearance: none will let you style anything and that from here you've to rebuild everything from scratch (well, isellsoap did that hard work). 当您了解到-webkit-appearance: none之外,Chrome赢得了金牌-webkit-appearance: none会给您任何样式,从这里开始,您必须从头开始重建所有内容(好了,Isellsoap做到了这一艰苦的工作)。

  • If you want to align vertically on the same line a label and an input[type="text"], then stick to vertical-align: middle . 如果要在同一行上垂直对齐标签和输入[type =“ text”],则请坚持vertical-align: middle Browser and CSS will take care of alignments for you... top value is the beginning of the need of a different padding on each element you'll encounter. 浏览器和CSS将为您处理对齐方式... top价值是对您将遇到的每个元素进行不同填充的需求的开始。 Still necessary if a label goes on two lines for example. 例如,如果标签在两行上粘贴,则仍然有必要。

EDIT: I've never seen a 48px-misplacement or 60px on left/right (except with absolute positioning on IE6/7 and its buggy behaviour). 编辑:我从未见过48px错位或左/右60px(除非在IE6 / 7上具有绝对定位及其错误的行为)。 Do you've a fiddle for this and the precise browser+OS combination where it can be seen? 您是否为此感到困惑,以及可以在其中看到的精确的浏览器+ OS组合?

user agents typically render form controls differently, something i think that dom shadow and web components api is going to fix for us....at the moment you have a plethora of libs that style controls cross-browser...but the end all is formalize.css; 用户代理通常以不同的方式呈现表单控件,我认为dom阴影和Web组件api会为我们解决....目前,您拥有过多的样式来控制跨浏览器的库...但是最终是formalize.css; it supports ie6+ and the other big 4 browsers out the box, and it also provides support for five js libs, whereas 99% of these solutions are jQuery only. 它支持开箱即用的ie6 +和其他四大浏览器,并且还支持五个js库,而这些解决方案中的99%仅是jQuery。 formalize.css is the bomb. formalize.css是炸弹。 http://formalize.me/ http://formalize.me/

here is the complete css for form fields cross browser test 这是跨浏览器测试的表单字段的完整CSS

form, fieldset, legend {
    margin:0;
    padding:0;
    border:none;
    line-height:normal;
}    
input[type="checkbox"] { 
    -moz-appearance: checkbox; -webkit-appearance: checkbox;
    margin-left:3px; border:0; 
    vertical-align: middle;    
    top: -1px;bottom: 1px;
    *overflow: hidden;}
input[type="radio"] { -moz-appearance: radio; -webkit-appearance: radio; }
input[type="search"] {
  -webkit-appearance: textfield;
}
input, button, textarea, select {
    font-family: inherit;
    font-size: inherit;
    color:#000000;
    font-size: 100%; /* for IE */
    padding:6px;
    margin:5px 0;
    border-radius:4px;
     -moz-border-radius: 4px;
    -webkit-border-radius: 4px;
    -khtml-border-radius: 4px;
    *border-radius:4px; 
    -border-radius:4px; 
    border-radius:4px/8;
    -ms-border-radius: 4px;
    background:#fff;        
    text-transform:capitalize;
    box-sizing: border-box; 
    -moz-box-sizing: border-box; -webkit-box-sizing: border-box;
    -moz-appearance: none; -webkit-appearance: none;*/
}
input[type="submit"], input[type="reset"] {

    color:#fff; font-weight:bold;
    vertical-align:middle;
}
input[type="submit"]:hover, input[type="reset"]:hover {
        cursor:pointer;
}
button, input[type="submit"], input[type="image"], 
label > input[type="checkbox"]  {
    box-sizing: border-box; /* 1 */  
    *height: 13px; /* Removes excess padding in IE 7 */
    *width: 13px;
    backgorund: #fff; 
}
input[type="text"]:focus, 
input[type="password"]:focus, 
input[type="email"]:focus, 
textarea:focus, select:focus { outline:0;
}
input::-moz-focus-inner, button::-moz-focus-inner { border: 0; padding: 0; }
textarea {      
    resize:none;
    overflow: auto; /* Removes default vertical scrollbar in IE 6/7/8/9 */
    vertical-align: top; /* Improves readability and alignment in all browsers */
} 

/* placeholder and tooltip styles */
*::-webkit-input-placeholder, /* WebKit browsers */
*:-moz-placeholder, /* Mozilla Firefox 4 to 18 */
*::-moz-placeholder, /* Mozilla Firefox 19+ */
*:-ms-input-placeholder /* Internet Explorer 10+ */ {
color: #999; 
}

[placeholder]:focus::-webkit-input-placeholder,
[placeholder]:focus:-moz-placeholder { color:transparent; }


/* mantatory field  styles */
::-webkit-validation-bubble-message {
  padding: 1em;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM