I am trying to vertically align the placeholder text in textarea (textbox). I am using textarea instead of text input because I need to use multiple lines.
.textbox1 {
width: 440px;
}
<textarea class="textbox1"name="mytextarea"placeholder="Name"></textarea>
One option is to use line-height:
.textbox1 {
width: 440px;
height:30px;
line-height:30px;
}
.textbox1 {
width: 440px;
height:30px;
line-height:30px;
}
<textarea class="textbox1"name="mytextarea"placeholder="Name"></textarea>
You can also use padding to control the position of the text.
Here's an example using padding-top
.textbox1 {
width: 440px;
padding-top:15px;
}
<textarea class="textbox1"name="mytextarea"placeholder="Name"></textarea>
UPDATE
Since the requirements include multi-line support, I'd recommend setting the top and bottom padding i.e:
.textbox1 {
width: 440px;
height:6px;
padding: 30px 5px;
}
.textbox1 {
width: 440px;
height:60px;
padding: 30px 5px;
}
<textarea class="textbox1"name="mytextarea"placeholder="Name"></textarea>
Instead of using padding-top which will increase the height of the textarea and extra space down use the line-height here is a sample, you can vary the line-height.
textarea::placeholder {
line-height: 90px;
}
you can also use transform property like this:
textarea::placeholder {
transform: translateY(-20px);
}
This works for latest Firefox, IE/Edge, Chrome in pure CSS:
textarea {
width: 440px;
height:600px; /* Note this is the same height as the placeholders line-height */
}
::-moz-placeholder { /* Mozilla Firefox 19+ */
line-height:600px;
}
::-webkit-input-placeholder { /* Webkit */
line-height:600px;
}
:-ms-input-placeholder { /* IE */
line-height:600px;
}
See this fiddle. The key is to set the height of the textarea to the same line-height as the placeholder.
Sadly vertical-align: middle; seems not to be supported yet.
Your best bet is to use padding, as line height will not work over multiple lines.
Additionally, make sure to take into account the line height / font size when calculating your padding.
I guess this is not exactly what you want, But try to look ..
To center it vertically, I multiplied the height of the element to 7.5% and make it line-height.
.textbox1{
width: 440px;
height:100px;
line-height: calc(100 * 7.5%);
}
.textbox1:focus{
line-height: 14px;
}
Check it here. pure CSS jsFiddle
Note: CSS3 calc() function only works on modern browsers. You can manually change/calculate the line-height if you want it to work on older browsers.
or you really have to use jQuery
I made a help of jQuery here jQuery jsFiddle
.textbox1 {
width: 440px;
height:70px
}
::-webkit-input-placeholder { /* Chrome/Opera/Safari */
line-height:70px;
text-align: center;
}
:-moz-placeholder { /* Firefox 18- */
line-height:70px;
text-align: center;
}
::-moz-placeholder { /* Firefox 19+ */
line-height:70px;
text-align: center;
}
:-ms-input-placeholder { /* IE 10+ */
line-height:70px;
text-align: center;
}
You can check here CSS jsfiddle
Use the line-height property to make the placeholder vertically centered.
Just as an alternate solution - you could set the rows to an odd number and add half the rows rounded down in line feed characters to put the placeholder in the middle...
.textbox1 {
width: 440px;
}
<textarea class="textbox1"name="mytextarea"placeholder="
Name" rows=3></textarea>
<textarea name="example" onFocus={onFocus} onBlur={outFocus} />
<label className={styles.Teaxarea_label}>Label</label>
and then you have those functions:
const outFocus = (event) => {
if (event.target.value !== '') {
event.target.nextSibling.style = "display:none;"
}
else {
event.target.nextSibling.style = "display:block;"
}
}
const onFocus = (event) => {
event.target.nextSibling.style = "display:none;"
}
CSS:
.Teaxarea_label{
position: absolute;
user-select: none;
pointer-events: none;
display: block;
}
textarea {
position: relative;
}
To improve on the padding answers, here is a way to guarantee calculations that center it. This works better than line-height for multiple lines, but it still means that once the user enters multiple lines of text, there will now be overflow to scroll, rendering the centering essentially useless
.textbox1 {
--title-modal--textarea--height: 100px;
/* font-size variable for calculations cannot be em, otherwise calculations become doubly-dependent on font-size */
--title-modal--textarea--font-size: 1rem;
--title-modal--textarea--line-height: 1.5;
height: var(--title-modal--textarea--height) !important;
font-size: var(--title-modal--textarea--font-size);
line-height: var(--title-modal--textarea--line-height);
padding-top: calc((var(--title-modal--textarea--height) - (var(--title-modal--textarea--line-height) * var(--title-modal--textarea--font-size)))/2);
/* I used these to make sure the border doesn't take away from the height, */
/* affecting the padding calculation, */
/* but you could similarly turn them into variables and subtract */
border-bottom: 0;
border-top: 0;
}
You can use absolutely positioned placeholder:
textarea::placeholder {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Check this example
Related
I created multi-line-padded text based on Matthew Pennell's solution (codepen by CSS Tricks). In Chrome all looks fine, but in Firefox height of span elements bigger than height of their ancestor. If I adjust vertical padding for Firefox, in Chrome will be same problem, and vice versa.
Why it happens? What the real technical reasons of this problem?
HTML Code:
<div class="padded-multiline">
<h1>
<strong>How do I add padding to subsequent lines of an inline text element?</strong>
</h1>
</div>
CSS Code:
:root {
font-family: Arial, sans-serif;
font-size: 20px;
}
.padded-multiline {
line-height: 1.3;
padding: 2px 0;
border-left: 20px solid #c0c;
width: 400px;
margin: 20px auto;
}
.padded-multiline h1 {
background-color: #c0c;
padding: 4px 0;
color: #fff;
display: inline;
margin: 0;
}
.padded-multiline h1 strong {
position: relative;
left: -10px;
}
Setting a line-height: 1; on strong will fix the problem also read my comment.
Chrome and Firefox seems to use different text layout system.
In Chrome it will floor the line-height attribute and Firefox seems to use the correct one.
To achieve the same effect for title, just use only the outline.
H1 does not need strong.
.padded-multiline {
line-height: 1.3;
padding: 2px 0;
width: 400px;
margin: 20px auto;
}
.padded-multiline h1 {
background-color: #c0c;
padding:1px;
color: #fff;
display: inline;
outline: 10px solid #c0c;
margin: 0;
font-size:16px;
}
<div class="padded-multiline">
<h1>How do I add padding to subsequent lines of an inline text element?</h1>
</div>
Here is codepen: http://codepen.io/anon/pen/vgRvjM
If you need exactly visual (that means less purple space from top and bottom, you can use for example border from after and before):
.padded-multiline:before{
content:'';
display:block;
border:5px solid #fff;
position:relative;
left:-10px;
top:-3px;
}
.padded-multiline:after{
content:'';
display:block;
border:5px solid #fff;
position:relative;
left:-10px;
bottom:-3px;
}
Codepen for this solution: http://codepen.io/anon/pen/QdmzxK
Unfortunately, there isn't a full and clean crossbrowser workaround. Because different UAs render text different, height of each textline may be taller a bit (or vice verca). So, I create a solution based on SCSS calculations of required box' sizes, and hide artefacts via overflow property.
Here is my solution, if you meet the same problem: http://codepen.io/ifiri/pen/ygEeeL
HTML:
<p class="multiline-text">
<span class="multiline-text__wrapper multiline-text__wrapper--outer">
<span class="multiline-text__wrapper multiline-text__wrapper--left">
<span class="multiline-text__wrapper multiline-text__wrapper--right">Multiline Padded text, which looks great on all browsers. No artefacts, no hacks, all clear and flexy, all alignment support. Change SCSS variables for see how it works.</span>
</span>
</span>
</p>
SCSS:
/*
Variables
*/
$base-line-height: 1.75;
$base-font-size: 1.25em;
$multiline-padding-base: ($base-line-height / 2) * 1em;
$multiline-padding-horizontal: $multiline-padding-base;
$multiline-padding-vertical: $multiline-padding-base - (1em / 2);
$multiline-bg-color: #a5555a;
$multiline-font-color: #fff;
/*
= Snippet Styles
This code is required
*/
.multiline-text {
color: $multiline-font-color;
padding: 0px $multiline-padding-horizontal;
// hide line-height artefacts
overflow: hidden;
position: relative;
}
.multiline-text__wrapper {
background-color: $multiline-bg-color;
padding: $multiline-padding-vertical 0px;
}
.multiline-text__wrapper--outer {
// Inner padding between text lines
line-height: $base-line-height;
}
.multiline-text__wrapper--left {
position: relative;
left: -($multiline-padding-horizontal);
}
.multiline-text__wrapper--right {
position: relative;
right: -($multiline-padding-horizontal / 2);
}
I am working on this page: link to page.
Inside h2 I have before and after elements. In IE they are too big, original width and height these images are not working. When I am trying to resolve this problem, in FF and Chrome everything is getting even worse.
In Edge things are a little bit different - I have figured out a way to make images smaller, but before element is inside h2 text.
Here are the examples:
Normal (from FF and Chrome)
A little strange (from Edge)
So crazy (from IE)
CSS code:
h2{/*How I am displaying h2 elem */
text-align: center;
width: 80%;
margin: 45px auto 115px !important;
text-transform: uppercase;
color: #fff;
}
h2::before {
content: url(img/pepper.svg);
margin-right: 10px;
position: relative;
height: 50px;
}
h2::after{
content: url(img/apple.svg);
margin-left: 10px;
position: relative;
height: 50px;
}
#supports (-ms-accelerator:true) { /*Trying to resolve problem in Edge */
h2::before {
position: absolute;
}
h2::after{
position: absolute;
}
}
Try making the positon of before and after leftmost and rightmost.
If it doesnt work,try making pixels to %.
As #ankit says, removing width: 80% is doing right on IE. Also removing part with supports resolved problem with Edge.
Another approach (assuming you have control of the HTML): add an empty right after the input, and target that in CSS using input+ span:after
.field_with_errors {
display: inline;
color: red;
}
.field_with_errors input+span:after {
content: "*"
}
<div class="field_with_errors">Label:</div>
<div class="field_with_errors">
<input type="text" /><span></span>
</div>
I'm using this approach in AngularJS because it will add .ng-invalid classes automatically to form elements, and to the form, but not to the .
Hey guys Im currently trying to get a textbox a select menu and a button all into one sized div cleanly but im running into an issue where each element has odd borders/margins which prevent it from rendering properly (the button appears below the text box and select menu)
Heres the html Im currently using
<div class="content">
<div class="search-panel">
<div class="search-panel-logo">
<img src="img.png" class="search-panel-logo-img" />
</div>
<div class="search-panel-searchbar">
<form class="search-panel-frm" action="" id="fsearchbar">
<input class="search-panel-frm" type="text" id="tseachtext" name="tsearchtext" value="Search" />
<select class="search-panel-frm" id="ssearchselect" name="ssearchselect">
<option value="Cars">Cars</option>
</select>
<input class="search-panel-frm" type="button" id="bsearchgo" name="bsearchgo" value="Search!" />
</form>
</div>
</div>
</div>
and heres the CSS:
.content {
background:inherit;
width:950px;
height:600px;
margin: 0 auto;
}
.search-panel {
width:inherit;
height:500px;
background:#093;
margin:0 auto;
}
.search-panel-searchbar {
width:inherit;
height:30px;
}
.search-panel-searchbar-frm {
width:inherit;
height:inherit;
}
.search-panel-searchbar-frm-text {
width:60%;
height:70%;
}
.search-panel-searchbar-frm-select {
width:20%;
height:80%;
}
.search-panel-searchbar-frm-go {
width:20%;
height:80%;
}
any idea what I can add to get all the elements to appear in one line as opposed to two, Ive already tried
border:0;
margin:0;
and it didnt fix the problem.
To achieve your goal, some CSS tricks had to be applied, see below the CSS:
CSS
/* wrapper */
.search-panel-searchbar {
width: 100%;
height: 30px;
overflow: hidden;
}
/* form elements must be wrapped and not a direct child of the form tag */
.search-panel-searchbar > form > div {
background-color: #fff;
width: 100%;
height:30px;
}
/* input[type="text"] */
.search-panel-searchbar-frm-text {
margin: 0;
padding: 0;
font-size: 12px;
line-height: 16px;
height: 16px; /* necessary to fix height issue on windows browsers */
padding: 6px 0;
display: block;
border: 1px solid #ccc;
background-color:#fff;
width: 60%;
text-indent: 4px;
float: left;
}
/* select */
.search-panel-searchbar-frm-select {
margin: 0;
padding: 0;
font-size: 12px;
height: 30px;
padding: 6px 4px;
display: block;
border: 1px solid #ccc;
background-color:#fff;
width: 20%;
float: left;
margin-left:-1px; /* pull to prevent border overflow issue with % */
}
/* input[type="button"] */
.search-panel-searchbar-frm-go {
margin: 0;
padding: 0;
font-size: 12px;
height: 30px;
padding: 6px 0;
display: block;
border: 1px solid #ccc;
background-color:#fff;
width: 20%;
float: left;
text-align:center;
margin-left:-1px; /* pull to prevent border overflow issue with % */
cursor: pointer;
}
Note:
CSS does not include style given on the Fiddle example to a visual demonstration, like the button hover and body background.
Please take note that unfortunately, each browser (and OS!) deals with the select in different ways, and it's likely that on one or two browsers, the style may differ.
The working Fiddle example!
Screen shot matches tests performed on:
Linux Ubuntu 12.04
Firefox 12.0
Chromium 18.0.1025.151 (Developer Build 130497 Linux)
Windows XP Profissional versão 2002 Service Pack 3
Internet Explorer 8.0.6001.18702
Opera 11.62
Firefox 3.6.16
Safari 5.1.2 (only select box height fail)
Google Chrome 18.0.1025.168 m
K-Meleon 1.5.4 (fail due to font-family)
Windows 7 Home Edition Service Pack 1
Internet Explorer 9.0.8112.164211C
Opera 11.62
Firefox 12.0
Safari 5.1.4
Google Chrome 18.0.1025.168 m
The font-family that you will use must contain a font family declaration at the end to prevent line-height and text-size issues. e.g., font-family: Helvetica, Arial, sans-serif
Remove the whitespace between the elements.
Also, using width: inherit is probably not a good idea - try width: 100% instead or something along those lines. height: inherit can be replaced with height: auto; (or just omitted).
You've posted some CSS classes that doesn't apply to the HTML you posted (eg. .search-panel-searchbar-frm-go)
You're applying the class search-panel-frm to your form element as well as elements within the form. Are you sure this is what you want?
I'm not seeing the same problem: The input, select and input:button all appear on the same line for me.
You can try adding this simple fix to keep everything on one line which may give you some ideas.
.search-panel-frm *
{
white-space:nowrap;
}
If all your elements are already on the same line and you want them to line up better try different values for vertical-align, such as:
.search-panel-frm *
{
vertical-align:top;
}
The html is:
<div class="choose-os">
<p>
Microsoft Windows
Apple Mac OS
</p>
</div>
The CSS is:
.choose-os {
margin: 20px 0;
padding: 20px;
background: #e7eefa;
}
.choose-os p {
margin: 0;
}
.choose-os p a {
display: inline-block;
text-indent: -100000px;
height: 56px;
width: 308px;
}
.choose-os p a.windows {
background: url(../images/button-windows-bg.png) 0 0;
}
.choose-os p a.macos {
background: url(../images/button-macos-bg.png) 0 0;
}
.choose-os p a:hover {
background-position: 0 -56px;
}
Any suggestions would be greatly appreciated as to have the background image also appear on IE7.
The text-indent: -100000px; in combination with inline-block is what's causing the two elements to not be visible in IE7, due to a bug.
You need to find some other way to hide the text for IE7 (or not use inline-block at all, see below for this more suitable fix).
Options include the method in the comment by #Sotiris, or:
.choose-os p a {
display: inline-block;
height: 56px;
width: 308px;
text-indent: -100000px;
/* for ie7 */
*text-indent: 0;
*font-size: 0;
*line-height: 0
}
Which uses the *property: value hack several times to hide the text in IE7.
The problem does seem to be related to the use of display: inline-block.
So, another workaround (which I prefer to my previous one) is:
.choose-os {
margin: 20px 0;
padding: 20px;
background: #e7eefa;
overflow: hidden
}
.choose-os p a {
float: left;
margin-right: 4px;
text-indent: -100000px;
height: 56px;
width: 308px;
}
To display inline-block properly in IE7, add the following styles to .choose-os p a
zoom:1
*display:inline
(The star is important! It's ignored by modern browsers, but not IE6/7)
IE7 doesn't respect inline-block, so you have to do a little magic to make it work. There's a great description here: http://blog.mozilla.com/webdev/2009/02/20/cross-browser-inline-block/
[edit] If text-indent is also part of the culprit, you may be better of sticking with display:block and setting float:left on your elements. Probably multiple valid paths to take :)
IE7 has some serious limitations in CSS. I would recommend avoiding the shorthand notation and explicitly declaring each property, then validate the CSS sheet here.
I have a very little but hard (for me) problem to solve.
I have a text input, and a submit button. I need them to be the exact same height and for this to be true across Chrome and Firefox, ideally internet explorer also.
HTML
<input type="text" name="email" /><input type="submit" value="»" />
CSS
input[type=text] {
width: 218px;
}
input[type=submit] {
background: #000;
color: #fff;
}
input[type=submit], input[type=text] {
padding: 9px;
font-size: 18px;
line-height: 18px;
float: left;
border: 0;
display: block;
margin: 0;
}
I've setup this basic code on a jsfiddle here.
You should notice if you load it in chrome, the button is less height than the text input and in firefox, its larger.
What am I missing?
Remove/add line-height: 18px; for both.
Vertical padding of the submit button has no effect. This seems to be a webkit bug. You can solve the problem by specifying explit heights and increasing the height of the submit button by the top and bottom padding of the input field.
input[type=text] {height: 60px;}
input[type=submit] {height: 78px;}
The problem is your padding that is applying wrong to your button.
Trying solving it like this.
input[type=submit], input[type=text] {
font-size: 18px;
line-height: 18px;
float: left;
border: 0;
display: block;
margin: 0;
height: 30px; /* or whatever height necessary */
}
Additionally, you can keep the padding left and right on your button like this.
input[type=submit] {
background: #000;
color: #fff;
padding: 0px 9px;
}
input {
height: 19px;
}
This maybe?
Also, remove the padding property.
http://jsfiddle.net/xkeshav/e6aTd/1/
Maybe it's the padding that is making problems. Try removing the padding, setting the button to a fixed height and make the offset with line-height.
You need to remove the height and work on the actual height of the input text field just by padding/font-size
jsfiddle
Removing/adding line-height: 18px; for both is not a perfect solution because I see a little difference height in firefox...
The best solution I found is to put a div arround and set its style to display: flex.
All is perfect this way.
body {
background: #ccc;
}
div{
display: flex;
}
input[type=text] {
width: 218px;
}
input[type=submit] {
background: #000;
color: #fff;
}
input[type=submit], input[type=text] {
padding: 5px;
font-size: 25px;
border: 0;
margin: 0;
}
<div><input type="text" name="email" /><input type="submit" value="»" /></div>
TRY
body {
background-color: #ccc;
}
input[type=text] {
width: 218px;
}
Working DEMO