How to control the width of an apex inputfield in a VisualForce Page

Sam picture Sam · Jan 19, 2012 · Viewed 20.9k times · Source

Since the standard Page Layouts don't allow you to adjust the width of an inputfield bound to a text field, I'm trying to create a VisualForce Page but there isn't a width attribute for the apex:inputfield component. I think I will have to use styles or CSS or something, but I don't know how.

What do I need to do to adjust the width of a text inputfield?

Please provide instructions using the web interface (not Force.com IDE). Thanks.

EDIT

I ended up using an embedded style like this

<apex:inputfield value="{!Country__c.Full_Name__c}" style="width:400px"/>

Answer

Matt Lacey picture Matt Lacey · Jan 19, 2012

You should be able to do this using css. Nearly all of the Visualforce tags have an attribute called styleClass, which is the name of a css class to use, i.e.:

<apex:inputText styleClass="myClass" ... />

Becomes:

<input type="text" class="myClass" ... />

So using this you could then specify the width using CSS at the top of the page. Here is a complete example for a page using the standard Contact controller (no nice formatting though!):

<apex:page standardController="Contact">
    <style type="text/css">
        .myClass { width: 400px; }
    </style>

    <apex:form >
        <apex:outputLabel for="firstName" value="First Name"/>
        <apex:inputText styleClass="myClass" id="firstName" value="{!Contact.FirstName}"/>
    </apex:form>
</apex:page>

This works for <apex:inputField> as well, though be wary of what may happen with different field types. To be more specific with the css (CSS 3 only!) you could do this:

input[type="text"].myClass = { width: 400px; }