How do I use CSS to reposition this image? - asp.net

In this image you see two versions of the same elements. The top one is what I am getting, the bottom is what I want to end up with. Of course the bottom of the image is lined up with the text in the label and the text in the textbox, but I need it lined up with the textbox's box. I'm somewhat newbie at CSS, and the things I've tried so far do not get me even off the plate.
The controls are coded as follows:
<asp:Label ID="TimeTextRequiredLabel" runat="server" Text="*"></asp:Label>
<asp:Label ID="TimeTextLabel" runat="server" Text="Time: "></asp:Label>
<asp:TextBox ID="TimeTextBox" runat="server" ReadOnly="false" Width="100"></asp:TextBox>
<asp:Image ID="TimePickerImageButton" runat="server" BorderWidth="0"
Width="34" Height="21" CssClass="TimePickerImage"
ImageUrl="~/UserControls/Images/ClockPicker.gif"
ToolTip="Pick a time." />
What should be in my CSS, in the class "TimePickerImage" to scootch it leftwise and down the few pixels necessary?
Edited to Add:
Ultimately went with #JuanMendes solution, and this class:
<style type="text/css">
.TimePickerImage {
position: relative;
top: .2em;
right: .3em;
}
</style>
This snugs it in right exactly where I needed it. Thanks! Next task is to get serious with learning CSS. Thus far I've been playing script-kiddie with it.
Edited Further to Add:
I've tried all the other variations proposed by both #MarcAudet and #JuanMendes and the above code works best. I guess I don't care so much about comforming to some theoretical "ideal" as making the thing result in what I need.

You need to add or adjust the vertical-align: bottom declaration in #TimePickerImageButton
From your screenshots, it appears that the image is aligned with the baseline of the text line, the default behavior for an inline image.
vertical-align will take care of the vertical positioning.
To move the element to the left, try adjusting margin-left. However, check the margin on the input field since it may have a margin, and also look for some white space between the input field and the image.
Demo
If you have the following HTML:
<div class="parent ex2">
<label for="the-time">Time:</label>
<input id="the-time" type="text">
<img src="http://placehold.it/30x25">
</div>
and apply the following CSS:
.ex2 img {
vertical-align: bottom;
}
You will see the positioning that you want.
Fiddle: http://jsfiddle.net/audetwebdesign/2PkUF/
If you have tall lines...
If you apply a larger value for line-height: 4.0, aligning elements to the bottom of the line box may look goofy.
You can also try vertical-align: text-bottom which should work.
See Example 3 in the demo.

