Extra space when input and button are next to each other [duplicate] - html

This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 5 years ago.
I can't figure this out for the life of me. Why is there an extra pixel at the top of the button? Also why is there extra white space to the left of it? All I am trying to do is have the input and the button next to each other looking connected.
Is this possible?
html{
background: green;
}
form {
height: 40px;
input {
height: 30px;
width: 75%;
max-width: 400px;
padding: 5px 10px;
font-size: 16px;
border: 0;
background: gray;
}
button {
background: #6699FF;
height: 40px;
width: 60px;
background-image: url('https://i.imgur.com/Tp7TTNO.png');
background-repeat: no-repeat;
background-position: center center;
background-size: contain;
background-origin: content-box;
padding: 7px;
border: 0;
color: transparent;
}
}
<form>
<input type="text" name="Term" class="saearch-input" placeholder="Search more than 3800 summaries">
<button type="submit" class="">Search</button>
</form>
If you can't explain it either but know how I can achieve this another way, I'll accept it as an answer as well.

The space is there because a line jump (return) is considered a white space. To avoid this you can:
Put the input and button tags right next to each other (harder to read your code)
Use the "comment hack" where you'd write your code with a <!-- right after the <input> and --> right before the <button>.
Use display: flex on your form to avoid the whitespace being rendered.
For your button positioning issue, it's related to the alignment on the baseline. vertical-align: bottom; on your button will fix this.
See the solution on this Fiddle

You need to change the position of the button:
position: absolute;
top : 8px;
This is the complete css code:
html{
background: green;
}
form {
height: 40px;
input {
height: 30px;
width: 75%;
max-width: 400px;
padding: 5px 10px;
font-size: 16px;
border: 0;
background: gray;
}
button {
position: absolute;
top : 8px;
background: #6699FF;
height: 40px;
width: 60px;
background-image: url('https://i.imgur.com/Tp7TTNO.png');
background-repeat: no-repeat;
background-position: center center;
background-size: contain;
background-origin: content-box;
padding: 7px;
border: 0;
color: transparent;
}
}

Related

Displaying an Image in Search Bar HTML

