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

How to float labels

I was trying to learn "how to float label in input. I came around this pen on CSS Tricks. Here , at first input has been written after label element and then flex-flow:column-reverse has been used. Why this reverse approach has been used. Why not label element has been used before input element and flex-direction:column is used as both will give the same output. Please help. My whole day has been gone in learning about this concept and I still have doubt in it. Please help Treehouse. Thanks.

1 Answer

Steven Parker
Steven Parker
243,318 Points

This trick uses some pretty advanced concepts!

The reason for the reverse flex layout is to display the label before the input, and the reason that the label must come after the input in the HTML is to make it possible to use an "adjacent sibling" selector in the CSS ("input:placeholder-shown + label").

The nice thing about having a CodePen example is that you can easily play around with it to help your understanding. Try making changes to some of the settings (like removing the reverse or changing the direction) and see what happens.

Also note that the word "float" here is being used to describe the particular animation effect being produced and has nothing to do with the CSS "float" property.

Ok got this. Thanks I have some more questions about this pen -

  1. Do input: placeholder-shown+label select only label who are next adjacent sibling of "input" with "placeholder"?
  2. -::webkit-input-placeholder - MDN says "don't use this attribute as this is recognised by only WebKit , then why it's been used here and is there any limitations of using this element? What is the main role of this element?
Steven Parker
Steven Parker
243,318 Points
  1. That's right, the "+" symbol means "next adjacent sibling". Using this is the reason for the HTML order and reversing it with the flex setting.
  2. The author created this specifically for webkit browsers and was not concerned about compatibility.

Did you see his blog post where he explains the limitations of the demo?