Skip to content
Fix Code Error

How do I vertically align text in a div?

March 13, 2021 by Code Error
Posted By: Anonymous

I am trying to find the most effective way to align text with a div. I have tried a few things and none seem to work.

.testimonialText {
  position: absolute;
  left: 15px;
  top: 15px;
  width: 150px;
  height: 309px;
  vertical-align: middle;
  text-align: center;
  font-family: Georgia, "Times New Roman", Times, serif;
  font-style: italic;
  padding: 1em 0 1em 0;
}
<div class="testimonialText">
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
  in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

Solution

The correct way to do this in modern browsers is to use Flexbox.

See this answer for details.

See below for some older ways that work in older browsers.


Vertical Centering in CSS

http://www.jakpsatweb.cz/css/css-vertical-center-solution.html

Article summary:

For a CSS 2 browser, one can use display:table/display:table-cell to center content.

A sample is also available at JSFiddle:

div { border:1px solid green;}
<div style="display: table; height: 400px; overflow: hidden;">
  <div style="display: table-cell; vertical-align: middle;">
    <div>
      everything is vertically centered in modern IE8+ and others.
    </div>
  </div>
</div>

It is possible to merge hacks for old browsers (Internet Explorer 6/7) into styles with using # to hide styles from newer browsers:

div { border:1px solid green;}
<div style="display: table; height: 400px; #position: relative; overflow: hidden;">
  <div style=
    "#position: absolute; #top: 50%;display: table-cell; vertical-align: middle;">
    <div style=" #position: relative; #top: -50%">
      everything is vertically centered
    </div>
  </div>
</div>
Answered By: Anonymous

Related Articles

  • Open/Close content and anchor link
  • Paper-dialog shows up behind drawer-panel when…
  • How can I show the caption of a single grid item…
  • Subquery on SELECT with conditional WHERE clause after it
  • Only show HTML body after a CSS animation is…
  • Scroll part of content in fixed position container
  • Footer not staying at the bottom when scrolling
  • Navbar not filling width of page when reduced to mobile view
  • Generating PDF file with Images in flutter
  • center 3 items on 2 lines
  • Why my "title" bloc doesn't go to bottom?
  • Link color is not changing after adding a class with jQuery
  • Dynamically replaced text string with Vue component
  • Pandas pivot_table: filter on aggregate function
  • Right way to do navigation with Ember
  • Active tab issue on page load HTML
  • How to use html template with vue.js
  • Smart way to truncate long strings
  • Regex on htaccess file gives INTERNAL REDIRECT error
  • How to replace a string with parts of it between…
  • How do I make responsive images?
  • Azure Sql : Unable to Replace HTML String
  • Applying an ellipsis to multiline text
  • Adobe XD to responsive html
  • Parse Json string in C#
  • Use jQuery to scroll to the bottom of a div with…
  • Vertical (rotated) text in HTML table
  • Video with "float: right" property will not play -…
  • JavaScript Loading Screen while page loads
  • How to show title in hover - css / jquery
  • Save window binding in svelte store
  • Having trouble with my nav bar/header, It used to…
  • SQL find sum of entries by date including previous date
  • Using jQuery Tabs with Marionette Layouts
  • How do i update a javascript variable as its value changes?
  • Reloading page is flashing content on slot in vue component
  • insert tables in dataframe with years from 2000 to…
  • Add calculated column to df2 for every row entry in…
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • Make div 100% Width of Browser Window
  • How do I do a drag and drop using jQuery to move…
  • How can can ı resize an image which is into a card?
  • HTML5 best practices; section/header/aside/article elements
  • After a little scroll, the sticky navbar just is not…
  • How to get the same space between text and block
  • div "container" which does not center correctly
  • I need to sum values with specific condition
  • Cannot make table responsive
  • jQuery Set Cursor Position in Text Area
  • Is it possible to apply CSS to half of a character?
  • Putting content under a row that takes up 100% of…
  • Bootstrap - center child when parent isn't centered…
  • Why my first title isn't center like the second title?
  • How to set Bullet colors in UL/LI html lists via CSS…
  • Prevent wrapping of span or div
  • Fix top buttons on scroll of list below
  • How to force table cell content to wrap?
  • Proper way to sideload data with ember-model
  • Limit array of dom-repeat within iron-list
  • Vertical Align Center in Bootstrap 4
  • Jquery fadeToggle Trouble
  • Scroll to a div using jquery
  • Increasing size of semi circle using css
  • jQuery Dialog Box
  • How to separate Polymer button and dialog
  • How do I assign a timeout variable to the tag in Vuetify?
  • Does bootstrap have builtin padding and margin classes?
  • Valid values for android:fontFamily and what they map to?
  • try/catch blocks with async/await
  • How to vertically center a container in Bootstrap?
  • Indent starting from the second line of a paragraph with CSS
  • How to dismiss Vuetify v-dialog when clicked outside…
  • How do I keep only the first map and when the game…
  • Why is my SwiftUI view not updating when the model changes
  • Nextjs external css not updating on client side page…
  • Table overflowing outside of div
  • How to apply a CSS filter to a background image
  • When tab is selected, show next tab.. if last tab…
  • Open only one accordion panel vue.js / bootstrap vue
  • Twitter Bootstrap Button Text Word Wrap
  • .map is not returning the values(not a function)
  • Bootstrap Vue perPage for custom columns or rows
  • property flex-wrap not working correctly?
  • Add color to vuetify component
  • Obtain most recent value for based on index in a…
  • Vertically align an image inside a div with…
  • How to make scroll navbar to appear at top of page…
  • Centering in CSS Grid
  • Multiline TextView in Android?
  • Oracle sql query to group two sets of dates if sequencials
  • Setting max-width of vuetify expansion panel component
  • Make Flip Card HTML CSS
  • Vue.js Compile Error: no-unused-vars & no-undef
  • Daisy is overlapping the other contents of the div
  • VueJS masonry layout
  • How to Stop Propagation / Opening v-expansion-panel…
  • Dynamically loading/changing Vuetify Tabs
  • How to use 2 columns as "key" to get MAX value of…
  • HTTP Status 500 - org.apache.jasper.JasperException:…
  • How to toggle tabs in VueJS Vuetify by clicking on…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Hiding the scroll bar on an HTML page

Next Post:

Change a HTML5 input’s placeholder color with CSS

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error