You can always cheat and position the image yourself fudging values until it lines up(if you can't find a nicer way) http://jsfiddle.net/vTCHW/
Tested in FF, IE 8/9 and Chrome
img {
position: relative;
top: .3em;
}
You mentioned that Marc's answer is almost good enough. I think that is a better solution. You can make all three line up correctly by removing padding/margin from the input. http://jsfiddle.net/vTCHW/1/ Note that many people use a CSS reset system that would have taken care of the margins/paddings for you
img {
    vertical-align: bottom;
}
input {
padding: 0;
margin: 0;
}

Related

Why does a simple image get a margin-bottom c.q. output space under the image?

Please have a look at this simple jsfiddle. It contains the following code:
<div style="background:yellow; display:inline-block;">
<img src="http://www.wedesoft.de/test/test.png" />
</div>
As you can see, this will output a space under the image so that you can see the yellow colored container. I do not know why, because no space was defined.
Can somebody tell me what is going on please?
An image is an inline element. That means it is treated as text. Text has a line-height. The line-height is what is causing the space at the bottom. There are multiple ways to solve this.
The following are my favorites:
div {
line-height: 0;
}
By setting line-height to 0, the space goes away.
img {
display: block;
}
By making the image a block element, it's no longer considered text, thus, line-height isn't applicable anymore.
As Marc Audet stated in the comments, another way to solve this would be by using vertical-align.
img {
vertical-align: top;
}
It doesn't matter whether you use top or bottom.
This occurs due to the line-height attribute; try this Jsfiddle
Where i just set the line height to 0.
i think you missed line-height try this
<div style="background:yellow;display:inline-block;line-height: 0px;">
<img src="http://www.wedesoft.de/test/test.png">
</div>
other way you can apply style display: block; to img. like
<div style="background:yellow;display:inline-block;">
<img src="http://www.wedesoft.de/test/test.png" style="display: block;">
</div>
For some reason
display:block;
solves this problem as you can see in the accepted answer here:
Remove white space below image
Is still do not know why...
take a look at line-height propety on this web: http://www.w3schools.com/cssref/pr_dim_line-height.asp
basicly what's happening is that there's some default space on the div so that's why you see the yellow line. Adding line-height: 0px; should solve the problem.

Checkbox with label in the middle

I need to figure out how to to put label text next to checkbox button. I need the text in the middle of the checkbox button. I always got the checkbox a little above the text or little below it. Hard to get it exactly in the middle. When I fix in to the middle in one browser, in other browser it doesn't exactly in the middle. There is always a pixel or two that ruins it.
Here's the fiddle
http://jsfiddle.net/JhPHm/
<style>
.field input
{
margin: 0;
padding: 0;
vertical-align: top;
}
.field label
{
margin: 0;
padding: 0;
vertical-align: top;
font: normal 12px/14px arial;
}
</style>
<div style="padding: 30px;">
<div class="field">
<label for="x"><input type="checkbox" id="x" name="x" value="1" /> Text in the middle</label>
</div>
</div>
Please help me get the text in the middle in all browsers. Here a picture with the differences:
http://img708.imageshack.us/img708/5410/checkbox.png
Instead of vertical-align:top, try vertical-align:middle. It may help a bit, but input elements are notorious for being uneven across browsers.
Aligning input elements is tough, and sometimes nearly impossible.
If the problem is that it's misbehaving in old browsers, consider that people browsing the web with these browsers will stumble upon misaligned checkboxes on more places than just yours. They are browsing the web while it's crumbling around them so to speak. They won't care because they either know they're using an old browser or they don't notice because all sites show these tiny glitches.
If you're going to have to add all sorts of tweaks and fixes for various browsers to get to an acceptable end result, also consider that all these tweaks add to the size of your CSS and to the complexity making it tougher to maintain in the future.
In the end for me, this stuff depends on the audience, the budget and obviously the amount of checkboxes. ;-)
The way I've hacked around this is to use relative positioning, like this:
input[type="radio"], input[type="checkbox"] {
cursor: pointer;
line-height: normal;
margin: 0px;
position: relative;
top: -3px;
}
For example, if you want to shift them up 3 pixels. Like you've pointed out above, you might get different results in different browsers.
In this situation, Firebug or Chrome Developer Tools is going to help you a lot. There may be some garbage you're inheriting from elsewhere. Like in the example above, I set the margin back to 0px because something higher up (and unavoidable) in the CSS structure was setting a margin of 4px on all input and screwing me.
Good luck!

Aligning the bottom of an inline block with the bottom of text (excluding descenders)

How can I align the bottom of an inline block (call it 'IB') with the bottom of the text - excluding descenders like that on 'g' - in a parent element (call it 'PE')? This should be in a way which generalises whatever the size of the text - I don't want to hardcode size-specific pixel values.
Here is an example of the HTML I'd use, with the classes I'd need CSS for:
<div class="pe">
Parent text line
<span class="ib" style="display: inline-block;">
- and child text line
</span>
</div>
And here's what I'd like it to look like:
OP updated saying: "Thanks, but I've edited the question to clarify I don't want to hardcode size-specific pixel values."
In that case, I'm afraid there isn't a solution that will automatically fix different lines with different text sizes. The other solution I provided isn't even perfect across all of the browsers with some combinations of font sizes, because Chrome/Opera round inexact values differently than Firefox/IE, so even with my solution, you'd need to use some browser-specific css. The only thing similar to an universal solution would be setting vertical-align: middle; but I wouldn't trust that to work consistently.
You can add below css to ib. And change the bottom margin to control alignment.
.ib{
display: inline-block;
font-size: 10px;
vertical-align: bottom;
margin:0 0 1px 0;
}​
#Rorok_89 I know i am adding one more line of css but its justa way to do it in a different way. Your answer is perfect.
This seems to have worked for me: http://jsfiddle.net/Rorok_89/Z8TWH/
.ib{
display: inline-block;
font-size: 10px;
vertical-align: 1px;
}

Images have gap between them

