Transform
CoddyのHTMLジャーニー「CSSマスタリー」セクションの一部。レッスン 23/43。
CSS transformを使うと、element の拡大縮小、回転、移動(translate)、または歪みによって、その外観を変更できます。これらの変更は、transition と組み合わせない限り、瞬時に適用されます。
<strong>scale()</strong>: element のサイズを変更する(大きくしたり小さくしたりする)。<strong>rotate()</strong>: element を degrees 単位で回転させる。
<strong>translate()</strong>: 要素を左右または上下に移動します。<strong>skew()</strong>: 要素を X または Y の axis に沿って傾けます。
例:
/* Makes the image 20% larger */
img {
transform: scale(1.2);
}/* Rotates the box 45 degrees clockwise */
.box {
transform: rotate(45deg);
}/* Moves the element 50px to the right and 100px down */
div {
transform: translate(50px, 100px);
}/* Skews the element 20 degrees on the X axis and 10 degrees on the Y axis */
div {
transform: skew(20deg, 10deg);
}チャレンジ
簡単4枚の画像を含むギャラリーがあり、それぞれにclass(img1、img2など)が付いている場合、hover時に次の変換を適用します:
- Image 1(
.img1):scale()を使用して、hover時にimageを1.2倍大きくします。 - Image 2(
.img2):rotate()を使用して、hover時にimageを45 degrees回転させます。 - Image 3(
.img3):translate()を使用して、hover時にimageをright方向へ-5px、down方向へ-3px移動します。 - Image 4(
.img4):skew()を使用して、hover時にimageをX-axis上で20 degrees、Y-axis上で10 degrees傾けます。
自分で試してみよう
<!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プレイグラウンド