Positioning an image inside a text input box - css

I wanted to put an image inside a text input box and tried to position the image by nesting the image tag inside the input tag and using relative position for the input and absolute positioning for the image and setting the images 'right' and 'top' value to 0. I thought this would place the image on the right hand side of the text box. Instead it placed the image on the right hand side of the webpage. I'm unsure why this is, I found a solution by changing the 'top' and 'right' values to position it where I wanted but it just seemed like the first way should have worked, could anyone explain why it didn't?
This is the HTML for the text input and image.
<input = "text" id="searchbar">
<img src ="microphone.png" id="voicebutton"/>
</input>
This is the CSS I though would work.
#searchbar{
border: 0.6px solid #dbdbdb;
display:block;
margin-left:auto;
margin-right:auto;
width:600px;
height:37.5px;
position:relative;
}
#voicebutton{
width:16px;
height:23px;
position:absolute;
right:0;
top:0;
}
This is the CSS that worked.
#searchbar{
border: 0.6px solid #dbdbdb;
display:block;
margin-left:auto;
margin-right:auto;
width:600px;
height:37.5px;
position:relative;
}
#voicebutton{
width:16px;
height:23px;
position:absolute;
right:395;
top:207;
}

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">
2- Use a negative margin to place the image over it (you can set the image to be a link). Example:
input[type=text] {
box-sizing: border-box;
padding-left: 10px;
font-size: 40px;
width:85%;
height:55px;
border: 2px solid black;
background-color: honeydew;
vertical-align: top;
}
input[type=text]:focus {
background-color: skyblue;
outline: none;
}
img {
margin-top: 3px;
margin-left: -55px;
}
<input type="text" placeholder="write here" id="searchbar"><image src="https://cdn1.iconfinder.com/data/icons/DarkGlass_Reworked/128x128/actions/emoticon.png" alt=img style="width:50px; height:50px;">
3- Let both inputbox and image inside a container; set the container position: relative and the image position: absolute (you can set the image to be a link). Example:
input[type=text] {
box-sizing: border-box;
padding-left: 10px;
font-size: 40px;
width:100%;
height:55px;
border: 2px solid black;
background-color: MintCream;
vertical-align: top;
}
input[type=text]:focus {
background-color: LightGreen;
outline: none;
}
img {
position: absolute;
top: 3px;
right: 5px;
}
#container {
position: relative;
width:85%;
}
<div id="container">
<input type="text" placeholder="write here" id="searchbar">
<image src="https://cdn1.iconfinder.com/data/icons/DarkGlass_Reworked/128x128/actions/emoticon.png" alt=img style="width:50px; height:50px;">
</div>

First of all, you can't have decimals in your pixels, like height: 37.5px;. This won't work. Also right: 395; does not work, because it doesn't specify what usage: pixels, ems, percentage? The input="text" is incorrect, it should be input type="text" as this can be e.g. email or radio.
That said, to achieve what you want, you can add a wrapper around your input field (like .box-wrapper) and give it a relative positioning with the same size as the input field. This will conclude to the example below.
.box-wrapper {
position: relative;
width: 600px;
height: 38px;
}
#searchbar{
border: 1px solid #dbdbdb;
display: block;
margin-left: auto;
margin-right: auto;
width: 600px;
height: 38px;
position: relative;
}
#voicebutton{
width: 16px;
height: 23px;
position: absolute;
top: 0;
right: 0;
}
<div class="box-wrapper">
<input type="text" id="searchbar">
<img src ="http://dummyimage.com/50x50/ffff00/fff" id="voicebutton"/>
</div>

The input tag has no closing tag in HTML, you should remove the closing tag from your HTML code, additionally you can position the element over the input box changing your CSS to something like this (your CSS was almost right, the problem is the HTML) :
#voicebutton{
width:16px;
height:23px;
position:absolute;
right:16px;
top:16px;
}

In HTML, the input tag has no end tag (as opposed to XHTML), so your a tag is outside of your input tag.

Related

CSS - Create a progression indicator

