HTML/CSS Position icon above text - html

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;
}

Related

Add a hovering effect on an element with masked or backround SVG Icon

How do I add an hovering effect on an element that serves as an SVG Icon that can also change the color of the icon itself.
This is what I have right now, two option, using a mask image or display the icon as background.
The problem using background in CSS, the display is correct when hovering but I can't change the icon color.
.icon {
display: inline-block;
background: url('https://cdn-icons-png.flaticon.com/512/151/151773.png') no-repeat center; /* imagine the file here as an SVG file */
background-size: 100px 100px !important;
height: 100px;
width: 100px;
}
.icon_interactive:hover {
cursor: pointer;
background-color: white;
border-radius: 50%;
padding: 25px;
}
<div style="padding: 100px; background-color: gray; text-align: center;">
<span class="icon icon_interactive"></span>
</div>
The problem using mask-image in CSS, the display is incorrect when hovering but I can change the icon color.
.icon {
-webkit-mask: url('https://cdn-icons-png.flaticon.com/512/151/151773.png');
-webkit-mask-size: cover !important;
mask-image: url('https://cdn-icons-png.flaticon.com/512/151/151773.png');
mask-size: cover !important;
display: inline-block;
background-color: black;
height: 100px;
width: 100px;
}
.icon_interactive:hover {
cursor: pointer;
background-color: white;
border-radius: 50%;
padding: 25px;
}
<div style="padding: 100px; background-color: gray; text-align: center;">
<span class="icon icon_interactive"></span>
</div>
What I want to create is an interactive icon when hovering that you can change the color of the icon itself.
EDIT:
To summarize my problem, I have a masked element which acts as an Icon an I need to have it controllable color, size and also a background circle when hovering the element.
Consider pseudo element to combine both tricks:
.icon {
display: inline-block;
height: 100px;
width: 100px;
border-radius: 50%;
padding: 25px;
}
.icon:before {
content: "";
display: block;
height:100%;
background: #000;
-webkit-mask: url('https://cdn-icons-png.flaticon.com/512/151/151773.png') center/100px 100px no-repeat;
}
.icon_interactive:hover {
cursor: pointer;
background-color: white;
}
.icon_interactive:hover::before {
background:red;
}
body {
background:gray;
}
<span class="icon icon_interactive"></span>

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

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

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;
}
}

multiple background colors for an image

