HTML|CSS: Space between input buttons - html

I have a problem in which I have a space between these two buttons.
The code is as follows:
<input id="NeedBtn" class="PostBtn" type="button" />
<input id="ProvBtn" class="PostBtn" type="button" />
.PostBtn
{
background: url(../Images/Buttons/PostButtonF.png) no-repeat;
width: 50px;
height: 28px;
border: none;
margin: 0;
padding: 0;
}
#NeedBtn
{
background-position: 0 0;
}
#ProvBtn
{
background-position: -50px 0;
}
How do I remove that space?
Browser:
Firefox 3.5
IE8

The line feed between the two <input>s creates a space between them on the page. You have to remove the line feed, or use this trick :
<input id="NeedBtn" class="PostBtn" type="button" /><!--
--><input id="ProvBtn" class="PostBtn" type="button" />

Surprised no one mentioned this method yet:
The problem is the white-space between the two buttons is being rendered. Any white-space (line breaks, tabs, spaces) between the buttons will be rendered as a single space by the browser. To fix this, you can set the font-size to 0 on a parent element.
I've added DIV#button-container around the buttons and a style for it showing the font-size trick.
Note: I also had to change the positioning on the button background graphic you linked since it had some extra pixel space around it. Maybe this was part of the problem, maybe not.
Here's a link to the fiddle: http://jsfiddle.net/dHhnB/ and the code:
<style>
#button-container
{
font-size:0;
}
.PostBtn
{
background: url(http://img22.imageshack.us/img22/5066/capturebtn.png) no-repeat;
width: 50px;
height: 28px;
border: none;
margin: 0;
padding: 0;
}
#NeedBtn
{
background-position: -4px 0;
}
#ProvBtn
{
background-position: -59px 0px;
}
</style>
<div id="button-container">
<input id="NeedBtn" class="PostBtn" type="button" />
<input id="ProvBtn" class="PostBtn" type="button" />
</div>

As others have pointed out you can use floats to counter act the whitespace between elements
<input id="NeedBtn" class="PostBtn floated" type="button" />
<input id="ProvBtn" class="PostBtn floated" type="button" />
.floated {
float:left;
}
.floated {
float:left;
}
<input id="NeedBtn" class="PostBtn floated" value="Next" type="button" />
<input id="ProvBtn" class="PostBtn floated" value="Prev" type="button" />
As well as the various hacks for inline-block:
Using 0px font-size in parent and resetting the font-size in the child elements.
<div class="parent">
<div class="child">Some Text</div>
<div class="child">Some More Text</div>
</div>
.parent {
font-size:0px;
}
.parent > * {
display:inline-block;
font-size:14px;
}
Putting all the elements next to each other, ie: <div></div><div></div>
Putting the closing tag on the next line and next to the next element, ie:
<div>
</div><div>
</div>
Putting the closing bracket of the previous element on the next line and next to the next element, ie:
<div></div
><div></div>
Or using html comments
<div></div><!--
--><div></div>
And as stated by others this isn't an optimal solution.
With modern browsers Flexbox styles can now be used
<div class="flex">
<input id="NeedBtn" class="PostBtn flex-child" type="button" />
<input id="ProvBtn" class="PostBtn flex-child" type="button" />
</div>
.flex {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
}
.flex-child {
-webkit-box-flex: 0 1 auto;
-moz-box-flex: 0 1 auto;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
}
.flex {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
}
.flex-child {
-webkit-box-flex: 0 1 auto;
-moz-box-flex: 0 1 auto;
-webkit-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
}
<div class="flex">
<input type="button" class="flex-child" id="slide_start_button" value="Start">
<input type="button" class="flex-child" id="slide_stop_button" value="Stop">
</div>
A guide for flex can be found here, and support list here

You can use css to fix it. Set float:left or float:right on the input buttons. That fixed the problem for me.

Try using a CSS reset - it may solve the browser discrepancy : http://meyerweb.com/eric/tools/css/reset/reset.css

I see they have a set height and width. Adding overflow: hidden should hide the whitespace outside of your defined width. That is an alternative to eliminating the whitespace, as #Pikrass noted. Usually the whitespace is a IE problem, I've not noticed it in FF before.

Related

Unable to center the elements [duplicate]

This question already has answers here:
How can I horizontally center an element?
(133 answers)
Closed 2 years ago.
So, I am new at coding. I was trying to make a very basic static webpage of a calculator using html and css and js.
This is the html
#input {
margin: 0 auto;
}
#num {
border-radius: 25px;
background: #73AD21;
display: inline-block;
padding: 15px;
width: 200px;
height: 100px;
}
<body>
<div id="input">
<div id="num">
<label for="num1">Enter the first number</label>
<input type="number" name="num1" id="num1">
</div>
<div id="num">
<label for="num2">Enter the second number</label>
<input type="number" name="num2" id="num1">
</div>
</div>
<div id="op">
<div id="opadd"><input type="submit" name="add" class="add" value="Add" onclick="add()"></div>
<div id="opsbtrct"><input type="submit" name="subtract" class="sbtrct" value="Subtract" onclick="sbtrct()"></div>
<div id="opmult"><input type="submit" name="multiply" class="mult" value="Multiply" onclick="mult()"></div>
<div id="opdvde"><input type="submit" name="divide" class="add" value="Divide" onclick="dvde()"></div>
</div>
</body>
I want that the #num be centered horizontally.
I tried using
margin: auto;
and
margin: 0 auto;
but nothing works. Please help.
I've been at it for hours.
Here is the complicated way of doing this.
You should create a container div, so you can center the object in.
#container {
display: flex; /* establish flex container */
flex-direction: row; /* default value; can be omitted */
flex-wrap: nowrap; /* default value; can be omitted */
justify-content: space-between; /* switched from default (flex-start, see below) */
background-color: lightyellow;
}
#container > div {
width: 100px;
height: 100px;
border: 2px dashed red;
}
<div id="container">
<div></div>
<div></div>
<div></div>
</div>
when you add margin: 0 auto be sure the html tag not inline or inline-block it should be 'block' css with specific width.
#num {
border-radius: 25px;
background: #73AD21;
display: block;
padding: 15px;
width: 200px;
height: 100px;
margin: 1em auto;
}
label{
white-space: nowrap;
}
just replace this code hope your problem will fix.
add white-space:nowrap for the label to use one line text. #num should be 'block' css with specific width like 200px and display block. thanks

