Labels under inputs inline with other text - html

I need help aligning labels under input boxes such as a description of what should be input into the textbox, but I also need text aligned with the textbox after
I tried making new table rows below the input boxes and using margins, but this doesn't line up in Chrome, it does in IE, but not Chrome.
I've tried
< tr >< td colspan='1' >< span style='display: inline-block;' >You will arrive on: < input type='text' >< label >(Date)< /label >at < input type='text' > < label >(Time)< /label>< /span >, please bring all documents with you.
This should be:
You will arrive on: at (inline with textboxes) , please bring all documents with you.(align with previous text boxes, so this should be above the labels as well.)
Rephrasing: I need something like a paragaph, with a block of text, but with inputs mixed into this text. Then I need small descriptive labels directly under the inputs. I don't want these small descriptive labels to be inline with the other paragraph's text only right under the inputs... example:
"This is a paragraph < input text here > < label that goes under input > paragraph text continues on here < another input text here > < label that goes under this input > again the paragraph text goes on here inline with the other paragraph text and the inputs"

I think I've understood what you're trying to ask. You need to wrap the label and input in a div so that the label can be positioned off of the input tag.
.label-under {
position: relative;
display: inline;
}
.label-under label {
position: absolute;
left: 0px;
top: 10px;
}
span {
line-height: 2.5;
}
<span style='display: inline-block;' >You will arrive on: <div class="label-under"><input type='text' >
<label>(Date)</label></div> at <div class="label-under"><input type='text' >
<label>(Time)</label></div>, please bring all documents with you.</span>
Edit: You may need to adjust the line height of the span element if you're viewing on a device with a small width.
// Is optional and can be removed.
span {
line-height: 2.5;
}
// But top: 10px must be changed to top: 20px

skip the table and use display flex instead.
.form {
display: flex;
align-items: center;
}
.input-container {
display: flex;
align-items: flex-end;
margin-right: 2rem;
}
.input-container__label {
margin-right: .5rem;
}
<form action="" class="form">
<div class="input-container">
<label for="email" class="input-container__label">Email</label>
<input type="text" class="input-container__element">
</div>
<div class="input-container">
<label for="something" class="input-container__label">Something</label>
<input type="text" class="input-container__element">
</div>
</form>

Related

How can I get this input element to fill all remaining space on the page without being pushed to the next line?

I'm attempting to recreate Zork, and so I have a large chunk of text that always ends with ">" on a new line, to mark where the input area is, as seen here:
https://i.imgur.com/NYwSfB4.png
The issue is that I'd like that input area to take up the remaining space on the page. Everything I have tried so far either pushes it to the next line, or works but is pretty janky in terms of implementation. To get it to work I had to break the text into two sections, the majority of the text, and the indicator. This can be seen here:
<div id="game-text">
West of House<br/>
You are standing in an open field west of a white house,
with a boarded front door.<br/>
There is a small mailbox here.<br/>
<br/>
</div>
<span id="indicator">
>
</span>
<span id="input-span"><input id="user-input"/></span>
#indicator{
display: table-cell;
}
#input-span {
display: table-cell;
width: 100%;
}
#user-input{
width: 100%;
}
This works, but ideally i'd like to remove the indicator span entirely and have the '>' symbol placed within the game-text div itself, which I imagine from my experimenting will require me to switch the div to a span anyways. Any help to smooth this out would be greatly appreciated.
EDIT: This is what i'd like the html to end up looking like, if possible:
<span id="game-text">
Big wall of text<br/>
<br/>
>
</span>
<span id="input-span"><input id="user-input"/></span>
No need to use another span around the > to get the desired look. You can use display:flex; and add flex-grow:1; to the input to make it fill the row.
body {
color: #fff;
background: #000;
}
.user-input-container {
display: flex;
justify-content: start;
}
#user-input {
margin-left: 5px;
flex-grow: 1;
}
<div id="game-text">
West of House<br/> You are standing in an open field west of a white house, with a boarded front door.<br/> There is a small mailbox here.<br/>
<br/>
</div>
<div class="user-input-container">><input id="user-input" /></div>
You can use Flex properties:
HTML
<div class="flexContainer">
<span>
>
</span>
<input id="user-input"/>
</div>
CSS
.flexContainer{
display: flex;
flex-direction: row;
input{
width: 100%
}
}
If the idea is to use the table-layout, then you need a parent acting like a table and only set the first span as a cell, This should not be your answer :
p[id] {
display: table;
width: 100%;
padding: 0 1em;
box-sizing: border-box;
}
#indicator {
display: table-cell;
width: 0;/* first funny thing about table-layout */
}
#input-span {
display: block;/* nop , i'll be a block, not a cell, so i use the whole space left !! */
}
#user-input {
width: 100%;/* let's use the width of my parent */
}
<p id> <!-- i'll be a table or tbody, tr -->
<span id="indicator"> > </span><!-- i'll be a cell -->
<span id="input-span"> <!-- i'll be a block -->
<input id="user-input"/> <!-- i'll be resized to 100% width -->
</span>
</p>
But today, flex or grid are better option and much more easy to understand and set in action ;) that's what i'd go for. In any case, you'll need to wrap those elements together.

