1

What's a reasonable strategy for solving this problem that meets the understanding level of a novice coder?

Here's what I've tried so far for a writing a simple form: 1) write the html in its entirety 2) style it 3) try to change the html to suit the style methods I'm trying to apply

It doesn't work because I get confused about how I should structure the html hierarchy to suit the design parameters I have on paper.

Here's where I am: https://codepen.io/tapzx2/pen/wvMgGGY

<div class="baby-form">
<h2>Class Signup</h2>
<form action="#" method="post">
  <ul>
    <li>
      <input type="text" name="first-name" id="first-name">
      <label for="first-name">First Name</label>
    </li>
    <li>
      <input type="text" name="last-name" id="last-name">
      <label for="last-name">Last Name</label>
    </li>
    <li>
      <input type="email" name="email" id="email">
      <label for"email">Email</label>
    </li>
    <li>
      <button type="submit">Register</button>
    </li>
  </ul>
</form>

Here's where I'd like to go: https://codepen.io/tapzx2/pen/qBbqxjX

<div class="content-container">
  <h2>Class Signup</h2>
  <div class="form-container">
    <form>
      <div class="question-container">
        <div class="question">
          <label for="first">First Name</label>
          <input type="text" id="first" name="first">
        </div>
        <div class="question">
          <label for="last">Last Name</label>
          <input type="text" id="last" name="last">
        </div>
        <div class="question">
          <label for="email">Email Address</label>
          <input type="email" id="email" name="email">
        </div>
      </div>
      <button type="submit">Register</button>
    </form>
  </div>
</div>
tapzx2
  • 1,021
  • 1
  • 8
  • 11
  • none, use fieldset tag, and don't use `id` for form elements – Mister Jojo Jun 18 '20 at 20:53
  • 2
    @MisterJojo _"don't use id for form elements"_ Why not? – j08691 Jun 18 '20 at 20:59
  • you don't need them (for the code you have) – Mister Jojo Jun 18 '20 at 21:11
  • 1
    I always ask my students: "does it work?" If the answer is yes (styled the way you wanted, form submits, etc) then don't worry about changing it. The more of this you do the better you'll get at it. Almost all code can be optimized but that doesn't mean it needs to be. If your goal is continued learning. They write it again from scratch a few times. (don't change your existing) This way you learn how to plan the structure as you go. – Bryce Howitson Jun 18 '20 at 21:24
  • As a caveat to `fieldset` elements, they don't work as a flex parents: https://stackoverflow.com/questions/28078681/why-cant-fieldset-be-flex-containers – disinfor Jun 18 '20 at 21:48

2 Answers2

2

I struggled with this for a long time too. My CSS never had the effect that I wanted it to. Practice is key, but I find that when beginning, it is easier to take a "general-to-specific" or approach and style as you go.

Once you are comfortable with CSS, you can do all the HTML before you start styling. In the meantime, I recommend you style with each layer (see below).

For example, if I wanted to build what you've given as your end result I would do something like...

  1. Create a wrapper that will contain all content, because everything will be centered. Set the width, display, etc. for the wrapper.
  2. Inside the wrapper, add a <form>. Style the form if needed.
  3. Inside the form, add a <header>, and inside the header, add an <h_> tag. The header isn't necessary, but it makes it easier to add other elements to the top of the form in the future.
  4. Inside the form, create a <fieldset> followed by a button. Style the button. You can put the button in a footer if you want.
  5. Inside the fieldset, create 3 input wrappers. Each will contain a label and an input. Style the wrappers.
  6. Add the labels and inputs inside each wrapper. Style these and style the fieldset if needed.

I like to work downwards in layers, don't start working on new child elements until the layer is complete. I started with the "general" elements (ex. the main wrapper) and worked towards the "specifics" (ex. the labels, inputs, buttons). As you style each layer, be conscious of what will be in the next. Eventually you'll get the feel of what elements you need to have in place so that you can style properly.

