# center with "transform"

## 重點 <a href="#keys" id="keys"></a>

```css
.centered-element {

  margin: 0;
  
  /* ⭐️  child 跟隨 parent 的坐標系   */
  position: absolute;
  
  /* ⭐️  垂直水平置中   */
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
```

## 程式碼 <a href="#code" id="code"></a>

{% tabs %}
{% tab title="Result" %}
{% embed url="<https://codepen.io/lochiwei/pen/KKmrGLP>" %}

{% endtab %}

{% tab title="CSS" %}

```css
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
} 

.container {
  
  /* ⭐️ 1. parent 設定坐標系 */
  position: relative;
  
  height: 100vh;
  border: 2px solid #006100;
  background-color: orange;
}

.centered-element {
  margin: 1em;;
  padding: 1em;
  border: 1px solid black;
  background-color: rgba(255,255,255, 0.5);
  
  /* ⭐️ 2. child 跟隨 parent 的坐標系   */
  position: absolute;
  
  /* ⭐️ 3. 垂直置中   */
  top: 50%;
  transform: translateY(-50%);
}
```

{% endtab %}

{% tab title="HTML" %}

```markup
<div class="container">
  <div class="centered-element">
    <p>I'm a Camper, and I'm vertically centered. I'm a Camper, and I'm vertically centered. I'm a Camper, and I'm vertically centered. I'm a Camper, and I'm vertically centered. I'm a Camper, and I'm vertically centered. I'm a Camper, and I'm vertically centered.</p>
  </div>
</div>
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/css/layout/center/transform.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