How do I make an HTML element start at the same line but end with a newline?

I'd like to have a newline (<br>) after each input control. How do I do that in CSS?
I must add: I want the input control to start on the same line as the label or anything that precedes it but to have a newline at its end so that anything follows it starts on a separate line.
I understand this would have to do with the Flex box model but I am not quite acquainted with that yet.
label {
font-weight: bold;
}
input {
display: / * what goes here? */
}
<label for="theButton">Button</label>
<input name="theButton" type="button" value="Click me">
<label for="theTextBox">Button</label>
<input name="theTextBox" type="text">
Use a pseudoelement on the labels to force a line-break
label::before {
content: "\A"; /* U+000A is the newline sequence */
display: block;
}
Codepen demo
This will place the label on a new line but it will keep the input in the same line of the previous label. You can also control the space between rows by setting an height on the pseudoelement
In case you cannot/will not change the markup set display:block with input and float:left with label.
label {
font-weight: bold;
float: left;
margin-right: 10px;
}
input {
display: block;
}
<label for="theButton">Button</label>
<input name="theButton" type="button" value="Click me">
<label for="theTextBox">Button</label>
<input name="theTextBox" type="text">
By the way It's better to consider adjusting the HTML and use more elegant ways.

How long label (spread on multiple lines) of the HTML checkbox input can be aligned with the start of the label on the first line?

This checkbox is HTML input of type checkbox. Its picture and HTML is given below.
<div id="div123" style="word-wrap:break-word;">
<input tabindex="5" enterastab="" type="checkbox" id="chkBx" name="v_108950" value="OPTION_1111111111111"/>
<label id="lbl123" for="chkBx" unselectable="on">Option 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111</label>
</div>
Question: When the labels are too long, how can I make the label printed on next lines start right below the "O" instead of starting from the left side of the checkbox.
You can use Flexbox and set display: flex on parent div element. Also you can add word-break: break-all on label but you also need to add br after first word so that numbers appear under option.
div {
display: flex;
align-items: flex-start;
}
label {
word-break: break-all;
}
<div id="div123">
<input tabindex="5" enterastab="" type="checkbox" id="chkBx" name="v_108950" value="OPTION_1111111111111" />
<label id="lbl123" for="chkBx" unselectable="on">Option <br> 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111</label>
</div>
You can create a 2-column layout with Flexbox
#div123 {
/* Create a flexible container with the display property */
/* A flexible container will create a separate column for each of its children, here a checkbox and a label */
display: flex;
/* align-items decide how to align the columns vertically */
/* with flex-start, the checkbox and the label will be align to the top */
align-items: flex-start;
}
<div id="div123">
<input type="checkbox" id="chkBx" name="v_108950" value="OPTION_1111111111111" />
<label id="lbl123" for="chkBx" unselectable="on">Option 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111</label>
</div>
You can use inline-block property here or float property
input {
display: inline-block;
vertical-align: top;
}
label {
display: inline-block;
vertical-align: top;
width: calc(100% - 40px);
}

Changing/Styling individual input elements in HTML