I am finishing up this table and I added a search function to it. But in the search bar, I want to put a search icon png file for the background image just like the example on W3Schools. I put it in the myInput field, but either nothing appears in the search bar, or it is so massive you can see a tiny top corner piece of the search icon and I cant figure out how to fix it.
#myInput {
background-image: url('https://cdn1.iconfinder.com/data/icons/hawcons/32/698627-icon-111-search-512.png');
background-position: 10px 10px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}
<input type="text" id="myInput">
You need to use the background-size property. Because the image is larger than the input, you are seeing a white portion of the picture. By setting the property to contain the image is shrunk to the size of your input.
#myInput {
background-image: url('https://cdn1.iconfinder.com/data/icons/hawcons/32/698627-icon-111-search-512.png');
background-repeat: no-repeat;
background-position: left center;
background-size: 30px;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}
<input placeholder="Search..." type="text" id="myInput">
Note: You should also set the background-position property to 0 or remove it all together; otherwise, the search icon will be skewed to the right and downwards.
If instead you want to make the icon smaller, change background-position to left center and set background-size to a px value of your choice.
HTML
<div class="fake-input">
<input type="text" />
<img src="http://www.zermatt-fun.ch/images/mastercard.jpg" width=25 />
</div>
CSS
.fake-input { position: relative; width:240px; }
.fake-input input { border:none; background-color:#fff; display:block; width: 100%; box-sizing: border-box }
.fake-input img { position: absolute; top: 2px; right: 5px }
Source is here

how to put an image inside a button? using html and css only

I'm trying to put an image inside a button like this: example
The code I have so far is:
<button class="buttoni" style="width: 270px; height: 46.9px;">Earn Money Helping <b>People</b> <div><img src="img/pla.png" style="width: 25px; height: 25.7px; margin-left: 220px; margin-top: -50px;"> </div></button>
You could create the icon using CSS instead. This would allow you to apply transitions / animations etc.
.buttoni {
width: 270px;
height: 46.9px;
position: relative;
transition: .2s ease-in-out;
cursor: pointer;
border: 0;
}
.buttoni:before,
.buttoni:after {
position: absolute;
content: '';
transition: .2s ease-in-out;
}
.buttoni:before {
background: #29d4a2;
border-radius: 50%;
width: 25px;
height: 25px;
right: 15px;
top: 10px;
}
.buttoni:after {
width: 0;
height: 0;
display: block;
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-top: 6px solid #FFF;
transform: rotate(-90deg);
right: 20px;
top: 19px;
}
.buttoni:hover {
background: #29d4a2;
}
.buttoni:hover:before {
background: blue;
}
<button class="buttoni">Earn Money Helping <b>People</b>
</button>
You can try this:
<button class="buttoni" style="width: 270px; height: 46.9px;">Earn Money Helping <b>People</b><img src="img/pla.png" style="position: relative; float: right; width: 25px;"></button>
Adjust the height as needed for the image.
Are you just trying to learn HTML/CSS, I'd suggest putting the styling into a stylesheet rather than inline.
Well, if you want to do it using html
<button class="buttoni" style="width: 270px; height: 46.9px;">Earn Money Helping <b>People</b><img src="img/pla.png" style="width: 25px; height: 25.7px; margin-left: 220px; margin-top: -50px;"></button>
Do not call div inside button because it is an erroneous form, since the button supports the insertion of images by default
In case you want to do it using CSS
This should do do what you want, assuming your button image is 16 by 16 pixels.
.buttoni {
background-image: url(/images/buttons/add.png); /* 16px x 16px */
background-color: transparent; /* make the button transparent */
background-repeat: no-repeat; /* make the background image appear only once */
background-position: 0px 0px; /* equivalent to 'top left' */
border: none; /* assuming we don't want any borders */
cursor: pointer; /* make the cursor like hovering over an <a> element */
height: 16px; /* make this the size of your image */
padding-left: 16px; /* make text start to the right of the image */
vertical-align: middle; /* align the text vertically centered */
}

Setting background image to div in css leaving an offset in left and right

I have a set of divs in html 5 having class name submenuButton. I want to add a common background image to these divs using css. I tried the following:
.submenuButton{
background-image:url(images/but-up-center.png);
background-position: 5px 0px;
background-repeat:repeat-x;
background-size: contain;
}
This sets the div image leaving 5px from the left and extends to the end of the button. But I want to have this image ended at 5px from the right also.
So, could anybody suggest what can be done in the css so that it leaves 5px space both on the left and the right.
You could use background-clip: content-box; in combination with a 5px padding left and right.
This would give you the following code:
.submenuButton{
padding: 0 5px;
background-image:url(images/but-up-center.png);
background-repeat:repeat-x;
background-size: cover;
background-clip: content-box;
}
More information on background-clip can be found on developer.mozilla.org
Example on jsFiddle
You can do it with a pseudo
.submenuButton {
position: relative;
border: 1px solid red;
height: 100px;
width: 200px;
}
.submenuButton::before {
position: absolute;
content: '';
top: 0;
left: 5px;
right: 5px;
height: 100%;
background:url(http://placehold.it/200/) no-repeat center;
background-size: cover;
}
<div class="submenuButton"></div>

HTML/CSS Position icon above text

Currently i have a lot of back buttons on my page:
<input type="button" id="Back" value="Back" onclick="back();" class="backButton">
I need to add icon to it to look something like this:
First how do I add icon above text and aligne them centrally.
And second is it possible to do it using only CSS. ( if not with only a minor modifications to HTML )
Thx in advance.
I need to add icon to it to look something like this:
First how do I add icon above text and align them centrally.
You should use button element for that. It exists for this very purpose (custom styling and markup). However, you need not to use a background-image for that. To be able to control everything via CSS, just make sure you have same markup for all the buttons you have and then control using classes.
For example:
Markup:
<button class="cancel">
<i></i>
<span>Cancel</span>
</button>
CSS:
button.cancel i::after {
content: '\00d7'; display: block;
font-size: 26px; font-style: normal; font-weight: 600; color: red;
}
Use the after psuedo-element on i (or span whatever) and depending on the class use the content property to insert your icon as text (glyph) which you can style as you want.
And second is it possible to do it using only CSS. ( if not with only
a minor modifications to HTML )
This is very much possible, but cumbersome. I would not recommend this method, it is not worth the effort. You have been warned.
To use the existing input as-is without any change in the markup, you need to style the input itself and will have to use a background-image (in fact two background images). The input styling has a problem, that it loses its platform style as soon as you tinker with its style. So, you will lose the button like behaviour and Windows like button gradient and effects. You will have to replicate all that functionality via CSS.
For example:
Markup:
<input type="button" value="Cancel" data-value="Cancel" />
CSS:
input[type=button] {
min-width: 72px; height: 64px; position: relative;
display: inline-block; vertical-align: top; padding-top: 36px;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/1/1e/Emoji_u274c.svg'),
linear-gradient(#f5f5f5, #dfdfdf);
background-repeat: no-repeat, no-repeat;
background-position: center 4px, center center;
background-size: 24px, auto;
border: 1px solid #aaa; border-radius: 3px;
}
input[type=button]:active {
background-image: url('https://upload.wikimedia.org/wikipedia/commons/1/1e/Emoji_u274c.svg'),
linear-gradient(#dfdfdf, #f5f5f5);
background-repeat: no-repeat, no-repeat;
background-position: center 4px, center center;
background-size: 24px, auto;
outline: 0px;
}
input[type=button]:focus { outline: 0px; }
The above code uses first background-image to show the icon, and second background-image to show the gradient (like Windows platform style). It uses padding-top to push the text down and :active state to set the behaviour of inverting the gradient when clicked. :focus state to remove the outline.
All this to mimic the behaviour of a button! It is much better to use button itself.
Here is a combined demo of both the techniques:
Fiddle: http://jsfiddle.net/abhitalks/yw7wmvwh/1/
Snippet:
button {
min-width: 72px; height: auto;
display: inline-block; vertical-align: top;
}
button.ok i::after {
content: '\2713'; display: block;
font-size: 23px; font-style: normal; font-weight: 600; color: green;
}
button.cancel i::after {
content: '\00d7'; display: block;
font-size: 26px; font-style: normal; font-weight: 600; color: red;
}
input[type=button] {
min-width: 72px; height: 64px; position: relative;
display: inline-block; vertical-align: top; padding-top: 36px;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/1/1e/Emoji_u274c.svg'),
linear-gradient(#f5f5f5, #dfdfdf);
background-repeat: no-repeat, no-repeat;
background-position: center 4px, center center;
background-size: 24px, auto;
border: 1px solid #aaa; border-radius: 3px;
}
input[type=button]:active {
background-image: url('https://upload.wikimedia.org/wikipedia/commons/1/1e/Emoji_u274c.svg'),
linear-gradient(#dfdfdf, #f5f5f5);
background-repeat: no-repeat, no-repeat;
background-position: center 4px, center center;
background-size: 24px, auto;
outline: 0px;
}
input[type=button]:focus { outline: 0px; }
<button class="ok">
<i></i>
<span>Ok</span>
</button>
<button class="cancel">
<i></i>
<span>Cancel</span>
</button>
<hr/>
<input type="button" value="Cancel" data-value="Cancel" />
just add your class to css file
.backButton{
background: url(https://cdn3.iconfinder.com/data/icons/musthave/24/Delete.png) no-repeat;
background-position:center top;
height: 40px;
width: auto;
text-align: center;
padding:24px 10px 10px 10px;
border: 1px solid #555555;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
<input type="button" id="Back" value="Back" onclick="back();" class="backButton">
I hope this will help
HTML
button {
font-size: 20px;
width: 100px;
height: 100px;
}
#rock {
padding:10px;
background: url(https://upload.wikimedia.org/wikipedia/commons/6/65/Crystal_button_cancel.svg) top center ;
background-repeat: no-repeat;
background-position: 50% 10%;
}
<button id="rock" onClick="choose(1)">Cancle</button>
HTML
<button id="btnCancel" onClick="choose(1)">Cancel</button>
CSS
button {
font-size: 20px;
width: 100px;
height: 100px;
}
#btnCancel {
padding:10px;
background: url(https://upload.wikimedia.org/wikipedia/commons/d/da/Crystal_button_cancel.png) top center ;
background-repeat: no-repeat;
background-position: 50% 10%;
border-radius : 20px;
}

Why do buttons without text have different vertical alignment?

I have the following form that I want to customize.
<form>
<input type="text" value="" />
<button></button>
</form>
I want the button to have a background image and no text.
The problem is that the button loses its vertical alignment when I don't write any text into it.
Can someone explain this behaviour?
input {
width: 200px;
height: 20px;
border: 1px solid #0066cc;
border-radius: 20px;
padding: 0;
}
button {
margin-left: -30px;
background-color: white;
background-image: url("http://s18.postimg.org/k6rruvokl/loupe_recherche.gif");
background-repeat: no-repeat;
background-position: center center;
border: none;
width: 19px;
height: 19px;
padding: 0;
}
<form>
<input type="text" value="" />
<button></button>
</form>
<form>
<input type="text" value="" />
<button>Some text</button>
</form>
Because they have a default vertical-align property of baseline, which alignes inline elements according to the bottom of the element's content (the text).
If there is no content, the vertical alignment cannot be based upon it, instead it is based on the bottom of the element.
You can fix this with a non-breaking space, or by changing the vertical alignment.
<form>
<input type="text" value="" />
<button> </button>
</form>
JSFiddle
-- Or --
button {
margin-left: -30px;
background-color: white;
background-image: url("http://s18.postimg.org/k6rruvokl/loupe_recherche.gif");
background-repeat: no-repeat;
background-position: center center;
border: none;
width: 19px;
height: 19px;
padding: 0;
vertical-align:middle;
}
JSFiddle
A good read:
All you need to know about vertical-align - Christopher Aue
If you add the vertical-align indicator to the button it should help it move to the position you want, it will need a little bit of extra margin just to push it up a bit though.
Try this out:
input {
width: 200px;
height: 20px;
border: 1px solid #0066cc;
border-radius: 20px;
padding: 0;
}
button {
margin-left: -31px;
background-color: white;
background-image: url("http://s18.postimg.org/k6rruvokl/loupe_recherche.gif");
background-repeat: no-repeat;
background-position: center center;
border: none;
width: 19px;
height: 19px;
padding: 0;
vertical-align: bottom;
margin-bottom: 1px;
}
<form>
<input type="text" value="" />
<button></button>
</form>
This seems to be a matter of vertical alignment because the elements are inline-(block?)
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
margin: 0;
padding: 0;
}
input {
width: 200px;
height: 20px;
border: 1px solid #0066cc;
border-radius: 20px;
vertical-align: top;
}
button {
background-color: white;
background-image: url("http://s18.postimg.org/k6rruvokl/loupe_recherche.gif");
background-repeat: no-repeat;
background-position: center center;
border: none;
width: 20px;
height: 20px;
border: 1px solid red;
vertical-align: top;
}
<form>
<input type="text" value="" />
<button></button>
</form>