设置登录表单样式(转)

表单样式基础

在所有的元素中,如果说表单元素样式的复杂性排第二,那么没有其他元素敢称第一。

基础表现一致

首先我们看下normalize.css对表单元素样式表现一致性的一个纠正情况,你就知道它是如何复杂的了。代码如下:

/* Forms
   ========================================================================== */

/**
 * Remove the margin in Firefox and Safari.
 */

button,
input,
optgroup,
select,
textarea {
  margin: 0;
}

/**
 * Show the overflow in IE.
 * 1. Show the overflow in Edge.
 */

button,
input { /* 1 */
  overflow: visible;
}

/**
 * Remove the inheritance of text transform in Edge, Firefox, and IE.
 * 1. Remove the inheritance of text transform in Firefox.
 */

button,
select { /* 1 */
  text-transform: none;
}

/**
 * 1. Prevent a WebKit bug where (2) destroys native `audio` and `video`
 *    controls in Android 4.
 * 2. Correct the inability to style clickable types in iOS and Safari.
 */

button,
html [type="button"], /* 1 */
[type="reset"],
[type="submit"] {
  -webkit-appearance: button; /* 2 */
}

/**
 * Remove the inner border and padding in Firefox.
 */

button::-moz-focus-inner,
[type="button"]::-moz-focus-inner,
[type="reset"]::-moz-focus-inner,
[type="submit"]::-moz-focus-inner {
  border-style: none;
  padding: 0;
}

/**
 * Restore the focus styles unset by the previous rule.
 */

button:-moz-focusring,
[type="button"]:-moz-focusring,
[type="reset"]:-moz-focusring,
[type="submit"]:-moz-focusring {
  outline: 1px dotted ButtonText;
}

/**
 * 1. Correct the text wrapping in Edge and IE.
 * 2. Correct the color inheritance from `fieldset` elements in IE.
 * 3. Remove the padding so developers are not caught out when they zero out
 *    `fieldset` elements in all browsers.
 */

legend {
  box-sizing: border-box; /* 1 */
  color: inherit; /* 2 */
  display: table; /* 1 */
  max-width: 100%; /* 1 */
  padding: 0; /* 3 */
  white-space: normal; /* 1 */
}

/**
 * 1. Add the correct display in IE 9-.
 * 2. Add the correct vertical alignment in Chrome, Firefox, and Opera.
 */

progress {
  display: inline-block; /* 1 */
  vertical-align: baseline; /* 2 */
}

/**
 * Remove the default vertical scrollbar in IE.
 */

textarea {
  overflow: auto;
}

/**
 * 1. Add the correct box sizing in IE 10-.
 * 2. Remove the padding in IE 10-.
 */

[type="checkbox"],
[type="radio"] {
  box-sizing: border-box; /* 1 */
  padding: 0; /* 2 */
}

/**
 * Correct the cursor style of increment and decrement buttons in Chrome.
 */

[type="number"]::-webkit-inner-spin-button,
[type="number"]::-webkit-outer-spin-button {
  height: auto;
}

/**
 * 1. Correct the odd appearance in Chrome and Safari.
 * 2. Correct the outline style in Safari.
 */

[type="search"] {
  -webkit-appearance: textfield; /* 1 */
  outline-offset: -2px; /* 2 */
}

/**
 * Remove the inner padding and cancel buttons in Chrome and Safari on macOS.
 */

[type="search"]::-webkit-search-cancel-button,
[type="search"]::-webkit-search-decoration {
  -webkit-appearance: none;
}

/**
 * 1. Correct the inability to style clickable types in iOS and Safari.
 * 2. Change font properties to `inherit` in Safari.
 */

::-webkit-file-upload-button {
  -webkit-appearance: button; /* 1 */
  font: inherit; /* 2 */
}

盒模型计算方式

不同的表单元素盒模型的计算方式不太一样,多数是默认的“content-box”,但是还有些是“border-box”,如 chrome 浏览器对按钮就设置 border-box。所以为了以后的计算方便,还是统一设置为border-box方便。

* {
    box-sizing: border-box;
}

focus 样式

webkit 内核浏览器会自动添加 focus 样式, 效果如下:

而这一般在项目中都是要去掉的,代码如下:

input,
select,
textarea,
button {
    outline: 0;
}

样式不可控

  • 单选框(radio)及复选框(checkbox)的大小,颜色等都不可控,现在一般流行使用伪元素来设置该样式,覆盖默认的丑陋样式(如),或者使用 JS 来模拟单选框及复选框
  • select 选项样式没法设置样式,所以如果不使用默认的又得用 JS 来搞
  • 上传文件(file)各个浏览器表现不一致,而且几乎不可以美化(最多只能搞成一个按钮形式)
  • placeholder IE9- 不支持,如果要兼容 IE9- 则需要 JS 来实现兼容
  • 行高及高度未解之谜。有些元素某些浏览器设置行高无效,同样有些是高度无效,所以要实现一个高度且文字垂直居中最保险的办法是设置上下padding
  • 更多 HTML 5 新加的各种表单元素的兼容问题

当然这还不是全部,所以对于表单,掌握基础的,了解些常用解决办法,能搞则搞,再不行就上 JS。

下面是一些设置样式的参考资料:

题目

posted @ 2017-06-21 13:33  Garven  阅读(219)  评论(0)    收藏  举报