Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pure HTML/CSS to create triangle pointer under button

Tags:

html

css

Currently I have a div that looks like this:

enter image description here

I need to edit the HTML/CSS so that it displays like this with a small triangle underneath. Ideally, I would like to do this using purely HTML and CSS, no image files. According to CSS-Tricks it can be done.

enter image description here

HTML:

<ul id="nav">
    <li class="active"><a href="#"><div class="triangle"></div>Dashboard</a></li>
    <li><a href="users/index.html"><div class="triangle"></div>Manage Users</a></li>
    <li><a href="tickets/index.html"><div class="triangle"></div>Manage Tickets</a></li>
    <li><a href="reports/index.html"><div class="triangle"></div>Reports</a></li>
</ul>

CSS:

#nav {
    float: right;
    margin: 0;
    padding: 8px 0 0 0;
    list-style: none;
    display: inline-block;
}

#nav li {
    float: left;
    padding: 7px 5px;
    margin: 0 5px;
    font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; 
    font-weight: 300;
    border-radius: 7px;
}

#nav li a {
    color: #0b70cc;
    text-decoration: none;
    padding: 7px 5px;
}

#nav li.active a {
    color: #ffffff;
}

#nav li.active {
    background-color: #0b70cc;
    color: white;
}

JSFiddle

If anyone can help me do this I'd really appreciate it!

like image 644
jskidd3 Avatar asked Aug 13 '13 12:08

jskidd3


People also ask

How do I center a triangle in CSS?

Simply use left: calc(50% - 50px) . You can remove the negative left margin.

How do you make an arrow shape in CSS?

Arrows. To create a simple arrow without a tail, make a box with a width and height, border, as well as zero left and top borders. To make an up arrow, add the transform: rotate(225deg); property, and to make a down arrow, add the transform: rotate(45deg); property to rotate the arrow to 225 and 45 degrees respectively ...


2 Answers

Generate your own div with an arrow at http://cssarrowplease.com/

You can configure it like you want and become a clean CSS


How it works:
Lets' create a very simple triangle with this technique:

.container {
  position: relative;
  display: block;
  width: 120px;
  height: 50px;
  background: blue;
}

.container:after{
  position: absolute;
  bottom: 0;
  height: 0;
  width: 0;
  left: 50%;
  border: 40px solid transparent;
  border-bottom-color: red;
  content: "";
}
<div class="container"></div>

The important thing is height: 0; & width: 0 of the pseudo-element where we apply the border. So you can imagine an element of no size. And this is the origin of the border. So each side of the border is a triangle (try colorize each side in a different color to understand it).
So to create the "single triangle" effect, just apply a transparent color to the border and colorize the triangle you want to display.


Alternative: CSS Clip-Path

You can create a rectangle now very easy with the new clip-path with CSS. Just watch out for the browser-support. As usual, IE & sadly Edge both won't support it as well as Opera Mini: Can I Use

Quick Example:

.new_way {
  position: relative;
  margin: 100px auto;
  background: #88b7d5;
  width: 100px;
  padding: 20px;
  text-align: center;
}

.new_way::after {
  content: '';
  position: absolute;
  top: 100%;
  left: calc(50% - 10px);
  background: #88b7d5;
  width: 20px;
  height: 20px;
  
  /* The points are: (left top: x y, right top: x y, center bottom: x y) */
  clip-path: polygon(0 0, 100% 0, 50% 100%);
}
<div class="new_way">
  Clip Path
</div>

So now you only need 1 line. Nice, isn't it?

like image 131
Michael Schmidt Avatar answered Oct 17 '22 03:10

Michael Schmidt


This is called "callout".

Here are some examples:

http://cssdeck.com/labs/bv45bh6p (some examples)

http://mrcoles.com/blog/callout-box-css-border-triangles-cross-browser/ (with explanation of how it works)

like image 31
keaukraine Avatar answered Oct 17 '22 02:10

keaukraine