Align input on current line at specific position in CSS [duplicate] - html

This question already has answers here:
Align labels in form next to input
(8 answers)
Closed 5 years ago.
I am trying to create a settings page in HTML/CSS. I have a rounded corner div with some text and an input box inside.
I want to align the input boxes as seen in the image below with the red line. Is there a way in CSS to push the input boxes over and have them line up in each div?
CSS
.mainItem {
background-color: white;
color: #38434e;
padding: 10px;
border-radius: 10px;
margin-bottom: 10px;
border: 1px solid #d6d9e5;
}
.pageInput {
position: absolute:
left: 200px;
}
HTML
<div class="mainItem">Business Name<input type="text" class="pageInput"></div>
<div class="mainItem">Address<input type="text" class="pageInput"></div>
Bhuwan's solution doesn't work and looks like this:

I would put your text in a label and give that a width:
.mainItem {
background-color: white;
color: #38434e;
padding: 10px;
border-radius: 10px;
margin-bottom: 10px;
border: 1px solid #d6d9e5;
}
.mainItem>label {
display: inline-block;
width: 150px;
}
<div class="mainItem"><label for="input1">Business Name</label><input type="text" id="input1" class="pageInput"></div>
<div class="mainItem"><label for="input2">Address</label><input type="text" id="input2" class="pageInput"></div>
Labels make your form more accessible too as you can now click on the label to focus your input

Firstly, you don't need position absolute for this. Second, use labels for your input fields and give them the same width:
<div class="mainItem">
<label for="input1">Name </label>
<input type="text" class="pageInput" id="input1">
</div>
<div class="mainItem">
<label for="input2">Address</label>
<input type="text" class="pageInput" id="input2">
</div>
In the CSS:
.mainItem label {
width: 20%;
display: inline-block;
}

Related

How to create a color box in front of a radiobutton in html?

I want to create a radiobutton with a small color box in front of it.
This is the current code that I have written:
<label class="container" style="color: #0B9BCD">
<div class="foo"></div>
<input type="radio" name="radiobutton" id="highlight1" value="highlight1"/>
<span class="checkmark"></span>
</label>
where the class foo has the following style:
.foo {
float: left;
width: 20px;
height: 20px;
margin: 5px;
border: 1px solid black;
background: #13b4ff;
}
And the output I am getting is something like this:
How can I move the color box to the right of the radiobutton?
By moving the div with className "foo" below the radio input and giving the container a display: flex property we can achieve the desired solution.
follow this link to know more about the flexbox.
https://yoksel.github.io/flex-cheatsheet/
<label class="container">
<input type="radio" name="radiobutton" id="highlight1" value="highlight1" />
<div class="foo"></div>
</label>
.foo {
width: 20px;
height: 20px;
border: 1px solid black;
background: #13b4ff;
}
.container{
display: flex;
}
Change the order. Your foo is ordered before your input.

Horizontaly aligning placeholder in input field