inline-flex input element breaks to new line in IE11

I have a few html elements next to each other in a container positioned with display:inline-flex.
This works well for button elements, but as soon as I try to add an input type="text" element, the textbox is placed below the buttons (only in Internet Explorer 11; not sure about IE10 or below).
It works as expected (textbox in same line as buttons) in Firefox, Chrome and even Edge.
How can I get IE to display this correctly?
See jsFiddle for full html and css code to illustrate the problem: https://jsfiddle.net/vm2kcwd9/1/
.container {
height: 2em;
}
.container>* {
height: 100%;
display: inline-flex;
vertical-align: top;
justify-content: center;
align-items: center;
}
<div class="container">
<button>test</button>
<button>test 2</button>
<button>test 3</button>
<input type="text" value="hello" />
</div>
IE11 is known for having many flex-related bugs.
A simple and easy solution in this case would be to make the parent a flex container:
.container {
display: flex; /* new; forces all children into a single row */
height: 2em;
}
.container>* {
height: 100%;
display: inline-flex;
/* vertical-align: top; <--- not necessary anymore */
justify-content: center;
align-items: center;
margin: 5px; /* for demo only */
}
<div class="container">
<button>test</button>
<button>test 2</button>
<button>test 3</button>
<input type="text" value="hello" />
</div>
Also, since you're making button elements into flex containers, consider this:
Flexbox not working on <button> element in some browsers
Easy solution just add display:flex to parent instead
.container {
display: flex;
justify-content: center;
/* align-items: center; you might not need this */
height: 2em;
}
.container>* {
height: 100%;
margin: 10px;
}
<div class="container">
<button>test</button>
<button>test 2</button>
<button>test 3</button>
<input type="text" value="hello" />
</div>

