Welcome to Idea R | Branding - Web Agency - Digital Strategies
Switch to the mobile layout

      Idea R - Bring your website contents to first postion. Get it automatically with Infrared CMS!

Blog

Take a software geek from the 80s, add a new generation graphic designer and dilute with a longtime marketing strategist. Shake vigorously and you'll get the Idea R's blog.

Change language... italiano

Centrare i testi con CSS

Published on 11/7/2011
Categories: Web Design
Tags: CSS, HTML, Tutorials
Centrare i testi con CSS

This article is available in English too.

Per centrare verticalmente un contenuto, si deve usare il seguente codice CSS

div.vCenterWrapper
{
    height: 100%;
    display: table;
    overflow: hidden;
}
div.vCenterContainer
{
    display: table-cell;
    vertical-align: middle;
}
div.vCenterContent
{
}

ed il seguente HTML

<div class="vCenterWrapper">
    <div class="vCenterContainer">
        <div class="vCenterContent">
            ...
        </div>
    </div>
</div>

Il codice CSS però purtroppo non funziona con le versioni precedenti la 8 di Internet Explorer: bisogna aggiungere il seguente codice condizionale

[if lt IE 8.]>
<style type="text/css">
    div.vCenterWrapper
    {
        position: relative;
    }
    div.vCenterContainer
    {
        position: absolute;
        top: 50%;
    }
    div.vCenterContent
    {
        position: relative;
        top: -50%;
    }
</style>
<![endif]

Se si deve centrare anche orizzontalmente la tentazione sarebbe quella di usare la centratura del testo:

<div class="vCenterWrapper" style="text-align:center;">
    <div class="vCenterContainer">
        <div class="vCenterContent">
            ...
        </div>
    </div>
</div>

Purtroppo questo sistema funziona con tutti tranne che con Internet Explorer 6 e Internet Explorer 7.
La maniera corretta di farlo, per centrare ad esempio in una colonna di 500 pixel, è di modificare gli stili:

div.vCenterWrapper
{
    height: 100%;
    width: 500px;
    display: table;
    overflow: hidden;
}
div.vCenterContainer
{
    display: table-cell;
    vertical-align: middle;
}
div.vCenterContent
{
    width: 500px;
    margin-left: auto;
    margin-right: auto;
    text-align:center;
}

You are the reader number 10,054.

Comments

Previous article

Previous article

Reflections on the water with Photoshop

Next article

JavaScript events? What a mess!

Next article

Scroll to top