單頁式網頁切版 :置中方法與複習

更新 發佈閱讀 6 分鐘

※ 各式置中方法與複習:

raw-image

※ 建立center_practice專案:

raw-image

1. margin: 0 auto:

index.html

<body>
<!-- margin: 0 auto; 的置中方式 -->
<h2>1. margin: 0 auto; 的置中方式</h2>
<div class="outter">
<div class="box box1">讓我置中,拜託</div>
</div>
</body>

style.css

/* 1. margin: 0 auto;  */
/* 水平置中 */
.box1 {
/* 區塊元件 */
display: block;
/*寬度值設定 */
width: 200px;
/* 只能做水平方向的置中無法做垂直式的置中 */
margin: 0 auto;
}

成果顯示:

raw-image

2. inline 系列:

index.html

<body>
<!-- inline-block, inline 行内元素的置中方式 -->
<h2>2. inline-block, inline 行内元素的置中方式</h2>
<div class="outter outter2">
<div class="box box2">讓我置中,拜託</div>
</div>
</body>

style.css

/* 2. inline 系列 */
.outter2 {
/* 元素裡面的文字置中 ,具有繼承的設定*/
text-align: center;
}

/* 把box2視為一個文字區塊,再利用她的上一層去設定 text-align:*/
.box2 {
/*不讓文字斷行去影響元件的分行 */
display: inline-block;
/* 協調繼承的設定,讓文字偏左 */
text-align: left;
}

成果顯示:

raw-image

3. flex:

index.html

<body>
<!-- flex 彈性排版的置中方式 -->
<h2>3. flex 彈性排版的置中方式</h2>
<div class="outter outter3">
<div class="box box3">讓我置中,拜託</div>
</div>
</body>

style.css

/* 3. flex 要用到父子層的結構*/
.outter3 {
/* 告訴瀏覽器要做彈性的排版 ;要寫在父層*/
display: flex;
/* 由上至下 */
/* 重直方向居中 */
flex-direction: column;
justify-content: center;
/* flex-direction: row-reverse; */
/* 自適應內容 */
/* justify-content: center;
justify-content: space-between;
justify-content: space-around; */
/* justify-content: flex-start; */
/* justify-content: flex-end; */
/* 裡面的項目置中對齊,是垂直置中對齊 */
align-items: center;
}

.box3 {

/*上下左右置中設定 */
/* margin: auto; */
/* 自己置中對齊 */
/* align-self: center; */
}

成果顯示:

raw-image


4. position: absolute:

index.html

<body>
<!-- position: absolute 絕對定位的置中方式 -->
<h2>4. position: absolute 絕對定位的置中方式</h2>
<div class="outter outter4">
<div class="box box4">讓我置中,拜託</div>
</div>
</body>

style.css

/* 4. position: absolute; */
.outter4 {
/* 定位參考點 */
position: relative;
}

.box4 {
/* 找上一層的定位點 */
position: absolute;
/* 定位 */
top: 50%;
left: 50%;
/* 置中 ;水平或垂直的偏移*/
transform: translate(-50%, -50%);
}

成果顯示:

raw-image

5. 文字的置中 :

index.html

<body>
<!-- 文字的置中 -->
<h2>5. 文字的置中</h2>
<div class="outter">
<div class="box box5">讓我置中,拜託 </div>
</div>
</body>

style.css

/* 5. 文字的置中 */

.box5 {
/* 水平方向置中 */
text-align: center;
/* 垂直方向置中 */
/*讓它等於box高度*/
line-height: 200px;

}

成果顯示:

raw-image







留言
avatar-img
留言分享你的想法!
avatar-img
奧莉薇走在成為後端工程師之路上
23會員
167內容數
全端網頁開發專業知識分享
2025/08/24
※ 圖片放置結合商品卡範例: 商品成果範例: 建立product-card-2_example專案: index.html: <body> <!-- 網頁的最外層 --> <div class="wrapper"> <!-- 商品卡 --> <div class="prod
Thumbnail
2025/08/24
※ 圖片放置結合商品卡範例: 商品成果範例: 建立product-card-2_example專案: index.html: <body> <!-- 網頁的最外層 --> <div class="wrapper"> <!-- 商品卡 --> <div class="prod
Thumbnail
2025/08/23
※ 常見圖片放置方式介紹: 第一種: 第二種: 第三種: 第四種: ※ 討論圖片放置方式的目的: ※ Background屬性: ※ Background背景屬性設定: ※ Background使用時機: ※ Background標籤和Image標籤比較: ※ 圖片放置範例:
Thumbnail
2025/08/23
※ 常見圖片放置方式介紹: 第一種: 第二種: 第三種: 第四種: ※ 討論圖片放置方式的目的: ※ Background屬性: ※ Background背景屬性設定: ※ Background使用時機: ※ Background標籤和Image標籤比較: ※ 圖片放置範例:
Thumbnail
2025/08/16
※ 成果範例: ※ 範例實作: 1.建立navbar_example專案: 2.建立navbar:index.html <!-- Start header --> <header class="header"> <div class="navbar"> <div clas
Thumbnail
2025/08/16
※ 成果範例: ※ 範例實作: 1.建立navbar_example專案: 2.建立navbar:index.html <!-- Start header --> <header class="header"> <div class="navbar"> <div clas
Thumbnail
看更多
你可能也想看
Thumbnail
自由接案好像很自由、容易,卻需要點方向的指引,希望這篇的分享能給予你一些幫助。
Thumbnail
自由接案好像很自由、容易,卻需要點方向的指引,希望這篇的分享能給予你一些幫助。
Thumbnail
不論是平面設計、介面設計,都能見排版這一詞。而排版的作用,即在明確的設計目標中,將所有元素做有組織的安排,精確地傳達訊息。
Thumbnail
不論是平面設計、介面設計,都能見排版這一詞。而排版的作用,即在明確的設計目標中,將所有元素做有組織的安排,精確地傳達訊息。
Thumbnail
這篇文章教你如何製作側邊欄,包括準備工作、HTML和CSS的部分,還有一些互動效果。文章涵蓋了連結、圖片、超連結、大小、顏色、排版、flex和滑鼠互動等內容。
Thumbnail
這篇文章教你如何製作側邊欄,包括準備工作、HTML和CSS的部分,還有一些互動效果。文章涵蓋了連結、圖片、超連結、大小、顏色、排版、flex和滑鼠互動等內容。
Thumbnail
利用文字紀錄,明確寫下自己的採購項目......
Thumbnail
利用文字紀錄,明確寫下自己的採購項目......
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News