I have a some images that I need to line up without any gaps. I can get them fine in jsFiddle, see http://jsfiddle.net/QZLSf/2/
But on the actual SharePoint site the images have a gap between them, kind of like http://jsfiddle.net/QZLSf/1/
I have checked with FireBug and the images, and links, have all the properties they should have, but I can't get rid of that gap.
What could I be missing?
EDIT: I know that the second link has footerlinks defined as a class, but I was just using that to illustrate the problem I'm having. That's not what my actual code is.
EDIT: EDIT: Ok guys there seems to be a misunderstanding as to what I am asking here. I know HOW to get the required result, just that it isn't working on the SharePoint site. I just need advice on what might be wrong as everything that should work isn't working.
Remove the whitespace/line breaks between images.
Demo: http://jsfiddle.net/QZLSf/12/
Just posted this solution elsewhere and think it's the same thing.. is your Sharepoint implementation putting the <img> elements on separate lines in the HTML?
In your fiddle you have them all on one line.. if that's the difference then I'm afraid it's natural behaviour for inline elements (space between words).. there are hacks out there that involve HTML comments or removing the spacing or splitting the img tags, but if you can't have (or don't want) an HTML workaround - then something like this should work
CSS:
div {word-spacing: -4px; background: #eee; border: 1px solid #000; width: 600px;}
div p {word-spacing: 0;}
HTML
<div>
<img src="http://dummyimage.com/150x50/dad/fff" alt="my mini thing" />
<img src="http://dummyimage.com/150x50/000/fff" alt="my mini thing" />
<img src="http://dummyimage.com/150x50/dad/fff" alt="my mini thing" />
<img src="http://dummyimage.com/150x50/000/fff" alt="my mini thing" />
<p>the div containing these images and text has it's word-spacing set to -4px which removes the default whitespace</p>
<p>but then you want some text with normal spacing reset the word-spacing to 0 on the <p> elements, which is the default</p>
</div>
this is your code:
#footerlinks a, #footerlinks img{
but footerlinks is class not an id, so use this:
.footerlinks a, .footerlinks img{
ways to skin cats...
http://jsfiddle.net/eCSYt/45/
Update for bazmegakapa:
Sorry assumed the code was pretty easy to follow and I just presented it as an alternative way to approach it..
The gaps were caused by the white space in the HTML formatting - which is significant. By setting the font-size to 1px (actually 0 would be better if it is supported xbrowser) the white space is too small to render. In a real page you may also need to zero the line-height as well.
I used text-align to centre the text just to show an alternative method... and it has the advantage that you don't need to know the total width of the images
That's just the way it is. You have to set the margin-left to -4px
.footerlinks img {
margin-left: -4px;
}
.footerlinks img:first-child {
margin-left: 0px;
}
Demo: http://jsfiddle.net/QZLSf/11/
EDIT: This solution is more correct. I fixed the margin on the first child.

Impossible to collapse <br> in webkit?

As illustrated in this jsfiddle: http://jsfiddle.net/qrbhb/
If you take this markup:
<div>There should be no gap between us</div>
<br />
<div>There should be no gap between us</div>
and this css:
div {
background: #999;
}
br {
clear: both;
display: block;
overflow: hidden;
visibility: hidden;
width: 0;
height: 0;
}
All webkit based browsers will display a gap equal to the line height of the parent element, while firefox and IEs will not display a gap. I don't know who is following the spec here, but I can't for the life of me get this to display the same in all browsers and it's driving me crazy. Any ideas?
EDIT: Sorry folks, I was looking at a rather complicated layout and mistakenly thought some elements were floating that weren't. Floated elements behave as expected.
Odd. I can see some logic to what's going on. It seems to be using the line-height from the preceding element as the height. If you add this, for example, just before the <br /> as shown:
<div class="weird" /><br />
...and then set its line-height:
div.weird {
line-height: 0;
}
(jsFiddle here)
...then the <br /> loses its height.
So, I'd guess that the line-break "inherits" -- although that's rather the wrong word -- the height of the preceding bit of text. I'm not certain that's really what's going on, but it makes the most sense of the explanations I can think of.
Really, though, I'm with everyone else -- if you don't want a break between lines, don't use a line-break. If you're going to go a bit non-semantic for clearing stuff anyway, I'd just live with it and use a <div>; the practical elements of the web community will understand and forgive you :)
use display:none;
http://jsfiddle.net/qrbhb/13/

Resources