Is there anyway to target specific input text elements in a form and reposition it, because it's letting me do things such as adjust padding, but I cannot reposition it by using margin. I also can't change the color, unless I do a style on the label. But doing a color on the label is useless because there's still the color in the input that I want to change. In short, I don't know how to override the original rule that I have for my input elements. Margins are not working either.
code:
input[type=text] {
width: 75%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block border: 1px solid #ccc;
border-radius: 4px;
color: #ffb3ec;
font-size: 24px;
box-sizing: border-box;
background-color: #4d4100;
}
.sample {
width: 30px;
background-color;
green;
}
<div class="mainBox">
<form>
<fieldset>
<label>placeholder:
<input type="text" id="placeholder" name="placeholder">
</label>
<br>
<label>Targets
<input type="text" class="sample" name="sample">
</label>
</fieldset>
</form>
</div>
You can wrap your input into a div element like below and reposition that:
<div class = "mainBox" >
<form>
<fieldset>
<div class = "wrapper">
<label> placeholder: </label>
<input type = "text" id = "placeholder" name = "placeholder" >
</div>
<br>
<div class = "wrapper">
<label> Targets </label>
<input type = "text" class = "sample" name = "sample" >
</div>
</fieldset>
</form>
</div>
I have rearranged the code a bit to fix the colour problem. It occurred, because you wrapped your input element inside the label. It doesn't work this way. Also remember to use for = "[THE ID OF YOUR INPUT ELEMENT]" to bind them together like so:
<label for = "sample"> Targets </label>
<input id = "sample" type = "text" class = "sample" name = "sample" >
In addition, before trying to play with top, bottom, left, right, paddings and margins always define the position of the element, because the default position is set to static, which means it isn't supposed to be moved.
position: static /* default, not supposed to be moved */
position: relative; /* to have move around its relative area */
position: absolute; /* to have move around based on the parent element */
position: fixed; /* to have move around based on the body of the document */

Is it possible to put "default TEXT" in "CSS style file"?

I need this "default text" in my CSS file, for example to an <input> tag and to a <textarea>,
so I search for something like:
<style>
testcss{
default:"DefaultText";
or
value:"DefaultText";
}
</style>
So, here is my question,
I have several <input> in my form, and I need to set them all "same default value"! for example same "placeholter" ou same "value" and, I need this by CSS <style>!
You can use javascript
var inputs = document.getElementsByTagName('input');
for (i = 0; i < inputs.length; i++) {
inputs[i].value = "Default Text";
}
Yes it is possible, with a <label> placed behind the input using z-index and a transparent background-color on the <input>. Use :focus to change to a white background. Use :valid :invalid that the placeholder don't shine through if text is entered. With .input:before your "styling" the content of the label. :first-line has sometimes some Firefox issues. With my Firefox for mac it worked with this code.
HTML
<label class="input"><input type="text" required="required"/></label>
CSS
.input {
color: gray;
display: block;
font-size: small;
padding-top: 3px;
position: relative;
text-indent: 5px;
}
input {
background-color: transparent;
left: 0;
position: absolute;
top: 0;
z-index: 1;
}
input:focus, input:first-line {
background-color: white;
}
.input:before {
content: "Some CSS Text";
}
input:valid { background-color:white; }
input:invalid{ background-color:transparent; }
Screenshot (chrome browser)
without Text
without text and focus
with text and focus
with text in it.
See https://jsfiddle.net/uueojg2g/1/ for testing.
Summary
Would I recommend using css for your task? Perhaps not, cause you should use css for presentation only. So I would always to try to get a html variant with placeholder
How it works with "pure" html
Preferred method, and works in all current browsers:
<input type="text" name="" placeholder="Full Name"/>
For IE9 and before, we just need some javascript:
<input type="text" name="" value="Full Name" onfocus="value=''" onblur="value='Full Name'"/>
Remember to use html for content and css for presentation.
So you could actually do that inside of inside of html input tag by using value attribute:
<input value="default text">
As for the text area, you put the default value in between the tags:
<textarea> default text </textarea>
You can use javascript or jquery to make it more convenient, like making the default text disappear when user clicks on textarea, or input element, but that is out of the scope of this question.