What I am looking to achieve is to display an image with a transparent background layer, this image would sit over a background which would have a white border and gray box centred in the image area.
Basically to give the image a partial gray background, then to leave the rest white, to give the appearance of the image to "float" over the gray background as well as allow me to make simple css changes to change the background verses having to redo the images to change the look.
this is the css I have tried:
.borderlist img {
text-align:center;
vertical-align:middle;
background:
linear-gradient
(255,255,255, 0.9),
url('../images/gray.png') no-repeat;
max-width: 100%;
height:auto;
}
And the html:
<span class="borderlist"><img src="images/bounty.png" alt="BOUNTY" title=" BOUNTY " width="225" height="155"></span><br>BOUNTY
I removed the underline from the <a> because it creates a weird underline on the <br>. If you want the text to be underlined, you could throw it in a <span> with a class that tells it to have it. but this is what I got. Let me know if you're needing it to do something different.
.overflowing-img {
display: inline-block;
text-align: center;
text-decoration: none;
}
.undrline {
text-decoration: underline;
}
.borderlist {
text-align: center;
}
.borderlist img {
text-align:center;
vertical-align:middle;
background-image: linear-gradient(rgba(160,160,160, 0.5), rgba(160,160,160, 0.5));
background-repeat: no-repeat;
background-size: 80% auto;
background-position: center center;
}
<a href="http://www.domain.com/bounty.html" class="overflowing-img">
<span class="borderlist">
<img src="http://pngimg.com/upload/gift_PNG5950.png" alt="BOUNTY" title=" BOUNTY " width="225" height="155">
</span>
<br>
<span class="undrline">BOUNTY<span>
</a>
I used a different image, but is this what you are attempting to achieve?
.borderlist img {
width: 200;
height: 100;
}
.borderlist {
width: 225px;
height: 125px;
text-align: center;
vertical-align: middle;
background: linear-gradient(to bottom, #c8c8c8, #ffffff);
margin: auto;
}
.whiteBorder {
width: 255px;
height: 155px;
background-color: #ffffff;
text-align: center;
}
<div class="whiteBorder">
<div class="borderlist">
<a href="http://www.domain.com/bounty.html">
<img src="http://en.wikipedia.org/static/images/project-logos/enwiki.png" alt="BOUNTY" title="BOUNTY">
<br>BOUNTY</a>
</div>
</div>
Try doing using :before in your css to overlay the image on top of a div.
div{
width: 200px; height: 200px;
background-color: lightgray;
box-sizing: border-box;
border: 20px solid white;
position: relative;
}
div:before{
content: "";
display: block;
margin: -20px;
width: 200px;
height: 200px;
background-image: url('https://i.stack.imgur.com/eLzG5.png');
background-size: contain;
background-repeat: no-repeat;
}
<div></div>
Lets go very simple first:
You can't move the image to left by changing css though
.borderlist {
background: grey;
border: 60px solid white;
box-sizing: border-box;
width: 260px;
}
<div class="borderlist">
<img src="https://s9.postimg.org/d0odjmlcv/dwa.png" height="100px" width="150px" />
</div>
You can do this too, create a container div, inside, create the grey div, then float the image above the grey div, like this (I think this is the best):
.borderlist {
padding: 5%;
background: white;
width: 160px;
height: 120px;
position: relative;
}
.grey {
position: absolute;
background: grey;
width: 130px;
height: 90px;
margin: 10px;
}
.float {
position: absolute;
}
<div class="borderlist">
<div class="grey">
</div>
<img class="float" src="https://s9.postimg.org/d0odjmlcv/dwa.png" height="100px" width="150px" />
</div>
Feel free to change and play around to understand it properly
If you're OK with using a mask (white colored background covering the edges), you could use multiple backgrounds coupled with a background color. (credit to user Dave Cripps for the demo image that I shamelessly stole from his demo for mine.)
a {
text-align:center;
display:inline-block;
}
.borderlist {
display:inline-block;
text-align:center;
vertical-align:middle;
background:
linear-gradient(to right, white 15%, transparent 15%, transparent 85%, white 85%), linear-gradient(to bottom, white 15%, transparent 15%, transparent 85%, white 85%);
background-color: #eee;
transition: background-color 0.4s;
}
a:hover .borderlist {
background-color: #5C5;
}
.borderlist img {
height:auto;
}
<span class="borderlist"><img src="http://en.wikipedia.org/static/images/project-logos/enwiki.png " alt="BOUNTY" title=" BOUNTY " width="225" height="155"></span><br>BOUNTY

Remove border around image

guys. I style little form with submit button.
It looks like:
As you can see there is some white background around submit image and I don't have idea why! Image is cut fine and I always cut image with transparent background.
my HTML:
<form action="#">
<textarea rows="4" cols="50"> </textarea>
<input type="submit" class="join-us" value="PŘIDEJ SE K NÁM">
</form>
CSS:
.join-us{
background-image: url("images/join_us.png");
background-repeat: no-repeat;
width:181px;
height: 114px;
line-height: 114px;
color: #f7f7f7;
vertical-align: top;
margin-top: 10px;
margin-left: 10px;
cursor:pointer;
white-space: nowarp;
}
Live website can be find on http://funedit.com/andurit/new
Can you help me to remove that white backgroun from there?
It's not a white background, it's the input element's border. Just remove it using CSS by adding the following rule to the .join-us class:
border: none;
It seems that you also need to adjust the height of the button to 106px, so your final class definition will look like this:
.join-us{
background-image: url("images/join_us.png");
background-repeat: no-repeat;
width:181px;
height: 106px;
line-height: 106px;
color: #f7f7f7;
vertical-align: top;
margin-top: 10px;
margin-left: 10px;
cursor:pointer;
white-space: nowrap;
border: none;
}
Setting
border: none;
is an important part to remove the standard <button>-style. However, in your case it is not quite enough: You also have to set
height: 106px;
Since your image is only that high.
It's not a Background of input.
You can easily remove this white border, by setting the CSS property border:none;,
And the bottom white background is due to your Image. It's because your image have some transparent area at the bottom.
If you want to remove it, you can try to set height: 106px; into CSS class .join-us.
After doing this your Input look like this : -
As buttons have there default border style, you need to overwrite that:
border:none;
so ur css should be:
.join-us{
background-image: url("images/join_us.png");
background-repeat: no-repeat;
width:181px;
height: 114px;
line-height: 114px;
color: #f7f7f7;
vertical-align: top;
margin-top: 10px;
margin-left: 10px;
cursor:pointer;
white-space: nowarp;
.join-us{
background-image: url("images/join_us.png");
background-repeat: no-repeat;
width:181px;
height: 114px;
line-height: 114px;
color: #f7f7f7;
vertical-align: top;
margin-top: 10px;
margin-left: 10px;
cursor:pointer;
white-space: nowarp;
}
Just add the following CSS to your .join-us class
border:none;
.join-us {
height:106px;
border:none;
}
Add both of these styles to your .join-us class.
border: none;
background-size: 100% 108%;
The border style will remove the small while line around your image. The background-size style will stretch out the image to fit perfectly into your class background with no white space.
You need to remove border: 2px outset buttonface; from your stylesheet.
Your background image height and input height are not equals. So as you see under image you have a void space that shows background-color.
So yaou have to set background-color equals transparent or change input height.
Use this:
background-color: rgba(0, 0, 0, 0);
background-image: url("images/join_us.png");
background-repeat: no-repeat;
border: 0 none;
color: #F7F7F7;
cursor: pointer;
height: 114px;
line-height: 114px;
margin-left: 10px;
margin-top: 10px;
vertical-align: top;
width: 181px;