HTML: How to center align a form - html

I have the following HTML code and I want to make my form aligned in center.
<form action="advsearcher.php" method="get">
Search this website:<input align="center" type="text" name="search" />
<input type="submit" value="Search"/>
</form>
How can I do that?

#Lucius and #zyrolasting have it right.
However, you will probably need to give the form a specified width for it to work properly.
form {
margin: 0 auto;
width:250px;
}

Just put some CSS into the stylesheet like this
form {
text-align: center;
}
then you're done!

Being form a block element, you can center-align it by setting its side margins to auto:
form { margin: 0 auto; }
EDIT:
As #moomoochoo correctly pointed out, this rule will only work if the block element (your form, in this case) has been assigned a specific width.
Also, this 'trick' will not work for floating elements.

This will have the field take 50% of the width and be centered and resized properly
{
width: 50%;
margin-left : 25%
}
May also use "vw" (view width) units instead of "%"

Use center:
<center><form></form></center>
This is just one method, though it's not advised.
Ancient Edit: Please do not do this. I am just saying it is a thing that exists.

Just move align="center" to the form tag.
<form align="center" action="advsearcher.php" method="get">
Search this website:<input type="text" name="search" />
<input type="submit" value="Search"/>
</form>

The last two lines are important to align in center:
.f01 {
background-color: rgb(16, 216, 252);
padding: 100px;
text-align: left;
margin: auto;
display: table;
}

#form{
position:fixed;
top:50%;
left:50%;
width:250px;
}
You can adjust top & left depending on form size.

Try this :
Set the width of the form as 20% by:
width : 20%;
now if the entire canvas is 100 %, the centre is at 50%. So to align the centre of the form at the centre, 50-(20/2) = 40.
therefore set your left margin as 40% by doing this :
left : 40%;

simple way:Add a "center" tag before the form tag

<center><form></form></center>
does work in most cases like The Wobbuffet mentioned above...

Related

How can I show this 2 div side by side?

Into a page I have the following
<div id="submitEventButtonDiv">
<div id="wwctrl_backButton" align="right">
<input id="backButton" type="submit" onclick="return clickSubmitButton();" value="Back">
</div>
<div id="wwctrl_submitEventButton" align="right">
<input id="submitEventButton" type="submit" onclick="return clickSubmitButton();" value="Submit">
</div>
</div>
As you can see there is an external div having id="submitEventButtonDiv" that contains 2 divs containing in turn 2 input fields.
Obviously when the 2 input field are displayed appear one below the other (because they are contained into a div that is block element).
How can I display one beside the other? (I can't delete the div that contains each input field because it is automatically rendered by a tag library that I am using, this that I have post is the rendered HTML obtained from a page that us Struts 2 tag library that wrap HTML following its logic, so in this case I can only work on CSS to do it)
Tnx
Just display the child elements inline.
#submitEventButtonDiv > div {
display:inline;
}
codepen here http://codepen.io/anon/pen/jEmaed
Depending on your cross browser needs, use flexbox:
#submitEventButtonDiv {
display: flex;
}
Will make all of the children flex items, which by default, stack horizontally, like you want.
If you float them both they'll be positioned next to each other. Adding
#submitEventButtonDiv > div {
float:left;
display:inline;
}
To your css should position them both to the left of the page next to eachother.
If the inner div IDs are always same, then you can add the following styles in your css:
div#wwctrl_backButton {
display: inline-block;
width: 50%;
}
div#wwctrl_submitEventButton{
display: inline-block;
width: 50%;
}
This generates the desired effect.
#submitEventButtonDiv > div {
display:inline;
float:right;
}

Make two elements with dimensions inline

I am trying to make the two elements in the header, the pink section and the green section, be inline ( in one line and not wrapping to a new line as the green section is currently).
However, I can't use display:inline; as the items need to have a width.
How can I achieve both of these elements being inline and keep their widths?
Here is the page where the elements are: https://dl.dropboxusercontent.com/u/270523/help/new.html
The inline-block solution:
#logo {
width: [WIDTH1]%;
min-width: 225px;
background: pink;
}
#input{
width: [WIDTH2]%;
background: green;
}
#input,#logo {
display: inline-block;
height: 100%;
vertical-align:top;
}
Where [WIDTH1] + [WIDTH2] = 100%.
For this to work, you need to delete the whitespaces between the two elements.
So not like now:
<section id="logo"></section>
<section id="input">
<input id="searchInput" type="text" name="search" autocomplete="off">
</section>
But instead:
<section id="logo"></section><section id="input">
<input id="searchInput" type="text" name="search" autocomplete="off">
</section>
You've set the float to the #input section but not the #logo section.
#logo {
width:20%;
float: left;
}
Your section input is dropping because it is having extra width, make it around 77% then it will work for you.
#input{width:77%;}
first of all it is really nice You use html5 tags its good for You. In my opinion the easiest way is use a float for all of elements in header, but remember to clear:both parent tag.
Here is demo of modified your code: http://jsfiddle.net/bartekbielawa/WgtAP/

