Rotate a div containing text to center inside another div

I'm facing a challenge with rotating a div while trying to cover the entire width of the body. Initially, I attempted setting the width to 120% instead of 100%, however, this caused issues with centering the div correctly. How should I go about solving this problem?

Below is the code snippet I attempted to implement:

* {
  margin: 0;
  padding: 0;
}
body {
  background-color: yellow;
  overflow-x: hidden;
}
.rotation {
  position: relative;
  display: table;
  width: 120%;
  height: 500px;
  transform: rotate(-5deg);
  background-color: green;
  margin-left: -50px;
}
.wrapper {
  display: table-cell;
  vertical-align: middle;
  margin: 0 auto;
  text-align: center;
  transform: rotate(5deg);
}
 <h1>Some title</h1>

<div class="rotation">
  <div class="wrapper">
    <h1>Heading</h1>

    <span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quo quaerat, dolore quod cum ea rerum asperiores cupiditate esse harum. Voluptatibus soluta fuga, beatae quod dolorem, veniam sint ab non laboriosam.</span>
  </div>
</div>

JSFIDDLE

Answer №1

Could you explain the reason behind rotating the entire element first and then rotating the text element inside?

My suggestion would be to utilize a pseudo-element (::before or ::after), as it offers greater flexibility with the background and eliminates the need to rotate the text twice.

Feel free to check out this JSFiddle link for an example: https://jsfiddle.net/9kj3l4n6/2/

Answer №2

Modify the suggestion provided by @Andi North. If you want the wrapper to always match the height of the viewport, there are two options available: using JavaScript or CSS3. I recommend using CSS3.

By utilizing vw/vh units in CSS, we can adjust the size of elements based on the size of the viewport. These units are interesting because 1 unit corresponds to 1/100th of the viewport's width. For instance, setting width:100vw would make an element span the entire width of the viewport.

In your specific scenario, simply add 'height:95vh;' to the wrapper element.

* {
  margin: 0;
  padding: 0;
}

body {
  background-color: yellow;
  overflow-x: hidden;
}

.rotation {
  position: relative;
  display: table;
  height: 500px;
}

.rotation::after {
  top: 0;
  left: -10%;
  content: '';
  position: absolute; /* Bring below content */
  background: lightblue;
  z-index: 1; /* Bring below content */
  transform: rotate(-5deg);
  width: 120%;
  height: 100%;
}

