EP05 | 跟著影片做前端切版練習 - Wrap Text Around Circle Shape

練習影片

練習重點

  • float : 可以讓元素沿外容器的左或右側排列,並讓文字環繞該元素。

  • shape-outside : 指定元素的外圍形狀,相鄰的的文字會依據該元素形狀去排列。

  • object-fit : 讓某些元素(如 imgvideo )可以依指定的值去填充外容器(有點類似 background-size )。

結構

HTML

1
2
3
4
5
6
7
8
9
10
11
12
<div class="circle circle-left">
<img src="https://images.unsplash.com/photo-1603632076161-5836b146638c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=600&q=60">
</div>
<div class="circle circle-right">
<img src="https://images.unsplash.com/photo-1603094366868-4098bd9e0e1b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=600&q=60">
</div>
<section class="text">
<h2>What is Lorem Ipsum?</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</p>
</section>

CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
body {
background: #212121;
margin-top: 30px;
}
.circle {
margin: 20px;
/*讓元素的外圍形狀變成圓形,而相鄰的文字會依圓形去做排列*/
shape-outside: circle(50%);
}
.circle img {
width: 100%;
height: 100%;
/*形狀變圓形*/
border-radius: 50%;
/*圖片依比例放大到充滿外容器*/
object-fit: cover;
}
.circle-left {
/*靠左*/
float: left;
width: 400px;
height: 400px;
}
.circle-right {
/*靠右*/
float: right;
width: 300px;
height: 300px;
}
.text {
color: #fff;
line-height: 1.5;
}
.text h2 {
font-size: 3em;
}

Demo:

See the Pen 前端練習05 - Wrap Text Around Circle Shape by Celeste6666 (@celeste6666) on CodePen.