What would be correct approach to aligning placeholder to the top of the field, while input text appearing normally in the middle?
Any way to do that with CSS on input/::placeholder only, or should i rather construct a wrapper with span that would disappear when active and input field below it?
Here's a fiddle of what i've got now: https://jsfiddle.net/ejsLfvdn/1/
And that's what it should look like up to customers will:
The input masks are not the case here, i'm only struggling with the placeholder being aligned to the top, while input should appear normally in the middle. The placeholder MUST disappear after filling input.
I don't think that you will be able to do this by directly targeting the placeholder pseudo class (::placeholder).
Only a small subset of CSS properties can be applied to this element and position is not one of them:
https://developer.mozilla.org/en-US/docs/Web/CSS/::placeholder
I think you will need to take the approach of a wrapper with span and input and position appropriately.
You could use something like this with the only issue being the input must have the required attribute.
* {
box-sizing: border-box;
}
.input {
display: flex;
flex-flow: column-reverse nowrap;
border: 1px solid gray;
width: 220px;
}
.input input:valid + label {
opacity: 0;
}
.input input {
width: 100%;
padding: 10px;
border: none;
}
<div class="input">
<input required id="username" name="username" type="text" />
<label for="username">Username</label>
</div>
I hope I achieved what you need.
btw, I used jquery to hide the placeholder while typing and display it again if the field is empty.
$('.form-control').keyup(function(){
var val = $(this).val();
if(val == ""){
$('.placeholder').show();
}else{
$('.placeholder').hide();
}
});
.input-cont{
position: relative;
}
.form-control{
border: 1px solid #DDD;
border-radius: 5px;
height: 40px;
padding-left: 8px;
}
.placeholder{
position: absolute;
top: 5px;
left: 8px;
color: #3dc185;
font-size: 12px;
text-transform: uppercase;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<body>
<form>
<div class="input-cont">
<span class="placeholder">ImiÄ™</span>
<input class="form-control" type="text" name="name">
</div>
</form>
</body>
</html>
You can use translateY(-100%) on your placeholder to move the text upwards and then give your textbox some padding at the top to reveal the text:
.placeholder-offset {
font-size: 20px;
padding-top: 25px;
}
.placeholder-offset::placeholder {
color: red;
transform: translateY(-100%);
}
<input type="text" placeholder="Username" class="placeholder-offset" />

How can I add a red asterisk in a placeholder? [duplicate]

This question already has answers here:
2 colors in one placeholder of input field
(3 answers)
Closed 4 years ago.
The placeholder should show Enter a name* with a white text but the * red.
I'm using Bootstrap 4 in Chrome. My HTML and CSS code are these:
.form-control::-webkit-input-placeholder::after {
color: red;
text-align: right;
font-size: 50px;
content: "*";
}
<input type="text" placeholder="Your Name" class="form-control py-4" style="background: #273a71; color: white; border: 0;">
To achieve expected result, use below option
Use span or label instead of placeholder (Using span, in case you want label and placeholder)
Use on focus to hide span with css
.placeholder{
color: white;
position: relative;
top: -30px;
}
.placeholder:after{
content: '*';
color: red
}
.form-control:focus + .placeholder {
display: none;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<input type="text" class="form-control py-4" style="background: #273a71; color: white; border: 0;" required="required">
<span class="placeholder">Your Name</span>
codepen - https://codepen.io/nagasai/pen/GzQVev

How to group an icon and an input element

I'm trying to achieve the following:
Create 3 input elements in a row
Each should have a logo to the left of it, centered perfectly.
Each should have a border-bottom that spans the logo as well.
Like the following image:
However with my current code the images can't be centered and the border doesn't span them. Here's my code:
input {
border: none;
width: 250px;
background-color: #393d49;
border-bottom: 1px solid #767D93;
padding: 10px;
}
form img {
width: 24px;
height: 24px;
}
<form>
<img src="assets/images/envelope.png" alt="Envelope icon indicating user's E-Mail.">
<input type="email" placeholder="E-Mail"><br>
<img src="assets/images/locked.png" alt="Lock icon indicating user's Password.">
<input type="password" placeholder="Password"><br>
<img src="assets/images/avatar.png" alt="Avatar icon indicating user's Name.">
<input type="text" placeholder="Username"><br>
</form>
As it was suggested, I would also use the font-awesome library. But if your not comfortable with that idea, here is how you can do without.
form, .form-row, input {
background-color: #051024;
}
.input-icon, label, input {
display: inline-block;
}
form {
padding: 0.8em 1.2em;
}
.form-row {
padding: 0.8em 0;
padding-bottom: 0.2em;
}
.form-row:not(:last-child) {
border-bottom: solid #18273a 1px; /* Only the last row has a border */
}
.input-icon {
width: 15px;
height: 15px;
margin: 0 10px;
}
label {
max-width:4em; /* Or the maximum width you want your lebel to be */
min-width:4em; /* Same */
color:white;
font-weight: 100;
}
input {
border:none;
padding: 0.8em 0.5em;
color: #6691c9;
font-size: 15px;
outline: none; /* No glowing borders on chrome */
}
<form>
<div class="form-row">
<!-- Put your image here, like so -->
<img class="input-icon" src="http://1.bp.blogspot.com/-QTgDeozeWws/VLztRSNkMEI/AAAAAAAAKkQ/mrxdCfxWfvU/s1600/1f499.png" alt="oops"/>
<label for="form-email">Email</label>
<input id="form-email" type="email">
</div>
<div class="form-row">
<img class="input-icon" src="http://1.bp.blogspot.com/-QTgDeozeWws/VLztRSNkMEI/AAAAAAAAKkQ/mrxdCfxWfvU/s1600/1f499.png" alt="oops"/>
<label for="form-password">Password</label>
<input id="form-password"type="password" placeholder="(8 characters min)">
</div>
<div class="form-row">
<img class="input-icon" src="http://1.bp.blogspot.com/-QTgDeozeWws/VLztRSNkMEI/AAAAAAAAKkQ/mrxdCfxWfvU/s1600/1f499.png" alt="oops"/>
<label for="form-user">User</label>
<input id="form-user" type="text"><br>
</div>
</form>
If you're feeling adventurous
Try bootstrap, it has all you need to create cool web sites (it also includes the font-awesome library).

css - align label & textbox at right side of page

I have created 4 column grid in my html form. i want to have last label and textbox field to be align right side of the page.
I have tried using float:right property but it doesn't seem to work
In fiddle example it is not in single line.
.row {
margin: 10px;
}
.elements {
padding: 10px;
margin-left: 50px;
margin-top: 10px;
border-bottom: ridge;
border-bottom-color: #1f6a9a;
padding-left: 0;
}
.field {
font-size: 15px;
color: #b6d6ed;
}
label {
display: inline-block;
}
input {
background-color: transparent;
border: 0px solid;
height: 25px;
width: 60px;
color: #b6d6ed;
text-align: center;
}
/* I Tried Below Line to Right Align */
.row > .elements:nth-child(2) {
float:right;
}
<div class="row">
<span class="elements">
<label class="field" for="Title">Title</label>
<input id="Title" name="Title" type="text">
</span>
<span class="elements">
<label class="field" for="DateOfBirth">Date of Birth</label>
<input id="DateOfBirth" name="DateOfBirth" type="text" value="" class="hasDatepicker">
</span>
</div>
jsfiddle
Float the first span to the left:
.row > .elements:first-child {
float: left;
}
See it here: http://jsfiddle.net/3DtqB/2/
You have to put the elements class into a <div class=".."></div> and add an CSS command
.elements {
float: right;
margin-top: -[x]px
}
Also you should use two id's instead of a class elements like left_box and right_box and add the commands to the right box.
Simple fix, just add white-space:nowrap; to the .elements class.