How to center text within a text area? - css

.username::placeholder {
padding-top: 14px;
text-align: center;
vertical-align: middle;
color: $grey-blue;
overflow: hidden;
}
.username {
resize: none;
width: 250px;
height: 46px;
text-align: center;
vertical-align: middle;
font-size: 16px;
font-family: "SansSerif", sans-serif;
display: inline-block;
border: 2px solid $blue;
}
.submit
{
vertical-align : middle;
background-color : $blue;
color : $white;
border : 2px solid $blue;
text-align : center;
padding : 16px 32px;
text-decoration : none;
display : inline-block;
font-size : 16px;
margin : 4px 2px;
cursor : pointer;
}
<form class="center">
<textarea minlength="2" maxlength="24" title="Username" placeholder="Username" class="username"></textarea >
<button type="submit" class="submit">Submit</button>
</form>
I would like to vertically center some text in a text-field, I was able to get the placeholder text centered, but not the actual text that's being typed.
What I'm hoping to accomplish by reaching out, is to have the user's text be aligned with the placeholder text, and the "submit" button next to it.
If you run the code snippet above, and try to put text in the field, you'll see what I mean.
One solution I've tried is to wrap the in a div, didn't work.
Here is my HTML:
http://secure.serverbox.net/sharex/201803/2018-03-28_15-07-02.png
Here is my styling:
http://secure.serverbox.net/sharex/201803/2018-03-28_15-08-19.png
Here is what the problem looks like:
With Placeholder: http://secure.serverbox.net/sharex/201803/2018-03-28_15-09-25.png
VS
With Text: http://secure.serverbox.net/sharex/201803/2018-03-28_15-11-52.png
Notice how the placeholder text is vertically aligned differently than the user's input.
Thanks in advance!

It looks like the actual text that's being typed is already centered. Maybe you are talking about vertical align ? You added padding-top to the placeholder, why not the same thing for .username ? I suggest you to delete .username::placeholder and only work with .username (Don't forget to add padding top this time). I hope it's helpful :)

Related

CSS input field styling magic

I am trying to style an input field and some buttons in a consistent way, but there seems to be some magic going on. Event though the input has the exact same classes as the buttons it is slightly higher. Also the placeholder text is vertically aligned differently the the inputted text (one pixel higher). Can this be solved with a cross-browser solution?
Edit: As pointed out by Jan TuroĊˆ the line height fixed the problem in Webkit. Now, if you check the codepen in Firefox you'll notice that the element still has a 1px border. How to get rid of that?
Thx, PS
Codepen
HTML
<form action="">
Button
<input type="text" class="btn" placeholder="input"/>
<button class="btn">Login</button>
Button
</form>
CSS
.btn, input, button {
display: inline-block;
vertical-align: middle;
line-height: 15px;
font-size: 15px;
font-family: sans-serif;
border: 0;
padding: 0;
margin: 0;
cursor: pointer;
cursor: hand;
text-decoration: none;
color: #f2f2f2;
background-color: #1a1a1a;
padding: 7px 12px 8px 12px;
margin-right: 1px;
margin-bottom: 1px;
}
The line-height doesn't shrink the input height below font-size plus some pixels see the MDN info:
On replaced inline elements, like buttons or other input element, line-height has no effect.
Just remove the line-height and you should get what you want even without applying heightstyle. Setting line-height to 130% also does seem to work.
I am trying to solve this for days and every solution is not whole, and not working in different browsers.
I find that this code do the work:
float: left;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
box-sizing:border-box;
height: 33px;
I have updated Codepen - http://codepen.io/anon/pen/pvqRwE

Non-bold text to take the space of bold text [duplicate]

This question already has answers here:
Inline elements shifting when made bold on hover
(30 answers)
Closed 6 years ago.
I am using CSS to style some radio buttons as tabs. I would like the "selected" tab's font-weight to be bold. Of course, this causes the size of the tab to increase, as the text takes up more space. I would like to find a way to have the text go from bold to non-bold without the "tab" changing size
Short of setting a fixed width, is there a crafty and clean way of making the text take up the size of bold text without it actually being bold?
The only method I can think of would be to have the bold text exist (causing the text to exist twice in the HTML), but with visibility: hidden.
Markup:
<label class="tab active"><input type="radio" name="someTabs" value="someValueA" />Tab 1</label>
<label class="tab"><input type="radio" name="someTabs" value="someValueB" />Tab 2 (with longer text)</label>
Relevant CSS as it is now:
.tab {
display: block;
font-size: 1.2em;
height: 2em;
line-height: 2em;
margin-right: 1px;
padding: 0 2em;
position: relative;
text-align: center;
float: left;
}
.tab.active,
.tab:hover {
font-weight: bold;
}
.tab input[type=radio] {
display: none;
}
There is also a solution using ghost elements. Simply use the same text with a bold style, which lies underneath the visible ares:
HTML:
<div class="link">
<div class="text">Sample Text</div>
<div class="ghost">Sample Text</div>
</div>
CSS:
.link {
border: 1px #000 solid;
display: inline-block;
}
.link .ghost {
color: transparent;
font-weight: bold;
}
.link .text {
position: absolute;
text-align: center;
}
.link .text:hover {
font-weight: bold;
}
Here is a jsFiddle to check it out!
The only caveat is that the visible text is not centered within the outer div. Maybe someone can pick up from here?!
What about css letter-spacing? The result will be near the desired one:
.tab
{
letter-spacing: 1.5px;
}
.tab.active, .tab:hover
{
letter-spacing: normal;
}
jsfiddle example

