How do write and style HTML in a better way? [closed] - html

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
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>

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...
Create a wrapper that will contain all content, because everything will be centered. Set the width, display, etc. for the wrapper.
Inside the wrapper, add a <form>. Style the form if needed.
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.
Inside the form, create a <fieldset> followed by a button. Style the button. You can put the button in a footer if you want.
Inside the fieldset, create 3 input wrappers. Each will contain a label and an input. Style the wrappers.
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.

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>

Related

What to use instead of the <br> tag?

I have a simple form like this:
<form method="post" action="/registration">
<label for="alias">Alias:</label>
<input type="text" name="alias" id="alias">
<br>
<label for="email">E-mail:</label>
<input type="text" name="email" id="email">
<br>
<input type="button" value="registger">
</form>
It works fine, but the I have found out that <br> shouldn't be used for this purpose, as it is only intended to be used with text.
If I remove the <br>, then everything will be rendered on a single line, which I do not want.
What is the correct, most clean way to display name-input pairs in a form with CSS, like this:
Alias: [__field__]
E-mail: [__field__]
[SUBMIT BUTTON]
I'd use divs, which will put the labels and inputs into their own block.
<form method="post" action="/registration">
<div>
<label for="alias">Alias:</label>
<input type="text" name="alias" id="alias">
</div>
<div>
<label for="email">E-mail:</label>
<input type="text" name="email" id="email">
</div>
<input type="button" value="registger">
</form>
I typically would put the input inside of the label (so when you click the label, it focuses the input), and then tell the label to be display: block;.
So,
<form method="post" action="/registration">
<label for="alias">
Alias: <input type="text" name="alias" id="alias">
</label>
<label for="email">
E-mail: <input type="text" name="email" id="email">
</label>
<input type="button" value="registger">
</form>
Then do:
label[for], // just selects labels that have the "for" attribute.
input[type="button"] {
display: block;
// And a bottom margin for good measure :)
margin: 0 0 10px; // shorthand for margin-bottom
}
And that should get you what you want.
You could use divs with corresponding CSS:
.myFrm {
width: 250px;
}
input[type=text] {
float: right;
}
.form-group {
margin-bottom: 10px;
}
.form-group::after {
content: "";
clear: both;
display: table;
}
<form method="post" action="/registration">
<div class="myFrm">
<div class="form-group">
<label for="alias">Alias:</label>
<input type="text" name="alias" id="alias">
</div>
<div class="form-group">
<label for="email">E-mail:</label>
<input type="text" name="email" id="email">
</div>
</div>
<input type="button" value="registger">
</form>
I would just use a bit of css to do the trick. Give each of the labels a display:block;
label {
display: block;
}
You can use container divs around the label and input to group them or else make sure "display: block" is added to the label and input elements.
If you need the label to the left of the input then wrap both with a container div and to give you more control on the positioning you could float the label and input to the left or use flexbox.
You ask:
What is the correct, most clean way to display name-input pairs in a
form with CSS
I interpret your question to be related to matters of performance, code efficiency and maintainability. Since just changing the HTML structure does not address responsiveness in different view-ports, adding bits of CSS may have render blocking features but it does nevertheless makes your application ready for mobile responsiveness. This is how I see it:
form {
display: inline-block;
}
label {
margin: 10px;
font-weight: 600;
}
input{
position: absolute;
left: 15%;
}
input[type=button]{
top: 4%;
}
Note that for mobile viewports you may want to adjust the relative measures with media queries. So the question here is not about writing less code but the main requirements of the application.
Get a plunk for this here
What you have there is a list.
https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/HTML_text_fundamentals#Lists
http://reisio.com/temp/form1.html

What is the purpose of putting form elements inside DIVs?

