Skip to content
⚠️ This article was written in 2019. Some content may be outdated.

CSS Grid Responsive Layout in Practice

There are plenty of articles online about responsive CSS Grid layouts, but most lack hands-on experience. This article shares best practices drawn from real projects.

Core Principles ​

Here is a real-world example:

css
const { sum, debounce } = require('./utils')

describe('utils', () => {
  test('sum calculates correctly', () => {
    expect(sum(1, 2)).toBe(3)
    expect(sum(-1, 1)).toBe(0)
  })

  test('debounce delays execution', () => {
    jest.useFakeTimers()
    const fn = jest.fn()
    const debounced = debounce(fn, 300)

    debounced()
    debounced()
    debounced()
    expect(fn).not.toHaveBeenCalled()

    jest.advanceTimersByTime(300)
    expect(fn).toHaveBeenCalledTimes(1)
  })
})

Rolling this pattern out across the team worked well and noticeably cut maintenance costs.

Source Analysis ​

Here's one way to implement it:

css
{% raw %}
<template>
  <div>
    <p>{{ message }}</p>
    <button @click="reverse">Reverse</button>
  </div>
</template>

<script>
export default {
  data() {
    return { message: 'Hello Vue 2' }
  },
  methods: {
    reverse() {
      this.message = this.message.split('').reverse().join('')
    }
  }
}
</script>
{% endraw %}

Keep an eye on the performance characteristics in the code above, and avoid needless computation.

Practical Application ​

Here's the implementation:

css
export default {
  props: ['items'],
  computed: {
    sorted() {
      return [...this.items].sort((a, b) => b.score - a.score)
    },
    count() {
      return this.items.length
    }
  },
  filters: {
    formatDate(val) {
      return new Date(val).toLocaleDateString('en-US')
    }
  }
}

This setup has been proven in production and runs reliably.

Best Practices ​

Let's start with a basic example:

css

MIT Licensed