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

The Bootstrap card is elegantly framed by a crisp white border surrounding the

After referencing a tutorial, I implemented the following code: <div class="card" style="width:400px"> <img class="card-img-top" src="image.png" alt="Card image"> <div class="card-body"> <h4 class="card-title">John Doe</ ...

Is there a combination of 'auto' and 'none' values in any CSS properties?

Is it safe to assume that if a property is set to auto, it cannot have the value of none, and vice versa? Or if a property has none, can it not have auto as well? I understand that these values have distinct meanings, but I am curious if this concept has ...

Sending data to API using AngularJS Http Post

Upon clicking "Add new User", a modal pop-up will appear with a form containing a text field and a checkbox. However, upon clicking the create button, the data is not being posted to the API and the modal pop-up remains open without closing. I would like ...

Modifying a div element upon the successful completion of an ajax/json request

Currently, I have a form on my webpage for creating a new album. Alongside this form, there is also a delete album form. After successfully creating an album, I want to automatically update the checkbox list in the delete album form with the details of the ...

What is the best way to extract text using Selenium in Java?

My goal is to extract the text $1.00 from the HTML code snippet below (I already have the xpath, so no need to worry about that). Let's assume the xpath is //*[@id="price-string"] <strong id="price-string">$1.00</strong> ...

Creating an interactive form with file uploads using JQuery Mobile

I'm currently working on a JQuery Mobile page that is designed to enable video uploads to the Google App Engine Blobstore, complete with title, description, and more. Below is the HTML code for the form: <form class="form-horizontal" action="{{ u ...

Enhancing an image with zoom effect in a 2-column layout on hover

Could someone help me achieve a 2-column layout where the left column contains text and the right column an image? I want the image in the right column to zoom when the user hovers over either the left or right column. The issue is that when the user hove ...

Sending information from a Java class to an HTML WebView

I have a webpage loaded in my webView that includes the following HTML code: Now I need to automatically populate and submit the textbox within this HTML content from a variable in my Java class, but I'm unsure of how to achieve this. Any assistance ...

Fetch data in JSON format from a specified URL

I have been attempting to fetch a JSON from a specific URL. Here is my current code snippet: <script> var co2; $(document).ready(function(){ alert("0"); $.getJSON(url,function(result){ var jsonObject = result; alert(result); ...

Is there a way to trigger element closure using the esc key?

I have a panel that shows on initialization and hides when I press the 'close' icon. How can I make the panel close when I press the 'esc' button? Below is the HTML code for the panel: <div ng-init="vm.onInit()" class="mainContent"& ...

Tips for utilizing browser cache in AJAX requests to prevent loading the refreshed JSON file

It may seem like a strange question, but I'm experiencing an issue with an AJAX call to a JSON file. Both the request and response headers do not indicate to not use cache, and in the browser settings, the Disable cache option is not checked. What mor ...

Navigating a Multi-Page Website with Sleek Scrolling

I've been searching for a solution everywhere but haven't had any luck. I'm trying to implement a smooth scroll effect that works seamlessly across multiple pages. For instance, the smooth scroll effect is present on the homepage with naviga ...

Approach to decoupling design elements from DOM interactions

Seeking recommendations or guidelines for effectively segregating classes utilized for browser styling and DOM manipulation. Specifically, we are developing a single-page app using backbone.js and heavily relying on jQuery for dynamically updating page el ...

Interactive state for associated product within list without order

I am currently working on a project that involves an unordered list with separate list items. The menu design includes product images in the top list item and the corresponding product names in the list item below. I have successfully implemented a hover s ...

The issue of video tags not displaying previews on iPhone across all browsers is also accompanied by problems with controls not functioning correctly

As I delve into the world of HTML5 video tags, I encountered an issue where the video wouldn't show a preview frame initially. Instead, only the Resume button would appear. Furthermore, after playing the video with the Resume button, it wouldn't ...

css code creates a stable block with a blurred transparent effect

I need assistance with creating a transparent fixed header using only CSS and no JS. I attempted to use ::before for creating a blur effect with a negative z-index, but so far, my attempts have failed. I know how to achieve this using jQuery, but I am spec ...

Design featuring a combination of vertical columns and a fixed image placed in

I'm currently in the process of creating a website and I have a specific page layout in mind that I would like to implement: ------------------------------------------ | A catchy title | | | Here | An unconve ...

Is there a way to update page content without having to refresh the entire page?

My goal is to refresh specific content on a page without having to reload the entire page using JavaScript or jQuery. Since my project is built in PHP and JavaScript, I encountered this issue. Note : I want the page content to refresh when a user performs ...

Changing the width of the file input using css

Clicking just below the word demonstration also triggers a click on the input type file. How can this be avoided so that the click event only fires in the intended area regardless of size? <!DOCTYPE html> <html> <body> <style> in ...

Unable to create account using PHP

Every time I attempt to create an account, the data I receive is always problematic. The username must be between 3 and 15 characters I find it frustrating that the account creation never goes through successfully. What baffles me even more is that af ...