I'm reading through an HTML file right now and I noticed that the form elements inside one of its forms are placed inside a DIV.
For example.
<fieldset>
<legend>Your Contact Details</legend>
<div>
<label for="author">Name: <em class="required">(Required)</em></label>
<input name="author" id="author" type="text" />
</div>
<div>
<label for="email">Email Address:</label>
<input name="email" id="email" type="text" />
</div>
<div>
<label for="url">Web Address:</label>
<input name="url" id="url" type="text" />
</div>
</fieldset>
I observed that it's perfectly fine to not place them inside the DIVs anyway.
What is the purpose of this?
PS: There was no styling involved in the CSS that target the DIVs, all the styles were specifically targeted for "labels", "inputs" and so on. But none that target the DIVs.
Honestly, the only effect it has on the layout is some tiny padding which can also be set by specifying a special class for the elements, that's all.
And if it's for styling, there's no associated styles set for it anyway, so why put it in the first place?
Can anybody crack the reasoning behind this?
Here's the entire document.
body {
font: 62.5%/1 "Myriad Pro", Frutiger, "Lucida Grande", "Lucida Sans", "Lucida Sans Unicode", Verdana, sans-serif;
}
form {
font-size: 1.4em;
width: 30em;
}
/* fieldset styling */
fieldset {
margin: 1em 0; /* space out the fieldsets a little*/
padding: 1em;
border : 1px solid #ccc;
}
/* legend styling */
legend {
font-weight: bold;
}
form div {
padding: 0.4em 0;
}
/* style for labels */
label {
display: block;
}
/* style for required labels */
label .required {
font-size: 0.75em;
color:#760000;
}
input {
width: 20em;
}
textarea {
width: 100%;
height: 10em;
}
input.radio, input.submit {
width: auto;
}
#remember-me .radio {
margin-right: 1em;
}
/* style form elements on focus */
input[type="text"]:focus, textarea:focus {
background: #ffc;
}
-->
</style>
</head>
<body>
<form id="comments_form" action="#" method="post">
<fieldset>
<legend>Your Contact Details</legend>
<div>
<label for="author">Name: <em class="required">(Required)</em></label>
<input name="author" id="author" type="text" />
</div>
<div>
<label for="email">Email Address:</label>
<input name="email" id="email" type="text" />
</div>
<div>
<label for="url">Web Address:</label>
<input name="url" id="url" type="text" />
</div>
</fieldset>
<fieldset>
<legend>Comments</legend>
<div>
<label for="text">Message: <em class="required">(Required)</em></label>
<textarea name="text" id="text" cols="20" rows="10"></textarea>
</div>
</fieldset>
<fieldset id="remember-me">
<legend>Remember Me</legend>
<div>
<label for="remember-yes"><input id="remember-yes" class="radio" name="remember" type="radio" value="yes" />Yes</label>
</div>
<div>
<label for="remember-no"><input id="remember-no" class="radio" name="remember" type="radio" value="no" checked="checked" />No</label>
</div>
</fieldset>
<div>
<input id="submit" class="submit" name="submit" type="submit"/>
</div>
</form>
</body>
</html>
In the given case, the div markup causes each label/input pair to appear on a line of its own, which is generally a good idea. There are many alternative ways to achieve that, but this is one of the simplest and also useful for styling that you might want to add later.
Moreover, in this case there is a style sheet rule that uses the markup: the selector form div matches these div element, and the rule for it sets 0.4em padding above and below the content of each div.
Besides styling, it also depends on doctype of the markup. For example, with XHTML 1.0 Strict, form elements like <label>, <input> must be wrapped in block-level elements, otherwise it can't pass the W3C Markup Validation Service. However, some other doctypes allow such markup.
Years ago it's a fashion to have a W3C validate banner on your site. Because of that fashion, many people overreact about validation. Like in this example, wrapping with fieldset is valid but people may still go with div or p, just like people avoid using table even if presenting tabular data. If you are reviewing some legacy codes, this might be a possible reason.
the purpose is usually for styling. For example, say you wanted the label to sit above the input like the below example. And you wanted a copy of the same thing just to the right:
Label Here Label Here
Input Here Input Here
it would be very difficult to accomplish this with the following
<label for="author">Name: <em class="required">(Required)</em></label>
<input name="author" id="author" type="text" />
<label for="author">Name: <em class="required">(Required)</em></label>
<input name="author" id="author" type="text" />
Now add in divs and you can float the divs instead (as well as add other CSS like margin to separate the two columns):
<div> <-----Add styles to parent
<label for="author">Name: <em class="required">(Required)</em></label>
<input name="author" id="author" type="text" />
</div>
<div> <-----Add styles to parent
<label for="author">Name: <em class="required">(Required)</em></label>
<input name="author" id="author" type="text" />
</div>
UPDATE
I saw your update, it could also be to clear each "row" since labels and inputs are inline by default. Check out the difference in the fiddle:
FIDDLE
It could be that the developer believes it to be more semantic with divs, it could be to set things up for later incase the layout changes and they want to add CSS like the above example, it could be he/she doesn't really know what they're doing. It could be a number of things
#Johnsy Omniscient : yes some times developers will add additional tags to update the site for future technology. when responsive design was new; we added extra divs, etc to help the transformation when going mobile etc

How to output inline text with bootstrap

