仅重置/删除元素的 CSS 样式 [英] Reset/remove CSS styles for element only

查看:38
本文介绍了仅重置/删除元素的 CSS 样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这之前一定被提及/询问过,但一直在寻找一个没有运气的年龄,我的术语一定是错误的!

I'm sure this must have been mentioned/asked before but have been searching for an age with no luck, my terminology must be wrong!

我依稀记得我不久前看到的一条推文,该推文表明有一个可用的 css 规则可以删除先前在样式表中为特定元素设置的任何样式.

一个很好的使用示例可能是在移动优先的 RWD 网站中,其中用于小屏幕视图中特定元素的大部分样式需要重置"或删除桌面视图中的同一元素.

A good use example might be in a mobile-first RWD site where much of the styling used for a particular element in the small-screen views needs 'resetting' or removing for the same element in the desktop view.

可以实现以下目标的 css 规则:

A css rule that could achieve something like:

.element {
  all: none;
}

示例用法:

/* mobile first */
.element {
   margin: 0 10;
   transform: translate3d(0, 0, 0);
   z-index: 50;
   display: block;
   etc..
   etc..
}

@media only screen and (min-width: 980px) {
  .element {
    all: none;
  }
}

因此我们可以快速删除或重新设置样式,而无需声明每个属性.

So we could quickly remove or re-set styling without having to declare every property.

有意义吗?

推荐答案

CSS3 关键字 initial 设置 CSS3 属性为规范中定义的初始值.initial 关键字具有广泛的浏览器支持,除了 IE和 Opera Mini 系列.

The CSS3 keyword initial sets the CSS3 property to the initial value as defined in the spec. The initial keyword has broad browser support except for the IE and Opera Mini families.

由于 IE 缺乏支持可能会导致问题,这里有一些方法可以将某些 CSS 属性重置为其初始值:

Since IE's lack of support may cause issue here are some of the ways you can reset some CSS properties to their initial values:

.reset-this {
    animation : none;
    animation-delay : 0;
    animation-direction : normal;
    animation-duration : 0;
    animation-fill-mode : none;
    animation-iteration-count : 1;
    animation-name : none;
    animation-play-state : running;
    animation-timing-function : ease;
    backface-visibility : visible;
    background : 0;
    background-attachment : scroll;
    background-clip : border-box;
    background-color : transparent;
    background-image : none;
    background-origin : padding-box;
    background-position : 0 0;
    background-position-x : 0;
    background-position-y : 0;
    background-repeat : repeat;
    background-size : auto auto;
    border : 0;
    border-style : none;
    border-width : medium;
    border-color : inherit;
    border-bottom : 0;
    border-bottom-color : inherit;
    border-bottom-left-radius : 0;
    border-bottom-right-radius : 0;
    border-bottom-style : none;
    border-bottom-width : medium;
    border-collapse : separate;
    border-image : none;
    border-left : 0;
    border-left-color : inherit;
    border-left-style : none;
    border-left-width : medium;
    border-radius : 0;
    border-right : 0;
    border-right-color : inherit;
    border-right-style : none;
    border-right-width : medium;
    border-spacing : 0;
    border-top : 0;
    border-top-color : inherit;
    border-top-left-radius : 0;
    border-top-right-radius : 0;
    border-top-style : none;
    border-top-width : medium;
    bottom : auto;
    box-shadow : none;
    box-sizing : content-box;
    caption-side : top;
    clear : none;
    clip : auto;
    color : inherit;
    columns : auto;
    column-count : auto;
    column-fill : balance;
    column-gap : normal;
    column-rule : medium none currentColor;
    column-rule-color : currentColor;
    column-rule-style : none;
    column-rule-width : none;
    column-span : 1;
    column-width : auto;
    content : normal;
    counter-increment : none;
    counter-reset : none;
    cursor : auto;
    direction : ltr;
    display : inline;
    empty-cells : show;
    float : none;
    font : normal;
    font-family : inherit;
    font-size : medium;
    font-style : normal;
    font-variant : normal;
    font-weight : normal;
    height : auto;
    hyphens : none;
    left : auto;
    letter-spacing : normal;
    line-height : normal;
    list-style : none;
    list-style-image : none;
    list-style-position : outside;
    list-style-type : disc;
    margin : 0;
    margin-bottom : 0;
    margin-left : 0;
    margin-right : 0;
    margin-top : 0;
    max-height : none;
    max-width : none;
    min-height : 0;
    min-width : 0;
    opacity : 1;
    orphans : 0;
    outline : 0;
    outline-color : invert;
    outline-style : none;
    outline-width : medium;
    overflow : visible;
    overflow-x : visible;
    overflow-y : visible;
    padding : 0;
    padding-bottom : 0;
    padding-left : 0;
    padding-right : 0;
    padding-top : 0;
    page-break-after : auto;
    page-break-before : auto;
    page-break-inside : auto;
    perspective : none;
    perspective-origin : 50% 50%;
    position : static;
    /* May need to alter quotes for different locales (e.g fr) */
    quotes : '201C' '201D' '2018' '2019';
    right : auto;
    tab-size : 8;
    table-layout : auto;
    text-align : inherit;
    text-align-last : auto;
    text-decoration : none;
    text-decoration-color : inherit;
    text-decoration-line : none;
    text-decoration-style : solid;
    text-indent : 0;
    text-shadow : none;
    text-transform : none;
    top : auto;
    transform : none;
    transform-style : flat;
    transition : none;
    transition-delay : 0s;
    transition-duration : 0s;
    transition-property : none;
    transition-timing-function : ease;
    unicode-bidi : normal;
    vertical-align : baseline;
    visibility : visible;
    white-space : normal;
    widows : 0;
    width : auto;
    word-spacing : normal;
    z-index : auto;
    /* basic modern patch */
    all: initial;
    all: unset;
}