Flex box column row issue in Internet Explorer

In having an issue in Internet explorer where it does not render flex box elements correctly in conjunction to rows.
Columns seem to work fine in both browsers but...
IE 11 seems to be shrinking the rows for no reason? meaning I can fix it by applying flex: 1 0 auto(prevent shrinking) to rows and flex:1 to columns but is not constant code.
Is there a fix to it in IE or am I doing something wrong as Chrome renders it correctly this is my current fix and seems like a hack to me.
Chrome
IE:
<html>
<body>
<style>
div.form {
display: block;
width: 500px;
height: 500px;
overflow-y: scroll;
}
div.container-row {
display: flex;
flex-direction: column;
background-color: white;
}
div.container-col {
display: flex;
flex-direction: row;
background-color: white;
}
div.field {
display: inline-flex;
flex: 1;
background-color: purple;
padding: 10px;
box-sizing: border-box;
}
div.value {
display: inline-flex;
flex: 1;
background-color: pink;
}
input[type=text] {
width: 100%;
padding: 10px;
}
</style>
<div class="form">
<div class="container-row">
<div class="field">hiiiiiiiidddsssssssdddddddd</div>
<div class="value">
<input type="text" />
</div>
</div>
<div class="container-row">
<div class="field">hiiiiiiiidddsssssssdddddddd</div>
<div class="value">
<input type="text" />
</div>
</div>
<div class="container-col">
<div class="field">hiiiiiiiidddsssssssdddddddd</div>
<div class="value">
<input type="text" />
</div>
</div>
<div class="container-col">
<div class="field">hiiiiiiiidddsssssssdddddddd</div>
<div class="value">
<input type="text" />
</div>
</div>
</div>
</body>
</html>
Fiddle: https://jsfiddle.net/e2jwc371/3/
Cheers for the help ;)
When using flex: 1;, you're not only setting flex-grow and flex-shrink. You're also setting flex-basis (relative sizing between the elements) to 0%. That's probably what's confusing IE.
Change the flex properties to use auto-sizing (flex: 1 auto;), and it works correctly in IE too:
...
div.field {
display: inline-flex;
flex: 1 auto;
background-color: purple;
padding: 10px;
box-sizing: border-box;
}
div.value {
display: inline-flex;
flex: 1 auto;
background-color: pink;
}
...
Updated JSFiddle: https://jsfiddle.net/e2jwc371/4/

distributing differnet elements horizontally inside a div with CSS

