Creating a rotating triangle in CSS from the middle of the screen

Is it possible to create a spinning triangle that rotates precisely from the center?

Check out this Codepen for an example

Here is the code snippet:

<div class="loader-wrapper">
    <div class="loader"></div>
</div> 

And here is the CSS code:

.loader-wrapper {
  width: 100%;
  height: 100vh;
  background-color: #11e;
}

@keyframes load {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.loader {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 100px 173.2px 100px;
  border-color: transparent transparent #007bff transparent;
  animation: 4s linear 0s infinite load;
}

Answer №1

If you're looking for a complete solution, check out this example.

In order to achieve the desired effect, it's important to adjust the transform origin to align with the center of the triangle (which is typically around 66.66% based on calculations).

Here's how you can structure your HTML:

<div class="loader">
    <div class="loader-wrapper">
        <div class="triangle"></div>
    </div>
</div>

And here's the corresponding CSS code:

.loader {
  display: inline-block;
  position: relative;
  width: 100%;
  height: 300px;
}

.loader-wrapper {
  display: block;
  position: absolute;
  top: 50%;
  left: 50%;
  /* translate by half of its width and height */
  transform: translate(-50%, -50%);
}

.triangle {
  display: block;
  position: relative;
  width: 0;
  height: 0;
  border-color: transparent transparent #e44750 transparent;
  border-width: 0px 100px 173.20508076px 100px;
  border-style: solid;
  transform-origin: 50% 66.66%;
  animation: spin 3s infinite linear;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

Answer №2

If you want to adjust the origin of the transformation point, you can utilize the transform-origin property. Simply include transform-origin: 107px 111px; in your .loader class.

However, some fine-tuning may be necessary to achieve the desired result.

Answer №3

Give this a shot:

.spinner {
  position: relative;
  top: 50%;
  transform: translate(0, -50%);
  margin: auto;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 100px 150px 100px;
  border-color: transparent transparent #ff4500 transparent;
  animation: 4s linear 0s infinite spin;
}

CodePen

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Angular 2 to toggle the visibility of a component when hovering over and moving away from it

I have developed a custom filtering component in Angular 2, which I have integrated into the table header. My goal is to show the filter when the mouse hovers over a specific span, and hide it when the mouse moves away. <th> <span class="headCol ...

Can the @keyframes in CSS3 be modified?

Exploring CSS3 animations has been a fun project for me, and I want to push the boundaries of what is possible. Currently, I have an infinite animation cycle that is working perfectly. However, I'm curious if it's possible to dynamically change ...

What is the best way to update only the fields in a user's profile that they have filled out in the form?

I am having an issue updating user profile details using my form, as it only works when all input fields are filled. How can I update only the profile details that are filled in the form? The form allows for updating 4 fields: Name, Email, Age, and Passwo ...

The magical unveiling of words through the art of animation

After spending considerable time learning how to code, I find myself seeking assistance in creating a specific animation. For the past two days, I've been struggling to bring my vision to life (see attached image). Unfortunately, my current knowledge ...

wunderground compass tool indicating the direction of the wind

I am using weather jQuery from the Wunderground API, and my jQuery is working well. However, the problem lies in the Wunderground API wind direction degrees. I want to display the wind degrees on a compass, and I found this answer on StackOverflow: CSS: ...

Tips for keeping table row heights consistent while scrolling in Firefox

I am dealing with an issue on my HTML table where the headers are static and the rest of the body scrolls within a fixed size window. The problem arises when the number of rows is less than what fits in the scrolling window, causing the row size to automat ...

What is the best way to align this form perfectly in Bootstrap 4?

HTML <div class="container-fluid background"> <div class="row"> <!-- background effect --> <div id="particles-js"> <div class="login col-12"> <form class="login-form" method="post" action=""> ...

Having issues with the presentation of full_numbers pagination in IE while using jQuery dataTables

I am currently utilizing the DataTables jQuery plugin (found at ) with the initialization code below: $('#reconcile_table').dataTable( { 'bSort' : true, 'bFilter' : true, 'bSortClasses' : false, &a ...

Issues with HTML Anchor Buttons

I'm in the process of adding a button towards the lower section of my website. Here is the HTML code I've used: <center><a href="#" id="btn1" class="btn1"> View </a></center> Now, let's take a look at the CSS ...

Maintaining columns using HTML::TextToHTML

I've been using a Perl script that utilizes HTML::TextToHTML to convert text into HTML. Some of the text I'm working with contains quasi-tables where alignment is crucial. For instance: Job no Description Completed 15 Paving ...

Using jQuery's .html() method will remove the selected attribute from the dropdown menu option

When generating HTML dynamically, a select element is included. After choosing an option inside it, everything works fine. For example: <div id="test"> <select> <option value="1">1</option> <option value="2" selec ...

CSS Animation Effect on Transparent PNG Image

Today, while working on my website, an interesting CSS effect came to mind. I vividly remember seeing it a few months ago, but unfortunately, I couldn't find it among my bookmarks. The website featured a captivating design with a prominent logo that ...

I'm having difficulty integrating boostrap with my react application due to issues with the css-loader

In my React index.js file, I am importing Bootstrap and using Webpack 4: import React from 'react'; import ReactDOM from 'react-dom'; import './assets/css/index.css'; import App from './App'; import * as serviceWork ...

The alignment of the content within a div is mismatched compared to a div that doesn't have any

Can you figure out why the element with content is out of order compared to the other elements without content in the code below? This issue seems to disappear when all elements have content. Any insights on this peculiar behavior? (Thank you in advance fo ...

Hiding the +/- button in Vue.js based on specific conditions

Is there a way to hide the +/- button when there are no items below? Current Challenge Example Sandbox Link <template v-for="item in SevenLengthlist" :key="item"> <ul> <li><input type="checkbox" id ...

Exploring the world of CSS font families in Sublime Text 3 and linking them to the web

Is there a way to connect a font from the internet (such as those on Google Fonts: ) to my Sublime Text CSS file? I have tried linking it to an HTML file that is already linked to my CSS code, but it hasn't been successful. ...

Cart filled with desired items but no payment necessary

Hey there! I'm currently working on building a static website using HTML and CSS. While I don't have knowledge of Javascript or PHP, I am able to incorporate jQuery into websites by simply copying and pasting the code in the right place. I was w ...

What is the best way to conceal a `div` containing an error message if there are no errors present?

On this page, you'll find a server-side and client-side validation form. I'm currently using a div id=er> to display error messages when the requirements are not met. However, having the div element always present is causing styling issues for ...

Changing the color of a div's text based on its content with CSS

I am facing a challenge in styling the text inside a “div” element using a Javascript function. Initially, when the page loads, these “divs” contain no value. The values of the "divs" will change between "Open" and "Closed" twice a day when the Jav ...

Displaying three tables in each row using ng-repeat, with the ability to repeat the process multiple times

Is it possible to repeat a table multiple times with each set of three tables in the same line using AngularJS and Bootstrap? I am unsure how this can be achieved with ng-repeat. table 1 | table 2 | table 3 table 4 | table 5 | ... <div ng-repeat="zo ...