Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app" class="app">
  <section class="quiz" id="quiz" v-show="!askedForResults">    
    <div v-if="!isIdle" class="progress">{{currentIndex+1}} / {{total}}</div>   
    <h1>
      <img  alt="Can you count Specifity?" :class='["logo", !isIdle ? "mini" : ""]'src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/logos-can-you-count-specificity.svg"/>
    </h1>   
    <transition name="fade" mode="out-in">  
      <div v-if="isIdle" key="s1">
        <p><em>May The Force Be With You&hellip;</em></p>
        <button type="button" @click="start">Let's Start</button>
      </div>
      <question v-else :key="currentIndex" :v="v" :q="proposal" @answered="go" :disabled="disabled" :sp="sp"></question>
    </transition>
    <transition name="fade">
      <button v-show="shouldShowNext" type="button" @click="displayNext">Next</button>
    </transition>
    <transition name="fade">
      <button v-show="allDone" type="button" @click="showResults">Results</button>
    </transition>
  </section>
  <div class="rules" v-show="!askedForResults">
    <h2>Counting Specificity - Rules</h2>
    <ul>
      <li><img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-darth-vader.svg"/>count the number of ID selectors (#)<strong>strength value = 1&nbsp;0&nbsp;0</strong></li>
      <li><img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-boba-fett.svg">count the number of class selectors, attributes selectors, and pseudo-classes <strong>strength value = 1&nbsp;0</strong></li>
      <li><img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-stormtrooper.svg">count the number of type selectors and pseudo-elements <strong>strength value = 1</strong></li>
      <li><img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-null.svg">the universal selector (*) has no force</li>
    </ul>
    <p>The pseudo-class :not() adds no specificity by itself, only what's inside it's parentheses.</p>
    <ul class="extra">
      <li><img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-emperor-palpatine.svg"/>
        If the element has inline styling it wins <strong>strength value = 1&nbsp;0&nbsp;0&nbsp;0</strong></li>
      <li><img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-death-star.svg">
        The !important value appended to a CSS property value overrides even inline styles from the markup. <strong>strength value = 1&nbsp;0&nbsp;0&nbsp;0&nbsp;0</strong></li>
    </ul>
    <h3>Examples:</h3>
    <ul class="examples-list">
      <li>
        <div class="revealed code"><span class="type-c">section</span> <span class="type-c">p</span> > <span class="type-c">span</span></div>
        <div class="example-result">
          <div>
            <img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-darth-vader.svg">
            <span>0</span>
          </div>
          <div>
            <img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-boba-fett.svg">
            <span>0</span>
          </div>
          <div>
            <img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-stormtrooper.svg"><span>3</span>
          </div>
        </div>
      </li>
      <li>
        <div class="revealed code"><span class="type-a">#top</span> > * </div>
        <div class="example-result">
          <div>
            <img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-darth-vader.svg">
            <span>1</span>
          </div>
          <div>
            <img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-boba-fett.svg">
            <span>0</span>
          </div>
          <div>
            <img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-stormtrooper.svg"><span>0</span>
          </div>
        </div>
      </li>
      <li>
        <div class="revealed code"><span class="type-c">p</span><span class="type-b">.intro</span> > <span class="type-c">span</span></div>
        <div class="example-result">
          <div>
            <img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-darth-vader.svg">
            <span>0</span>
          </div>
          <div>
            <img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-boba-fett.svg">
            <span>1</span>
          </div>
          <div>
            <img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-stormtrooper.svg"><span>2</span>
          </div>
        </div>
      </li>
    </ul>
  </div>
  <transition name="fade2">
    <section class="score"  v-show="askedForResults">
      <h1>Your Result is</h1>
      <finalresults :finalpercentage="finalPercentage"></finalresults>
      <button type="button" @click="playAgain">Play Again</button>
    </section>
  </transition>
</div>

<script type="text/x-template" id="finalresults">
 <div>
   <div class="images">
   <img class="small" alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/rebel-alliance-2.svg">
   <img alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/rebel-alliance-2.svg">
   <img class="small" alt="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/rebel-alliance-2.svg">
   </div>
   <p>{{finalpercentage}}</p>
  </div>
</script>

              
            
!

