"Place text at the center of a div that is rotated

I'm facing the challenge of centering a heading both vertically and horizontally within a div that has been rotated 45 degrees (transform:rotate(45deg);). Due to this rotation, I have attempted to counteract it by rotating the heading in the opposite direction (transform:rotate(-45deg);) and then applying typical centering techniques, but without success. What would be the solution for achieving this?

#wrap {
position: relative;
transform: rotate(45deg);
top: 150px;
background-color: blue; 
height: 300px;
width: 300px;
margin:0 auto;
}

h1 {
    position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transform: rotate(-45deg);
}
<html>
  <body>
    <div id="wrap"><h1>some centered text</h1></div>
  </body>
</html>

Answer №1

Within your h1 tag, you have defined the following style:

h1 {
    ...
    transform: translate(-50%, -50%);
    transform: rotate(-45deg);
}

The issue here is that by overriding the initial transform property with the rotate(), you are losing the centering effect achieved by the negative translate(). It's recommended to chain both transformations together like this:

h1 {
    position: absolute;
    top: 50%;
    left: 50%;
    margin: 0;
    transform: translate(-50%, -50%) rotate(-45deg);
}

Additionally, make sure to remove the default margin that is applied to the h1 element (try removing it in the demo to see the impact).

For a visual example, visit: http://codepen.io/anon/pen/jWjxeW?editors=1100

Answer №2

It is recommended to include multiple transform functions one after the other.
A slight modification was made to your CSS, including the addition of text-align: center;

transform: rotate(-45deg) translate(0, -100%);

#wrap {
position: relative;
transform: rotate(45deg);
top: 150px;
background-color: blue; 
height: 300px;
width: 300px;
margin:0 auto;
}

h1 {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: rotate(-45deg) translate(0, -100%);
    text-align: center;
}
<html>
  <body>
    <div id="wrap"><h1>some centered text</h1></div>
  </body>
</html>

Answer №3

try out this CSS snippet for rotation and centering:

transform: translate(-50%, -50%) rotate(-45deg);

#wrap {
position: relative;
transform: rotate(45deg);
top: 150px;
background-color: blue; 
height: 300px;
width: 300px;
margin:0 auto;
}

h1 {
    position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) rotate(-45deg);
}
<html>
  <body>
    <div id="wrap"><h1>centered text here</h1></div>
  </body>
</html>

Answer №4

To accomplish this, simply nest your h1 within another div

#wrap {
  position: relative;
  transform: rotate(45deg);
  top: 150px;
  background-color: blue;
  height: 300px;
  width: 300px;
  margin: 0 auto;
}
#text {
  position: absolute;
  height: 300px;
  width: 300px;
  transform: translate(-50%, -50%);
  transform: rotate(-45deg);
  background-color: red;
}
h1 {
  text-align: center;
  line-height: 300px;
  margin: 0; /* H1 has default margin, read more: https://www.w3.org/TR/html-markup/h1.html */
}
<html>

<body>
  <div id="wrap">
    <div id="text">
      <h1>some centered text</h1>
    </div>
  </div>
</body>

</html>

Answer №5

If you're looking to set a specific height and width for your h1 elements, try using the following CSS code:

h1 {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: rotate(-45deg);
    height: 100px;
    line-height: 35px;
    width: 130px;
    margin-top: -50px;
    text-align: center;
    margin-left: -65px;
}

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

Color the text differently if the values are not the same (CSS and jQuery only)

In my code snippet below, you can see a set of HTML elements with specific classes and values: <div class="verizon-data-duplicate-device-key-89148000004605691537 k-content k-state-active" style="float: left; opacity: 1; display: block; ...

SVG Mask not functioning properly with SVGs created in Illustrator (in all web browsers)

Alright, let's get to it - I've got a couple of SVGs here. The first one is a blue circle and the second one is a map of the United States. (Both of these were created in Illustrator) Here's what I want to do: I want the map to serve as a ...

After refreshing the page, RouterLinkActive in Angular 6 fails to work

Scenario In my application, there is a menu with various items. The selected item is distinguished by adding the selected class to it, which changes its appearance. Problem While navigating between routes works smoothly, the issue arises when the page ...

