vertical align html form - html

I'm looking to center this textbox and submit button on the page both vertically and horizontally but the following code just puts it in the top center. How can I center this to the page? It's like it's ignoring the vertical-align setting.
<div style="text-align:center; vertical-align:middle">
<form action="save_thought.php" method="post">
<input type="text" name="thought"><br>
<input type="submit" value="Submit">
</form>
</div>

You can use position:absolute DEMO http://jsfiddle.net/kevinPHPkevin/U8dZr/
div#form-wrapper {
position:absolute;
top:50%;
right:0;
left:0;
}

You can also go for this:
HTML
<div id="main" >
<form id="frm" action="save_thought.php" method="post">
<input type="text" name="thought"><br>
<input type="submit" value="Submit">
</form>
</div>
CSS
#main
{
line-height: 400px;
text-align:center;
vertical-align:middle;
}
#frm
{
display: inline-block;
vertical-align: middle;
line-height: 14px;
}
Demo Here

None of the solutions provided above worked for me for my real proyect in which I wanted to center both vertically and horizontally a form inside a div (not taking as reference the full page) but I got it using display: flex; property. Just display your parent div as flex and then use the property margin: auto; for your child form.
In your case, it would be like this:
HTML code:
<div id="centeringDiv" style="display: flex;">
<form id="mainForm" action="save_thought.php" method="post">
<input type="text" name="thought"><br>
<input type="submit" value="Submit">
</form>
</div>
CSS code:
html, body{
height: 100%;
width: 100%;
margin: 0;
}
#centeringDiv{
height: 100%;
width: 100%;
display: flex;
}
#mainForm{
margin: auto;
}
JSFiddle in which you can see that the form it is being centered both vertically and horizontally in the full page.

CSS will probably never stop to amaze me. Given Dhaval Marthak's answer I was actually able to achieve what I wanted, right aligned labels and left aligned fields (on the right of the labels of course):
label {
display: block;
position:relative;
text-align: right;
width: 100px;
margin: 0 0 5px 0;
}
label > input,
label > select,
input {
position: absolute;
left: 120px;
}
The JSfiddle still points out my remaining problem, the field will align with the bottom of the label if the label breaks the line. But I am sure that can be amended as well.
Of course it would also be nice if the space between labels and fields could be assigned more "dynamically" based on the sizes of the labels (e.g. in different languages) but I have not seen this done anywhere so far. I guess I will have to resort to tables if I really need it.

You have have to change the width from "100%" to "px" and then you have to do margin: "auto".
Then the form will be centered horizontally.

Related

Why does "inline-block" property wont work on this page?

#searchBar {
display: inline-block;
}
#container {
width: 580px;
background-color: white;
}
#logo {
margin-left: 25%;
}
<div id="container">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/2880px-Google_2015_logo.svg.png" id="logo" height="40px">
<form>
<input type="text" id="searchBar">
<input type="submit" value="Search Google">
<input type="submit" value="Feeling Lucky">
</form>
</div>
I'm trying to replicate the Google home page(sort of),what I'm trying to achieve is that the search bar (where input type equals search)will be one line above the two buttons,i can achieve that by other ways for sure but wanted to check the inline-block property..the object should act like its "inline",captures only the width of the content and "block" too,but for some reason the "block" does not work and the buttons are just next to the right side of the search bar..
Compared to display: block, the major difference is that display: inline-block does not add a line-break after the element, so the element can sit next to other elements.
So you will have to use display: block to fit the width.
display : block will serve the purpose. As {display: inline-block} will not add the line break. So, you have to modify the CSS as:
#searchBar {
display: block;
}
#container {
width: 580px;
background-color: white;
}
#logo {
margin-left: 25%;
}

How do I specify in CSS that a button should only take up as much width as its text?

