How to align input element in the middle of div? [duplicate] - html

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Closed 7 years ago.
For below html code,
.shoppingform {
width: 400px;
height: 800px;
background: #7CB9E8;
/* url(some img)*/
padding-left: 15px;
padding-top: 10px;
color: white;
font-size: 12px;
font-weight: bold;
border-radius: 5px;
}
.customername {
border: 1px solid white;
color: black;
font-weight: normal;
padding: 10px 2px 5px 5px;
background: #B284BE;
width: 90%;
border-radius: 5px;
}
.customername {
height: 5%;
}
.customername {
margin-top: 5px;
}
.shoppingform > div > input {
border-radius: 5px;
width: 60%;
}
.formlabel {
display: inline-block;
width: 30%;
}
<form class="shoppingform" action="someaction.php" method="get" enctype="multipart/form-data">
Step1: Your details
<br>
<div class="customername">
<label class="formlabel">Name:</label>
<input type="text">
</div>
</form>
There are multiple div elements(like customername), which above code does not have,to make question simple.
label and input text are towards top side of the div container.
How do I vertically align the label and input text in the middle of the div container? To add, there are multiple div elements in the form.

Modified your code a little to have your elements vertically aligned as suggested.
However I do advice you to think about your element positioning better, this form will likely not be good in terms of responsive behavior and layout.
.shoppingform {
width: 400px;
height: 800px;
background: #7CB9E8;
/* url(some img)*/
padding-left: 15px;
padding-top: 10px;
color: white;
font-size: 12px;
font-weight: bold;
border-radius: 5px;
text-align: center;
}
.customername {
border: 1px solid white;
color: black;
font-weight: normal;
padding: 10px 2px 5px 5px;
background: #B284BE;
width: 90%;
border-radius: 5px;
}
.customername {
height: 5%;
}
.customername {
margin-top: 5px;
}
.shoppingform > div > input {
border-radius: 5px;
width: 60%;
}
.formlabel {
display: inline-block;
width: 30%;
}
<form class="shoppingform" action="someaction.php" method="get" enctype="multipart/form-data">
Step1: Your details
<br/>
<div class="customername">
<label class="formlabel">Name:</label>
<br/>
<input type="text">
</div>
</form>

.shoppingform {
width: 400px;
height: 800px;
background: #7CB9E8;
padding-left: 15px;
color: white;
font-size: 12px;
font-weight: bold;
border-radius: 5px;
padding-top: 47.5%;
}
.customername {
margin: auto;
border: 1px solid white;
color: black;
font-weight: normal;
padding: 10px 2px 5px 5px;
background: #B284BE;
width: 90%;
border-radius: 5px;
height: 5%;
margin-top: 5px;
}
.shoppingform > div > input {
border-radius: 5px;
width: 60%;
}
.formlabel {
display: inline-block;
width: 30%;
}
<form class="shoppingform" action="someaction.php" method="get" enctype="multipart/form-data">
Step1: Your details
<br>
<div class="customername">
<label class="formlabel">Name:</label>
<input type="text">
</div>
</form>

Related

