CSS - Responsible Web - Media Query

|

Media Query

편의상 Vue로 개발한 예제입니다.

App.vue

화면에 보이는 상자는 화면 크기의 90%만큼 동적으로 변하는 반응성 상자입니다.

<template>
  <div id="head">SnowDeer's Example</div>
  <div>Hello. SnowDeer</div>
</template>

<script>
export default {
  name: "App",
};
</script>

<style>
#head {
  width: 90%;
  height: 500px;
  margin: 0 auto;
  background: yellowgreen;
  border: 4px solid black;
  text-align: center;
}

@media all and (min-width: 500px) {
  body {
    background: teal;
  }
}

@media all and (min-width: 800px) {
  body {
    background: orange;
  }
}
</style>

실행 화면

웹 브라우저가 500px 이상이면 배경이 teal 색상으로 변경됩니다.

Image

웹 브라우저가 800px 이상이면 배경이 orange 색상으로 변경됩니다.

Image


Media Query 문법

Media Query 문법은 다음과 같습니다.

@media [only 또는 not] [미디어 유형] [and 또는 ,] (조건문) {실행문}

Vue.js 3.0 기본 레이아웃 - (2) AppBar 레이아웃

|

AppBar 레이아웃

<template>
  <div id="wrapper">
    <div id="drawer"></div>
    <div id="main">
      <div id="appbar">
        <div id="hamburg-menu" class="h-center v-center">
          <i class="bi bi-list"></i>
        </div>
        <div id="title" class="v-center">AppBar Title</div>
        <div id="action-items">
          <i class="action-item bi bi-heart"></i>
          <i class="action-item bi bi-three-dots-vertical"></i>
        </div>
      </div>
      <div id="content"></div>
    </div>
  </div>
</template>

<style>
.h-center {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.v-center {
  display: flex;
  flex-direction: row;
  align-items: center;
}

#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#wrapper {
  display: -webkit-flex;
  display: flex;
  flex-flow: row wrap;
  justify-content: flex-start;
  width: 100vw;
  height: 100vh;
  margin: 0 auto;
}

#appbar {
  display: flex;
  flex-direction: row;
  height: 48px;
  background: #edb1f1;
}

#hamburg-menu {
  width: 48px;
  height: 48px;
  background: #d59bf6;
  cursor: pointer;
}

#title {
  flex: 1;
  height: 48px;
}

#action-items {
  display: flex;
  flex-direction: row;
  height: 48px;
  background: #d59bf6;
}

.action-item {
  width: 48px;
  height: 48px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

#drawer {
  width: 200px;
  height: 100vh;
  background: #5585b5;
}

#main {
  flex: 1;
  flex-flow: column wrap;
  justify-content: flex-start;
  background: #bbe4e9;
}
</style>

image

Vue.js 3.0 기본 레이아웃 - (1) 스켈레톤 레이아웃

|

기본 스켈레톤 레이아웃

<template>
  <div id="wrapper">
    <div id="drawer"></div>
    <div id="main">
      <div id="appbar"></div>
      <div id="content"></div>
    </div>
  </div>
</template>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

#wrapper {
  display: -webkit-flex;
  display: flex;
  flex-flow: row wrap;
  justify-content: flex-start;
  width: 100vw;
  height: 100vh;
  margin: 0 auto;
}

#drawer {
  width: 200px;
  height: 100vh;
  background: #5585b5;
}

#main {
  flex: 1;
  flex-flow: column wrap;
  justify-content: flex-start;
  background: #bbe4e9;
}

#appbar {
  height: 48px;
  background: #53a8b6;
}
</style>

image

Vue.js 3.0 기본 여백(margin) 제거하기

|

Vue 기본 여백(margin) 제거하기

Vue.js로 화면을 띄우면 기본적으로 상하좌우 8px의 여백이 있습니다. 아래의 코드를 이용해서 여백을 없앨 수 있습니다.

위치는 App.vue에 넣어도 되고, public/index.html에 넣어도 되는데 후자가 더 깔끔한 위치라고 생각합니다.

<style>
body {
  margin: 0px;
}
</style>

Vue.js 3.0 Event 상위로 전달하는 방법(Emit)

|

Emit 사용 방법

emit는 컴포넌트에서 발생한 이벤트를 상위 부모에게 전달하는 키워드입니다. 다음과 같이 script 코드에서 이벤트를 발생하는 방법과 template 코드에서 이벤트를 발생시키는 2가지 방법이 있습니다.

emit 예제

<template>
  <h2>Child Component</h2>

  <button @click="onClicked">Emit from script</button>
  <button @click="$emit('event2', 2)">Emit from template</button>
</template>

<script>
export default {
  emits: ["event1", "event2"],
  setup(props, context) {
    const { emit } = context;
    const onClicked = () => {
      emit("event1", 10);
    };

    return {
      onClicked,
    };
  },
};
</script>

getCurrentInstance 사용

emitgetCurrentInstance()를 이용해서도 획득할 수 있었습니다. 기존에는 useContext를 이용해서 획득할 수 있었는데, Vue 3.2.0 버전부터 useContextdeprecated 되었습니다.

import { getCurrentInstance } from "vue";

export default {
  emits: ["event1", "event2"],
  setup() {
    const { emit } = getCurrentInstance();
    const onClicked = () => {
      emit("event1", 10);
    };

    return {
      onClicked,
    };
  },
};

<script setup> 내에서 사용하는 방법

<script setup>
import { defineEmits } from "vue";

const emit = defineEmits(["toggle-drawer"]);
const toggleDrawer = () => {
  emit("toggle-drawer");
};
</script>