Embedding a video element results in an unexpected increase in height

When I insert an HTML5-Video element into a div, it results in the wrapper having a height larger than the video itself. The wrapper is 7px taller than the actual video source, without any min-height set. Check it out! (Scroll down to the Video) The vide ...

Loading indicator for buttons

Issue with submit button onclick function (onClick="mandatoryNotes()"). It is taking a while to load, so a preloader script was added. The preloader is now working, but the function is not being called. Seeking assistance. Thank you. function mandatoryN ...

Implement a fullscreen hero image on a specific blogroll template page in Wordpress

My portfolio website uses custom page templates for each page. The blog page is not the landing page, but an interior page. I have been able to add a custom hero image to every page template except for the blog page. Currently, I am using the content.php p ...

What is the best method to deallocate and remove the background-color property of CSS once it has been defined?

There is a perplexing issue that I am unable to resolve. The "Shop" section on this page has an intriguing translucent blue-green background: This background is set using the ID "#left-area". Interestingly, on another page, which can be found at , the sam ...

Designing a unique CSS border for custom list item bullets

I'm currently exploring the possibility of implementing a CSS border around an image that I have imported as a custom bullet for my list items: ul { list-style: none; margin-right: 0; padding-left: 0; list-style-position: inside; } ul > ...

Obtaining the URL from the anchor tag

I am currently working on a project that involves scraping data from the cinch.co.uk website. My tools of choice are Python along with the BeautifulSoup4 and Request libraries. My goal is to extract car data from each advertisement, starting by navigating ...

Using Javascript and jQuery, populate a dropdown menu with options that are extracted from a different dropdown menu

These are my two <select> elements: <select class="js-select-1"> <option disabled>Starting point</option> <option>A</option> <option>B</option> <option>C</option> <option>D</op ...

Ways to prevent hidden fields from being submitted in a form?

When submitting my HTML form, the hidden elements with display:none are also being posted. How can I prevent this from happening? ...

"Overlooked by z-index, absolutely positioned overlay causes confusion

I have been working on a template where I am trying to create a glow effect in the center of three divs with different color backgrounds. I added an absolutely positioned container with 10% opacity, but it ended up overlaying everything and ignoring z-inde ...

Adjusting the CSS class name based on the screen size and applying styles to that class only during the "onload" event

The website I am currently developing can be found at . It is built on WordPress using the Avada theme and everything seems to be functioning correctly. The image move up effect on this site is achieved with the following JavaScript: window.onload = funct ...

Adjusting the alignment of Bootstrap navbar items upon clicking the toggle button

When I click the toggle button on a small screen, my navbar items appear below the search bar instead of aligning with the home and about elements. Below is an image depicting this issue: https://i.stack.imgur.com/4rabW.png Below is the HTML code structu ...

Best practices for evenly distributing images within a navigation bar?

Within a horizontal navbar, I have various small images that are currently spaced out with different intervals between them. Each image has its own unique spacing from the others. I am looking to achieve consistent spacing between each image in this horiz ...

Malfunctioning carousel

I've been attempting to set up a carousel, but it's not functioning properly. I'm unsure of where the issue lies. The navbar section is working fine, but the carousel isn't appearing at all. Should the carousel be placed above the navba ...

Transferring scope between pages without the need for an angular service definition

Looking to open a new JSP page while passing the $scope in order to utilize an array response generated in the initial page. Example from test.js file: (function() { 'use strict'; angular .module('test', []) .control ...

Top 10 SQL Query based on two specific columns

As a beginner programmer diving into the world of SQL, I am on a mission to create a table for my website that showcases the lowest 10 grades alongside student information. While I feel confident in most aspects of this task, I am struggling with crafting ...

What are some methods for converting data from data tables into alternative formats?

I need assistance exporting data from data tables in various formats such as Copy, CSV, Excel, PDF, and Print. Can someone show me how to do this for the example provided below? <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.4 ...

Enhancing the appearance of a JSX component in React

I have a segment of code within my project that calculates a specific percentage and displays it on the dashboard. <text className="netProfit-circle-text" x="50%" y="50%" dy=".2em" textAnchor="middl ...