AJ_
  • 1,455
  • 8
  • 10
  • This is extremely helpful. Follow up question: is there a word / concept you know of that would describe the difference between my process and your process? Reason: I'm looking for resources that talk about the ways to approach the process of coding, maybe, depending on level? Like you pointed out, I'm writing in a way might work for someone more experienced. Doesn't work for me as a novice. The process you suggested will probably work really well for me, but I'd have like to have known about it 2 months ago. – tapzx2 Jun 18 '20 at 21:38
  • 1
    Really good question, and I really wish I had an answer because I want a resource like that. The problem is that there is too much variation and possibility with the programming problems we face for an process to be useful. It's difficult to balance applicability and vagueness. You want the process to work with many problems, but in doing so you have to use very broad terms which can leave beginners lost. You might want to look at the different software development philosophies, but these are more guidelines for within different programming paradigms than processes for specific languages. – AJ_ Jun 18 '20 at 22:03
  • 1
    I think it all just comes down to practice. As I did more projects, my development process got better across all languages, not just the ones I work with. You realize it's easier to plan even minor projects properly and work in layers, and over time you refine the process. In the beginning, progress is slow and it can be painful, but it feels really good when you finally start feeling comfortable. Just don't give up on it – AJ_ Jun 18 '20 at 22:06
  • I've read through a few of the software dev philosophies and they'll be extremely useful in... 2 years. I'm down to practice, and I do, but if something is like pulling teeth because the processes is poor, my instinct is that there's got to be a better way. I think you're onto something with the working in layers idea. I'm very curious about how programmers do that planning and then do that work in layers. Follow up question: How do you think could I ask what you think is a "really good question" on the SE network w/o the question being voted down and closed? Is there a better forum? – tapzx2 Jun 18 '20 at 23:19
  • There's a typo in your question, but in terms of forums I have found SO to be the best for programming. It's a very efficient system. When it comes to asking questions, be as direct as possible. Creating [minimal reproducible](https://stackoverflow.com/help/minimal-reproducible-example) examples makes it easier for people to answer your question and it makes it easy for you to find the source of the problem. I think that you should use the meta forums for asking questions about creating quality questions though. – AJ_ Jun 19 '20 at 01:34
  • Check my [my SO profile](https://stackoverflow.com/users/2015909/rene-van-der-lende?tab=profile) for a moment. Even without having learned a software development methodology, **w8b4udo** will help you to structure your thoughts and workflow. It may seem to be tedious and much work initially, but it will save much time overall. **Think 'pyramid'**: a tiny oversight or omission in the beginning will lead to major problems later in the process. The fun part: you can start using it right now and only requires you to have your head screwed on (the right way). BTW, @Bryce Howitson has a good point! – Rene van der Lende Jun 19 '20 at 13:38
  • @tapzx2, check my previous comment here. And the Q&A of [this SO question (62389047)](https://stackoverflow.com/questions/62389047) may be of interest to you too... – Rene van der Lende Jun 19 '20 at 15:30
0

this way:

see JS Code : there is only one const (myForm) needed for access every form elements.

Form elements necessarily need a name attribute (used on the submission), and their use is easy to match in JS to identify each element of the form

const myForm = document.getElementById('my-form')

myForm.onsubmit=e=>
  {
  e.preventDefault()

  console.log(  'first-name = ', myForm['first-name'].value )
  console.log(  'last-name = ', myForm['last-name'].value )
  console.log(  'email = ', myForm.email.value )
  }
#my-form {
  width: 14em;
  margin: 1em auto;
}
fieldset {
  border: 1px solid lightblue;
  padding: 1em .6em 0em .6em;
}
label {
  display: block;
  float: left;
  clear: both;
  min-width: 12em;
  margin-bottom: 1em
}
button {
  margin-top: 1em;
  float: right;
}
<form action="#" method="post" id="my-form">
  <fieldset>
    <legend>Class Signup</legend>
    <label>
      First Name
      <input type="text" name="first-name" >
    </label>
    <label>
      Last Name 
      <input type="text" name="last-name" >
    </label>
    <label>
      Email Address
      <input type="text" name="email" >
    </label>
  </fieldset>
  <button type="submit">Register</button>
</form>
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40