Transform
CoddyのHTMLジャーニー「CSSマスタリー」セクションの一部。レッスン 23/43。
CSS transformを使うと、要素を拡大・縮小、回転、移動(平行移動)、または傾斜させて、見た目を変えることができます。トランジションと組み合わせない限り、これらの変更は瞬時に適用されます。
scale(): 要素のサイズを変更します(大きくしたり小さくしたりします)。rotate(): 要素を度数で回転させます。正の値は時計回り、負の値は反時計回りに回転します。
translate(): 要素を左右または上下に移動します。正の値は右(X)および下(Y)に移動し、負の値は左および上に移動します。skew(): X軸またはY軸に沿って要素を傾けます。
例:
/* 画像を20%大きくする */
img {
transform: scale(1.2);
}/* boxを時計回りに45度回転させる */
.box {
transform: rotate(45deg);
}/* 要素を右に50px、下に100px移動します */
div {
transform: translate(50px, 100px);
}/* 要素を X 軸方向に 20 度、Y 軸方向に 10 度傾斜させる */
div {
transform: skew(20deg, 10deg);
}チャレンジ
簡単4枚の画像を含むギャラリーで、それぞれにクラス(img1、img2など)が設定されています。ホバー時に次の変形を適用します:
- 画像 1(
.img1):scale()を使用して、ホバー時に画像を1.2倍に拡大します。 - 画像 2(
.img2):rotate()を使用して、ホバー時に画像を45度回転させます。 - 画像 3(
.img3):translate()を使用して、ホバー時に画像を右に-5px、下に-3px移動します。 - 画像 4(
.img4):skew()を使用して、ホバー時に画像をX軸方向に20度、Y軸方向に10度傾けます。
自分で試してみよう
<!DOCTYPE html>
<html>
<head>
<style>
.gallery {
display: flex;
flex-wrap: wrap; /* Allows the images to wrap into two rows */
gap: 20px; /* Space between the images */
justify-content: center; /* Centers the images */
margin-top: 20px;
}
.gallery img {
width: 200px;
height: 260px;
}
/* Write your CSS rule here */
</style>
</head>
<body>
<h1>CSS Filters Gallery Challenge</h1>
<div class="gallery">
<div class="img1">
<img src="https://upload.wikimedia.org/wikipedia/commons/c/ce/German_shepherd_dog_colt.jpg" alt="Blurred Image">
</div>
<div class="img2">
<img src="https://upload.wikimedia.org/wikipedia/commons/8/86/Dog%2C_Helgafell%2C_Sn%C3%A6fellsnes_peninsula%2C_Iceland%2C_20230505_1507_5224.jpg" alt="Brightened Image">
</div>
<div class="img3">
<img src="https://upload.wikimedia.org/wikipedia/commons/0/05/Pyreneean_Mountain_dog_guarding_sheep.jpg" alt="Grayscale Image">
</div>
<div class="img4">
<img src="https://upload.wikimedia.org/wikipedia/commons/4/4a/Fawn_and_white_Welsh_Corgi_puppy_standing_on_rear_legs_and_sticking_out_the_tongue.jpg" alt="Sepia Image">
</div>
</div>
</body>
</html>このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。
CSSマスタリーのすべてのレッスン
自分で練習してみよう: Webプレイグラウンド