how can i have my resizable textarea not leaving my container when i`m resizing it?

.ctnb{
background: black;
opacity: 0.4;
width: 665px;
border-radius: 4px;
margin-left: 0px;
margin-top: 30px;
height: 220px;
}
.surb textarea{
margin-top: -12px;
width: 566px;
height: 105px;
box-sizing: border-box;
border: none;
outline-color: black;
padding-right: 10px;
padding-left: 11px;
font-size: 18px;
padding-top: 9px;
border-radius: 2px;
min-height: 105px;
resize: vertical;
position: inherit;
}
.subme button{
padding: 13px 254px;
background-color: green;
margin-left: 52px;
border-radius: 2px;
color: white;
font-size: 16px;
}
.surb textarea:placeholder-shown{
font: menu;
padding: 9px;
}
<form>
<div class="ctnb">
<div class="surb">
<p>Any comments or suggestions?</p>
<textarea id="sub" name="comments" placeholder="Enter your comment here"></textarea>
</div>
<br>
<div class="subme">
<button type="button">Submit</button>
</div>
</div>
the textarea is inside the container at it's min-height but it tends to stretch outside the container whenever i resize it. I've tried giving the container a fixed position but it still not working.

Adjust text input position in input box

How do I adjust the text input position in the input box? I did do input[type=text] but it messes up my input box's position completely. I did change my ::placeholder left margin a little bit in case if you want to know.
HTML & CSS
.registerbox {
width: 500px;
height: 450px;
background: rgba(255, 255, 255, 0.7);
margin: 10px auto;
border-radius: 20px;
box-shadow: 0 0 40px 10px rgba(0, 0, 0, 0.6);
color: white;
}
.inner-registerbox {
position: relative;
width: 100%;
height: 100%;
}
.registerbox-front {
position: absolute;
height: 100%;
width: 100%;
padding: 55px;
box-sizing: border-box;
border-radius: 14px;
}
input label {
display: block;
}
.registerbox h2 {
text-align: center;
font-size: 25px;
font-weight: normal;
margin-bottom: 20px;
margin-top: 0;
color: black;
}
.input-box2 {
width: 95%;
background: transparent;
border: 1px solid black;
height: 32px;
border-radius: 5px;
padding: 0 0px;
box-sizing: border-box;
outline: none;
text-align: left;
color: black;
display: table;
margin-top: 2px;
margin-bottom: 15px;
}
.input-box3 {
width: 105%;
background: transparent;
border: 1px solid black;
height: 32px;
border-radius: 5px;
padding: 0 0px;
box-sizing: border-box;
outline: none;
text-align: left;
color: black;
display: table;
margin-top: 2px;
margin-bottom: 15px;
}
.registerbox-front ::placeholder {
padding-left: 10px;
}
.box-firstline, .box-secondline {
margin-top: 10px
}
<body>
<div class="registerbox">
<div class="inner-registerbox">
<div class="registerbox-front">
<h2>BORANG PENDAFTARAN MURID</h2>
<form>
<section>
<div class="box-firstline">
<div style="float:left;margin-right:25px;">
<label for="idmurid">ID Murid</label> <br />
<input type="text" name="idmurid" class="input-box2" placeholder="ID Murid" required>
</div>
<div style="float:left;">
<label for="namamurid">Nama Murid</label> <br />
<input type="text" name="namamurid" class="input-box3" placeholder="Nama Murid" required>
</div>
</div>
<br style="clear:both;" />
</section>
<div class="box-secondline">
<div style="float:left;margin-right:25px;">
<label for="namakelas">Nama Kelas</label> <br />
<input type="text" name="namakelas" class="input-box2" placeholder="Nama Kelas" required>
</div>
<div style="float:left;">
<label for="katalaluan_murid">Kata Laluan</label> <br />
<input type="password" name="katalaluan_murid" class="input-box3" placeholder="Katalaluan" required>
</div>
<br style="clear:both;" />
</div>
</div>
</div>
</body>
I took the code you posted, removed the ::placeholder padding and then added
input[type="text"] {
padding-left: 10px;
}
input[type="password"] {
padding-left: 10px;
}
And it adjusted the the text to match the placeholder.
Here is the full CSS:
.registerbox {
width: 500px;
height: 450px;
background: rgba(255, 255, 255, 0.7);
margin: 10px auto;
border-radius: 20px;
box-shadow: 0 0 40px 10px rgba(0, 0, 0, 0.6);
color: white;
}
input[type="text"] {
padding-left: 10px;
}
input[type="password"] {
padding-left: 10px;
}
.inner-registerbox {
position: relative;
width: 100%;
height: 100%;
}
.registerbox-front {
position: absolute;
height: 100%;
width: 100%;
padding: 55px;
box-sizing: border-box;
border-radius: 14px;
}
input label {
display: block;
}
.registerbox h2 {
text-align: center;
font-size: 25px;
font-weight: normal;
margin-bottom: 20px;
margin-top: 0;
color: black;
}
.input-box2 {
width: 95%;
background: transparent;
border: 1px solid black;
height: 32px;
border-radius: 5px;
padding: 0 0px;
box-sizing: border-box;
outline: none;
text-align: left;
color: black;
display: table;
margin-top: 2px;
margin-bottom: 15px;
}
.input-box3 {
width: 105%;
background: transparent;
border: 1px solid black;
height: 32px;
border-radius: 5px;
padding: 0 0px;
box-sizing: border-box;
outline: none;
text-align: left;
color: black;
display: table;
margin-top: 2px;
margin-bottom: 15px;
}
.box-firstline, .box-secondline {
margin-top: 10px;
}

How to use position:relative to different browsers? Or is it not the best way?

I'm creating static web site and sometimes use position:relative. And when I tested my page on different browsers I got different displays of some elements like 1-2 pixels higher or lower.
How I can solve the problem or I should use something else?
Thought about some gulp package but didn't find anything.
Example.
HTML:
<p id="search-form-menu">
<input type="search" name="search-input" placeholder="Поиск">
<input type="submit" value="">
</p>
CSS:
input[type='search'] {
height: 35px;
width: 250px;
border: 0px;
border-radius: 5px;
position: relative;
top: -10px;
padding-left: 15px;
padding-right: 45px;
}
input[type='submit'] {
height: 35px;
width: 45px;
background-color: white;
border: 0;
border-left: 3px solid #d1d1d1;
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
position: relative;
top: -10px;
left: -47px;
background-image:url("../image/search-loop-2.svg");
background-position: center;
background-repeat:no-repeat;
}
Why not use float style on the submit input?
<p id="search-form-menu">
<input type="search" name="search-input" placeholder="Поиск"/>
<input type="submit" value=""/>
</p>
css
p#search-form-menu{
width: 250px;
height: 35px;
}
input[type='search'] {
display: inline-block;
height: 35px;
width: 205px;
border: 0px;
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
padding-left: 15px;
padding-right: 15px;
}
input[type='submit'] {
display: block;
float: right;
height: 35px;
width: 45px;
background-color: white;
border: 0;
top:10px;
border-left: 3px solid #d1d1d1;
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
background-image:url("../image/search-loop-2.svg");
background-position: center;
background-repeat:no-repeat;
}
created a fiddle and got this to work in chrome and ie.
https://jsfiddle.net/1r8a2u87/2/
There should be no need to use positioning here at all.
Just box-sizing and vertical-align.
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
background: #c0ffee;
}
input[type='search'] {
height: 35px;
width: 250px;
border-radius: 5px;
border: none;
}
input[type='submit'] {
height: 35px;
width: 45px;
background-color: white;
border: none;
border-left: 3px solid #d1d1d1;
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
vertical-align: middle;
}
<p id="search-form-menu">
<input type="search" name="search-input" placeholder="Поиск">
<input type="submit" value="Go">
</p>

CSS textbox focus

I am trying to disable/remove the border or the blue glow in the text box.
.user {
width: 300px;
height: 50px;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-bottom: -8px;
color: #ffffff;
}
.user::-webkit-input-placeholder {
color: #ffffff;
}
.password {
width: 300px;
height: 50px;
border: #e9e9e9;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-top: -8px;
}
.password::-webkit-input-placeholder {
color: #ffffff;
}
The form part:
<form method="post" class="login_form">
<div style="font-size: 20px; font-family: bebasregular; text-align: right; color: #ffffff;">Login</div>
<input type="text" name="user" class="user" id="user" placeholder="Username or Email"/>
<hr/>
<input type="password" name="password" class="password" placeholder="Password"/><br/>
<input type="submit" name="login_btn" class="login_btn" value="Login"/>
</form>
Now, what I'm trying to make is that the two text boxes in the form will become transparent, even they're in focus.
i tried this code below, but it doesn't work.
.user{
width: 300px;
height: 50px;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-bottom: -8px;
color: #ffffff;
}
.user:focus{
width: 300px;
height: 50px;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-bottom: -8px;
color: #ffffff;
}
.user::-webkit-input-placeholder {
color: #ffffff;
}
.password {
width: 300px;
height: 50px;
border: #e9e9e9;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-top: -8px;
}
.password:focus {
width: 300px;
height: 50px;
border: #e9e9e9;
background-color: rgba(222,222,222,.0);
border: 2px solid rgba(222,222,222,0);
padding-left: 5px;
padding-right: 5px;
font-family: arial;
margin-top: -8px;
}
.password::-webkit-input-placeholder {
color: #ffffff;
}
You can remove this by adding: outline: none; to the .user class (or any element that may receive the outline):
JS Fiddle
Sidenote:
I think its worth noting that on your hover states, you only need to specify the properties that change on that that state. For instance, if the element text color is white initially, and you want it to still be white on hovered state, you can omit that on the hover state.
The bluish glow is coming from the default outline styles. To remove it try:
input {
outline: none;
}
This will remove it from the user, password, and when the button is pressed.
JS Fiddle
use
input {
outline: none;
}
or change
border: 2px solid rgba(222,222,222,0);
to
border: 0px;

How to center an element (search form)?

Here's the code. How to center a search form? This form should be always at the center of the screen no matter what screen resolution is.
I tried margin: 0 auto; but it doesn't work.
Thank You.
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</form>
</div>
Aligning text
Just add text-align: center to form and it is centered on all screen sizes.
text-align: center
This property describes how inline-level content of a block container is aligned.
form {
text-align: center; /* Add */
}
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</form>
</div>
Flexbox solution(Modern):
align-items: center
Items are centered in the cross-axis
Note that in the example we are using flex-direction: column. otherwise just use justify-content: center for flex-direction: row
.search-p {
display: flex;
flex-direction: column; /* Cross axis alignment; simply said vertical stacking/positioning */
align-items: center; /* Center cross-axis */
}
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</form>
</div>
I like to use the new Flexbox layout to place something in the center, a good guide is here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/ Although it's rather new, but it's already supported in majority browsers and it's looking to become the new standard in the next year or so.
/* added new code */
.search-p {
display: flex;
flex-flow: column;
justify-content: center;
align-items: center;
}
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</form>
</div>
CSS and form validation have nothing to do with each other, one is simply styling - the other is data checking which is handled either client-side by JS (bad idea without server validation) and / or server side by (always do this) so there will be no problem. (reacted on comment).
Some other problems might surface tho since text-align: center only works when elements are display: inline-block - float: left; float: right; display: block; will all break this center.
What I would suggest doing is adding a wrapper that will center the search in the form through means of margin: 0 auto - which is a much more solid way to center elements than text-align: center which as it says is meant for text (even tho I have also abused this property many many many times over).
.search-p--i {
color: #333;
font-weight: normal;
font-size: 20px;
text-align: center;
margin-bottom: 30px;
}
.search-p_wrap {
text-align: center;
}
/* Added a selector here */
.search-form .search-form-field-wrap {
display: block; /* optional - divs are block by default. */
width: 80%; /* or anything else */
margin: 0 auto; /* you're gonna want to use this for centering block elements */
}
.search-form button {
font-weight: bold;
color: #fff;
background: blue;
border: 0;
-webkit-border-radius: 28px;
-moz-border-radius: 28px;
border-radius: 28px;
padding: 10px 15px 10px 15px;
font-size: 14px;
}
.search-form input {
width: 240px;
height: 40px;
padding: 0px 5px;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4x;
border: 1px solid #ccc;
}
form {
text-align: center;
}
<div class="search-p">
<div class="search-p_wrap">
<h2 class="search-p_h2">Discover</h2>
<span class="search-p--i">Hi!</span>
</div>
<form class="search-form">
<!-- added a wrapper here -->
<div class="search-form-field-wrap">
<input type="text" placeholder="Enter" required>
<button type="submit">Search</button>
</div>
</form>
</div>