This question already has answers here:
How can I horizontally center an element?
(133 answers)
Closed last month.
Have been struggling with adding icons in input. After finally getting it the next problem occurs. Im having trouble centering the placeholder in and . I want to vertically center my text(placeholder) in input on the same as my icon. Also button doesnt want to center horizontally. would greatly appreciate your help.
how it looks now
\<div class="form-outer"\>
\<div class="container"\>
<form class="form">
<div class="form-input">
<span class="icon"><i class="fa fa-envelope" aria-hidden="true"></i></span>
<input type="email" name="email" placeholder="Enter your email">
</div>
<div class="form-input">
<span class="icon"><i class="fa fa-lock" aria-hidden="true"></i></span>
<input type="password" name="password" placeholder="Enter your password">
</div>
<button type="submit" class="knop-aanvraag">Aanvragen</button>
</form>
</div>
</div>
.knop-aanvraag{
background-color: #ffc350;
margin-top: 40px;
box-shadow: 0 10px 20px -18px #333333;
border: none;
height: 53px;
width: 244px;
font-size: 32px;
font-weight:500;
color: #FFFFFF;
text-align: center;
border-radius: 12px;
}
.form-outer {
position: absolute;
top: 17%;
right: 22%;
width: 100%;
}
.container {
width: 336px;
height: 578px;
background-color: #C9003A;
float: right;
padding-top: 10px;
box-shadow: 0 0 9px 2px #333333;
}
.form-input {
margin-top: 10px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
input {
outline: none;
padding: 15px 20px 5px 50px;
font-size: 20px;
width: 70%;
background-color: rgba(170,6,53,255);
border: transparent;
font-family: 'Roboto', sans-serif;
}
.icon {
position: absolute;
left: 25px;
top: 0;
font-size: 20px;
color: #FFFFFF;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
font-family: 'Roboto', sans-serif;
}
try margin auto for the button (parent div might need to specify width)
.knop-aanvraag{
margin-left: auto;
margin-right: auto;
}
and for the input, since you use padding
input {
outline: none;
padding: 15px 20px 5px 50px;
font-size: 20px;
width: 70%;
background-color: rgba(170,6,53,255);
border: transparent;
font-family: 'Roboto', sans-serif;
}
top padding is 15px,
right padding is 20px,
bottom padding is 5x,
left padding is 50px,
so the text in place holder will have less padding on the bottom than the top bottom.
Related
I'm currently trying to use HTML together with CSS and my current problem is that I am not able to connect the input beside the arrow button:
I'm here asking how I am able to make the >> button as a "submit" button and that its beside the input?
--------------
| | >>
--------------
body {
padding: 5px 15px 30px 15px;
width: 500px;
height: 250px;
background-image: url('../images/bg2.jpg');
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
}
.text-center {
text-align: center;
}
.form-control {
margin: 600;
}
label {
display: block;
}
select,
input {
min-width: 100px;
border: 1px solid #fff;
background: #292942;
color: #fff;
border-radius: 25px;
padding: 5px 10px;
}
.mt10 {
margin-top: 20px;
}
.submit {
background-image: url('../images/arrow.png');
background-repeat: no-repeat;
background-position: center center;
width: 23px;
height: 23px;
margin-left: 350px;
}
<body>
<div class="text-center">
<div class="form-control">
<input type="text" id="discord-id-input" name="discord-id-input" placeholder="Discord ID" class="mt10">
<div id="discord-id-button" type="submit" class="submit">
</div>
</body>
Some simple flexbox properties get things into shape.
Other tips:
Use a button for submit. If you don't want button styling, take it off. Semantic use of elements is critical for consistent, familiar usage and for accessibility.
Whenever you find yourself using huge margins for layout, take a step back. That's not a good approach. Use flexbox or CSS grid to create a structure in which your content resides, and use margin or padding only to crate a bit of space between elements, or between grid containers and content.
Don't put hard widths on the body. That should almost always remain flexible to fit the screen.
body {
padding: 5px 15px 30px 15px;
/* width: 500px; */
height: 250px;
background-image: url('../images/bg2.jpg');
background-size: cover;
background-repeat: no-repeat;
background-position: center center;
}
.text-center {
text-align: center;
}
.form-control {
margin: 600; /* invalid value */
display: flex;
align-items: center;
justify-content: center;
}
label {
display: block;
}
select,
input {
min-width: 100px;
border: 1px solid #fff;
background: #292942;
color: #fff;
border-radius: 25px;
padding: 5px 10px;
}
.mt10 {
margin-top: 20px;
}
.submit-btn {
background-image: url('https://via.placeholder.com/30');
background-repeat: no-repeat;
background-position: center center;
width: 23px;
height: 23px;
margin-left: 12px;
/* margin-left: 350px; */
}
<body>
<div class="text-center">
<div class="form-control mt10">
<input type="text" id="discord-id-input" name="discord-id-input" placeholder="Discord ID">
<button id="discord-id-button" type="submit" class="submit-btn" />
</div>
</div>
</body>
You could use flexbox, like so (Notice I changed the submit button to an HTML button):
.form-control {
display: flex;
align-items: center;
gap:10px;
}
input {
min-width: 100px;
border: 1px solid #fff;
background: #292942;
color: #fff;
border-radius: 25px;
padding: 5px 10px;
}
.submit {
background-image: url("https://img.icons8.com/material-two-tone/24/000000/arrow.png");
background-repeat: no-repeat;
background-position: center center;
background-color: transparent;
outline: none;
border: none;
width: 23px;
height: 23px;
}
<div class="form-control">
<input type="text" id="discord-id-input" name="discord-id-input" placeholder="Discord ID">
<button id="discord-id-button" type="submit" class="submit">
</button>
</div>
My assignment is to make a login page, and the inputs and buttons have to have a 320px width, including a 2px border and 15px padding. I create two classes for the inputs and button, and in CSS I am specifying these widths for both but the button keeps coming out shorter. Right now it's coming out like this: 1
I'm fairly new at this so I apologize if my code is messy/this might seem like a silly question.
Here's my code:
HTML
<form class="signup" action="/signup/" method ="post">
<fieldset name="sign-up">
<legend>Sign up</legend>
<div class="input">
<label for="email">Email</label></br>
<input class="inputbar" placeholder="foo#bar.com" type="email" name="email" id="email" required/></br>
<label for="password">Password</label></br>
<input class="inputbar" placeholder="1234passw0rd" type="password" name="password" id"password" required/></br>
</div>
</fieldset>
<button type="signupbutton">Sign up</button>
</form>
CSS
.signup {
width: 320px;
padding: 40px;
margin-top: 30px;
margin-left: auto;
margin-right: auto;
text-align: center;
background-color: white;
}
.signup fieldset{
border: none;
}
.input{
text-align: left;
}
.inputbar, button{
border: 2px solid lightgrey;
border-radius: 2px;
padding: 15px;
width: 250px;
}
button{
background-color: mediumseagreen;
color: white;
}
Thanks everyone!
It's best to set the container width and then the elments inside to 100%. Using box-sizing: border-box; is key here.
* {
box-sizing: border-box;
}
.signup {
width: 320px;
padding: 40px;
margin-top: 30px;
margin-left: auto;
margin-right: auto;
text-align: center;
background-color: white;
}
.signup fieldset{
border: none;
padding: 0;
margin: 0;
}
.input{
text-align: left;
}
.inputbar, button{
border: 2px solid lightgrey;
border-radius: 2px;
padding: 15px;
width: 100%;
}
button{
background-color: mediumseagreen;
color: white;
}
.inputbar, button{
border: 2px solid lightgrey;
border-radius: 2px;
padding: 15px;
width: 250px;
}
It appears you have set the width of the button to 250px. Try changing that to 320px.
Remove the text align on th input class
.input{
text-align: left;
}
and do this
.signup .input label {
text-align: left;
display: inline-block;
width: 100%;
padding-left: 20px;
}
Aside that the everything is working alright in chrome
what browser are you using to run your test?
this is whati have on my browser
How can I add an image inside this search box? I'd like the image to be positioned inside and to the left of the palceholder text...
Here is the fiddle
HTML:
<div class="searchbar">
<h1>Welcome...</h1>
<div id="sb-search" class="sb-search">
<div class="search-wrapper">
<input class="search-input" placeholder="Search..." type="text" value="" name="search" id="search"/>
<img class="search-pic"src="img/search-icon.png"/>
</div>
</div>
</div>
thanks
Add this to your CSS which will position the image inside the input field.
.search-wrapper {
position: relative;
}
.search-wrapper img {
position: absolute;
top: 0px;
left: 0px;
}
Then you just need to use padding and change the top and left values to move everything about so it fits nicely and nothing is overlapped.
You might also need to set a width and height to the image so it's not too big for the input field.
You can solve it like this:
#mixin searchbar-font {}
#mixin searchbar-styles {
border: none;
outline: none;
color: $header-bg-color;
padding: 20px 65px 20px 20px;
background: transparent;
flex:1;
}
#mixin search-bar-input {
border: none;
outline: none;
}
.search-input {
border: none;
outline: none;
color: $header-bg-color;
padding: 20px 65px 20px 20px;
background: transparent;
flex:1;
}
.search-wrapper {
position: relative;
background: white;
display: flex;
align-items: center;
.search-input {
#include searchbar-font;
#include searchbar-styles;
}
.search-submit {
#include search-bar-input;
}
}
<div class="searchbar">
<h1>Welcome...</h1>
<div id="sb-search" class="sb-search">
<div class="search-wrapper">
<img class="search-pic" src="img/search-icon.png" />
<input class="search-input" placeholder="Search..." type="text" value="" name="search" id="search" />
</div>
</div>
</div>
Attached Fiddle
Technically you need to set your parent tag's position to relative, then set the image inside's position to absolute. Then you can overlay the image on your Input field. Also, one more thing to remember is you might want to set you z-index. Just in case, your image does not get behind of your input field. Make sure you are giving enough space to for your image by setting the input's padding left to somewhere around your image's width.
.search-wrapper{
.search-input{
padding-left: {image.width}px;
}
img{
position:absolute; left:0; top:0;
}
}
This should do the work.
<div class="search-container">
<input type="text" placeholder="Search...">
<img src="search-icon.png" alt="search icon">
</div>a
.search-container {
display: flex;
align-items: center;
position: relative;
}
input[type="text"] {
border: 1px solid gray;
border-radius: 20px;
padding: 10px 40px 10px 20px;
font-size: 16px;
width: 500px;
}
img {
position: absolute;
right: 20px;
top: 50%;
transform: translateY(-50%);
height: 20px;
width: 20px;
}
<div class="search-container">
<input type="text" placeholder="Search...">
<img src="search-icon.png" alt="search icon">
</div>
.search-container {
display: flex;
align-items: center;
position: relative;
}
input[type="text"] {
border: 1px solid gray;
border-radius: 20px;
padding: 10px 40px 10px 20px;
font-size: 16px;
width: 500px;
}
img {
position: absolute;
right: 20px;
top: 50%;
transform: translateY(-50%);
height: 20px;
width: 20px;
}
I'm trying to implement some designs in CSS, but having a bit of trouble figuring out how to align this <span> correctly.
I am trying to achieve the <input> and <button> elements being centrally aligned, but the <span> element being absolutely to the right of the <input>, example:
It's important to make sure that the <span> does not affect the alignment of the other elements. The <input> and <button> should always be exactly in the middle of the parent.
Would be great if I could do this in CSS only. This is what I have so far:
div {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
}
.verify {
display: block;
width: 20px;
height: 20px;
background: red;
position: absolute;
top: 0; /* Not sure how to calculate these? */
right: 0; /* Input.X + Input.Width + 15px ?? */
}
<div>
<input placeholder="Enter code" maxlength="8" size="8"/><br />
<span class="verify"></span>
<button>Register</button>
</div>
Additional Info:
It only has to work in Chrome
I can make all elements a fixed width if required
I can make DOM changes if required
I would prefer not to hardcode X/Y co-ordinates for the <span>...I might want to change the input/button dimensions later
Wrap the input and the span inside a div with position: relative and display:inline
The span .verify will get absolutely positioned, leaving the input element to it's original position (centered aligned)
By giving a top:50% and then margin-top: -10px (half of its height), it will get in the middle of it's parent height.
.wrp {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
.inpWrp {
display: inline;
position:relative;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
}
.verify {
display: block;
width: 20px;
height: 20px;
background: red;
position: absolute;
margin-top: -10px;
top: 50%; /* Not sure how to calculate these? */
right: -20px; /* Input.X + Input.Width + 15px ?? */
}
<div class="wrp">
<div class="inpWrp">
<input placeholder="Enter code" maxlength="8" size="8"/>
<span class="verify"></span>
</div>
<br />
<button>Register</button>
</div>
You can wrap your input element in span and using pseudo-element :after to create the square. No need of position absolute:
div {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
vertical-align: middle;
}
.verify:after {
content: "";
width: 20px;
height: 20px;
background: red;
display: inline-block;
vertical-align: middle;
}
<div>
<span class="verify">
<input placeholder="Enter code" maxlength="8" size="8"/>
</span>
<br />
<button>Register</button>
</div>
After #kapantzak comment you can use position absolute like:
div {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
}
.verify:after {
content: "";
width: 20px;
height: 20px;
background: red;
display: inline-block;
position: absolute;
top: 17px;
}
<div>
<span class="verify">
<input placeholder="Enter code" maxlength="8" size="8"/>
</span>
<br />
<button>Register</button>
</div>
Try this...
.main_container {
position: relative;
text-align: center;
background: #B7B7B7;
width: 400px;
}
input {
padding: 5px;
margin: 10px 0;
text-align: center;
font-size: 1.2em;
width: auto;
}
.verify {
display: inline-block;
width: 20px;
height: 20px;
background: red;
position: relative;
top: 2px;
left: 10px;
}
<div class="main_container">
<div><input placeholder="Enter code" maxlength="8" size="8"/>
<span class="verify"></span></div>
<button>Register</button>
</div>
I am making a login page and I would to position a custom image border to the center underneath the form.
The image is not positioning to the margin-left properly.
Can anyone suggest a solution?
Below is the code:
#import url(http://fonts.googleapis.com/css?family=Droid+Sans);
#import url(http://fonts.googleapis.com/css?family=Roboto);
* {
text-align: center;
}
img {
z-index: -1;
width: 350px;
height: 200px;
margin-top: 200px;
background-size: cover;
position: absolute;
}
body {
background-color: #ecf0f1;
}
#username {
margin-top: 250px;
margin-bottom: inherit;
margin-right: inherit;
width: 250px;
height: 35px;
font-size: 20px;
font-family: 'Droid Sans', serif;
border: 0px solid;
background-color: transparent;
}
#username:focus {
outline: none;
}
#password {
margin-top: 5px;
margin-bottom: inherit;
margin-right: inherit;
width: 250px;
height: 35px;
font-size: 20px;
font-family: 'Droid Sans', serif;
border: 0px solid;
background-color: transparent;
}
#password:focus {
outline: none;
}
#submit {
margin-top: 35px;
border: 0;
background: url(submit1.png);
background-repeat: no-repeat;
width: 200px;
height: 50px;
border: 0;
}
#submit:focus {
outline: none;
}
#submit:hover {
background-image: url(submit2.png);
}
#footer {
clear: both;
position: relative;
z-index: 5;
margin-top: 17em;
background-color: transparent;
font-family: 'Roboto', serif;
color: #bdc3c7;
height: 20px;
font-size: 10px;
}
<div id="main">
<div id="login">
<form action="Scripts/xxx.php">
<img src="border_transparent.png">
<input id="username" type="text" name="username" placeholder="Enter Username" />
<br>
<input id="password" type="password" name="password" placeholder="Enter Secret Password" />
<br>
<button id="submit" value="" />
</form>
</div>
</div>
<div id="container">
<div id="content"></div>
</div>
<div id="footer">© Project Blackhat Operatives 2015</div>
This is currently how my page looks: http://s7.postimg.org/vvfi8m1uj/Screen_Shot_2015_01_11_at_7_07_21_PM.png
This is what I am trying for the page to look like: http://s11.postimg.org/avy2caylv/Screen_Shot_2015_01_11_at_7_09_01_PM.png
It is centered, the issue is the background image behind it that you are absolute positioning. That isn't centered, you can add left: 50% and a negative margin to bring it back to the center (left positions it from the corner not the center):
img {
z-index: -1;
width: 350px;
height: 200px;
background-size: cover;
position:absolute;
left: 50%; //add
margin: 200px 0 0 -175px; //add
}
FIDDLE
What you should be doing though is adding that image as a background-image to either your form or #login containers. Positioning an img tag is not a great way of handling this.
Try
#submit:hover {
background-image: url("submit2.png");
}
Need quotes/double quotes.