I need to create an input text box with a bottom border and the side borders should span half the height of the input on left and right.
Is there an easy way to design this in CSS?
Image is show below:
Maybe this could be an elegant solution.
If you use background then you can specify really nicely where what goes and it improves readability a bit.
input[type="text"] {
padding: 10px;
background: linear-gradient(#000, #000), linear-gradient(#000, #000), linear-gradient(#000, #000);
background-size: 1px 20%, 100% 1px, 1px 20%;
background-position: bottom left, bottom center, bottom right;
background-repeat: no-repeat;
border: none;
color: #999;
}
<input type="text" />
With 2 box-shadows, you can achieve the bottom border and small borders on both sides. Here is an example:
input[type=text] {
width:300px; height:17px;
border: 0; outline:none;
padding:0;
box-shadow: -5px 5px 0px -4px #000, 5px 5px 0px -4px #000;
text-align:center;
}
<input placeholder="Email" type="text" value=""/>
The spread radius and the X/Y offset of the box-shadows need to be tweaked according to the height of the input and the desired height of left/right borders. As you can see in this example with a different height on the input:
input {
width:300px; height:40px;
padding:0;
border: 0; outline:none;
box-shadow: -18px 18px 0px -17px #000, 18px 18px 0px -17px #000;
text-align:center;
}
<input placeholder="Email" type="text" />
Browser support for box-shadows is IE9+.
I would go for a slightly different approach to web-tiki's answer, and use a wrapper div and a pseudo element, as this would not require a fixed height input (but would require this extra element):
input {
border: none;
}
div {
display: inline-block;
position: relative;
margin-bottom: 30px;
}
div:before {
content: "";
position: absolute;
bottom: 0;
left: -2px;
height: 50%;
width: 100%;
border: 2px solid black;
border-top: none;
}
textarea {
display: block;
height: 100px;
/*border:none*/
/*This was commented to show where text area is*/
}
<div>
<input type="text" placeholder="Please Enter your PIN" />
</div>
<br/>
<div>
<textarea placeholder="Please Enter your bank details and mother's maiden name;)"></textarea>
</div>
A linear-gradient based answer already exists in this thread but it is a bit complex and makes use of multiple linear gradients to achieve the border but it can be done with just one linear-gradient.
Using border-bottom on the element avoids the need for one of those linear gradients. Using a linear gradient which is colored for 2px (2 x required border width), repeats in the X-axis and has a negative offset for background-position in X-axis can produce border on left and right using only one gradient instead of two. The negative offset is equal to border's width (1px). It means that 1px of the gradient is visible on left side and since the gradient repeats in X-axis (and its size is only 100%), the 1px border on the right side is produced by the second tile of the gradient.
A few advantages of this approach are:
It doesn't need any extra real or pseudo-elements. Since input elements are self-closing and so cannot have pseudo-elements, this implies that no extra wrapper elements are required.
The border produced is responsive and can adapt itself even if the input element's dimensions change.
One drawback is that linear-gradient background images are supported only from IE10+.
input[type="text"] {
padding: 2px;
background-image: linear-gradient(to right, black 2px, transparent 2px);
background-repeat: repeat-x;
background-size: 100% 50%;
background-position: -1px bottom;
border: none;
border-bottom: 1px solid black;
color: #999;
text-align: center;
}
input[type="text"].padding {
padding: 4px 10px;
}
.narrow {
width: 100px;
}
.wide {
width: 250px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<input type="text" placeholder="Name" />
<input type="text" placeholder="Address" class="wide" />
<input type="text" placeholder="City" class="narrow" />
<input type="text" placeholder="Email" class="padding" />
You can use pseudo elements :
input {
position: relative;
width: 200px;
height: 18px;
border: none;
outline: none;
}
::-webkit-input-placeholder {
text-align: center;
text-transform: uppercase;
}
:-moz-placeholder {
/* Firefox 18- */
text-align: center;
text-transform: uppercase;
}
::-moz-placeholder {
/* Firefox 19+ */
text-align: center;
text-transform: uppercase;
}
:-ms-input-placeholder {
text-align: center;
text-transform: uppercase;
}
div {
position: relative;
}
div::before {
content: '';
display: block;
position: absolute;
left: -1px;
bottom: -1px;
width: 204px;
height: 9px;
background-color: red;
}
<div>
<input type="text" placeholder="email" />
</div>
Related
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
I need to underline text in input that will display value of expression (I'm making a calculator).
If I use text-decoration: underline the line is too thick and too close to the text.
I need to make line like this. Line should underline only text, not a whole input.
I use fixed width: 200px for input (max value can be five-digit of six-digit). If I don't set width input takes 100% of available place. Thus we can't use border-bottom because line have width like input, but text have line about 70% of input width and we'll get something like this .
If it possible, we shouldn't use JavaScript, only CSS.
JSFiddle
.calc__total-cost {
font-size: 56px;
font-weight: 700;
width: 200px;
height: 60px;
background: transparent;
color: #efae02;
text-align: right;
text-decoration: underline;
}
<div class="calc__total">
<input type="text" class="calc__total-cost" id="calc__total-cost-value" value="5000">
</div>
You can use something like this:
.calc__total-cost{
border: none;
}
.calc__total-cost:active, .calc__total-cost:focus{
border: none;
outline: none;
}
.calc__total{
position: relative;
}
.calc__total:after{
position: absolute;
width: 35px;
height: 1px;
background: #333;
content: '';
left:0;
bottom: 0;
}
<div class="calc__total">
<input type="text" class="calc__total-cost" id="calc__total-cost-value" value="5000">
</div>
You could play with height, borders and outline: you could partially overlap the underline created with text-decoration using a white bottom border. Then you could restore the border using the outline property, e.g.
input {
width: 300px;
height: 4.1rem;
text-align: right;
padding: 0 10px;
text-decoration: underline;
font: 5rem Arial;
color: gold;
border: 0;
border-top: 10px #fff solid;
border-bottom: 10px #fff solid;
outline: 1px #d8d8d8 solid;
}
<input type="text" value="5000"/>
As the final result only the number is underlined with a light line (play with bottom border to adjust the tickness)
If i style input then the height affects a text input different then a button.
Why is this? And what can be done about it?
(and as a extra, why don't they align?)
<input type="text" value="foobar"/>
<input type="button" value="foobar"/>
css:
body {
background: red;
}
input {
border: 0;
height: 20px;
padding: 10px;
}
http://jsfiddle.net/clankill3r/sxbzav34/
Some of html elements such as input of type button has predefined styles in browsers. Among others, this results into input of type button to have box-sizing: border-box. Enforce this to be box-sizing: content-box, so your css will look like:
body {
background: red;
}
input {
border: 0;
height: 20px;
padding: 10px;
box-sizing: content-box;
}
One solution is to set line-height instead of height:
body {
background: red;
}
input {
border: 0;
line-height: 50px;
padding: 0 10px 0 10px;
height: 50px;
}
<input type="text" value="foobar"/>
<input type="button" value="foobar"/>
Fix: After testing on other browsers discover an issue. You have to use specific padding e.g. padding: 0 10px;(top and bottom have to be 0)
The size differs because the vertical padding is not added to the size of the button, but it is added to the size of the textbox. You can use height and line-height to set the height and vertical position of the text.
The alignment is off because they have different vertical alignment by default. If you specify a vertical alignment they will align themselves equally.
input {
vertical-align: bottom;
border: 0;
height: 40px;
line-height: 40px;
padding: 0 10px;
}
Demo: http://jsfiddle.net/Guffa/kx7wzufr/
Because of default styling of browsers. Give them different classes and style them.
You can assign two different classes to your text input field and the input button and then style them accordingly, please check this: DEMO.
HTML:
<input class="text" type="text" value="foobar" />
<input class="button" type="button" value="foobar" />
CSS:
body {
background: red;
}
.text {
border: 0;
height: 20px;
padding: 10px;
}
.button {
border: 0px;
padding: 12px 12px 13px 12px;
}
Each browser gives inputs special styling. If you take a look at Chrome developer tools and scroll down a little bit you'll see something along the lines of below. You have to overwrite those styles by adding a class or using input[type="button"]:
input[type="button"], input[type="submit"], input[type="reset"], input[type="file"]::-webkit-file-upload-button, button {
align-items: flex-start;
text-align: center;
cursor: default;
color: buttontext;
padding: 2px 6px 3px;
border: 2px outset buttonface;
border-image-source: initial;
border-image-slice: initial;
border-image-width: initial;
border-image-outset: initial;
border-image-repeat: initial;
background-color: buttonface;
box-sizing: border-box;
}
I am facing an issue with IE browser (all versions till IE 10). Issue
Text in input box is overlapping its background image. I cant put this image in parent div as its a dynamic input box and having lot of other functionality. and i cant increase the width of textbox than 100px and value is about 50 characters.
pls help.
CSS
.some {
background:url(http://cmsresources.windowsphone.com/windowsphone/en-us/How-to/wp8/inline/hardware-icon-search-button.png) no-repeat right center;
padding:1px 15px 1px 1px;
border:1px solid #ccc;
width:100px;
color:red;
overflow:hidden;}
HTML
<input type="text" class="some" value="I am a messy input box" />
Try this demo with IE8: http://jsfiddle.net/74u4w/2/
If it works, just add the border to the image and you will have a fix
.some {
background-image: url(http://cmsresources.windowsphone.com/windowsphone/en-us/How-to/wp8/inline/hardware-icon-search-button.png);
background-position: 105px -6px;
background-repeat: no-repeat;
padding: 1px 1px 1px 1px;
border: 1px solid #ccc;
border-right: 25px solid transparent;
width: 100px;
color: red;
overflow: hidden;
}
If not, then the last resort is a small javascript that appends an img/div after your input. (which can be done dynamically)
With an absolute position set, it will not affect the flow. This css sample shows how it could look:
.some {
padding: 1px 1px 1px 1px;
border: 1px solid #ccc;
width: 80px; /* narrowed to fit img */
color: red;
overflow: hidden;
}
.someimg {
background-image: url(http://cmsresources.windowsphone.com/windowsphone/en-us/How-to/wp8/inline/hardware-icon-search-button.png);
background-position: center center;
background-repeat: no-repeat;
position: absolute;
overflow: hidden;
left: ?px; /* needs to be calculated based on each input */
top: ?px; /* needs to be calculated based on each input */
width: 20px;
height: 20px;
}
Decrease input field width and add padding from right
DEMO FIDDLE
I have an HTML input field like this. I would like to place an image inside the textbox on the right side. Can someone help me out with its CSS for that?
<input type="text" id="name"/>
In the picture is an image which is inside the text field email address, which is what I want. How do you do this?
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/
try this:
input {
background-image: url("icon.png");
background-position: right top;
background-repeat: no-repeat;
background-size: contain;
}
you can try something like this
.textBox{
background-image:url(iconimage.jpg);
background-position:right;
background-repeat:no-repeat;
padding-left:17px;
}
Then apply this style to your text box:
<input type="text" class="textBox" />
Use background-image and background-position property
DEMO
CSS:
input {
height: 70px;
width: 200px;
background-image:url("http://cloud.ohloh.net/attachments/25869/plone-icon-64_med.png");
background-repeat:no-repeat;
background-position: 133px 3px;
}
.text3 {
width: 300px;
height: 30px;
}
#text3 {
background-image: url(https://cdn3.iconfinder.com/data/icons/interface-8/128/InterfaceExpendet-09-128.png);
background-size: 30px;
background-repeat: no-repeat;
}
input {
text-align: center;
}
<p class="email">
Email
</p>
<input type="text" placeholder="Email_ID" class="text3" id="text3">
The answers above didn't replicate the format shown in the questioners image, or provide any explanation. This is a solution using background images.
Explanation of the background image syntax.
background: url('https://d1ululg65bfe3q.cloudfront.net/images/troy/email-icon.png') no-repeat 97.25% 10px white;
Location of image: url('https://d1ululg65bfe3q.cloudfront.net/images/troy/email-icon.png')
Show image once: no-repeat
Distance from left: 97.25%
Distance from top: 10px
Background color: white
.bg-email-icon {
background: url('https://d1ululg65bfe3q.cloudfront.net/images/troy/email-icon.png') no-repeat 97.25% 10px white;
padding-left: 5px;
padding-right: 50px;
width: 255px;
height: 30px;
border: 1px gray solid;
border-radius: 5px 5px 0px 0px;
}
.bg-lock-icon {
background: url('https://d1ululg65bfe3q.cloudfront.net/images/troy/lock-icon.png') no-repeat 96.75% 10px white;
padding-left: 5px;
padding-right: 50px;
width: 255px;
height: 30px;
border: 1px gray solid;
border-top-width: 0px;
border-radius: 0px 0px 5px 5px;
}
<input name="exampleEmail" class="bg-email-icon" placeholder="Email" type="email">
<input name="examplePassword" class="bg-lock-icon" placeholder="Password" type="password">
You can add class to your input
<input type="text" id="name" class="inputImage" />
and then background image to class in css
.inputImage
{
background:#FFFFFF url('img.jpg') no-repeat top right;
}
You need to add class like this:
CSS:
.bgInput {
background: url(path of your image.jpg) top right no-repeat;
}
HTML:
<input type="text" class="bgInput" id="name"/>
A small answer: you can do this by setting as a background image of input field.
try:
#name {
background: url('path/image.png') right no-repeat;
}