CSS: Styling an input element's text (as opposed to the input element itself)?

Holland

EDIT: I've added the relevant code below at the bottom of this question. As you'll see there, the button is wrapped within a div. Also, this problem only occurs in one browser, that being Firefox, and I'll be using a hack to target that browser only.

I have an input element of type submit (i.e., basically a submit button). The text displayed in this element, as defined in the element's value attribute, appears too low (i.e., too close to the bottom of the button instead of vertically centered). The button has a fixed height.

Naturally, I want to move the button's text, as defined in the value attribute, one or two pixels upwards.

I've tried a few things with the button's padding (top and bottom), but that didn't change anything. [Is that to be expected, BTW?] Therefore, I would like to use relative positioning to move the text upwards a bit.

The thing is, however, that I need to target the text itself, NOT the input/button element. And that's of course because the button itself should stay at its current location, I only want to move the TEXT displayed on the button.

Thus my question: Is there a way, in CSS, to target not the button but only its displayed text (as defined in the value attribute) ?

Of course, other solutions (preferably CSS only) are welcome as well.

Code:

HTML:

<form id="zoekform">
   <input type="text" class=""  id="search-text" name="search-text" placeholder="Search"> 
   <div class="erom" id="erom2">
      <input id="zoekknop" style="float: right" type="submit" method="GET" value="Search!" />
   </div>
</form>

CSS:

#zoekform {
    height: 29px;
    box-sizing: border-box;
    margin-top: 6px;
    margin-bottom: 9px;
 }

 .erom {
   height: 100%;
   display: inline-block;
   box-sizing: border-box;
}

   #erom2 {
      border: solid 1px #452F5D;
      width: 27%;
      display: inline-block;
      box-sizing: border-box;
   }

   #zoekknop {
      float: right;
      height: 100%;
      color: white;
      font-size: 19px;
      box-sizing: border-box;
      background-color: #446666;
      color: white;
      letter-spacing: 2px;
      border: solid 1px white;
      width: 100%;
    }

And finally the part where I'm targeting Firefox only, and where I can't get the padding working (and to be sure, the "media query" (it's not really a media query) does work, and in any case I've also tried this without the media query, i.e. as part of the regular CSS):

@-moz-document url-prefix() { 
#zoekknop {
padding-top: -1px !important;
padding-bottom: 9px !important; // I set it to 9px for now, so that I could clearly see if it worked
}
}
zer00ne

For some reason form elements are particular and quirky about font.

  • Assign a font to the <submit>'s parent, then use font: inherit on the <submit> button.

  • On the <submit> assign line-height of 1.4 to 2 (notice there's no unit like px or em.) I actually have the line-height assigned by inheriting the font from <form> 1.4.

  • Set width using the ex unit of measurement. One ex is as wide as ax character, making it a great way of gauging how much space you are using in relation to your text. I used 9ex for a 6 character word (i.e. Submit).

  • This ruleset may help you for Firefox:

     input::-moz-focus-inner {
        border: 0;
        padding: 0;
    
        /* Some users have said these last two are 
           unnecessary or should be -2px */ 
    
        margin-top:0;  
        margin-bottom: 0;
      }
    

Here's some changes I did to your button and search field:

     #zoekknop {....
        ....
        border: 2px double white;
        line-height: 1.65;
        vertical-align: baseline;
      }

      #search-text {
         line-height: 1.75;
         vertical-align: baseline;
         padding: 4px 3px 0;
       }

Review the Snippet below:

#form {
  font: 400 16px/1.4'Verdana';
}
#form .sub {
  font: inherit;
  width: 9ex;
  color: blue;
  border-radius: 5px;
}
#form .sub:hover {
  color: cyan;
  background: #888;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#zoekform {
  height: 29px;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 9px;
  font: 400 16px/1.4 'Verdana';
}
#zoekform #zoekknop {
  
  color: white;
  font-size: 18px;
  box-sizing: border-box;
  background-color: #446666;
  color: white;
  
  border: 2px double white;
  line-height: 1.65;
  vertical-align: baseline;

}
#search-text {
  line-height: 1.75;
  vertical-align: baseline;
  padding: 4px 3px 0
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

input::-moz-focus-inner {
  border: 0;
  padding: 0;
  margin-top: 0;
  margin-bottom: 0;
}
<form id="form" name="form">
  <input type="submit" class="sub" value="Submit" />
</form>

<form id="zoekform">
  <input type="text" class="" id="search-text" name="search-text" placeholder="Search">
  
    <input id="zoekknop" type="submit" method="GET" value="Search!" />
  
</form>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Styling input element using only CSS

From Dev

Styling md-input-element

From Dev

Materialize.css remove styling from input element

From Dev

Polymer - Styling paper-input element

From Dev

Styling the height of a input type='file' element in chrome

From Dev

Input element and CSS not working

From Dev

CSS Styling for Artificial Text Input from DIV to mimic HTML input

From Dev

Replacing a label's text if there's a nested input element

From Dev

Text and input element on same line with 100% input

From Dev

Styling a text input as a date input

From Dev

Hiding Text inside of an input element

From Dev

Get parent text of input element

From Dev

CSS: How to align text to baseline of height-adjusted input element?

From Dev

Delete Parent Div to correctly display Form Input Element without Styling

From Dev

Can I access an element with inline styling like an input?

From Dev

How can I change the text of an element but maintain CSS styling in Jquery?

From Dev

Styling the caret in an input text area

From Dev

Styling the caret in an input text area

From Dev

Access all input/text boxes in td element

From Java

How to remove the border highlight on an input text element

From Java

Font Awesome icon inside text input element

From Dev

Input type='text' element not taking spaces

From Dev

Jquery get element attribute and insert it to an input text

From Dev

Create element from input type='text' JavaScript

From Dev

Access all input/text boxes in td element

From Dev

Jquery get element attribute and insert it to an input text

From Dev

Use inline text on Bootstrap input element

From Dev

ng-init select text of input element

From Dev

Get the text value of an input element in C#

Related Related

HotTag

Archive