.wrapper {
  position: relative;/* Bring above background shape */
  z-index: 2; /* Bring above background shape */
  display: table-cell;
  vertical-align: middle;
  margin: 0 auto;
  text-align: center;
  height:95vh;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<title>Document>
</head>
<body>
<h1>Some title>

<div class="rotation>
<div class="wrapper>
<h1>Heading>

<span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quo quaerat, dolore quod cum ea rerum asperiores cupiditate esse harum. Voluptatibus soluta fuga, beatae quod dolorem, veniam sint ab non laboriosam.</span>
</div>
</div>
</body>
</html>

Cheers!!!

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

Delete all pictures beginning with this specific ID from the posts

Currently, I have approximately 300 Wordpress posts, and each one contains a "tracking pixel" from a previously used service (which is implemented using IMG tags). This is an example of how it appears: <img id="serviceTrack_3274570" style="margin: 0px ...

Adjust Anchor ID for Media Query Activation

I need help with changing the ID of an anchor tag when a media query is triggered. Here is the code I am currently attempting, but it's not working: $(window).resize(function() { if ($('.container').css('width') == "100%"){ ...

Design a blurred glass effect background with a circular spotlight focusing on a specific section of an HTML webpage

I've been attempting to achieve a frosted glass effect on a background, with a circular section unblurred. Unfortunately, my current implementation in the sandbox editor is not working as expected - the area of the circle remains blurred. If you&apos ...

Delete an entry from the list

I have a fully functional Ajax call and function that fills up a datalist. This datalist is utilized to choose from a limited list and add it to a UL element on the page. Once an option is added from the datalist to the actual list, I aim to eliminate that ...

Achieving centered positioning for the 'row' class in Foundation 5, regardless of browser size

While viewing on desktop, the body is centered which is ideal. However, resizing the browser causes it to stretch to fit the full width and I want to avoid this. Regardless of the browser size, I want the first div with the "row" class to be centered with ...

Submit data from one form to another form located within an iframe

I am currently using a JX Browser that allows content to be displayed in an iframe. My goal is to automatically transfer the username and password of a user logging into my ticketing software to another form within an iframe. The page within the iframe is ...

Struggling with a Bootstrap v5.0.2 Modal glitch? Let's dive into a real-life case study to troub

I am seeking assistance with a problem that I am encountering. I am currently using Bootstrap version 5.0.2 (js and css). Despite coding all the required parameters, I am unable to make the modal functionality work. I have been trying to figure out what&ap ...

applying a blur effect to the Vue modal mask

Trying to incorporate a blur panel behind my modal using vue.js, however, The issue arises when the modal-mask contains nested content. This makes it challenging to apply the filter: blur() property without blurring everything. Currently, I am only able t ...

Close button for colorbox modal window containing an iframe

I'm currently utilizing colorbox for a modal popup, and the content of the popup is being sourced from a URL. Since it's displayed within an iFrame, I'm wondering how I can incorporate a close button to the modal popup. Thank you The follo ...

Designing a custom CSS for enhancing the look of the orchard menu

Right now, I am in the process of implementing a layout that is already established in an MVC4 project. I am facing challenges in figuring out how to create a custom menu in the latest version of Orchard (using the source version so it runs in Visual Stud ...

Choosing between JavaScript and Handlebars.js depends on the specific needs

Can anyone explain the advantages of utilizing Handlebars.js or similar libraries over solely relying on JavaScript? Thus far, I haven't come across any functionality in Handlebars that cannot be achieved with just pure JavaScript. ...

Having trouble getting Sass extending to work in a basic scenario?

Here we have a simple example of Sass code, an Angular 2 component that accesses the Sass code, and the rendered HTML. However, there seems to be an issue with the blueBig span element in the last part. As someone new to Sass, I am not sure why this basic ...

What could be the reason for the page scrolling upwards when clicking on href="#"?

I am encountering an issue with a hyperlink <a href="#" id="someID">Link</a> that is located further down the page. This link is used to trigger an Ajax request, but whenever it is clicked, the page automatically scrolls back to the top. I have ...

Issue with WordPress navigation menu bug

On my website, I have implemented WP nav menu and it seems to be functioning correctly. However, I am facing an issue with the sub link under the home link. The screenshot shows the problem clearly. In the provided image, the "support our work" sub link a ...

Database not receiving input data from AngularJS form submission

Having some trouble saving form data to the database using angularjs. Not sure what I'm doing wrong. Here's my HTML form: <form id="challenge_form" class="row" > <input type="text" placeholder="Challenge Name" ng-model="ch ...

"What is the best way to prevent a button from being enabled in Angular if a list or array is not

I am incorporating data from my service in the component: ngOnInit() { this._sharedService. getReceiptItem().takeUntil(this.ngUnsubscribe). subscribe(products => this.receiptItems = products); } Is there a way to deactivate a button if there ...

Unlocking the potential of resizable bootstrap table columns in React

Currently utilizing a bootstrap table <div class="table-responsive"> <table class="table table-bordered"> <thead> <tr> <th>#</th> <th>Table heading</th> </tr> < ...

Tips on using the Hover property in CSS for an element that includes both :after and :before properties

I've created a hexagon using CSS after and before properties, and now I'm attempting to add a glowing effect around the hexagon when hovering. It works perfectly for the center of the hexagon but not for the top and bottom points of the shape. ...

The images in Bootstrap 5 are displayed individually on separate lines, even though they could easily be accommodated within a

I'm facing an issue with displaying several 50x50 images in a row of 6 columns on my website. Despite the simple code setup, each image appears on its own row, creating the illusion of stacked images rather than a single row. This is the code structu ...

The link element could not be found

Having trouble finding the 'Search' link, and encountering the error message: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"link text","selector":"Search"} Upon inspecting the object using Firebug: < ...