Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HTML5 Inputs and CSS3 Selectors

HTML5 Inputs and CSS3 Selectors

Covering some CSS3 selectors and their use with HTML input elements. Tips and tricks on using pseduo-classes on form elements.

Presented at Adobe WebUP, February 2012.

Razvan Caliman

February 23, 2012
Tweet

More Decks by Razvan Caliman

Other Decks in Programming

Transcript

  1. ¡  E  +  F   §  Matches  any  F  element

     immediately  preceded  by  an  E  element   ¡  E  >  F   §  Matches  any  F  element    that  is  a  direct  descendant  of  an  E  element   §  More  specific  than  all  descendants  (  E    F  )      
  2. ¡  E[att  =  “val”]   §  Matches  any  E  element

     whose  att  attribute  value  is  “val”   ¡  E[att  ^=  ”val”]   §  Matches  any  E  element  whose  att  attribute  value  begins  with  “val”   ¡  E[att  $=  “val”]   §  Matches  any  E  element  whose  att  attribute  value  ends  with  “val”   ¡  E[att  *=  ”val”]   §  Matches  any  E  element  whose  att  attribute  value  contains  the  substring  “val”  
  3. /* target all text inputs with the name attribute value

    of “first_name” */ input[type = “text”][name = “first_name”]{ . . . }
  4. /* target all <div> elements which have a single class

    starting with “widget_” */ div[class ^= “widget_”] { . . . }
  5. /* target all <a> elements which point to a secure

    URI */ a[href ^= “https://”] { . . . }
  6. /* target all inputs which have the “data-property” attribute set

    */ input[data-property] { . . . } <input type=“text” data-property=“value” />
  7. /* target all inputs which have the “data-property” attribute set

    to “5” */ ul[data-count = “5”] { . . . } <ul id=“items” data-count=“5”> . . . <ul>
  8. // Set the data-count attribute from JS var list =

    document.getElementById(“items”); list.dataset[“count”] = 5;
  9. ¡  Select  elements  based  on  their  attributes   §  input[type=“text”]

      §  a[href  ^=  “https”]   §  img[src  $=  “jpg”]   ¡  Support  for  custom  attributes  in  CSS   §  input[data-­‐count]    
  10. ¡ State     :focus   :checked   :disabled   :invalid

        ¡ Structure   :first-­‐child   :last-­‐child   :nth-­‐child(n)   :not(selector)    
  11. .details{ display : none } /* toggle the visibility of

    an element with class “details” which follows a checkbox */ input[type = “checkbox”]:checked + .details{ display : block }
  12. /* target all disabled inputs */ input:disabled { . .

    . } <input type=“text” disabled /> The same as <input type=“text” disabled=“disabled” />
  13. /* target all invalid inputs */ input:invalid { . .

    . } /* target all invalid inputs that are required */ input[required]:invalid { . . . }
  14. /* target the first input node */ input:first-child{ . .

    . } /* target the last input node */ input:last-child{ . . . }
  15. /* target odd fieldset nodes */ fieldset:nth-child(odd){ } /* target

    even fieldset nodes */ fieldset:nth-child(even){ }
  16. /* target all inputs that do not have the “required”

    attribute */ input:not([required]){ . . . }
  17. ¡  Pseudo-­‐class  selectors  (state  /  structure)   §  checked  

    §  required   §  :not(:checked)   ¡  Presentation  logic  in  CSS  based  on  state  
  18. ¡  http://www.browserscope.org   §  Distributed  testing  by  crowdsourcing   § 

    Every  visitor  runs  the  the  tests  in  the  background   §  Write  your  own  JS  tests   §  Graphs,  feature  support  evolution  
  19. ¡  http://www.browserscope.org/user/tests/ table/ agt1YS1wcm9maWxlcnINCxIEVGVzdBib2KQ GDA   ¡  Up  to  date

     metrics  on  feature  support   ¡  Desktop  and  mobile  browser  coverage  
  20. ¡  http://dochub.io/#css/   §  Quick  property  reference   §  CSS

     /  DOM  /  JS  …   §  Built  on  Twitter  Bootstrap  J  
  21. ¡  Never  consistent   ¡  Vendor-­‐specific  styles   ¡  Device-­‐specific

     optimizations   ¡  Leave  it  to  the  environment  (iOS  /  Android)    
  22. §  style  the  <label>  on  top  of  the  input  

    §  background  for  <label>   §  style  <label>  based  on  input  state   ¡  Advantages:   §  Defer  usability  and  accessibility  to  the  browser  
  23. input[type = “checkbox”]{ position: absolute; left: -9999px; } input[type =

    “checkbox”]:checked + label{ background-image: url(“checked.png”) } <!– the element order is important --> <input type=“checkbox” id=“check1” /> <label for=“check1” > Newsletter </label>
  24. div:not(#foo)  >  input:checked  +  label   ¡  CSS  engines  MUST

     parse  the  entire  rule  in   order  to  apply  it  
  25.   ¡  BUG  IE  7:  partial  rule  matching   § 

    Workaround:  use  selector  descent   §  <body  class=“ie7”>     ¡  http://lea.verou.me/2011/05/rule-­‐filtering-­‐ based-­‐on-­‐specific-­‐selectors-­‐support/  
  26. §  wrap  a  <div>  around  the  input   §  reset

     the  <input>  border,  margin,  padding   §  style  the  <div>    
  27. ¡  JS  solution  requirements:   §  Use  a  classic  <select>

     element  as  a  datasource   §  Keyboard  navigation   §  Multi-­‐instance  support   §  Augment!  Augment!  Augment!   ¡  http://harvesthq.github.com/chosen/    
  28. I skate to where the puck is going to be,

    not where it has been. Wayne Gretzky http://www.flickr.com/photos/tazphotos/4400220464/  
  29. thanks!   Razvan  Caliman   [email protected]     now,  go

     build  something  cool!   http://speakerdeck.com/u/razvancaliman/p/ html5-­‐inputs-­‐and-­‐css3-­‐selectors