Czy możliwe jest wyrównanie tekstu w pionie w div? [duplikat]

To pytanie ma już odpowiedź tutaj:

Poniższy kod (dostępny również jako demo na JS Fiddle) nie umieszcza tekstu w środku, tak jak bym tego chciał. Nie mogę znaleźć sposobu na wyśrodkowanie tekstu w div, nawet używając atrybutu margin-top. Jak mogę to zrobić to?

<div id="column-content">
    <img src="http://i.stack.imgur.com/12qzO.png">
    <strong>1234</strong>
    yet another text content that should be centered vertically
</div>
#column-content {
    display: inline-block;
    border: 1px solid red;
    position:relative;
}

#column-content strong {
    color: #592102;
    font-size: 18px;
}

img {
    margin-top:-7px;
    vertical-align: middle;        
}
Author: YakovL, 2012-02-12

10 answers

Utwórz kontener dla swojej zawartości tekstowej, być może span.

#column-content {
  display: inline-block;
}
img {
  vertical-align: middle;
}
span {
  display: inline-block;
  vertical-align: middle;
}

/* for visual purposes */
#column-content {
  border: 1px solid red;
  position: relative;
}
<div id="column-content">

  <img src="http://i.imgur.com/WxW4B.png">
  <span><strong>1234</strong>
    yet another text content that should be centered vertically</span>
</div>

JSFiddle

 391
Author: Andres Ilich,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-27 21:45:44

Andres Ilich ma rację. Na wypadek, gdyby ktoś przegapił jego komentarz...

A.) Jeśli masz tylko jedną linijkę tekstu:

div
{
  height: 200px;
  line-height: 200px; /* <-- this is what you must define */
}
<div>vertically centered text</div>

B.) Jeśli masz kilka linijek tekstu:

div
{
  height: 200px;
  line-height: 200px;
}

span
{
  display: inline-block;
  vertical-align: middle;
  line-height: 18px; /* <-- adjust this */
}
<div><span>vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text vertically centered text</span></div>
 599
Author: Jenny O'Reilly,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-10 15:41:47

Aktualizacja Kwiecień 10, 2016

Flexboxy powinny być teraz używane do wyrównywania elementów w pionie (lub nawet w poziomie).

<div class="flex-container">
    <div class="flex-item">Item</div>
    <div class="flex-item">Item</div>
    <div class="flex-item">Item</div>
    <div class="flex-item">Item</div>
</div>

<style>
.flex-container {
    display:flex;
    align-items: center; /* Vertical center alignment */
    justify-content: center; /* Horizontal center alignment */
}
</style>

Dobry przewodnik po flexbox można przeczytać na sztuczki CSS. Dzięki Ben (z komentarzy) za wskazanie, nie miałem czasu na aktualizację.


Dobry facet o imieniu Mahendra opublikował bardzo działające rozwiązanie tutaj

Następująca klasa powinna wyśrodkować element poziomo i pionowo do jego rodzica.

.absolute-center {

/* Internet Explorer 10 */
display:-ms-flexbox;
-ms-flex-pack:center;
-ms-flex-align:center;

/* Firefox */
display:-moz-box;
-moz-box-pack:center;
-moz-box-align:center;

/* Safari, Opera, and Chrome */
display:-webkit-box;
-webkit-box-pack:center;
-webkit-box-align:center;

/* W3C */
display:box;
box-pack:center;
box-align:center;

}
 168
Author: Omar Tariq,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:10:41

To stare pytanie, ale przyjęta odpowiedź nie działa w przypadku tekstu wielowierszowego. Zaktualizowałem JSfiddle , aby pokazać tekst wielowierszowy css pionowe wyrównanie Jak wyjaśniono TUTAJ :

<div id="column-content">
    <div>yet another text content that should be centered vertically</div>
</div>

#column-content {
    border: 1px solid red;
    height: 200px;
    width: 100px;
}
div {
    display: table-cell;
    vertical-align:middle;
    text-align: center;
}

Działa również z <br /> w "yet another..."

 59
Author: Eduard Gamonal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-04 13:49:50

Spróbuj tego:

HTML

<div><span>Text</span></div>

CSS

div {
    height: 100px;
}

span {
    height: 100px;
    display: table-cell;
    vertical-align: middle;
}
 24
Author: Petr Voborník,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-03 21:14:48

To ma po prostu działać:

#column-content {
        --------
        margin-top:auto;
        margin-bottom:auto;
    }
Próbowałem na Twoim demo.
 11
Author: ParPar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-02-12 14:31:11

Aby rozwiązanie Omara (lub Mahendry) było jeszcze bardziej uniwersalne, blok kodu w stosunku do Firefoksa należy zastąpić następującym tekstem:

/* Firefox */
display:flex;
justify-content:center;
align-items:center;

Problem z kodem Omara, w przeciwnym razie operacyjny, pojawia się, gdy chcesz wyśrodkować okno na ekranie lub w jego bezpośrednim przodku. Centrowanie to odbywa się poprzez ustawienie jego pozycji na

position: relative; lub position:static; (nie z pozycją: absolutną ani stałą).

A następnie margin: auto; lub margin-right: auto; margin-left: auto;

/ Align = "center" bgcolor = "# e0ffe0 " / cesarz chin / / align = center / Nie działa również w IE8 (jeszcze 7,7% udziału w rynku). Tak więc w przypadku IE8 (i innych przeglądarek) należy rozważyć obejście, jak widać w innych powyższych rozwiązaniach.
 6
Author: user2931920,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-02 19:39:09

Jest to najprostszy sposób, aby to zrobić, jeśli potrzebujesz wielu linii. Zawiń span ' D tekst w inny span i określ jego wysokość za pomocą line-height. Sztuczka polega na zresetowaniu wewnętrznych span'S line-height.

<span class="textvalignmiddle"><span>YOUR TEXT HERE</span></span>
.textvalignmiddle {
    line-height: /*set height*/;
}

.textvalignmiddle > span {
    display: inline-block;
    vertical-align: middle;
    line-height: 1em; /*set line height back to normal*/
}

DEMO

Oczywiście zewnętrzny span może być div lub whathaveyou

 4
Author: Hashbrown,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-01 02:11:12

Dodaj pionowe wyrównanie do css #column-content strong too:

#column-content strong {
    ...
    vertical-align: middle;
}

Zobacz także swój zaktualizowany przykład .

=== Aktualizacja = = =

Z rozpiętością wokół drugiego tekstu i innym wyrównaniem pionowym:

Html:

... <span>yet another text content that should be centered vertically</span> ...

Css:

#column-content span {
    vertical-align: middle;
}

Zobacz także następny przykład .

 3
Author: scessor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-02-12 14:12:55

Wiem, że to całkowicie głupie i zwykle nie powinieneś używać tabel, gdy nie tworzysz tabel, ale: komórki tabeli mogą wyrównywać wiele linii tekstu pionowo, a nawet robić to domyślnie. Więc rozwiązaniem, które działa całkiem dobrze, może być coś takiego:

Html:

<div class="box">
  <table class="textalignmiddle">
    <tr>
      <td>lorem ipsum ...</td>
    </tr>
  </table>
</div>

Css: (aby element tabeli zawsze pasował do pola div)

.box {
  /* for example */
  height: 300px;
}

.textalignmiddle {
  width: 100%;
  height: 100%;
}

Zobacz tutaj: http://www.cssdesk.com/LzpeV

 -2
Author: lars at upstruct,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-15 10:57:02