I would like to display a specific CSS presentation that I'm trying to achieve : a completion bar indicator with a two-tone font coloring.
The goal is to display something like this : http://jsfiddle.net/ddz86cr3/
But this one is ajusted by pixels borders.
I used the question Two-tone font coloring in CSS? to create something approaching :
HTML
<div>
<span id="span1">15%</span>
<span id="span2">15%</span>
</div>
CSS
div {
position: relative;
color: green;
font-size: 50px;
font-family: Georgia;
width: 500px;
border: 1px solid green;
}
div span#span1 {
display: inline-block
height: 100%;
width: 100%;
position: absolute;
color: green;
background-color: white;
overflow: hidden;
text-align: center;
}
div span#span2 {
display: inline-block
height: 100%;
width: 15%;
border-left: 200px solid green;
position: absolute;
color: white;
background-color: green;
overflow: hidden;
}
See example : http://jsfiddle.net/va3whf86/
This one works great and is very close to what I want, except it's not center.
SOLUTION
I used modified version of the solution from Midas in the question Is there any way to change the color of text "halfway" through a character on a webpage?
My version is without javascript and with real colors.
Here is the code : http://jsfiddle.net/ytt2r2sa/
HTML
<span class="progressbar">
<span>50%</span>
<strong style="width: 50%;">
<em>50%</em>
</strong>
</span>
CSS
.progressbar, .progressbar strong {
display:block;
height:1.2em
}
.progressbar, .progressbar em {
width:10em
}
.progressbar strong, .progressbar em {
position:absolute;
top:0;
left:0
}
.progressbar {
color:green;
background:window;
border:1px solid green;
text-align:center;
position:relative
}
.progressbar strong {
background:green;
width:0;
font-weight:normal;
overflow:hidden
}
.progressbar em {
color:white;
font-style:normal
}
One way is to create a DIV and place the % inside the DIV.
http://jsfiddle.net/va3whf86/9/
<div>
<span id="span1"> </span>
<span id="span2"> </span>
<div id="text1">15%</div>
</div>
And add this CSS:
#text1 {
width: 100%;
text-align: center;
display: inline-block;
position: absolute;
}

Cannot control size/positon of Div within a Div

I'm working on this project for an instagram search. I get the results I want, and I want the main images to display with the small thumbnail of the user profile underneath the main photo, along with user name,etc. Right now im working on just getting the main image with a small thumbnail of the user under the photo. Right now all i get is the main photo and profile picture taking up the entire width of the div. I can't get it to move and the chrome developer tool shows thats completely ignoring my css for these sub-divs.
Here is the CSS snippet:
html {}
body { font-family: Calibri, Candara, Segoe, "Segoe UI", Optima, Arial, sans-serif; }
small { display: block; color: #555; padding: 10px 0px; }
#searchresults { width: 960px; }
#sform { width: 710px; margin: 0 auto; margin-top: 25px; margin-bottom: 35px; }
#sform #s {
padding: 10px 11px;
padding-left: 60px;
color: #999;
width: 200px;
border: 1px solid #ddd;
font-size: 22px;
.
.
.
.
#photos { margin-left: 100px; text-align: center; }
#box { width:180px;
height:260px;
display:inline-block;
margin-left:20px;
margin-bottom:30px; }
#box .mainimg { width: 160px;
height 230px; }
#box .footer { width: 180px;
height: 30 px; }
and the HTML:
<body>
<div id="searchresults">
<section id="sform"><input autocomplete="off" class="sfield" id="s" name="s" placeholder="Enter a search tag..." type="text" /></section>
<section id="photos"></section>
</div>
</body>
and the code that #each passes
<div class="box"><div class="box.mainimg"><img src="'+data[i].thumb+'"></div><div class="box.footer"><img src="'+data[i].avatar+'"></div></div>'
Thanks in advance. I can't seem to find this anywhere.
since you've changed it #box img {} - also in the html you can just put class="mainimg" and because that div is within the box div you can call it with the #box .mainimg css selector
i re-read this and it worked. css is picky!!
In your html you've assigned your box div a class, not an id - in the css your selectors point to a box id.
So
.box { width:180px;
height:260px;
display:inline-block;
margin-left:20px;
margin-bottom:30px; }
.box .mainimg { width: 160px;
height 230px; }
.box .footer { width: 180px;
height: 30 px; }
Should fix it - or change the div to id="box"

CSS layout - what is causing additional space