CSS

              
                :root {
  --red: #911816;
  --yellow: #f8bf29;
  --black: rgb(64, 89, 107);
}
*, *:before, *:after {
  box-sizing: border-box;
}
html {
  font-family: Montserrat, sans-serif;
  line-height: 1.5; 
  color: var(--yellow);
  overflow-y: scroll;
	min-height: 100%;
}
body {
  background: #111;
}
.logo {
  width: 400px;
  transition: .5s .5s;
}
.mini {
  width: 200px;
}
.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.quiz {
  text-align: center;
  min-height: 38rem;
  overflow: hidden;
  display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
}

.quiz p {
  font-size: calc(1rem + .25vw + .5vh);
  padding: 0 1.5rem;
}

pre, 
.code {
  font-family:  monospace;
  font-size: calc(1.5rem + .25vw + .5vh);
  color: var(--yellow);
  margin: 2rem 0;
}

.progress {
  position: fixed;
  top: 0;
  left: 0;
  background: var(--yellow);
  color: #1d1d1d;
  padding: 0.25rem 1rem;
  font-family: monospace;
}

button {
  font-family: inherit;
  text-transform: uppercase;
  margin: 1.5rem;
  border: 2px solid;
  padding: .875rem 2rem .875rem;
  border-radius: .25rem;
  color: var(--yellow);
  letter-spacing: 2px;
  font-weight: 300;
  cursor: pointer;
  transition: .1s;
  font-size: 1rem;
  line-height: 1;
  background: transparent;
}
button:hover {
  color: #1d1d1d;
  background: var(--yellow);
  border-color: var(--yellow);
}