/* basic modern patch */

#reset-this-root {
    all: initial;
    * {
        all: unset;
    }
}

  • 2017 年 12 月相关的 github 存储库更详尽的列表
  • 相关
  • 与 MDN 相关
  • 相关的 W3C 规范
    • Relevent github repo with a december 2017 more exaustive list
    • Related
    • Related from MDN
    • Related W3C specs
    • 正如@user566245 在评论中提到的:

      As mentioned in a comment by @user566245 :

      这在原则上是正确的,但个人里程可能会有所不同.为了例如,默认情况下,某些元素(例如 textarea)具有边框,应用此重置将减少那些 textarea 的边框.

      this is correct in principle, but individual mileage may vary. For example certain elements like textarea by default have a border, applying this reset will render those textarea's border less.


      JAVASCRIPT?


      JAVASCRIPT ?

      没有人想过用css来重置css吗?是吗?

      Nobody thought about other than css to reset css? Yes?

      有一个完全相关的片段:https://stackoverflow.com/a/14791113/845310

      There is that snip fully relevant : https://stackoverflow.com/a/14791113/845310

      getElementsByTagName("*") 将返回 DOM 中的所有元素.那么你可以为集合中的每个元素设置样式:

      getElementsByTagName("*") will return all elements from DOM. Then you may set styles for each element in the collection:

      由 VisioN 于 2013 年 2 月 9 日 20:15 回答

      var allElements = document.getElementsByTagName("*");
      for (var i = 0, len = allElements.length; i < len; i++) {
          var element = allElements[i];
          // element.style.border = ...
      }
      


      说了这么多;我不认为 css 重置是可行的,除非我们最终只有一个网络浏览器..如果浏览器最终设置了默认".


      With all this said; i don't think a css reset is something feasable unless we end up with only one web browser .. if the 'default' is set by browser in the end.

      为了比较,这里是 Firefox 40.0 的值列表<blockquote style="all: unset;font-style:oblique"> 其中 font-style:oblique 触发 DOM 操作.

      For comparison, here is Firefox 40.0 values list for a <blockquote style="all: unset;font-style: oblique"> where font-style: oblique triggers DOM operation.

      align-content: unset;
      align-items: unset;
      align-self: unset;
      animation: unset;
      appearance: unset;
      backface-visibility: unset;
      background-blend-mode: unset;
      background: unset;
      binding: unset;
      block-size: unset;
      border-block-end: unset;
      border-block-start: unset;
      border-collapse: unset;
      border-inline-end: unset;
      border-inline-start: unset;
      border-radius: unset;
      border-spacing: unset;
      border: unset;
      bottom: unset;
      box-align: unset;
      box-decoration-break: unset;
      box-direction: unset;
      box-flex: unset;
      box-ordinal-group: unset;
      box-orient: unset;
      box-pack: unset;
      box-shadow: unset;
      box-sizing: unset;
      caption-side: unset;
      clear: unset;
      clip-path: unset;
      clip-rule: unset;
      clip: unset;
      color-adjust: unset;
      color-interpolation-filters: unset;
      color-interpolation: unset;
      color: unset;
      column-fill: unset;
      column-gap: unset;
      column-rule: unset;
      columns: unset;
      content: unset;
      control-character-visibility: unset;
      counter-increment: unset;
      counter-reset: unset;
      cursor: unset;
      display: unset;
      dominant-baseline: unset;
      empty-cells: unset;
      fill-opacity: unset;
      fill-rule: unset;
      fill: unset;
      filter: unset;
      flex-flow: unset;
      flex: unset;
      float-edge: unset;
      float: unset;
      flood-color: unset;
      flood-opacity: unset;
      font-family: unset;
      font-feature-settings: unset;
      font-kerning: unset;
      font-language-override: unset;
      font-size-adjust: unset;
      font-size: unset;
      font-stretch: unset;
      font-style: oblique;
      font-synthesis: unset;
      font-variant: unset;
      font-weight: unset;
      font: ;
      force-broken-image-icon: unset;
      height: unset;
      hyphens: unset;
      image-orientation: unset;
      image-region: unset;
      image-rendering: unset;
      ime-mode: unset;
      inline-size: unset;
      isolation: unset;
      justify-content: unset;
      justify-items: unset;
      justify-self: unset;
      left: unset;
      letter-spacing: unset;
      lighting-color: unset;
      line-height: unset;
      list-style: unset;
      margin-block-end: unset;
      margin-block-start: unset;
      margin-inline-end: unset;
      margin-inline-start: unset;
      margin: unset;
      marker-offset: unset;
      marker: unset;
      mask-type: unset;
      mask: unset;
      max-block-size: unset;
      max-height: unset;
      max-inline-size: unset;
      max-width: unset;
      min-block-size: unset;
      min-height: unset;
      min-inline-size: unset;
      min-width: unset;
      mix-blend-mode: unset;
      object-fit: unset;
      object-position: unset;
      offset-block-end: unset;
      offset-block-start: unset;
      offset-inline-end: unset;
      offset-inline-start: unset;
      opacity: unset;
      order: unset;
      orient: unset;
      outline-offset: unset;
      outline-radius: unset;
      outline: unset;
      overflow: unset;
      padding-block-end: unset;
      padding-block-start: unset;
      padding-inline-end: unset;
      padding-inline-start: unset;
      padding: unset;
      page-break-after: unset;
      page-break-before: unset;
      page-break-inside: unset;
      paint-order: unset;
      perspective-origin: unset;
      perspective: unset;
      pointer-events: unset;
      position: unset;
      quotes: unset;
      resize: unset;
      right: unset;
      ruby-align: unset;
      ruby-position: unset;
      scroll-behavior: unset;
      scroll-snap-coordinate: unset;
      scroll-snap-destination: unset;
      scroll-snap-points-x: unset;
      scroll-snap-points-y: unset;
      scroll-snap-type: unset;
      shape-rendering: unset;
      stack-sizing: unset;
      stop-color: unset;
      stop-opacity: unset;
      stroke-dasharray: unset;
      stroke-dashoffset: unset;
      stroke-linecap: unset;
      stroke-linejoin: unset;
      stroke-miterlimit: unset;
      stroke-opacity: unset;
      stroke-width: unset;
      stroke: unset;
      tab-size: unset;
      table-layout: unset;
      text-align-last: unset;
      text-align: unset;
      text-anchor: unset;
      text-combine-upright: unset;
      text-decoration: unset;
      text-emphasis-position: unset;
      text-emphasis: unset;
      text-indent: unset;
      text-orientation: unset;
      text-overflow: unset;
      text-rendering: unset;
      text-shadow: unset;
      text-size-adjust: unset;
      text-transform: unset;
      top: unset;
      transform-origin: unset;
      transform-style: unset;
      transform: unset;
      transition: unset;
      user-focus: unset;
      user-input: unset;
      user-modify: unset;
      user-select: unset;
      vector-effect: unset;
      vertical-align: unset;
      visibility: unset;
      white-space: unset;
      width: unset;
      will-change: unset;
      window-dragging: unset;
      word-break: unset;
      word-spacing: unset;
      word-wrap: unset;
      writing-mode: unset;
      z-index: unset;
      

      这篇关于仅重置/删除元素的 CSS 样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