Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Creating a pointing arrow on one side of an element

Hi everyone,

I'm currently working through the jQuery Basics course, and on the sign up form section, we're given the basic code for the form which includes a <span> with a little arrow on the left-hand side pointing to the relevant form field. Screenshot here: http://s33.postimg.org/eyp073vn3/Screen_Shot_2016_05_30_at_16_11_56.png

Not relevant to the course at all, but how is that created? Feel like I'm missing something very obvious in the styling, but can't work it out.

Relevant parts of the code (I think) are...

<p>
<label for="password">Password</label>
<input id="password" name="password" type="password">
<span>Enter a password longer than 8 characters</span>
</p>
span {
  border-radius: 5px;
  display: block;
  font-size: 1.3em;
  text-align: center;
  position: absolute;
  background: #2F558E;
  left: 105%;
  top: 25px;
  width: 160px;
  padding: 7px 10px;
  color: #fff;
}
span:after {
  right: 100%;
  top: 50%;
  border: solid transparent;
  content: " ";
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;
  border-color: rgba(136, 183, 213, 0);
  border-right-color: #2F558E;
  border-width: 8px;
  margin-top: -8px;
}

Thanks a lot

1 Answer

Steven Parker
Steven Parker
243,318 Points

That little "arrow" is a pseudo-element that has no size but a border that is transparent except for the right side, creating a little blue triangle.

Some of the course project effects rely on each other. For example, without the positioning context established by the paragraphs, the absolute positioning was placing the "arrow" offscreen.

Try it this way, making the span its own positioning context:

span {
  position: relative;
  font-size: 1.3em;
  text-align: center;
  padding: 7px 10px;
  margin-left: 10px;
  border-radius: 5px;
  background: #2F558E;
  color: #fff;
}
span::before {
  position: absolute;
  left: -16px;
  top: 50%;
  content: " ";
  height: 0;
  width: 0;
  pointer-events: none;
  border: solid transparent;
  border-right-color: #2F558E;
  border-width: 8px;
  margin-top: -8px;
}

Ahh i see! That makes sense. Very neat.

Thanks very much for your help :-)