I have four elements that I would like to take up 100% of the width available in the parent DIV -- an image, a jQuery UI slider, another image, and a button.
<div id="sliderScaleDiv">
<div id="halo">
<img width="75" src="http://www.clker.com/cliparts/n/c/1/B/r/q/angel-halo-with-wings.svg" alt="Angel halo" />
</div>
<div class="fluid"> <div class="slider"></div>
</div>
<div id="skull">
<img width="75" src="https://temporarytattoos.com/pub/media/catalog/product/cache/image/700x560/e9c3970ab036de70892d86c6d221abfe/s/k/skull-and-crossbones-temporary-tattoos_1601.jpg" alt="Skull" />
</div>
<form class="voteForm" id="new_vote" action="/votes" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="Yp7vN3kTCg2brdU3/yHknGxnE3I6V8xA/C3+zj4lbVN7qRS+pWWS/V4UPawx/gngJBkEpWGTZSMltOkSQuUfdw==">
<input value="23" type="hidden" name="vote[person_id]" id="vote_person_id">
<input type="hidden" name="vote[score]" id="vote_score">
<button name="next" type="button" id="nextButton" class="btn-feedback">Skip</button>
</form>
</div>
The only item I would like to have a variable width is the slider (take up as much space as possible). Without hard-coding a pixel value for the button, how do I specify in CSS that I want the button to take up as much width as its text occupies but no more? Right now, it seems like all my items are getting compressed (at least the slider is not filling the remaining space) and I think its because I haven't specified some type of width CSS element for the button -- http://jsfiddle.net/u72596ta/8/ .
The styles I've used for the four elements are
#halo {
position: relative;
z-index: 20;
vertical-align: top;
display: inline-block;
width: 75px;
display: table-cell;
}
.fluid {
display: table-cell;
}
#skull {
position: relative;
z-index: 20;
vertical-align: top;
display: inline-block;
width: 75px;
display: table-cell;
}
#nextButton {
display: inline-block;
display: table-cell;
}
If you change #nextButton to be display: inline-block and allow your .fluid to be width: 100% I think that might be close to the result you
re looking for.
Example: http://jsfiddle.net/u72596ta/10/
EDIT: I also just did another glance over your code and noticed that you're overwriting the display property. Not sure if that's for debugging purposes or not. Another suggestion would be to combine your #halo and #skull selectors. Since they are using the same CSS declarations, it will make your stylesheet shorter and cleaner :)

How do I align my search-input and submit-button to the center of the page?