Here's the jsFiddle
Suppose I have the following HTML:
<div class="Trigger">click here</div>
<div id="TheContainer">
<div class="One">this is some text</div>
<input type="button" class="Two" value="button 1" />
<input type="button" class="Three" value="button 2" />
</div>
and the following CSS:
#TheContainer{
width:500px;
height:40px;
padding:10px 30px;
display:none;
background:red;}
.One{float:left;}
.Two{float:left;}
.Three{float:left;}
I use the following javascript to show/hide the container:
$(document).ready(function () {
$('.Trigger').click(function () {
var TheContainer = $('#TheContainer');
if (TheContainer.is(':visible') === false) {
TheContainer.show();
} else {
TheContainer.hide();
}
});
});
As you can see, we have a container of fixed width that's hidden, and that contains 3 elements: 1 div and 2 buttons, all 3 of variable sizes because the text of these elements changes at runtime.
I'm looking to horizontally distribute the 3 children elements evenly inside the container. I've looked around and for various reasons, none of the existing solutions seem to work for this particular case.
How can I distribute these elements horizontally and evenly using just CSS?
Note: I know I can do it in jQuery by calculating widths and then setting positions absolutely but I'm looking to see if there's a CSS only solution.
Thanks
I believe this is what you're looking for: http://jsfiddle.net/gq8s4/5/
It involves creating a div around each button so the buttons themselves are not really wide.
<div id="button1"><input type="button" class="Two" value="button 1" /></div>
EDIT: Adjusted answer to new details.
http://jsfiddle.net/gq8s4/3/
.triple-container {
float: left;
width: 33.333333%;
text-align: center;
border: 1px solid yellow;
box-sizing: border-box;
}
.One{
text-align: center;
width: 60px;
margin: 0 auto;
}
.Two{
width: 20px;
}
.Three{
width: 20px;
}
Flexbox is about as close as you're going to get.
http://tinker.io/eb980
#TheContainer {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-pack: justify;
-moz-box-pack: justify;
-ms-flex-pack: justify;
-webkit-justify-content: space-between;
justify-content: space-between;
width: 100%; /* for old Firefox */
}
Support: Firefox, Safari, Chrome, Opera, IE10. http://caniuse.com/flexbox
If you're comfortable adding extra markup, you could fake this with display: table-cell but it won't be perfect:
http://tinker.io/eb980/2
#TheContainer {
width: 100%;
display: table;
}
div {
display: table-cell;
width: 33%;
}
div:nth-child(2) {
text-align: center;
}
div:last-child {
text-align: right;
}
<div id="TheContainer">
<div class="One">this is some text</div>
<div><input type="button" class="Two" value="button 1" /></div>
<div><input type="button" class="Three" value="button 2" /></div>
</div>

How do I make an input element occupy all remaining horizontal space?

Here's the code:
<div style="width:400px">
some text..
<input type="text" />
<button value="click me" />
</div>
How can I make the input element expand to fill all the remaining space, while staying on the same line?
If I put 100% it goes to a line of its own...
See: http://jsfiddle.net/thirtydot/rQ3xG/466/
This works in IE7+ and all modern browsers.
.formLine {
overflow: hidden;
background: #ccc;
}
.formLine input {
width: 100%;
}
.formLine label {
float: left;
}
.formLine span {
display: block;
overflow: hidden;
padding: 0 5px;
}
.formLine button {
float: right;
}
.formLine input, .formLine button {
box-sizing: border-box;
}
<div class="formLine">
<button>click me</button>
<label>some text.. </label>
<span><input type="text" /></span>
</div>
The button must go first in the HTML. Slightly distasteful, but c'est la vie.
The key step is using overflow: hidden;: why this is necessary is explained at:
http://colinaarts.com/articles/the-magic-of-overflow-hidden/#making-room-for-floats
How does the CSS Block Formatting Context work?
The extra span around input is necessary because display:block; has no effect for input: What is it in the CSS/DOM that prevents an input box with display: block from expanding to the size of its container
If you want a cross-browser decision you can use table
<table>
<tr>
<td>some text</td>
<td><input type="text" style="width:100%" /></td>
<td><button value="click me" /></td>
</tr>
</table>
If you can't use table it would be more difficult.
For instance, if you know exactly the width of the some text you can try this way:
<div style="padding:0px 60px 0px 120px;">
<div style="width:120px; float:left; margin:0px 0px 0px -120px;">some text</div>
<input type="button" style="width:50px; float:right; margin:0px -55px 0px 0px;" />
<input type="text" style="width:100%;" />
</div>
If you don't care about old browsers support (IE <= 9) use a flexbox layout:
div{
background-color: red;
display: flex;
display: -webkit-flex;
display: -ms-flex;
display: -moz-flex;
flex-wrap: wrap;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
-moz-flex-wrap: wrap;
}
div input {
flex: 1;
-webkit-flex:1;
-ms-flex: 1;
-moz-flex: 1;
min-width: 200px;
}