Default input element size

The input element by default is size="20" if this is not defined inline or a CSS style rule is attributed to it.
How do you make the default the actual size of the value? For other elements:
<div style="display: inline; width: auto;">
The box will only fit the width of it's content
</div>
width: auto is all you need. I cannot put a defined width since the value may be longer. For example:
<input id="short" type="text" value="1000" />
<input id="long" type="text" value=" Areally long name is in this input field." />
I want #short to be the size of 1000 (essentially size="4" + the padding) but the #long to be as long as needed. These inputs are coming in programatically so I can't simply put a short class on the one's I want short and a long class on the one's I want long. I would really just like it if I could put:
input { width: auto; }
Anyway to do this?
The input element by default is size="20"
This "default size" depends on the browser, the version and your OS.
It is not possible to do this in inline styles.
the best solution is to set width and padding so it adds up to 100%
input {
width: 98%;
padding: 1%;
}
then set it to absolute left and right 0
<fieldset>
<input type="text" />
</fieldset>
finally add this css
<style type="text/css">
fieldset {
position: relative;
}
input {
position: absolute;
left: 0;
right: 0;
}
</style>
"How do you make the default the actual size of the value? (...) I would really just like it if I could put: input { width: auto; }
Anyway to do this?"
In an Input HTML Element, width is controlled by its size attribute. This is a quick way I use to simulate { width: auto; }, and it is browsers dependance proof:
Angular:
<input type="text" [size]="element.value.length + 1" #element>
Pure Vanilla JavaScript:
<input type="text" oninput="this.size = this.value.length + 1">
Assuming you have each input on its own row in the form, you can set size attribute to the desired size but also add a css of:
input[type="text"] {
max-width: 100%;
}
This ensures that the field does not overflow the form. This approach gives a visual cue of how much data is expected for short fields (because they will be the right size) while putting a cap on long ones.

Text input width max and input submit on one line

I make a small chat. There was a problem with CSS, because I'm more a programmer than a layout designer.
HTML:
<div class="chat_input_box">
<input type="text" class="chat_input_text" name="message">
<input type="submit" value="Отправить" class="chat_submit_button">
</div>
CSS:
.chat_input_box {
width: 100%;
}
.chat_input_text {
width: 83%;
}
.chat_submit_button {
margin-right: 0px;
}
The problem is that it is not goes to set the width of the text field maximum without hardcode (83% for example).
try to put button and input filed into two different divs. Give button div CSS parameter "float: right; width: someAmount px;"
Not really what you asked for, but as close as possible: http://jsfiddle.net/TQvg8/

CSS: Reset INPUT Element to Original Width

I don't think this is possible but I am going to ask it nonetheless, lets say I have the following HTML:
<div style="width: 500px;">
<input class="full" />
</div>
And the corresponding CSS:
input {
width: 100%;
}
.full {
display: inline;
float: left;
margin: 0 2%;
width: 96%
}
In this case my input element will have a width of 480px, this works fine for most of my needs but in some specific cases it doesn't, for instance:
<div style="width: 500px;">
<input name="day" size="2" />
<input name="month" size="2" />
<input name="year" size="4" />
</div>
This will make the browser render each input with a width of 500px (jsFiddle)...
Is there anyway to force the browser to rollback to the default style?
Just set width:auto to those inputs. This works with or without setting the size attribute (respects size if you have set it).
http://jsfiddle.net/Madmartigan/kQDpY/3/
BTW, float will automatically set the display type to block, your inline declaration for the div isn't doing anything so you don't need it.
Just override it with an !important selector:
input {
width: auto !important;
}
And a demo: http://jsfiddle.net/kQDpY/4/
width: auto;
Is this what you're after?
Basically I've set width:auto; to all inputs that define size attribute. Maybe this isn't the correct attribute but it shows how you can distinguish inputs between each other.
But basically you haven't told us what you'd like with these three inputs. Would you want them to occupy the whole width but should be 2:2:4 in size or is it just auto with as it seems we all did...