Sorry, I know this is super basic but I've been through my coding reference books all day and I think my mind's a little buggered. I need to get BOTH the input field AND the "submit" button in one line, in the center of the page, similar to Google.
.logo {
width: 50%;
display: block;
margin: auto;
}
.input-fields {
padding: 3%;
width: 40%;
display: block;
margin: auto;
font-size: 90%;
}
.submit {
padding: 3%;
width: 15%;
}
<header>
<img class="logo" src="OnSaleTodayMobile.png" alt="OnSaleToday.co.za">
</header>
<div class="form-wrapper">
<form class="center">
<input class="input-fields" name="search" type="text" placeholder="Search for anything...">
<input class="input-fields submit" name="find" type="submit" value="Find">
</form>
</div>
The problem I'm getting is that the button is stacking underneath the text-field. What am I missing out?
Well Google has it vertically and horizontally aligned so you should try something like this (simplified version):
* {margin: 0; padding: 0; box-sizing: border-box}
html, body {width: 100vw; height: 100vh}
body {
display: flex;
justify-content: center;
align-items: center;
}
.align-me {
display: flex;
flex-direction: column;
align-items: center;
}
.align-me > .form-wrapper > .center {
display: flex;
}
<div class="align-me">
<header>
<img class="logo" src="OnSaleTodayMobile.png" alt="OnSaleToday.co.za">
</header>
<div class="form-wrapper">
<form class="center">
<input class="input-fields" name="search" type="text" placeholder="...">
<input class="input-fields submit" name="find" type="submit" value="Search">
</form>
</div>
</div>
But their design is not responsive and this is.
What you are seeing is the default behaviour of display:block.
Using display:inline-block will make them block elements so you can add padding, etc, but make them inline so they will appear beside each other (assuming they fit, and other styles don't change the default behaviour).
Just change the display from block to inline-block in your CSS here:
.input-fields {
[...]
display:inline-block;
}
Working snippet:
.logo {width: 50%; display:block; margin:auto;}
.input-fields {
padding:3%;
width:40%;
display:inline-block; /* change this from block to inline-block */
vertical-align: middle; /* this will help with any vertical alignment issues */
margin:auto;
font-size:90%;
}
.submit {
padding:3%;
width:15%;
}
/* Add this this to center your inputs -
you included the "center" class in your HTML but not it in your CSS */
.center { text-align:center}
<header><img class="logo" src="OnSaleTodayMobile.png" alt="OnSaleToday.co.za"/></header>
<div class="form-wrapper">
<form class="center">
<input class="input-fields" name="search" type="text" placeholder="Search for anything..."/>
<input class="input-fields submit" name="find" type="submit" value="Find"/>
</form>
</div>
You are missing a
display: inline-block;
on the elements you want to display in line. You currently have 'display: block;' This will push elements on to there own line.
You may also want:
vertical-align: middle;
To make them vertically aligned relative to each other.
To make sure they both stay dead center in the page put them all in a container (or just use your existing form container) and style it like:
position: absolute;
top: 50%;
left: 0;
right: 0;
transform: translateY(-50%);
text-align: center;
This will ensure no matter what the screen size is the container is in the middle both vertically and horizontally.

Why does the font-size of an input breaks the inline-block alignment?

I recently stumble over the problem where I had to align to inputs next to each other which had slightly different font-size parameter. It turns out that is breaks the alignment of the input elements.
Here an exaggerated example to show the problem:
div input {
display: inline-block;
width: 100px;
height: 34px;
}
.field1 {
font-size: 50px;
}
<div>
<input class="field1" type="text" value="test">
<input class="field2" type="text" value="test">
</div>
What is the best way to fix that? How do I align the two input elements in one line next to each other?
vertical-align: top; seems to help solve this problem. I added only that rule which will align the input elements to the top of its parent container (the div in this case).
div input {
display: inline-block;
width: 100px;
height: 34px;
vertical-align: top; /* added */
}
.field1 {
font-size: 50px;
}
<div>
<input class="field1" type="text" value="test">
<input class="field2" type="text" value="test">
</div>

Why a div with float: right does not respect the margin of the hr tag below?

I'm using twitter-bootstrap framework and I'm trying to put a form-control with some text aligned to the right and some text aligned the the left (all in the same line).
Here is the fiddle: http://jsfiddle.net/2gu29/3/
HTML
<div class="inlineb">Hello</div>
<div class="floatr inlineb">
<input type="text" class="form-control inlineb"> cm
</div>
<hr>
<button class="btn btn-primary">Hello stackoverflow</button>
CSS
hr {
margin: 30px 0px 30px 0px;
border-color: red;
}
body {
padding: 30px;
}
.form-control {
width: 100px;
}
.floatr {
float: right;
}
.inlineb {
display: inline;
}
But, as you see, the div which contains the input above the hr tag, does not respect the margin setted to the hr in the css (margin-top: 30px) if I use float: right. But if I don't use this property, it respect it. see it: http://jsfiddle.net/2gu29/9/
Why my solution does not work? Could you give me an exmplanation, please? Do you have another alternative to do it?
Any tip, advice or help will be appreciated, and if you need more info, let me know and I'll edit the post.
This is always a problem with floated element and inline element. If you want to fix this issue then you need to add display:inline-block and width:100% in hr tag.
Here is updated fiddle
The float CSS property specifies that an element should be taken from the normal flow and placed along the left or right side of its container, where text and inline elements will wrap around it.
If you read this line from MDN Float Documentation you can understand that whenever we apply float property to any element, it is taken out from normal flow of the page.
Hence, margin is getting calculated from the Hello word instead of floated element.
Solution
Wrap the divs inlineb and floatr in another div and you should be fine. Checkout this JS Fiddle
Updated Code
<div class='container'>
<div class="inlineb">Hello</div>
<div class="floatr inlineb">
<input type="text" class="form-control inlineb" /> cm
</div>
</div>
<hr>
<button class="btn btn-primary">Hello stackoverflow</button>
#blunderboy has already explained what the problem is. My answer is just an alternate solution, which I have floated the left element and aligned the input to the right, therefore it still has height.
HTML
<div class="floatl">Hello</div>
<div class="rightinput">
<input type="text" class="form-control inlineb"/> cm
</div>
<hr>
<button class="btn btn-primary">Hello stackoverflow</button>
CSS
hr {
margin: 30px 0px 30px 0px;
border-color: red;
}
body {
padding: 30px;
}
.form-control {
width: 100px;
}
.floatl {
float: left;
}
.inlineb {
display: inline;
}
.rightinput {
text-align: right;
}