Spin the text within a div by 90 degrees and align it to the right side

I have been attempting to create an information box that contains a simple info text div. On the right side of this info box, I want to include a text labeled "more info" within a nested div to signify that the info box is interactive. To save space, I would like to rotate the text by 90 degrees so that it only takes up as much space as the height of the text "more info". However, I have been struggling to achieve this. When I try to rotate the text, it either breaks into two lines or is not vertically centered despite using white-space: nowrap;.

Does anyone have any suggestions for alternative CSS properties I could experiment with?

I have created a fiddle to demonstrate the issue: https://jsfiddle.net/girlscout/9e5u3j5w/3/

In this example, I have colored the two divs differently for clarity on their behavior.

Special thanks to Brett for the code snippet suggestion. Here is the code:

.first-div {
  border:1px solid blue; width:300px; height:100px; margin:20px auto;
  display:flex;
}
.second-div {
  background-color:green;
}

.mytext{
    transform:rotate(-90deg);
    /*white-space: nowrap;*/
}
<div class="first-div">
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa sociis nato.
  </p>
     <div class="second-div"><p class="mytext">more info</p></div>
</div>

Answer №1

To achieve the desired outcome, adjust the width of the .second-div and modify the padding and transform-origin of .mytext. It is important to note that these changes will need to be customized for each tab you create.

.second-div {
   background-color: green;
   width: 150px;
   position: relative;
}
.mytext {
   position: absolute;
   white-space: nowrap;
   top: 0;
   padding: 12px;
   color: #FFF;
   transform-origin: 50% 100%;
   transform: rotate(-90deg);
}

Answer №2

Utilizing the transform-origin property will play a crucial role in achieving the desired outcome.

Additionally, by applying position: absolute to the .mytext element and specifying fixed values for width, height, and other properties, we create a flexible design that can adapt to various content types.

Here's an example showcasing this concept:

.first-div {
  border: 1px solid blue;
  width: 300px;
  height: 100px;
  margin: 20px auto;
  display: flex;
}

.second-div {
  position: relative;
  background-color: green;
  height: 100%;
  box-sizing: border-box;
  flex-basis: 80px;
}

.mytext {
  position: absolute;
  top: 20px;
  transform: rotate(-90deg);
  transform-origin: 0% 0%;
  white-space: nowrap;
  margin: 100px 0 0 0;
  line-height: 30px;
  width: 100px;
  text-align: right;
}

.first-div.larger {
  height: 250px;
}
<div class="first-div">
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa sociis nato.
  </p>
  <div class="second-div">
    <p class="mytext">more info</p>
  </div>
</div>

<div class="first-div">
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa sociis nato.
  </p>
  <div class="second-div">
    <p class="mytext">info</p>
  </div>
</div>

<div class="first-div larger">
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa sociis nato.
  </p>
  <div class="second-div">
    <p class="mytext">even more info</p>
  </div>
</div>

Answer №3

I believe I have a good grasp on what you're seeking. To achieve the desired outcome, you'll need to apply positioning attributes to the first-div element as relative and the second-div element as absolute. Take a look at the code snippet below:

View the code here: CODEPEN

.first-div {
      border: 1px solid blue; 
      width: 400px; 
      height: 100px; 
      margin: 20px auto;
      display: flex;
      position: relative;
      padding-right: 40px;
    }
    .second-div {
      position: absolute;
      top: 0px;
      right: 0px;
      background-color: green;
    }
    .mytext{
      transform:rotate(-90deg);
      padding: 20px 0;
      font-size: 0.7em;
      color: #fff;
    }
<div class="first-div">
      <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa sociis nato.
      </p>
      <div class="second-div">
        <p class="mytext">more info</p>
      </div>
    </div>

Answer №4

There may be a potential solution, although it involves some duplication of your markup. You will require two elements: one for the invisible filler text, and the other for the visible text:

<div class="second-div">
  <!-- Used to precalculate/reserve space, not displayed -->
  <div class="mytext-spacer">more info</div>

  <!-- The actual text that is displayed and rotated -90deg -->
  <div class="mytext">more info</div>
</div>

This solution makes use of a spacer element that occupies the space needed for the rotated text. The .mytext-spacer element is styled to display text vertically in order to reserve the space:

.mytext-spacer {
  text-orientation: upright;
  writing-mode: vertical-rl;
  visibility: hidden;
}

The spacer element is hidden using visibility: hidden instead of display: none to ensure it still reserves the necessary space. Using display: none would remove it from the document flow and disrupt the layout.

After setting up the spacer, rotate your actual .mytext element. To center the text visually, a combination of position and translate properties is used, as flexbox would interfere with the layout involving the hidden spacer text:

.mytext {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(-90deg);
  text-align: center;
}

.first-div {
  border: 1px solid blue;
  width: 300px;
  height: 100px;
  margin: 20px auto;
  display: flex;
  /*justify-content:flex-end;  */
}

.second-div {
  background-color: green;
  position: relative;
}

.mytext-spacer {
  text-orientation: upright;
  writing-mode: vertical-rl;
  visibility: hidden;
}

.mytext {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(-90deg);
  text-align: center;
}
<div class="first-div">
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa sociis nato.
  </p>
  <div class="second-div">
    <div class="mytext-spacer">more info</div>
    <div class="mytext">more info</div>
  </div>
</div>

<div class="first-div">
  <p>Lorem ipsum dolor sit amet, consect-uer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa sociis nato.
  </p>
  <div class="second-div">
    <div class="mytext-spacer">this is a long long text</div>
    <div class="mytext">this is a long long text</div>
  </div>