I have added a !important to make sure that my css rules comes first but my inputs still behave like it was display in block; fiddle
input{
display: inline!important;
}
<p>My name is
<input type="text" placeholder="Name" class="form-control" style='display:inline;'/>
from USA. And I'm <input type="text" placeholder="21" class="form-control" style='display:inline;'/> years old.
</p>
All you need to do is reset the width to auto - it was 100%. It's worth noting that the Bootstrap styling is coming from the .form-control selector; use that to overwrite it.
Updated Example
.form-control {
width:auto;
display:inline-block;
}
No need for inline styling or !important.
<p>My name is
<input type="text" placeholder="Name" name="name" class="form-control" style='display:inline; width:100px'/>
from USA. And I'm <input type="text" name="age" placeholder="21" class="form-control" style='display:inline; width:100px'/> years old.
</p>
you can resize your input button with CSS including width in input style
I added the form tag for inline forms, it works when you have enough room on the page for one line.
<form class="form-inline" role="form">
<div class="form-group">
<p>My name is
<input type="text" placeholder="Name" name="name" class="form-control" style='display:inline;'/>
from USA. And I'm <input type="text" name="age" placeholder="21" class="form-control" style='display:inline;'/> years old.
</p>
</div>
</form>
Here's some more information explaining it from getbootstrap.com.
"Add .form-inline to your for left-aligned and inline-block controls. This only applies to forms within viewports that are at least 768px wide.
Requires custom widths
Inputs, selects, and textareas are 100% wide by default in Bootstrap. To use the inline form, you'll have to set a width on the form controls used within."
http://getbootstrap.com/css/
Works in jsfiddle when it's wide enough.

Aligment of textbox in proportion to the text

How do I correct the following E-mail textbox alignment: ?
To make it look like this:
I know I can use tables, but how do I solve this problem without using tables? CSS maybe?
HTML:
<form action="" name="contactform" method="post">
<p></p>
First name: <input type="text" class="contact" name="contactfirstname" value="">
<br/>
Last name: <input type="text" class="contact" name="contactlastname" value="">
<br/>
E-mail: <input type="text" class="contact" name="email" value="">
<p></p>
The most minimalized version I could think of...
<form>
<label>First Name: <input type="text" name="firstName"></label>
<label>Last Name: <input type="text" name="lastName"></label>
<label>Email Address: <input type="email" name="emailAddress"></label>
</form>​
and
form {
width: 300px;
}
label {
display: block;
margin: 5px;
padding: 5px;
clear: both;
}
label input {
float: right;
}​
Since OP has edited his question to include his markup, I'll expand the answer.
Some Points of Improvement:
Remove the empty <p> element, and the <br/> elements. They have no value inside a form.
Use <label>s, that's what they were made for. You can wrap the label and the input inside of the <label> tag, or you can use <label for="element_id">Label</label><input id="element_id">.
Be consistent. If you decided to go with the <br /> type of format for singular tags, stick with it to the <input />s as well.
Use correct input types for specific inputs, there is type="email" for the email field, which will optionally have the browser check for you if it's a valid email address or not!.
Use CSS for design and layout, not <p>s and <br>s.
Good luck!
I'm assuming your HTML is something like:
<p>
Email
<input />
</p>
Change this to:
<p>
<label>Email</label>
<input />
</p>
This means you can then apply a fixed width to all your labels, making them consistent:
label
{
width:100px;
float:left;
}
http://jsfiddle.net/zvWqk/1/
Or as #Zeta has pointed out, nest your input inside the label, and float right. This will prevent you needing to apply a for attribute to your label.
http://jsfiddle.net/tt8gx/
Use CSS to make the labels display as block elements and have a fixed width. Display the inputs as block elements and float them left. Put a clear:left on the labels so they'll each be on a new line.

Django not rendering CSS correctly

I have a site that I'm creating, part in static HTML, the other part is served via Django. Since I want the look and feel to remain the same (who doesn't?) I have used CSS for the static site. That same CSS I have included (almost successfully) in the dynamic site.
When I create a form, I can get a very nice two column listing on the static side
Label Input
Label Input
Label Input
But, when I do the same code on the dynamic side, it's not so nice
Label Input
Label Input
Label Input
The CSS I'm using is:
form.login label.fixedwidth {
display: block;
width: 240px;
float: left;
}
\.
Sorry, here's my form:
<form action="" method="post" class="login">
<fieldset>
<div>
<label for="username" class="fixedwidth">User name:</label>
<input type="text" name="username" value="" id="username">
</div>
<div>
<label for="password" class="fixedwidth">Password:</label>
<input type="password" name="password" value="" id="password">
</div>
<input type="submit" value="login" />
</fieldset>
</form>
[edit]
So, I noticed that my two 'input type' lines didn't close the tag (no '/'). But, no difference.
[/edit]
Try
clear:both; overflow: auto
on the surrounding DIV.
By the way, a <ul> with <li> s may be semantically more fitting than <div>s here. Won't make a difference in the output though.