Centering div does not work properly with font awesome - html

Problem: JSFiddle
I want to center the whole form and created a class called 'center' with the given attributes. But after adding the center class to the form in JSFiddle, the fontawesome-icons stay on the left and do not move. They should be positioned "inside" the input element left to the text. Therefore, to make place for the icon, I also added a padding on the left.
Can somebody give me the solution to fix this problem that both icons stay on the left because of centering? I really cannot find out why it is not working and it already took so much time :/
Thanks.
.center{
text-align: center;
margin-left: auto;
margin-right: auto;
}
.box {
position: relative;
margin-bottom: 20px;
}
.box .fa {
position: absolute;
top: 10px;
left: 5px;
}
.box input[type="text"],
.box input[type="password"] {
border: 0px;
border-bottom: 1px solid #ccc;
text-decoration: none;
width: 180px;
padding: 10px;
padding-left: 25px;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="center">
<form action="/login" method="POST" class="form">
<div class="box">
<i class="fa fa-user" aria-hidden="true"></i>
<input required name="username" type="text" placeholder="user">
</div>
<div class="box">
<i class="fa fa-key" aria-hidden="true"></i>
<input required name="password" type="password" placeholder="pass">
</div>
<div class="box">
<input name="submit" type="submit" value="Login">
</div>
</form>
</div>

The .box elements are divs and therefore naturally have a width of 100% of the parent element.
A simple solution would be to give them a fixed width equal to that of your inputs and centre them using left and right margins of auto:
.center{
text-align: center;
margin-left: auto;
margin-right: auto;
}
.box {
position: relative;
margin: 0 auto 20px;
width: 180px;
}
.box .fa {
position: absolute;
top: 10px;
left: 5px;
}
.box input[type="text"],
.box input[type="password"] {
border: 0px;
border-bottom: 1px solid #ccc;
text-decoration: none;
width: 180px;
padding: 10px;
padding-left: 25px;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="center">
<form action="/login" method="POST" class="form">
<div class="box">
<i class="fa fa-user" aria-hidden="true"></i>
<input required name="username" type="text" placeholder="user">
</div>
<div class="box">
<i class="fa fa-key" aria-hidden="true"></i>
<input required name="password" type="password" placeholder="pass">
</div>
<div class="box">
<input name="submit" type="submit" value="Login">
</div>
</form>
</div>

it occurs because
.box .fa {
position: absolute;
top: 10px;
left: 5px;
}
you can use position initial for it.

Related

How can set element alignment which is inside parent with the child style setting in element style attributes?

.popup {
position: relative;
display: inline-block;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.popup .popuptext {
visibility: hidden;
width: 160px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -80px;
}
I have above codes in my css file that added in html head <link href="/static/styles.css" rel="stylesheet">
In my html there where many uses of .popupand .popuptext and I wont change above styles.
In the html code is here↓
<div class="popup" style="display: inline"><div class="popuptext" id="tableHeader">
<label>data type</label>
<br>
<input type="checkbox" name="not_null"> NOT NULL
<br>
<input type="checkbox" name="unique"> UNIQUE
<br>
<button class="btn btn-danger" type="submit" name="delete">Delete</button>
</div></div>
All elements above are aligned as center.
But I need centralize the Delete button and the label and other input elements be left aligned.
As I consist change style from html code and stylesheet has other uses, I have tried below↓
<div class="popup" style="display: inline"><div class="popuptext" id="tableHeader">
<label>data type</label>
<br>
<span style="text-align: left;"><input type="checkbox" name="not_null"> NOT NULL</span>
<br>
<span style="text-align: left;"><input type="checkbox" name="unique"> UNIQUE</span>
<br>
<button class="btn btn-danger" type="submit" name="delete">Delete</button>
</div></div>
But input elements remain centralist and not left aligned!
I need input get left aligned only with change style settings in html element attributes.
Leaving the html as it is, you could style the .popuptext children as display:block so that they will take the whole width. Then you can style indipendently its parts:
The <label> element - its content gets centered with text-align: center
The <input> elements - they get aligned to left with text-align: left; but just because we are actually styling their parents
<span>
The <button> element - this required margin: 0 auto meaning that the left
and right margins will be set to the maximum available space in the
row
Everything else in the styles here in the demo is for decoration purpose.
.popup{
border: solid;
width: 10em;
background: lightgray;
}
.popuptext > *{
display: block;
}
.popuptext > span{
text-align: left;
}
.popuptext > label{
text-align: center;
margin-bottom: .5em;
}
.popuptext > button{
margin: .5em auto;
border: none;
background: red;
color: white;
padding: .8em .6em;
border-radius: 5px;
}
<div class="popup">
<div class="popuptext" id="tableHeader">
<label>data type</label>
<span><input type="checkbox" name="not_null"> NOT NULL</span>
<span><input type="checkbox" name="unique"> UNIQUE</span>
<button class="btn btn-danger" type="submit" name="delete">Delete</button>
</div>
</div>
You can restructure your HTML and CSS as such:
.popup .popuptext {
width: 160px;
background-color: #555;
color: #fff;
border-radius: 6px;
padding: 8px 0;
position: absolute;
z-index: 1;
}
.popuptext .checkbox-holder {
width: fit-content;
margin: auto;
}
<div class="popup" style="display: inline">
<div class="popuptext" id="tableHeader">
<div style="text-align: center;"><label>data type</label></div>
<div class="checkbox-holder">
<div><input type="checkbox" name="not_null" id="not_null"> <label for="not_null">NOT NULL</label></div>
<div><input type="checkbox" name="unique" id="unique"> <label for="unique">UNIQUE</label></div>
</div>
<div style="text-align:center"><button class="btn btn-danger" type="submit" name="delete">Delete</button>
</div>
</div>
</div>

Align Icon to The Right of an Input Field No Matter Width

I have three input fields, each with a set width, and want an icon aligned to the right of each.
Right now, it appears it's aligning to the right of the parent container.
Take a look at this codepen to see what I mean.
.input-container {
width: fit-content;
position: relative;
margin-top: 16px;
}
.input-field {
padding: 8px 12px;
border: 1px solid gray;
position: relative;
}
.input-field.short {
width: 100px;
}
.input-field.medium {
width: 200px;
}
.input-field.long {
width: 400px;
}
.icon {
position: absolute;
right: 8px;
top: 8px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/#fortawesome/fontawesome-free#5.15.4/css/fontawesome.min.css" integrity="sha384-jLKHWM3JRmfMU0A5x5AkjWkw/EYfGUAGagvnfryNV3F9VqM98XiIH7VBGVoxVSc7" crossorigin="anonymous">
<div class="input-container">
<input class="input-field short" type="text">
<i class="fa fa-user icon"></i>
<div>
<div class="input-container">
<input class="input-field medium" type="text">
<i class="fa fa-user icon"></i>
<div>
<div class="input-container">
<input class="input-field long" type="text">
<i class="fa fa-user icon"></i>
You're not closing the div tag that you were using. An opening <div> should be closed by it's closing counterpart </div>. Your code works, it's just that you forgot to use the closing tag for each div you were using. See the snippet below to see what I mean.
.input-container {
width: fit-content;
position: relative;
margin-top: 16px;
}
.input-field {
padding: 8px 12px;
border: 1px solid gray;
position: relative;
}
.input-field.short {
width: 100px;
}
.input-field.medium {
width: 200px;
}
.input-field.long {
width: 400px;
}
.icon {
position: absolute;
right: 8px;
top: 8px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/#fortawesome/fontawesome-free#5.15.4/css/fontawesome.min.css" integrity="sha384-jLKHWM3JRmfMU0A5x5AkjWkw/EYfGUAGagvnfryNV3F9VqM98XiIH7VBGVoxVSc7" crossorigin="anonymous">
<div class="input-container">
<input class="input-field short" type="text">
<i class="fa fa-user icon"></i>
</div>
<div class="input-container">
<input class="input-field medium" type="text">
<i class="fa fa-user icon"></i>
</div>
<div class="input-container">
<input class="input-field long" type="text">
<i class="fa fa-user icon"></i>
</div>
I've corrected your codepen code here as well.

Clickable image inside input field - position not working

I know this question has been asked and aswered before, but for some reason, the answers are not working for me; so any help is much appreciated.
I'm creating a hybrid mobile app and I need to add an clickable icon (SVG) inside an input field. Even though I have managed to add the icon, when I test my design in different screens, the icon doesn't not respect the position I need it to be in.
Here are some shots: This is how it's supposed to look in all screens and this is what it looks like in landscape mode, for example.
Here's my code: https://codepen.io/paulamourad/pen/djXvxq
CSS:
.wallet-body {
width: 100%;
margin: 0 auto;
}
.form-group {
position: relative;
}
.form-group input {
border: 1px solid #2C2C2C;
height: 48px;
}
.qr-scanner-img {
float: left;
width: 11%;
margin-top: -39px;
margin-left: 120px;
position: relative;
}
HTML:
<div class="wallet-body">
<form class="form-pagar">
<div class="form-group">
<input type="amount" class="form-control" id="amount" placeholder="Monto">
</div>
<div class="form-group">
<input type="IDuser" class="form-control" id="IDuser" placeholder="Email del destinatario">
<a href="#">
<img class="qr-scanner-img" src="img/qr.svg" alt="qr"></a>
</a>
</div>
</form>
</div>
.wallet-body {
width: fit-content;
margin: 0 auto;
float: left;
}
.form-group{
position: relative;
}
.form-group input {
border: 1px solid #2C2C2C;
height: 48px;
}
.qr-scanner-img {
width: 11%;
position: absolute;
top: 50%;
right: 0;
transform: translateY(-50%);
}
<div class="wallet-body">
<form class="form-pagar">
<div class="form-group">
<input type="amount" class="form-control" id="amount" placeholder="Monto">
</div>
<div class="form-group">
<input type="IDuser" class="form-control" id="IDuser" placeholder="Email del destinatario">
<a href="#">
<img class="qr-scanner-img" src="img/qr.svg" alt="qr"></a>
</a>
</div>
</form>
</div>

How can I vertically align a bootstrap glyph inside an <input> larger than default

I have to make an <input> that's larger than the usual. The height must be 55px. I am having trouble vertically aligning the glyph image inside the orange square. It looks like this:
It should look like this:
I have tried using margin-top on my <i> element but it moves the rectangle down too, I need to move just the glyph. It looks like this:
Please let me know if you have any ideas on how I could vertically center the glyph inside the orange box. I have setup a jsfiddle at https://jsfiddle.net/vteL375o/1/
HTML:
<div class="container">
<div class="form-group has-feedback" style="margin-top:20px;">
<input type="text" id="search-query" class="form-control not-rounded search-input">
<i class="glyphicon glyphicon-search form-control-feedback glyph-class" style=""></i>
</div>
</div>
CSS:
.not-rounded {
border-radius: 0;
}
.search-input {
font-size: 20px;
margin-bottom: 50px;
height: 55px;
}
.glyph-class {
height: 55px;
width: 55px;
color:white;
background-color: orange;
font-size: 20px;
}
You can use Flexbox for this, or inline-flex in this case.
.not-rounded {
border-radius: 0;
}
input.search-input {
font-size: 20px;
margin-bottom: 50px;
height: 55px;
}
.glyphicon.glyphicon-search.form-control-feedback.glyph-class {
height: 55px;
width: 55px;
color: white;
background-color: orange;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 20px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="form-group has-feedback" style="margin-top:20px;">
<input type="text" id="search-query" class="form-control not-rounded search-input">
<i class="glyphicon glyphicon-search form-control-feedback glyph-class" style=""></i>
</div>
</div>
Add line-height: 55px; to .glyph-class.
input.not-rounded {
border-radius: 0;
}
input.search-input {
font-size: 20px;
margin-bottom: 50px;
height: 55px;
}
i.glyph-class {
height: 55px;
width: 55px;
color: white;
background-color: orange;
font-size: 20px;
line-height: 55px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="form-group has-feedback" style="margin-top:20px;">
<input type="text" id="search-query" class="form-control not-rounded search-input">
<i class="glyphicon glyphicon-search form-control-feedback glyph-class" style=""></i>
</div>
</div>
Just add padding: 10px 0; in to your .glyph-class
like this
.glyph-class {
height: 55px;
width: 55px;
color:white;
background-color: orange;
font-size: 20px;
padding: 10px 0;
}
Here is the running sample: https://jsfiddle.net/vteL375o/6/

Text input and button input in the same line

I would like to create a div that contains a text input and a button input on the same line.
The width of the div will be set by me. These two inputs have to fill the div in the way that the button input's width is to be set by it's value, and the rest of the space to be filled by the text input.
I want something like:
<div style="width: 300px; background-color: orange">
<input type="text" />
<input type="button" value="Save" />
</div>
Sorry for my bad English.
Demo: http://jsfiddle.net/abhitalks/Y64ny/
You need to wrap your text input in another div. Apply display:table to the container div and display:table-cell and width:100% to the wrapper div.
HTML:
<div style="width: 300px; background-color: orange">
<div class="t"> <!-- This is the wrapper div around the text input -->
<input type="text" />
</div>
<input type="button" value="Save" />
</div>
CSS:
div { display: table; }
div.t {
display: table-cell;
width: 100%;
}
div.t > input {
width: 100%;
}
Update:
As the Op (#ChocapicSz) states in the comment below, adding box-sizing:border-box will fix the paddings.
Updated fiddle: http://jsfiddle.net/abhitalks/Y64ny/1/
http://jsfiddle.net/8FC2t/
<div>
<input type="text" class='save'/>
<input type="button" value="Save" />
</div>
<br>
<div>
<input type="text" class='dns' />
<input type="button" value="Do not Save" />
</div>
div{
width:200px;
background-color:orange;
}
.dns{
width:calc(100% - 105px)
}
.save{
width:calc(100% - 65px)
}
div>input[type='button']{
float:right;
}
Using position absolute and slightly restructuring the css might work better for you.
Also using <button> instead of <input type="button"> makes life a little easier to style.
I've created an example at codepen for you to see.
HTML:
<div class="container" style="width: 300px">
<input type="text" class="text_input" />
<button value="Save" class="btn">CLICK</button>
</div>
<div class="container" style="width: 500px">
<input type="text" class="text_input" />
<button value="Save" class="btn">CLICK</button>
</div>
<div class="container" style="width: 200px">
<input type="text" class="text_input" />
<button value="Save" class="btn">CLICK</button>
</div>
CSS:
.container {
position: relative;
border: 3px solid orange;
height: 50px;
overflow: hidden;
margin-bottom: 10px;
}
.text_input {
height: 44px;
width: 60%;
padding: 0;
line-height: 30px;
font-size: 20px;
padding: 0;
margin: 3px;
margin-left: 20px;
border: none;
}
.text_input:focus {
outline: none;
}
.btn {
position: absolute;
height: 50px;
line-height: 50px;
right: 0;
top: 0;
margin: 0;
padding: 0;
background: orange;
color: white;
border: none;
width: 30%;
font-weight: bold;
}
.btn:hover {
color: black;
cursor: pointer;
}
Gives you this:
See it working on codepen
<style>
.f-lft {
float:left;
border:2px solid orange;
}
</style>
<body>
<div style="width: 300px;">
<div>
<input class="f-lft"type="text" style="width:80%"/>
<input class="f-lft" type="button" value="Save"/>
</div>
</div>
</body>
I have attached a fiddle for you http://jsfiddle.net/GLAee/
I have done a single one. Remaining you better practice.
Edited :
add width of text field in percent
Another way is to use display flex on their parent.
<div class="parent" style="width: 300px; background-color: orange">
<input type="text" />
<input type="button" value="Save" />
</div>
CSS
.parent{
display: flex;
width: 100%;
height: 50px;
}
input[type="text"] {
height: 50px;
width: 90%;
border: none;
outline: none;
padding: 10px;
font-size: 14px;
}
input[type="button"] {
width: 10%;
height: 50px;
border: none;
outline: none;
background-color: orange;
}
Note, you can play around with the font-size and height to have the desired height and font-size that you want.