tags:

views:

93

answers:

4

A simple yet annoying one - I am tryign to set an input[type=text] width by using absolute positioning (ie right:10px;left:10px) yet I cant get it to play ball.

Does anyone have a solution to kick it into shape?

+3  A: 

What you are trying to do can't be done that way, as far as I know. One of the values (left or right) should suffice for the position, then you just have to set width and height.

Marco
This is partially true; Internet Explorer 7- (probably version 8 as well) does not accept both a `left:` and `right:` CSS property, whereas other browsers render it fine.
Duroth
firefox 3.5.5 sure doesn't accept it either, after a quick test
David Hedlund
A: 

As Marco said, you simply cannot do it that way. If you don't know the width of the page, you'd have to use javascript to achieve that effect:

$('#myInput').css({ right: 10, position: 'absolute', width: $(document).width() - 20 })
David Hedlund
A: 

Actually, what you're doing works fine. You just need to remember to set the parent element's position as well.

<div>
   <input type="text">
</div>

Then CSS it:

div {
    width: 400px;
    position: relative;
}
input {
    position: absolute;
    left: 5px;
    right: 5px;
}

This would result in the input box being 390px.

If you set the div to be flexible, then the input box would be too.

edit: seems to only work in Chrome, so you'd need to put the input inside another element. This works in FF and IE too:

<div id="parent">
    <div><input type="text"></div>
</div>

with this CSS:

#parent {
    position: relative;
    width: 400px;
}
  #parent div {
      position: absolute;
      left: 5px;
      right: 5px;
  }
    #parent div input {
        width: 100%;
    }

This has the expected effect. A bit hack-ish, maybe...

peirix
A: 

The css positioning doesn't work quite how you want it to. All it does is set it's position relative to it's parent. The key is that (with most browsers) you only get to specify one of left/right, and one of top/bottom.

You can set the width of the input to 100%, then set the padding-left to 10px and the padding-right to 10px of the parent container of the input. That should give you want you want.

xbakesx
You should fire up your IDE and test this out. Setting `position:absolute` along with both `left` and `right` will actually cause the element to stretch so as to be the specified length from the left side _and_ the right side.
peirix