---
description: 這篇主要簡單帶過幾種置中的方式：
---

# 置中篇  - 範例

## 範例１、Display - Flex

{% tabs %}
{% tab title="HTML" %}
```markup
    <div class="main">
        <div class="box"></div>
    </div>
```
{% endtab %}

{% tab title="CSS" %}
```css
        *,*::after,*::before
        {

            box-sizing: border-box;
        }
        body{
            margin: 0;
            padding: 0;
        }
        .main{
            position: relative;
            height: 100vh;
            background-color: #555;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .box{
            position: relative;
            height: 200px;
            width: 200px;

            background-color: plum;
        }
```
{% endtab %}
{% endtabs %}

## 範例２、Display - Grid

{% tabs %}
{% tab title="HTML" %}
```markup
    <div class="main">
        <div class="box"></div>
    </div>
```
{% endtab %}

{% tab title="CSS" %}
```css
        *,*::after,*::before
        {

            box-sizing: border-box;
        }
        body{
            margin: 0;
            padding: 0;
        }
        .main{
            position: relative;
            height: 100vh;
            background-color: #555;
            display: grid;

        }
        .box{
            position: relative;
            height: 200px;
            width: 200px;
            justify-self: center;
            align-self: center;
            background-color: plum;
        }
```
{% endtab %}
{% endtabs %}

### 範例２－１

{% tabs %}
{% tab title="HTML" %}
```markup
    <div class="main">
        <div class="box"></div>
    </div>
```
{% endtab %}

{% tab title="CSS" %}
```css
        *,*::after,*::before
        {

            box-sizing: border-box;
        }
        body{
            margin: 0;
            padding: 0;
        }
        .main{
            position: relative;
            height: 100vh;
            background-color: #555;
            display: grid;
        }
        .box{
            position: relative;
            height: 200px;
            width: 200px;
            margin: auto;
            background-color: plum;
        }
```
{% endtab %}
{% endtabs %}

### 範例２－２

{% tabs %}
{% tab title="HTML" %}
```markup
    <div class="main">
        <div class="box"></div>
    </div>
```
{% endtab %}

{% tab title="CSS" %}
```css
        *,*::after,*::before
        {

            box-sizing: border-box;
        }
        body{
            margin: 0;
            padding: 0;
        }
        .main{
            position: relative;
            height: 100vh;
            background-color: #555;
            display: grid;
        }
        .box{
            position: relative;
            height: 200px;
            width: 200px;
            margin: auto;
            background-color: plum;
        }
```
{% endtab %}
{% endtabs %}

## 範例３position -relative

{% tabs %}
{% tab title="HTML" %}
```markup
    <div class="wrap">
        <div class="box"></div>
    </div>
```
{% endtab %}

{% tab title="CSS" %}
```css
        body {
            margin: 0;
            padding: 0;
            background-color: #555;
        }

        .wrap {
            position: relative;
            width: 1200px;
            height: 100vh;
            margin-left: auto;
            margin-right: auto;
            background-color: #aaa;
        }

        .box {
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            margin: auto;
            position: fixed;
            width: 200px;
            height: 200px;
            background-color: #fff;

        }
```
{% endtab %}
{% endtabs %}

### 範例３－１

{% tabs %}
{% tab title="HTML" %}
```markup
    <div class="wrap">
        <div class="box"></div>
    </div>
```
{% endtab %}

{% tab title="CSS" %}
```css
        body {
            margin: 0;
            padding: 0;
            background-color: #555;
        }

        .wrap {
            position: relative;
            width: 1200px;
            height: 100vh;
            margin-left: auto;
            margin-right: auto;
            background-color: #aaa;
        }

        .box {
            position: fixed;
            left: 50%;
            top: 50%;
            transform:translate(-50%,-50%);
            width: 200px;
            height: 200px;
            background-color: #fff;
        }
```
{% endtab %}
{% endtabs %}

### 範例３－２

{% tabs %}
{% tab title="HTML" %}
```markup
    <div class="main">
        <div class="box"></div>
    </div>
```
{% endtab %}

{% tab title="CSS" %}
```css
        *,*::after,*::before
        {
            box-sizing: border-box;
        }
        body{
            margin: 0;
            padding: 0;
        }
        .main{
            position: relative;
            height: 100vh;
            background-color: #555;
        }
        .box{
            position: relative;
            height: 200px;
            width: 200px;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            background-color: plum;
        }
```
{% endtab %}
{% endtabs %}
