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

Having trouble producing block and inline block html/css form labels & inputs based on breakpoints.

Hi there!

I'm having a bit of trouble here with an html form I've created. I'm styling it mobile first and have a breakpoint set at 760px for some slight changes.

When in mobile I'd like the labels to be on top of each input. When greater than 760px I'd like the labels to be inline with each input.

Unfortunately the closest I can get to having them inline is having the label in the correct spot and the input to the right of it but slightly below the label too instead of having them on the same line.

HTML:

  <section id="contact">
    <form id="contact-form" action="index.html" method="post">
      <fieldset>
        <legend>Contact Information</legend>

        <div class="contact-item">
          <label for="name">Full Name </label>
          <input type="text" id="name" name="user_name" placeholder="Required">
        </div>

        <div class="contact-item">
          <label for="email">Email Address </label>
          <input type="text" id="email" name="user_email" placeholder="Required">
        </div>

        <div class="contact-item">
          <label for="phone">Phone Number </label>
          <input type="text" id="phone" name="user_phone" placeholder="Include 1 + area code">
        </div>

        <div class="contact-item">
          <label for="address">Street Address </label>
          <input type="text" id="address" name="user_address">
        </div>

        <div class="contact-item">
          <label for="city">City </label>
          <input type="text" id="city" name="user_city">
        </div>

        <div class="contact-item">
          <label for="state">State </label>
          <input type="text" id="state" name="user_state">
        </div>

        <div class="contact-item">
          <label for="name">Zip </label>
          <input type="text" id="zip" name="user_zip">
        </div>

      </fieldset>
    </form>
  </section>

I was under the impression that I could just set the label and input to 50% and float the label left and the input right and that should work.

Poul Larsen
Poul Larsen
5,902 Points

don't I deserve a point?

Didn't know how to do that... I think I gave you them now?

4 Answers

Poul Larsen
Poul Larsen
5,902 Points

I made another one with more color and css to it

Codepen: http://codepen.io/Slitch/pen/MKbEXg

Much appreciated Poul. You've been very helpful!!!

Poul Larsen
Poul Larsen
5,902 Points
<style>

/* code for label and input when screen is mobile */

  label {
    display: block; 
  }
  input {
    display: block;
  }



/* code for label and input when screen NOT mobile */

@media screen and (min-width: 760px) {

  label {
    display: inline;
  }
  input {
    display: inline;
  }

}

</style>

Right, so that works but I'd like to have the inputs floated to the right so that they all line up with each other. I'd imagine the labels would be floated to left then too? Doing that causes them to jump around quite about and setting each to 50%; width keeps them stacked all the time.

Here's a codepen

Poul Larsen
Poul Larsen
5,902 Points
<style>

  label {
    display: block;
  }
  input {
    display: block;
  }


@media screen and (min-width: 760px) {

  label {
    display: inline-block;
    width: 150px;
  }
  input {
    display: inline-block;
    width: 200px ;
  }

}

</style>
```html

Thank you, that'll work!