Ways to center text in a div using vertical alignment

/* Styles for alignment */

.floatsidebar {
  height: 100%;
  float: left;
  overflow: hidden;
  width: 30px;
  line-height: 1.5;
}

.vertical-text {
  height: 100%;
  display: inline-block;
  white-space: nowrap;
  transform: translate(0, 100%) rotate(-90deg);
  transform-origin: 0 0;
  font-size: 19px;
  color: grey;
}

.vertical-text:after {
  content: "...";
  float: left;
  margin-top: 100%;
}

.sidebarmain:hover {
  background: linear-gradient(to right, white, #a6a6a6)
}

.container-design {
  min-height: 100%;
  background: white;
  box-sizing: border-box;
  padding: 8px;
  border: 1px solid grey;
}

.sidebarmain:active {
  background: linear-gradient(to right, white, #989898)
}

.sidebarmain {
  cursor: pointer;
  border: 1px solid grey;
  box-sizing: border-box;
  height: 500px;
  background: linear-gradient(to right, white, lightgrey);
  border-radius: 2px;
}
<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
  <script src="script.js"></script>
</head>

<body>

  <div class="floatsidebar">
    <div class="sidebarmain ui-widget-header">
      <div class="vertical-text">hello</div>
    </div>
  </div>
</body>

</html>

I have been attempting to center align the text "hello" within its parent div but so far have had no success.

Could someone help me with aligning the text in the middle of its parent div?

<div class="floatsidebar">
  <div class="sidebarmain ui-widget-header">
    <div class="vertical-text">hello</div>
  </div>
</div>

Answer №1

Adjust the transform to be 50% instead of 100% and include a span element that can be positioned back half its own width

/* Custom Styles */

.floatsidebar {
  height: 100%;
  float: left;
  overflow: hidden;
  width: 30px;
  line-height: 1.5;
}

.vertical-text {
  height: 100%;
  display: inline-block;
  white-space: nowrap;
  transform: translate(0, 50%) rotate(-90deg);  /* update this */
  transform-origin: 0 0;
  font-size: 19px;
  color: grey;
}

.vertical-text:after {
  content: "...";
  float: left;
  margin-top: 100%;
}

.vertical-text > span {                        /* insert a span for accurate centering */
  transform: translateX(-50%);
  display: inline-block;
  max-width: 500px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow:ellipsis;
  padding: 0 0.5em;
  box-sizing:border-box;
}

.sidebarmain:hover {
  background: linear-gradient(to right, white, #a6a6a6)
}

.container-design {
  min-height: 100%;
  background: white;
  box-sizing: border-box;
  padding: 8px;
  border: 1px solid grey;
}

.sidebarmain:active {
  background: linear-gradient(to right, white, #989898)
}

.sidebarmain {
  cursor: pointer;
  border: 1px solid grey;
  box-sizing: border-box;
  height: 500px;
  background: linear-gradient(to right, white, lightgrey);
  border-radius: 2px;
}
<div class="floatsidebar">
  <div class="sidebarmain ui-widget-header">
    <div class="vertical-text"><span>a brown fox quick jump over the lazy dog a brown fox quick jump over the lazy dog</span></div>
  </div>
</div>

Answer №2

To easily center elements in your sidebar, try implementing flexbox

.sidebarmain {
    display: flex;
    justify-content: center;
    align-items: center;
}

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

CssLint detected an unfamiliar property: "fill"

Currently, I am updating the color of an SVG path using CSS: .compute .svg path { fill: #fff; } The functionality is working properly, however, during a CSSLint check, I received this warning: Unknown property: "fill" I am unsure if this is an erro ...

CSS-only: Align items in flexbox column with equal width and centered position, without the need for a wrapper

https://i.stack.imgur.com/vAJv2.png This particular challenge involves creating a responsive mobile menu. In this design, the width of all children is determined by the longest child element. The CSS should ensure that the .index class spans the full wi ...

Customize the appearance of a shadow-root element

Is there a way to modify the styles of a shadow element? Specifically, is it possible to extend or overwrite certain properties within a CSS class? I am currently using a Chrome extension called Beanote, which has not been updated since April 2017. There i ...

Executing functionality based on changes in Angular bindings

I am working on securing user passwords in my HTML form. Currently, the password field is stored in localstorage like this: In JS: $scope.localStorage = localStorage; And then in HTML: <input id="pass" type="password" ng-model="localStorage.pass" re ...

Discover the method for inserting a title attribute value into a span element

Utilizing AngularJS to retrieve and display data within a span element. I am now aiming to utilize this value as the title for another span element. The current code being used is shown below: <span style="display: inline-block; width: 160px">{{acti ...

The transition feature is not functioning properly in Firefox

I have a basic HTML and CSS setup below. It seems to be working fine in Chrome, but I'm having trouble with the transition effect in Firefox. I am currently using Firefox version 18. I tried looking up solutions on Google and Stack Overflow, but none ...

Switch the CSS class when clicking on a responsive table within a table

I am looking to update the background color of a row within a table when a tr is clicked. Below is the code for the table: <div class="table-responsive"> <table class="table table-hover"> <thead> ...

Guide to Incorporating a Marker into an SVG Blinking Rectangular or Circular Border Image on Google Maps

I have a link that points to a specific location on Google Maps using latitude and longitude: http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png Now, I am looking to add a blinking border to this marker link. Is there a way to achieve this ...

Is there a way to store image URLs in a fashionable manner?

Hey there! I've been working on creating a HTML page that showcases multiple images. Everything is running smoothly on my localhost, but when I try to access it online, the images take forever to load. Any suggestions on how I can cache image URLs in ...

Tips for shifting a stuck div 200px upward once the bottom of the page is reached while scrolling:

I've set up a page layout with two columns, one fixed and the other scrollable. The left column is fixed (containing Google ads) and stays in place as I scroll down the page. The right column displays posts and scrolls along with the rest of the con ...

Substituting HTML checkboxes with highlighted images for consistent display across different web browsers

I am looking to transform a list of HTML checkboxes into clickable images for the user to select or deselect. My goal is to create a similar functionality as shown in this video using just CSS if possible. I have successfully implemented it in Chrome wit ...

adaptive height that scales with content proportionally

I am looking to achieve a responsive height based on the content inside my container. For example, I want the height of the container to adjust according to the text it contains. <!DOCTYPE html> <html lang="en"> <head> < ...

Challenge with maintaining tab view data in Openui5

I am facing an issue with my application's tabs. Each tab renders data retrieved through ajax calls from the database. However, whenever I switch between tabs, the data gets refreshed each time. Is there a way to prevent this refreshing behavior and i ...

Tips for enabling auto-scroll feature in MuiList

Currently, I am working on a chat window component that utilizes Material UI for styling. I expected that setting a height or max-height on either the MuiList or MuiBox encapsulating the list would automatically scroll to the new message when it's sen ...

Loading Web Applications Screen

My web app contains multiple tree views. Upon loading the page, I first see the unordered lists before the styling of the trees is displayed in the DOM after a brief delay. Is there a method to mask the web app with a spinner placed in the center of the s ...

Icons from Material Design Lite are not appearing as expected when integrated into an Angular project

Recently, I integrated MDL into my Angular project. While most of it is functioning properly, the MDL icons seem to be giving me trouble... I've implemented them using <i class="material-icons">share</i>, but instead of displaying as an i ...

Issue with horizontal navigation in CSS

I'm struggling with styling my CSS navigation bar. The last list item is moving to a second line, which really messes up the look of my website. Can someone take a look at my code and point out what I'm doing wrong? Thank you in advance. HTML: ...

Tips for extracting the chosen value from a dropdown list within a table cell using JavaScript

Here is an example of an HTML element: <td> <select> <option value="Polygon 47">Polygon 47</option> <option value="Polygon 49">Polygon 49</option> </select> </td> I am looking for a ...

Extracting data from the web using PHP

I'm facing an issue with my code that scrapes data from a website. The current output looks like this: Agriculture Food Apparel I want to only display the first/nth category, for example just "Agriculture". I attempted: echo $sub_title[1].&ap ...

How to set element height based on screen height rather than browser height

How can I set the height of a div container to be 60% of the screen height, but ensure it maintains that height even when the browser window is resized? Setting the height as 60% in CSS works initially, but the div shrinks proportionately when the window s ...