Clicky

Triángulos con CSS

triángulos con CSS

Usando CSS puro se pueden crear triángulos compatibles entre navegadores con muy poco código

El secreto de estos triángulos es crear borders gigantes a los dos lados perpendiculares de la dirección en que desea que apunte el triángulo. Hacer el border del lado opuesto del mismo tamaño con el color de fondo o cualquier color que quieras poner en cada tooltip. Un border más grande, agranda el triángulo. Puedes colorear tus triángulos con cualquier color, cualquier tamaño y en cualquier dirección. La mejor parte es que es necesario muy poco código para lograr este efecto. Ver demo.

El CSS

/* create an arrow that points up */
div.arrow-up {
    width: 0;
    height: 0;
    border-left: 5px solid transparent;  /* left arrow slant */
    border-right: 5px solid transparent; /* right arrow slant */
    border-bottom: 5px solid #2f2f2f; /* bottom, add background color here */
    font-size: 0;
    line-height: 0;
}

/* create an arrow that points down */
div.arrow-down {
    width: 0;
    height: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-top: 5px solid #2f2f2f;
    font-size: 0;
    line-height: 0;
}

/* create an arrow that points left */
div.arrow-left {
    width: 0;
    height: 0;
    border-bottom: 5px solid transparent;  /* left arrow slant */
    border-top: 5px solid transparent; /* right arrow slant */
    border-right: 5px solid #2f2f2f; /* bottom, add background color here */
    font-size: 0;
    line-height: 0;
}

/* create an arrow that points right */
div.arrow-right {
    width: 0;
    height: 0;
    border-bottom: 5px solid transparent;  /* left arrow slant */
    border-top: 5px solid transparent; /* right arrow slant */
    border-left: 5px solid #2f2f2f; /* bottom, add background color here */
    font-size: 0;
    line-height: 0;
}

Triángulos con CSS : :before y :after

Los anteriores ejemplos CSS utilizan elementos reales, pero ¿qué pasa si no quieres añadir triángulos individuales? Los triángulos CSS pueden crearse con pseudo-elementos, lo que es el caso perfecto para, por ejemplo, sugerencias en un artículo. Aquí cómo se puede hacer:

div.tooltip {
    /* tooltip content styling in here; nothing to do with arrows */
}
/* shared with before and after */
div.tooltip:before, div.tooltip:after {
    content: ' ';
    height: 0;
    position: absolute;
    width: 0;
    border: 10px solid transparent; /* arrow size */
}

/* these arrows will point up */

/* top-stacked, smaller arrow */
div.tooltip:before {
    border-bottom-color: #fff;  /* arrow color */

    /* positioning */
    position: absolute;
    top: -19px;
    left: 255px;
    z-index: 2;
}

/* arrow which acts as a background shadow */
div.tooltip:after {
    border-bottom-color: #333;  /* arrow color */

    /* positioning */
    position: absolute;
    top: -24px;
    left: 255px;
    z-index: 1;
}

triángulos con CSSEl lado del border al que se agrega el color es el lado opuesto del puntero de flecha. Además no es necesario utilizar las dos cosas (pseudo-elementos :before y :after) sólo se necesita usar una. La segunda flecha, no obstante, podría ser utilizada como una sombra de background o "border" background.

Ver Demo

Jesus_Caceres