Center Text on a Button

I know this has been asked a couple of times before, but not of the solutions seem to be working in this case. Basically, I want the word "play" to be centered vertically and horizontally on this button. Horizontally, the text behaves itself, but vertically, not matter what I try, it is always a little bit lower than it should, in all browsers I test it on. Does anyone have any suggestions? Thanks
<style type="text/css">
button {
font-family: Verdana, Geneva, sans-serif;
color: white;
border-style: none;
vertical-align: center;
text-align: center;
}
button:hover {
cursor: pointer;
}
button::-moz-focus-inner /*Remove button padding in FF*/
{
border: 0;
padding: 0;
}
.start {
background-color: #0C0;
font-size: 2em;
padding: 10px;
}
</style>
<button type="button" class="start">play</button>
The padding on .start is likely what you'll have to play around with, although the way it's set, it should be centering it, but you can break it out to something like padding: 8px 10px 10px 10px;
You might also check and set the line-height under .start and see if it helps.
The correct value for vertical-align is middle, not center. However I'm not sure if that'll make the difference, because it might just affect where the button itself is aligned vertically relative to surrounding text. I'm pretty sure button text is vertically centered by default, though...

<button> padding / width problem

I'm using <button> to make a post request in a form. I also styled a a.button exactly like the <button> (I need the a.button to make some JS stuff).
The button has some padding, a fixed height. When I do specify the width of the button / a they both look the same. But when I add width to the <button> it ignores the padding.
I'm having this problem in Chrome, Firefox and Opera, so I guess it's not a rendering fault. Also same issue with <input type="submit" />
Here is the basic CSS:
.button, button {
margin: 0;
display: inline-block;
border: 0;
text-decoration: none;
cursor: pointer;
color: black;
background: #ddd;
font: 12px Verdana;
padding: 40px; /* 40px, so you can see that it won't be rendered with width */
text-align: center;
}
The HTML:
Some text
<button>Some text</button>
<!-- Works fine till here -->
<br /><br />
Some text
<button style="width:200px">Some text</button>
Fiddle: http://jsfiddle.net/9dtnz/
Any suggestions why the browsers are ignoring the padding? (top and bottom when using height / left and right when using width).
Very weird, I've seen my Chrome has a box-sizing: border-box; rule for input elements, so padding is included in width...
So to avoid that just specify box-sizing: content-box; (some prefix can be necessary).
It looks fine to me, so it might be a style sheet conflict issue. Try using !important to override whatever it may be and that could solve your problem.
.button, button {
margin: 0;
display: inline-block;
border: 0;
text-decoration: none;
cursor: pointer;
color: black;
background: #ddd;
font: 12px Verdana;
padding: 40px!important; /* 40px, so you can see that it won't be rendered with width */
text-align: center;
}
Hope this helps.
Michael.

The placeholder text in my text boxes is mis aligned in chrome

I've got a problem in Google chrome where the placeholder text sits too high on my website
http://www.myinvestmentdecision.com.au
Click "Feedback" and you'll see the text in the placeholder sits to high. I've got a placeholder script, but it turns off when it realises that chrome has support for placeholder.
Just to proove that point here's a jsfiddle of the form itself: http://jsfiddle.net/RAANa/
Must be a CSS thing. Any ideas?
Remove line-height:28px from your css below.
.form input[type="text"], .form input.text, .form .calculate_box .calc {
background: transparent url(../images/input-background.png) repeat-x left top;
border: 1px solid #A1A1A1;
margin-left: -1px;
height: 28px;
line-height: 28px;
display: block;
width: 284px;
}
See attached screenshot with line-height removed
I think your line-hight:28 from here: (form.css)
.form { font-family: arial, sans-serif; }
.form input[type=text],
.form input.text,
.form .calculate_box .calc{
background: transparent url(../images/input-background.png) repeat-x left top;
border: 1px solid #a1a1a1;
margin-left: -1px;
height: 28px;
line-height: 28px;
display: block;
width:284px;
}
Is causing the problem. Remember that this declaration applies to the content of the tag, as well as the tag itself. This can get you when it comes to forms..
Not sure if this is the "right" way to do it, but adding a padding of 5px to the input field would center the placeholder for the email field.
Just add this to your CSS wherever it applies to that email field:
padding: 5px;

Resources