Adding a Small Picture on the Right Side of Textfield with CSS

Adding a small picture on the right side of textField with CSS

As Marek points out in his answer you have the css attribute id wrong, you need to use -fx-background-position: right center;

Here is a short example which demonstrates adding a picture to the right side of a TextField using CSS:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TextFieldCssSample extends Application {
@Override public void start(Stage stage) throws Exception {
TextField textField = new TextField();
textField.setId("textField");
StackPane layout = new StackPane();
layout.getChildren().addAll(textField);
layout.getStylesheets().add(this.getClass().getResource("textfield.css").toExternalForm());
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) { launch(args); }
}

Css File:

/* textfield.css 
place in same location as TextFieldCssSample.java and ensure build system copies it to output directory
image used courtesy of creative commons attribution license: http://www.rockettheme.com/blog/design/1658-free-halloween-icon-pack1
*/
.root {
-fx-padding: 15;
-fx-background-color: cornsilk;
}

#textField {
-fx-background-image:url('http://icons.iconarchive.com/icons/rockettheme/halloween/32/pumpkin-icon.png');
-fx-background-repeat: no-repeat;
-fx-background-position: right center;
-fx-font-size: 20;
}

Sample output:

text field background position center right

If my png image is a local file in my jar file, how will I access or refer to it?

According to the uri section of the css reference:

"address can be an absolute URI . . . or relative to the location of the CSS file."

For example

  • a) Put the css and image files in the same location in the jar file and reference with just url('pumpkin-icon.png'); OR
  • b) Put the image files in an images directory underneath the directory holding the css, and reference like url('images/pumpkin-icon.png'); OR
  • c) Put the image files in an images directory at the root of your jar, and reference like url('/images/pumpkin-icon.png');

Do not use a relative reference which uses a .. parent specifier e.g. ../images/pumpkin-icon.png as, although it works for a disk file, the .. specifier is not a valid jar protocol path and will not extract a file from a jar.

Making a Input Text Field Be On The Right Side Of a Image

Position absolute gives you more control:

HTML

<div name="image_input">
<img src="missing-image.png" />
<input type="text" />
</div>

CSS

div {
position:relative;
}

input{
position:absolute;
right:10px;
bottom:20px;
}

How to add an image toward the right of a textbox and the text not overflowing to it

Here's what we're doing

Some slight changes to HTML

<div class='styledTB'>
<input type="textbox" id="gpsInput" />
<button id="searchBDir"></button>
</div>

What we're doing here is giving some layout context for your input and button. The container is going to serve as our bounding box for laying out the remainder of our elements. You could accomplish this visually with only the input, but since you've got a clickable element it makes sense to make the clickable area a button (for accessibility, semantics, blah, blah, blah...).

On to the CSS:

.styledTB {
position: relative;
display: inline-block;
height: 40px; /* Arbitrary number */
width: 400px; /* Arbitrary number */
}
.styledTB input {
width: 100%; /* Arbitrary number */
height: 100%;
padding-right: 40px;
box-sizing: border-box;
}
#searchBDir {
height: 100%;
width: 40px; /* Or however long you'd like your button to be, matches padding-right above */
background-image: url(http://i.stack.imgur.com/4v62r.png);
background-repeat: no-repeat;
background-position: 50% 50%;
border: none;
background-color: transparent;
position: absolute;
right: 0;
top: 0;
}

Read the comments as they are helpful, but as a high-level overview:

We're putting the bounding box (the div) into a relative position so that we can order the button absolutely. We define the size we want our input element to appear (I chose some number arbitrarily, it should work similarly no matter what size you set them to) and set display inline-block to mirror that of a normal input element.

We then force the box to fill its container with the width and height being declared as 100% and give the box a padding-right of 40px to match our intended size for the button. We set the box-sizing to border-box so that the padding doesn't force the input larger than its containing box. This just tells the browser to consider padding, borders and all internal spacing elements to not grow the outer bounds of the element.

Lastly, we set the button to position: absolute and give it the positioning values to force it to the right of the box. We give it a background image of your GPS icon, and then position that appropriately within the button.

The border and background-color rules are there to override the default browser rendering of a button as a raised gray box.

I hope that helps!

Fiddle here

Place an image inside a text field

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 }

Working demo

http://jsfiddle.net/HnJZa/

how to show image in right side of text box instead of left side

In CSS that would be:

background-position:right;

This looks like a usability problem though. If you're going to make a textbook look like a dropdown/combobox, users are going to expect it to behave like a combobox (like, being able to click the arrow and choose something). Are you going to be implementing some kind of JavaScript or clickable feature? If it's going to be a standard textbox I recommend avoiding any confusion with other control/input types.

How to add two images one in the right and other in the left side of the input textbox?

Thanks all for your valuable inputs.
I was able to get the solution by using the following CSS.

.mandatory {
background: #FFFFFF url(http://www.freecakefreecake.com/blog/wp-content/themes/Aggregate/epanel/shortcodes/images/list-dot.png) no-repeat 4px 4px;
background-position: left;
}

.mandatory.datepicker {
background-image: url(http://www.freecakefreecake.com/blog/wp-content/themes/Aggregate/epanel/shortcodes/images/list-dot.png), url(http://upload.wikimedia.org/wikipedia/commons/9/99/Dot1.png);
background-repeat: repeat-y;
background-position: left, right;
}

Fiddle: http://jsfiddle.net/kumareloaded/jcpLp492/

Positioning an image inside a text input box

Three ways to do it:

1- Use the properties background-size and background-position to set your background-image inside the input-box. Example:

input[type=text] {

box-sizing: border-box;

padding-left: 10px;

font-size: 40px;

width:85%;

height:55px;

border: 2px solid black;

background-color: floralwhite;

background-image: url("https://cdn1.iconfinder.com/data/icons/DarkGlass_Reworked/128x128/actions/emoticon.png");

background-size: 50px 50px;

background-repeat: no-repeat;

background-position: 99% 100%;

}

input[type=text]:focus {

background-color: pink;

outline: none;

}
<input type="text" placeholder="write here" id="searchbar">

aligning the image on the right side of text box

try

fieldset input {
float:left;
}

maybe you also need to set the input to inline or inline block if you have set it to block previously.

http://jsbin.com/akiviw/1/edit

or change js

$('#login_id_correct').css('display','block')

to

$('#login_id_correct').css('display','inline-block')

Put icon inside input element in a form

The site you linked uses a combination of CSS tricks to pull this off. First, it uses a background-image for the <input> element. Then, in order to push the cursor over, it uses padding-left.

In other words, they have these two CSS rules:

background: url(images/comment-author.gif) no-repeat scroll 7px 7px;
padding-left:30px;


Related Topics



Leave a reply



Submit