.score { 
  color: var(--yellow);
  z-index: 300;
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.score p {
  text-align: center;
  font-size: 3rem;
  font-weight: bold;
}
.score h1 + div {
    position: relative;
    padding: 0 2rem;
    width: 20rem;
    margin-top: 4rem;
    border: 5px solid;
    
}
.score .small {
  width: 4rem;
}
.score .images {
  position: absolute;
  top: -4.5rem;
  padding: 0 .5rem;
  background: #111; 
}
.score button:hover {
  color: #1d1d1d;
  background: var(--yellow);
  border-color: var(--yellow);
}
.correct {
  color: #1698d0;
}
.wrong {
  color: var(--red);
}
.correct {
  animation: yay 1.5s ease-in-out 1 both;
}
@keyframes yay {
  from {
    transform: scale(1);
    transform-origin: center center;
    animation-timing-function: ease-out;
  }
  50% {
    transform: scale(1.1);
    animation-timing-function: ease-out;
  }
}
.wrong {
	animation: shake-horizontal 0.8s cubic-bezier(0.455, 0.030, 0.515, 0.955) both;
}
@keyframes shake-horizontal {
  0%,
  100% {
    transform: translateX(0);
  }
  10%,
  30%,
  50%,
  70% {
    transform: translateX(-10px);
  }
  20%,
  40%,
  60% {
    transform: translateX(10px);
  }
  80% {
    transform: translateX(8px);
  }
  90% {
    transform: translateX(-8px);
  }
}
.label-ctnr {
  margin: 2rem 0 2rem;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.label-ctnr label {
  text-align: center;
  position: relative;
}
.label-ctnr strong {
  position: absolute;
  bottom: 1.5rem;
  right: -2px;
  font-size: 1.5rem;
}
label:last-child strong {
  display: none;
}
.label-ctnr img {
  height: 5rem;
  display: inline-block;
  
}
.label-ctnr input {
  display: block;
    background: transparent;
    color: inherit;
    padding: 1rem;
    margin: 1rem;
    border: 2px solid;
    width: 7rem;
    font-family: inherit;
    border-radius: 5px;
}
.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}

.fade2-enter-active, .fade2-leave-active {
  transition: all 1s 0s ;
  transform: scale(1);
}

.fade2-enter, .fade2-leave-to {
  opacity: 0;
 transform: scale(.5);
}


.revealed span {
  padding: .5rem;
}
.revealed .type-c {
  background: #e8e8e8;
  color: black;
}
.revealed .type-b {
  background: #829e83;
}

.revealed .type-a {
  background: #000000;
  color: white;
}
.rules {
  font-family: monospace;
  padding: 1rem 0 0;
  background: var(--yellow);
  color: black;
  padding: 0 calc(50% - 600px);
}

.rules h2,
.rules h3 {
  text-align: center;
}
.rules ul {
  margin: 0;
  padding: 0;
  display: flex;
  align-items: flex-start;
  list-style: none;
  font-size: .875rem;
}
.rules {
  text-align: left;
  margin-top: 1rem;
}
.rules li {
  flex: 1;
  padding: 0 0.5rem 1rem;
  align-items: flex-start;
}
.rules img {
  height: 2rem;
  margin: 0 .5rem 0 0;
  vertical-align: top;
  float: left;
}

.rules .extra {
  background: rgba(0,0,0,.1);
  padding: 1rem 0 0;
}

.rules p {
  text-align: center;
  background: black;
  color: var(--yellow);
  padding: 1rem 1rem;
  margin: 0;
}
.examples-list {
    text-align: center;
}
.examples-list .code {
  font-size: 1rem;
  margin: 0 0 1rem;
  color: inherit;
}
.example-result {
  display: flex;

  justify-content: center;
}
.example-result > div {
}
.example-result div > * {
 display: block;
text-align: center;
}
.example-result span {
  position: relative;
}
.example-result span:after {
  content: "·";
  font-weight: bold;
  position: absolute;
  right: 0; 
}
.example-result div:last-child span:after {
  content: none;
}
.example-result img {
  margin: auto;
}
              
            
!

JS

              
                String.prototype.insert = function (index, string) {
  if (index > 0) {
    return this.substring(0, index) + string + this.substring(index, this.length);
  }
  return string + this;
}

class Specificity {
  
  selectors = [
    {
      regex: /(\[[^\]]+\])/g,
      force: 'b'
    },
    {
      regex: /(#[^\#\s\+>~\.\[:\)]+)/g,
      force: 'a'
    },
    {
      regex: /(\.[^\s\+>~\.\[:\)]+)/g,
      force: 'b'
    },
    {
      regex: /(::[^\s\+>~\.\[:]+|:first-line|:first-letter|:before|:after)/gi,
      force: 'c'
    },
    {
      regex: /(:(?!not|global|local)[\w-]+\([^\)]*\))/gi,
      force: 'b'
    },
    {
      regex: /(:(?!not|global|local)[^\s\+>~\.\[:]+)/g,
      force: 'b'
    },
    {
        regex: /[\*\s\+>~]/g, 
        replace: ' ',
        force: 0
    },
    {
        regex: /:not/g, 
        replace: '    ',
        force: 0
    },
    {
        regex: /:local/g, 
        replace: '      ',
        force: 0
    },
    {
        regex: /:global/g, 
        replace: '       ',
        force: 0
    },
    {
        regex: /[\(\)]/g, 
        replace: ' ',
        force: 0
    },
    {
      regex: /([^\s\+>~\.\[:]+)/g,
      force: 'c'
    }
  ]

  constructor(selector) {
    this.parts = []
    this.typeCount = {
			'a': 0,
			'b': 0,
			'c': 0
		}
    this.selector = selector    
		this.calculate()
  }
  calculate() {
     this.selectors.forEach( (el) => {
        this.findMatch(el);
    })        
  }
  findMatch(el) {
    if (el.force === 0) {
      this.selector = this.selector.replace(el.regex, el.replace )
      
    } else {
      let matches, i, len, match, index, length;
		  if (el.regex.test(this.selector)) {
			  matches = this.selector.match(el.regex);
			  for (i = 0, len = matches.length; i < len; i += 1) {
				  this.typeCount[el.force] += 1;
				  match = matches[i];
				  index = this.selector.indexOf(match);
				  length = match.length;
				  this.parts.push({
					  selector: this.selector.substr(index, length),
					  type: el.force,
					  index: index,
					  length: length
				  });
				  this.selector = this.selector.replace(match, Array(length + 1).join(' '));
			  }
      }
    }
  }
}

Vue.config.devtools = true

const images = [
  'https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-darth-vader.svg',  
  'https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-boba-fett.svg',
  'https://s3-us-west-2.amazonaws.com/s.cdpn.io/74321/persos-stormtrooper.svg'
]

const questions = [
  'p.out',
  'a[href$=".pdf"]',
  'p:empty:last-child',
  '#form input.error',
  'input[type="email"]',
  'form input[type="submit"]',
  'section h2+p',
  'h2 ~ p',
  '#top h2 ~ p',
  ':root',
  'p:lang(en)',
  'section > *',
  '.special p:nth-child(2n+1)',
  'section:not(#intro) :last-child',
  'div[id="special"]'
]

Vue.component( 'question', {
  props: ['v', 'q', 'disabled', 'sp'],
  data() {
    return {
      answer: '',
      a: [0,0,0]
    }
  },
  computed: {
    selectorStructure() {
      let newSelector = this.q
      console.log(this.sp.selector)
      this.sp.parts.sort((a,b) => b.index - a.index).forEach( (el) => {
        newSelector =newSelector.insert(el.index+el.length, '</span>')
        newSelector = newSelector.insert(el.index, `<span class="type-${el.type}">`)
      })
      return newSelector
    },
    checked: {
      get() {
        return this.v
      },
      set(val) {
        this.a = val
      }
    },
    displaySelector() {
      return this.q
    }
  },
  template: `
<div class="question-answer">
  <div :class="['code', disabled ? 'revealed': '']" v-html="selectorStructure"></div>
  <div :class="['label-ctnr', disabled ? 'disabled': '', feedbackClass()]">
    <label :class="feedbackClass(item)" v-for="(item, i) in [0,1,2]">
      <img class="force-img" alt="" :src="images[item]">
      <input v-model="a[item]" min="0" type="number" :disabled="disabled"><strong>&middot;</strong>
    </label>
  </div>
  <button v-if="!disabled" type="button" @click="answerChecked" >Answer</button>
</div>`,
  methods: {
    answerChecked() {
      this.$emit('answered', this.a )
    },
    feedbackClass() {
      if (!this.disabled) {
        return
      }
      return this.a[0] == this.sp.typeCount['a'] && this.a[1] == this.sp.typeCount['b'] && this.a[2] == this.sp.typeCount['c'] ? 'correct' : 'wrong'
    }
  }
}
)

Vue.component( 'finalresults', {
  props: [ 'finalpercentage'],
  template: "#finalresults"
});

new Vue({
  el: '#app',
  data() {
    return {
      currentIndex: 0,
      checkedAnswers: [],
      v: [],
      score: 0,
      questions: [],
      isIdle: true,
      askedForResults: false,
    }
  },
  computed: {
    sp() {
      return new Specificity(this.questions[this.currentIndex])
    },
    total() {
      return this.questions.length 
    },
    finalPercentage() {
      return `${Math.round(this.score/this.total*100)}%`
    },
    proposal() {
      return this.questions[this.currentIndex]
    },
    selector() {
      return this.questions[this.currentIndex].selector
    },
    answer() { 
       return this.questions[this.currentIndex].answer
    },
    isAnswered() {
      return this.v.length !== 0
    },
    disabled() {
      return this.v.length !== 0
    },
    isFinished() {
      return this.currentIndex === this.total - 1
    },
    shouldShowNext() {
      return this.isAnswered && !this.isFinished
    },
    allDone() {
      return this.isAnswered && this.isFinished
    }
  },
  methods: {
    start() {
      this.questions = questions.sort(function(a, b){return 0.5 - Math.random()})
      this.isIdle = false
    },
    go(r) {
      this.v = r
      if (this.checkAnswer(r)) {
        this.score++
      }
      this.disabled = true
      this.isAnswered = true
    },
    checkAnswer(r) {
      return r[0] == this.sp.typeCount.a && r[1] == this.sp.typeCount.b && r[2] == this.sp.typeCount.c
    },
    resetStep() {
      this.isAnswered = false
      this.disabled = false
      this.v = []
    },
    displayNext() {
      this.currentIndex++
      this.resetStep()
    },
    playAgain() {
      this.score = 0
      this.start()
      this.currentIndex = 0
      this.askedForResults = false
      this.resetStep()
    },
    showResults() {
      this.askedForResults = true
      this.isIdle = true
    }
  }
})


              
            
!
999px

Console