search
HomeWeb Front-endCSS TutorialHow to set the base position of a rotated element in CSS?

如何在 CSS 中设置旋转元素的基本位置?

CSS, or Cascading Style Sheets, is a powerful tool that provides a range of effects to create beautiful, dynamic web pages. One of the most important tools in the CSS is the ability to rotate elements. Rotating elements, create a unique designs and animations that capture users' attention and help communicate the message. Here, we will explore how to set a rotated element's base placement in CSS.

CSS中的Transform属性

The Transform property allows to apply various transformations to elements, including rotation, scaling, and skewing. When a transform is applied to an element, the base location of the element changes, making it difficult to position the element correctly.

Rotate, scale, skew and translate are sub-properties of the transform property. Here, we will focus on the rotated sub-property. Rotate property allows to rotate an element around a fixed point on the page.

在CSS中的元素旋转

To rotate an element in CSS, the transform property is used with the rotate() function.

Syntax

<style>
   scc-selector{
      transform: rotate(angle);
   }
</style>

在这里,"angle" 是以度为单位指定要应用于元素的旋转量。

For example, the following code will rotate an element by 30 degrees −

.rotate {
   transform: rotate(30deg);
}

Rotated element's position is adjusted according to the rotation angle. This can cause the element to shift from its original position, which can be problematic to keep it properly.

CSS中可用的旋转变换类型

在CSS中有各种类型的旋转变换可用,如rotate()、rotateX()、rotateY()和rotateZ()。rotate()函数围绕元素的中心点旋转,而rotateX()和rotateY()分别围绕元素的水平和垂直轴旋转。rotateZ()函数围绕元素的z轴旋转,该轴垂直于屏幕。

设置旋转元素的基础位置的重要性

  • The base position of the rotated element determines where it is anchored in relation to its container. By default, the base placement is set to the center of the element. However, the base placement can be adjusted using the transform-origin property. This is important because it can affect how the element is positioned on the page.

  • 使用 transform-origin 属性来调整旋转元素的基本位置

  • The transform-origin property can be used to adjust the base position of a rotated element. This property specifies the point around which an element is rotated. By default, the base placement is the center of the element, which means the element is rotated around its center point.

  • To adjust the base placement, we can set the transform-origin property to a different value.

The following code will set the base placement to the top left corner of the element −

.placed {
   transform-origin: top left;
   transform: rotate(30deg);
}

Let's take a look at some examples of setting a rotated element's base placement in CSS.

示例1:围绕其中心旋转一个正方形

在这个例子中,我们使用transform属性将正方形旋转30度,基准位置默认设置为中心。

<!DOCTYPE html>
<html>
<head>
   <style>
      body { text-align: center; }
      .outer-line {
         position: relative;
         height: 200px;
         width: 200px;
         margin: auto;
         padding: 5px;
         border: 4px solid #fbf;
      }
      #box {
         width: 100px;
         height: 100px;
         background-color: red;
         transform: rotate(30deg);
      }
   </style>
</head>
   <body>
      <h3 id="The-square-is-rotated-by-degrees-around-its-center-point">The square is rotated by 30 degrees around its center point</h3>
      <div class="outer-line">
         <div id="box"></div>
      </div>
   </body>
</html>

Example 2: Rotating an element around its bottom-right corner

In this example, we rotate the square by 30 degrees using the transform property, and then set the transform-origin property to bottom right.

<html>
<head>
   <style>
      body { text-align: center; }
      .outer-line {
         position: relative;
         height: 200px;
         width: 200px;
         margin: auto;
         padding: 5px;
         border: 4px solid #fbf;
      }
      #box {
         width: 100px; height: 100px;
         background-color: red;
         transform: rotate(30deg);
         transform-origin: bottom right;
         position: absolute;
         bottom: 50px;
         right: 50px;
      }
   </style>
</head>
   <body>
      <h3 id="The-square-is-rotated-by-degrees-around-its-bottom-right-corner">The square is rotated by 30 degrees around its bottom-right corner</h3>
      <div class="outer-line">
         <div id="box"></div>
      </div>
   </body>
</html>

Example 3: Rotating animation that changes the element's base placement

In this example, we create a square with a red background color in a container and set its initial position using position: absolute, top, and left values. We also set its width and height to 100px, and then use the animation property to apply a keyframe animation called rotate. This animation runs for 2 seconds and repeats infinitely.

最后,这个动画创建了一个旋转效果,其中元素的基本位置从中心变为左上角,然后变为右下角,最后又回到中心。

<html>
<head>
   <style>
      body { text-align: center; }
      .container {
         position: relative;
         height: 200px;
         width: 200px;
         margin: auto;
         padding: 5px;
         border: 4px solid #fbf;
      }
      .sqr {
         position: absolute;
         top: 50px;
         left: 50px;
         width: 100px;
         height: 100px;
         background-color: red;
         animation: rotate 2s infinite;
      }
      @keyframes rotate {
         0% {
            transform: rotate(0deg);
            transform-origin: center;
         }
         50% {
            transform: rotate(180deg);
            transform-origin: top left;
         }
         100% {
            transform: rotate(360deg);
            transform-origin: bottom right;
         }
      }
   </style>
</head>
   <body>
      <h3 id="Rotating-animation-that-changes-the-element-s-base-placement">Rotating animation that changes the element's base placement</h3>
      <div class="container">
         <div class="sqr"></div>
      </div>
   </body>
</html>

Conclusion

在这里,我们讨论了如何使用CSS旋转元素。通过按照本文中给出的示例,我们可以确保旋转的元素在不同设备上的位置正确且一致。

The above is the detailed content of How to set the base position of a rotated element in CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Blue Beanie Day 2019Blue Beanie Day 2019Apr 13, 2025 am 09:25 AM

November 30th, the official "Blue Beanie Day," has come and gone. I'm not sure I ever grokked the exact spirit of it, but I've written about what it means to

Raw GraphQL QueryingRaw GraphQL QueryingApr 13, 2025 am 09:22 AM

GraphQL has all kinds of awesome tooling built around it. But like everything on the web, it ultimately comes down to data shootin' across the ol' network and

PSA: It's That Time to Update the Copyright Year on Your SitePSA: It's That Time to Update the Copyright Year on Your SiteApr 13, 2025 am 09:21 AM

Every year about this time I see articles going around reminding people how to update the copyright on their websites. Usually somewhere in the footer. You

Embedded Content in MarkdownEmbedded Content in MarkdownApr 13, 2025 am 09:12 AM

Markdown supports HTML, so if you need to, say, embed a YouTube video, you can just copy and paste the embed code from them, drop it into a Markdown document,

What We're Reading, 2019What We're Reading, 2019Apr 13, 2025 am 09:10 AM

There are so, so, so (so) many things to read out there on the internet. So many, in fact, that it's difficult to keep up with everything.

How We Tagged Google Fonts and Created goofonts.comHow We Tagged Google Fonts and Created goofonts.comApr 12, 2025 pm 12:02 PM

GooFonts is a side project signed by a developer-wife and a designer-husband, both of them big fans of typography. We’ve been tagging Google

Timeless Web Dev ArticlesTimeless Web Dev ArticlesApr 12, 2025 am 11:44 AM

Pavithra Kodmad asked people for recommendations on what they thought were some of the most timeless articles about web development that have changed their

The Deal with the Section ElementThe Deal with the Section ElementApr 12, 2025 am 11:39 AM

Two articles published the exact same day:

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)