金魚都能懂的網頁切版:22、23、24、25
在文字排版裡,html版面基本是一模一樣,只是我們可以運用css部分讓畫面增添更多不一樣的變化
其中有運用到幾個值得學習的css技巧。
https://codepen.io/mikeyam/pen/ExyjmZe
用偽元素做出下底線
.container h1::after{
content: '';
display: block;
height: 4px;
width: 5em;
background-color: #000;
margin-left: auto;
}
.container .text{
width: 800px;
box-sizing: border-box;
padding: 0 15px;
column-count: 2;
column-gap: 30px;
}
https://codepen.io/mikeyam/pen/NWrqjgp
改用float: left;呈現文繞圖
.container{
width: 1200px;
/* display: flex; */
margin: auto;
padding: 100px 0;
}
.container h1{
width: 300px;
text-align: right;
box-sizing: border-box;
padding: 15px;
border-right: 3px solid #000;
float: left;
margin-right: .5em;
}
.container .text{
box-sizing: border-box;
padding: 0 15px;
}
https://codepen.io/mikeyam/pen/zYBGwPm
這個版面是在文字上使用偽元素做出文字背景
並且學到使用 position: absolute; 時,寬度會不見,因此要加上 width: 100%;
.container h1::after{
content: '';
position: absolute;
/* 使用定位,寬度會不見 */
width: 100%;
bottom: 0;
display: block;
height: 70px;
background: #EEE;
z-index: -1;
}
.container .text{
box-sizing: border-box;
padding: 0 15px;
column-count: 2;
column-gap: 30px;
}
.container .text p::first-letter{
font-size: 40px;
float: left;
padding-right: 10px;
}
https://codepen.io/mikeyam/pen/zYBGMLG?editors=1100
這個版面是把文字使用定位方式,水平垂直居中
並且使用:first-child:first-letter 做出首字大寫
.container{
width: 1200px;
margin: auto;
position: relative;
margin-top: 200px;
}
.container h1{
width: 400px;
box-sizing: border-box;
padding: 15px;
background: #eee;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
display: flex;
align-items: center;
text-align: center;
font-size: 60px;
}
.container p:first-child:first-letter{
font-size: 40px;
float: left;
padding-right: 6px;
}
https://codepen.io/mikeyam/pen/gOMpExG?editors=1100
這個版面同樣是透過偽元素做出文字背景
並且學到 設定絕對定位時,會被強制轉成block
搭配transform: rotate(20deg);做出旋轉效果
.container h1{
width: 400px;
box-sizing: border-box;
padding: 15px;
float: right;
font-size: 90px;
color: #aaa;
position: relative;
}
.container h1::before{
content: '';
display: block;
position: absolute;
/* 設定絕對定位時,會被強制轉成block */
width: 200px;
height: 200px;
border: 30px solid #F1C555;
z-index: -1;
bottom: 0;
left: 60px;
transform: rotate(20deg);
}
https://codepen.io/mikeyam/pen/bGedJaX?editors=0110
這個版面先用flex做排版
在用flex-shrink: 0;,使兩個塊不會進行收縮
搭配position: absolute; 和 overflow: hidden;做出背景文字
.container{
width: 1200px;
margin: auto;
display: flex;
position: relative;
box-shadow: 0 0 30px #ccc;
justify-content: flex-end;
overflow: hidden;
}
.container h1{
width: 100%;
flex-shrink: 0;
box-sizing: border-box;
font-size: 160px;
padding-left: 15px;
color: #DDD;
position: absolute;
left: 0;
top: -60px;
line-height: 1.1em;
}
.container .text{
width: 600px;
flex-shrink: 0;
box-sizing: border-box;
column-count: 2;
column-gap: 30px;
padding: 15px;
position: relative;
z-index: 1;
}
https://codepen.io/mikeyam/pen/BazNevp?editors=0110
透過 text-shadow: 6px 6px 0px #eee; 做出文字背景
.container h1{
width: 100%;
flex-shrink: 0;
box-sizing: border-box;
font-size: 160px;
padding-left: 15px;
color: #fff;
position: absolute;
left: 0;
top: -60px;
line-height: 1.1em;
text-shadow: 6px 6px 0px #eee;
}
https://codepen.io/mikeyam/pen/oNLXrgg?editors=0110
透過偽元素做出 h1 兩條橫線
flex-grow: 1;可以自動填滿寬度
.container h1::before,
.container h1::after{
content: '';
display: block;
height: 5px;
/* width: 2em; */
flex-grow: 1;
/* 拿掉寬度,一樣可以填滿 */
background-color: #aaa;
margin: auto;
}
.container h1::before{
margin-right: 1em;
}
.container h1::after{
margin-left: 1em;
}
https://codepen.io/mikeyam/pen/jOrPjyp?editors=0100
使用偽元素搭配 border 做出文章前後的點綴效果
.container .text::before,
.container .text::after{
content: '';
position: absolute;
width: 50px;
height: 50px;
}
.container .text::before{
top: 0;
left: 0;
border-top: 3px solid #AAA;
border-left: 3px solid #AAA;
}
.container .text::after{
right: 0;
bottom: 0;
border-right: 3px solid #AAA;
border-bottom: 3px solid #AAA;
}