I have created the following jsFiddle to demonstrate my problem (or lack of understanding more like)
http://jsfiddle.net/gRCS6/
And Code here
<div id="scoreboard"></div>
<canvas id="game">
Your browser does not support canvas.
</canvas>
<div id="controls">
<button type="submit" id="newGame">New</button>
<button type="submit" id="pause">Pause</button>
<button type="submit" id="help">Help</button>
</div>
#game {
border: 1px solid #000000;
background-color:#333333;
width: 250px;
margin:0px;
}
#scoreboard {
border: 1px solid #000000;
background-color:#333333;
color: orange;
width: 250px;
height: 40px;
font:36px arial,sans-serif;
text-align: right;;
}
#controls {
margin-top: -5px;
padding:0px;
}
button {
border: 1px solid #000000;
margin-left:0px;
background-color:#333333;
color: orange;
width:82px;
height: 40px;
}
Why does the div with id "controls" need a margin-top of -5px to make it touch the canvas above it?
What is taking up that 5 pixels?
What is stopping the 3 buttons from being next to each other with no space between them?
"Why does the div with id "controls" need a margin-top of -5px to make it touch the canvas above it?"
Like ralph.m pointed out, can be fixed by adding
canvas {
display: block;
}
"What is stopping the 3 buttons from being next to each other with no space between them?"
Well, since there are spaces (the character ' ') between the button elements in the html code you will see those spaces between the buttons when the page is displayed. You can either remove the spaces:
<button type="submit" id="newGame">New</button><button type="submit" id="pause">Pause</button><button type="submit" id="help">Help</button>
Instead of
<button type="submit" id="newGame">New</button>
<button type="submit" id="pause">Pause</button>
<button type="submit" id="help">Help</button>
Or you can try to fix it with css styling, for example by adding float: left; to the button selector.
The canvas element is display: inline (or is it inline-block?) by default, which means by default there is a gap at the bottom so that it will align with the baseline of any text beside it.
You can change this by setting the canvas to display: block or vertical-align: bottom.
It's a similar problem with the buttons, which are display: inline-block, meaning that there is space between them (as there is a natural space between words). As mentioned in the chosen answer, removing the white space is an option, but a more elegant solution is as follows:
#controls {word-spacing: -2em; display: table; width: 100%;}
button {word-spacing:0;}
Answer to Q1: Check this topic. Different browsers have different algorythm, so you should some extra parameters for body css.
body
{
margin: 0;
padding: 0;
}
Answer to Q2: Avoid using to close button tab. is not necessary, if you remove it, the margin between buttons will disappear. http://jsfiddle.net/gRCS6/5/
<button type="submit" class="button">New
<button type="submit" class="button">Pause
<button type="submit" class="button">Help
Another way to fix the issue is to use absolute positioning to define the exact placement of your controls div. Then you have to ability to define the exact alignment of the buttons regardless of display: inline-block; or display: block; commands.
http://jsfiddle.net/gRCS6/34/
#game {
border: 1px solid #000000;
background-color:#333333;
width: 250px;
height: 120px;
margin:0px;
position: absolute;
}
#scoreboard {
border: 1px solid #000000;
background-color:#333333;
color: orange;
width: 250px;
height: 40px;
font:36px arial,sans-serif;
text-align: right;
}
#controls {
position: absolute;
top: 172px;
margin-top: 0px;
padding: 0px;
}
button {
border: 1px solid #000000;
margin:0px;
background-color:#333333;
color: orange;
width:81.5px;
height: 40px;
}

positioning a div inside another div?