</div>

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

How can I attach a cookie to a div that becomes visible after a few seconds of video playback?

After 20 seconds of video play, a div element appears. I am trying to set up a cookie so that once the div is shown, it continues to appear unless the user clears their cache (cookie logic). This is my current attempt - http://jsfiddle.net/9L29o365/ Any ...

labels placed beneath evenly spaced radio buttons arranged horizontally

When creating a user form, I am in need of multiple likert items to gauge opinions accurately. My project requires the use of the oTree library, which is closely integrated with django. It is essential that any solution implemented aligns with oTree as muc ...

JavaScript expression not being processed

What could be the reason behind not receiving the value for dish.price, dish,description, etc.? Where am I making a mistake in this code snippet? <!DOCTYPE html> <html lang="en" ng-app> <head> <meta charset="utf-8"> < ...

What could be causing the Bootstrap Navbar toggle icon button to not display navigation items when clicked?

Bootstrap has been a reliable tool for my projects until I encountered an issue in an Angular project. The navbar toggler icon button is visible, but clicking on it does not display the navigation items. <div> <nav class="navbar navbar-ex ...

How to Properly Adjust the Material UI CircularProgress Size

Is there a way to display the Material UI CircularProgress component at a size that nearly fills the parent div, which has an unknown size? Ideally, I want the size to be around 0.8 * min(parent_height, parent_width). I've experimented with adjusting ...

My current scroll animations are problematic as they are causing horizontal scrolling beyond the edge of the screen

I've implemented scroll animations on various elements of a website I'm constructing. The CSS rules I'm utilizing are as follows: .hiddenLeft { opacity: 0; filter: blur(5px); transform: translateX(-090%); transition: all 1s; ...

The issue of Ejs partial header and footer file only functioning in one file and not the other (both at the same directory level)

I'm in the process of setting up a view page for a post on the main page of a website. Both pages share the same header and footer files, located at the same directory level. However, while one page is functioning correctly, the other is not. The erro ...

What is the best way to resize SVG graphics effectively?

I am in need of creating a seating chart. I have generated an SVG with the seats. <div class="seats-map"> <svg xmlns="https://www.w3.org/2000/svg" id="seats-map-svg" viewBox="0 0 1000 300"> <g data-row ...

Achieving unique horizontal and vertical spacing in Material UI Grid

In my current Material UI setup, I have set spacing in the Grid Container to space Grid Items vertically. While this works well on larger screens, it causes awkward horizontal spacing between Grid Items on mobile devices. <Grid container spacing={24}> ...

Is it just me or does my wrapper always hover above the bottom of the screen?

I have been working on coding a simple layout that I created last year. Most of the work is done, but I ran into an issue where the 'wrapper' div doesn't extend to the bottom of the page as expected. It almost looks like it has the "position ...

Expanding the axes using Google Charts

Seeking to Expand Y-Axis I am attempting to stretch the Y-axis so that it counts up to 300 instead of just 100. The issue arises when I adjust the Y-axis to reach 300, as the values remain in the same position as they did when counting to 100. Fo ...

Various CSS libraries offer a range of design options, including DataTables and Bootstrap

Incorporating both Bootstrap and DataTable into my design has caused conflicts with CSS styles. This issue extends beyond just these libraries, making me wonder if there is a way to prioritize my own styles over library styles. Can anyone provide guidanc ...

Center the HTML elements on the page

After struggling for some time, I can't seem to figure out how to center elements in the middle of a page without having a lengthy navigation bar at the bottom of the screen. Does anyone have any suggestions? https://codepen.io/picklemyrickle/pen/XWj ...

Guide to adjusting the number of bounces using jQuery's User Interface (UI)

Check out my code on CodePen: http://codepen.io/Chiz/pen/jbwPLO (click anywhere in the blank area) I'm trying to figure out how to adjust the number of bounces a div makes before stopping. For example, I want the div to bounce 10 times and also chang ...

Using HTML to load an image is not possible, as it can only be done using CSS

I am currently dealing with a CSS issue in my project. Here is the code snippet from my stylesheet: .Img{ background: url('../assets/Trump.png'); } Now, let's take a look at the corresponding HTML code: <div class="Img"> However, ...

`We are experiencing alignment issues with the Bootstrap drop-down menu`

I'm struggling to understand why my menu isn't aligning properly on the sub dropdowns. They are appearing at the lower edge of their parent cell, making navigation quite difficult. You have to drop down diagonally just right to access the next me ...

What are the steps to reveal the second element once the first one has vanished?

Is there a way to delay the appearance of the second square until after the first square has disappeared? For example, I want the first square to appear after 3 seconds and then disappear, followed by the second square becoming visible after 11 seconds. ...

Struggling to format boxes or display images on your WordPress website?

When working on this website, I encountered an issue with adding links to Google+ and Facebook. Despite my efforts to style the div boxes using CSS, I was unsuccessful. I also attempted to insert images using an img tag, but the pictures did not load even ...

Div positioned absolutely beneath the footer

I'm facing an issue where the footer field I add under a div with an absolute value escapes on both mobile and desktop. Additionally, the iframe does not have full height. Any suggestions on what I should do? All the specifics are provided in the att ...

A universal setting takes precedence over Vuetify divider colors

Since upgrading Vuetify from version 1.1.9 to 1.2.3, I have encountered a strange issue where all of my dividers appear much darker than before and different from the examples in the documentation. This is how it should look: https://i.sstatic.net/GnxzV. ...