In a big div I have search box which basically is a div having text box glass image and button. Requirement is to positioned this search wizard vertically in middle and on right side in div. This box is coming on top left inside div. I have tried different things but not getting how to set it. Please guide me.
Thanks
<div class="Right">
<div class="header-search" style="position: relative; top: auto; bottom: auto; right: 0 left:100%;
margin: auto 0 auto auto;">
<input type="text" class="searchbox" />
<input type="button" class="searchbutton" value="›" />
</div>
</div>
div.Container div.Right
{
width:50%;
float:right ;
border: 01px dashed green;
height:95px !important;
padding:auto 0 auto 200px;
}
div.header-search
{
overflow:auto;
display:inline;
text-align:right !important;
border:3px dashed blue;
padding:20px 0 auto 50px;
}
div.header-search input.searchbox
{
border-bottom-left-radius:5px;
-webkit-border-bottom-left-radius:5px;
-moz-border-bottom-left-radius:5px;
border-top-left-radius:5px;
-webkit-top-left-radius:5px;
-moz-left-radius:5px;
border:2px solid #316200;
background-color:white;
height:16px;
padding:4px;
padding-left:28px;
padding-right:10px;
color:#4a4a4a;
float:left;
background:white url(../images/SearchImage.png) 0 50% no-repeat;
}
div.header-search input.searchbutton
{
border-bottom-right-radius:5px;
-webkit-border-bottom-right-radius:5px;
-moz-border-bottom-right-radius:5px;
border-top-right-radius:5px;
-webkit-top-right-radius:5px;
-moz-right-radius:5px;
background:#458A00;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#A5D376', endColorstr='#326400'); /* for IE */
background: -webkit-gradient(linear, left top, left bottom, from(#A5D376), to(#326400)); /* for webkit browsers */
background: -moz-linear-gradient(top, #A5D376, #326400); /* for firefox 3.6+ */
width:50px;
height:28px;
color:White;
font-size:16px;
font-weight:bold ;
border:2px solid #316200;
border-left:none;
}
The first step in understanding how positioned elements is reading an article like this one:
CSS-Tricks.com - absolute positioning inside relative positioning
you are using position: relative on the wrong div as it should be assigned to .Right- while header-search should have instead 'position: absolute;' and values for left/right and top/bottom
the article above explains it much better than I could ever do!
Perhaps this would be a good starting point:
<div class="Right">
<div class="header-search">
<input type="text" class="searchbox" />
<input type="button" class="searchbutton" value="›" />
</div>
</div>
div.Container div.Right {
position: relative;
width: 50%;
float: right;
border: 1px dashed green;
height: 95px !important;
padding: auto 0 auto 200px;
}
div.header-search {
position: absolute;
right: 0;
top: 0;
overflow: auto;
display: inline;
text-align: right !important;
border: 3px dashed blue;
padding: 20px 0 auto 50px;
}
remove all styling from your div's as this is bad practice. Next, convert your two styles for .Right and .header-search like this:
div.Right {
border: 1px dashed green;
height:95px;
position: relative;
}
div.header-search {
border:1px dashed blue;
position: absolute;
top: 30px;
right: 0;
}
This should accomplish what you are attempting. There isn't a clean, easy way to center vertically, but since you have a fixed height on the outter .Right div and a fixed height on the search elements, it's best just to use a fixed top position on the inner .header-search.
You can see it in action on this jsfiddle:
http://jsfiddle.net/L4sgc/

Centering two divs in a div: one of fixed width and the other of variable width/height

I have a situation where I have one div of fixed width, containing an image pulled from Twitter, and another div of variable width containing user text of variable length. What I want to achieve is something like the following:
I can do this well enough with a single div that has background-image and padding-left. But I want to be able to apply border-radius to the img element, which simply won't be possible with a background-image.
If I do text-align: center on the outer div, it gets me halfway there. Here's a DEMO and a screenshot:
But this obviously isn't fully what I want.
How can I accomplish this?
Ask and you shall receive — a simplified jsFiddle example:
As an added bonus, the text is vertically centered too!
HTML:
<div class="logo">
<div class="logo-container">
<img src="http://img.tweetimag.es/i/appsumo_b.png" />
</div>
<div class="logo-name">
AppSumo is a really really long title that continues down the page
</div>
</div>
CSS:
.logo {
background-color: #eee;
display: table-cell;
height: 100px;
position: relative;
text-align: center;
vertical-align: middle;
width: 600px;
}
.logo-container {
background-color: #fff;
border-radius: 10px;
left: 10px;
position: absolute;
top: 10px;
width: 75px;
}
.logo-name {
font: bold 28px/115% Helvetica, Arial, sans-serif;
padding-left: 85px;
}
Would it be something like this?
http://jsfiddle.net/uPPTM/6/
.logo {
width:80%;
margin:auto;
background-color: red;
}
.logo-container {
border: 1px solid gold;
width:73px;
height: 73px;
display: inline-block;
vertical-align:middle;
}
.logo-name {
display: inline-block;
}
You can float the image container (or image itself without the container) to the left, clearing anything the left... and then float the text to the left, clearing anything to the right.
.logo-container{
float:left;
clear:left;
}
.logo-name{
float:left;
clear:right;
}
You can adjust the distance of the text using margins.
.logo-name{
float:left;
clear:right;
margin-top:10px;
margin-left:5px;
}
Use absolute positioning with a left position to push the title text past the image.
http://jsfiddle.net/uPPTM/9/
.logo { width: 50px; }
.title {
position: absolute;
top: 0;
left: 50px;
font-size: 32px;
text-align: center;
}
img {
border: 1px solid gray;
border-radius: 15px;
}
<div class="logo">
<div class="logo-container">
<img src="http://img.tweetimag.es/i/appsumo_b.png">
</div>
<div class="logo-name">AppSumo</div>
</div>

Resources