I often use iconic fonts, and often face the same issue when I try to put an icon inside a button.
I've got an extra space, a̶n̶d̶ ̶I̶'̶m̶ ̶n̶o̶t̶ ̶a̶b̶l̶e̶ ̶t̶o̶ ̶f̶i̶n̶d̶ ̶w̶h̶e̶r̶e̶ ̶i̶t̶ ̶c̶o̶m̶e̶s̶ ̶f̶r̶o̶m̶:
EDIT:
You can see in this snippet the last button has not the same height as the previous one. This is caused by the default font-size applied in the iconic font. A solution first provided by Tibbers was to set the line-height property. It works, but the button is then no longer vertically aligned.
So here comes the question :
How to change a button font size, preserving its height and keeping it vertically aligned ?
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
input,
button {
line-height: 20px;
padding: 10px;
border: 1px solid black;
float: left;
}
button i:before {
content: "\25b6";
font-size: 20px;
font-style: normal;
}
<input type="text" value="VALUE">
<button>SEND</button>
<button><i></i></button>
Does somebody know where I should look ?
To illustrate what I'm searching, I made several screens :
Without change
With line-height: 0;
With vertical-align: middle;
With line-height: 0; vertical-align: middle;
Expected
What about this? Just add display:block to your i:before and the line-height in the input to make all the 3 elements aligned.
I added line-height:20px to your i:before because I reset the line-height to 0 in *
See snippet below.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
line-height: 0
}
input,
button {
line-height: 20px;
padding: 10px;
border: 1px solid black;
float: left;
}
input {
line-height: 22px;
}
button i:before {
content: "\25b6";
font-size: 20px;
font-style: normal;
display: block;
line-height: 20px;
}
<input type="text" value="VALUE">
<button>SEND</button>
<button><i></i>
</button>
I've sorted it out be setting up the line-height to the i:before element
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
input,
button {
line-height: 20px;
padding: 10px;
border: 1px solid black;
float: left;
}
button i:before {
content: "\25b6";
font-size: 20px;
line-height: 15px;
font-style: normal;
}
<input type="text" value="VALUE">
<button>SEND</button>
<button><i></i></button>
Just add line-height: 0 to the i:before :)
*{
box-sizing: border-box;
margin: 0;
padding: 0;
}
input,
button {
line-height: 20px;
padding: 10px;
border: 1px solid black;
float: left;
vertical-align: top;
}
button i:before {
content: "\25b6";
font-size: 20px;
font-style: normal;
line-height: 0px;
}
button::-moz-focus-inner,
input::-moz-focus-inner {
border: 0;
padding: 0;
}
<input type="text" value="VALUE">
<button>SEND</button>
<button><i></i></button>
The problem I see is that particular charachter (▶) is not vertically centered... just try to select it to understand what I mean.
i guess you can correct it placing it with line-height:20px (same as text in the button), vertical-align:middle and a litte negative margin on top... if you use em units for this property you can change the dimension of the font and it will be still working... i think that a margin-top:-0.1em; is enough.
here the complete solution
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
input,
button {
line-height: 20px;
padding: 10px;
border: 1px solid black;
float: left;
}
button i:before {
content: "\25b6";
font-size: 20px;
line-height:20px;
font-style: normal;
display:block;
vertical-align:middle;
margin-top:-0.1em;
margin-bottom:0.1em;
}
<input type="text" value="VALUE">
<button>SEND</button>
<button><i></i></button>
Mixin answers and testing my own ideas, I finally came to a suitable solution.
First, I discovered the \25b6 character is not perfectly vertically aligned. It's a few pixel under the middle line. So I made tries with other characters to be sure.
Here's my solution :
Use height: 40px instead of line-height: 20px on input, button (40 = line-height + padding-top + padding-bottom)
Add display: table; line-height: 0; to i. The i element itself had a default line-height and this size was pushing down the i:before element.
Add display: table-cell; vertical-align: middle; to i:before
Final code :
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
input,
button {
height: 40px;
padding: 10px;
border: 1px solid black;
float: left;
}
button i {
display: table;
line-height: 0;
}
button i:before {
content: "\25b6";
font-size: 20px;
font-style: normal;
line-height: 0;
display: table-cell;
vertical-align: middle;
}
<input type="text" value="VALUE">
<button>SEND</button>
<button><i></i></button>
Related
I've got multiple textareas. One underneath the other. There should not be any spacing between them, since I explicitly set their margin to 0.
However on chrome, there is a rather larger gap, on firefox it's small, but still there, and on IE it actually behaves as intended.
body{
background-color: #0087B3;
font-family: Helvetica, sans-serif;
}
.editor {
width: 460px;
display: inline-block;
}
.panel{
text-align: left;
margin: 10px;
padding: 12px;
background-color: rgba(255,255,255,0.1);
}
.panel .toolbar{
background-color: #007da6;
height: 40px;
}
.panel .lines{
height: 400px;
background-color: #ACE1F2;
}
.panel .lines textarea{
resize: none;
font-family: inherit;
font-size: 12pt;
padding: 8px;
box-sizing: border-box;
width: 100%;
height: auto;
overflow: hidden;
border: 0 none white;
outline: none;
margin: 0;
}
<div class="editor">
<div class="panel" id="panel">
<div class="toolbar"></div>
<div class="lines">
<textarea rows="1">There should be no space</textarea>
<textarea rows="1">between these textareas</textarea>
<textarea rows="1">however in chrome & firefox there is</textarea>
<textarea rows="1">except internet explorer</textarea>
</div>
</div>
</div>
JSFiddle to play around
Does anyone have a clue?
Thank you in advance!
You need to add display: block; to your textarea styles
.panel .lines textarea {
resize: none;
font-family: inherit;
font-size: 12pt;
padding: 8px;
box-sizing: border-box;
width: 100%;
height: auto;
overflow: hidden;
border: 0 none white;
outline: none;
margin: 0;
display: block;
}
Please check the updated fiddle here : https://jsfiddle.net/fu3ytLpt/4/
The only fix was to assign the display to match the box sizing.
display: -webkit-box;
I really need help on this: cracking me for 2nd day already. I have the following code:
* {
margin: 0;
padding: 0;
font: 16px Verdana, Arial, sans-serif;
color: #444;
line-height: 1.5rem;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
text-decoration: none;
}
.inlbtn {
width: 2rem;
height: 2rem;
display: table;
text-align: center;
font-weight: bold;
color: #666;
border: 1px solid #939393;
}
.plus {
font-size: 1.5rem;
font-weight: bold;
display: table-cell;
vertical-align: middle;
}
.plus:before {
content: "+";
}
<div class='inlbtn'><span class='plus'></span></div>
It basically has a div and span inside with a "+" symbol. The horizontal alignment seems fine, but the vertical is a little shifted down. How to make it perfectly centered vertically?
I played around with the code and it seems the code under * is the culprit, but why?
Here's fiddle http://jsfiddle.net/jk34josq/2/
You do everything right, I always use the same method. The problem is that this line
content: "+";
is a piece of text, so it automatically has top margin inside of the line-height preserved for the capital letters (and + symbol is not the one); the margin value could also be different depending on the font.
As a proof try the following:
content: "A";
This looks centered.
What you can do to avoid this behavior:
Negative margin / top property
Use image instead of text
Maybe play with reducing the line-height property but I have doubts about this method
I would use only a single HTML element, since there is no need for using an extra element nor a :before pseudo class:
<div class='inlbtn'>+</div>
Then I would use display: inline-block, instead of table.
As mentioned by Simon in his answer, the + character is smaller than A. But instead of using negative margins or paddings, I would alter the line-height:
.inlbtn {
width: 2rem;
height: 2rem;
font-size: 1.5rem;
line-height: 1.5rem;
display: inline-block;
text-align: center;
font-weight: bold;
color: #666;
border: 1px solid #939393;
}
Updated Fiddle
Try like this: Demo
.inlbtn {
width: 2rem;
height: 2rem;
display: block;
text-align: center;
font-weight: bold;
color: #666;
border: 1px solid #939393;
}
.plus {
font-size: 1.5rem;
font-weight: bold;
display: inline-block;
vertical-align: middle !important;
}
.plus:before {
content:"+";
display: inline-block;
}
I've this list of buttons
button {
background-color: grey;
color: #fff;
border: none;
border-radius: 2px;
box-shadow: 1px 1px 0 0.8px #C0CBD1;
height: 30px;
margin: 0;
padding: 0;
position: relative;
width: 30px;
font: 500 16px/36px sans-serif;
}
.special {
font-size: 30px;
}
<button>A</button>
<button>B</button>
<button class="special">C</button>
Now what I've done is that the special button has a bigger font-size. The weird thing is that increasing the font-size moves this button up. I guess this is all very logic but cannot find the explanation (which should help me to fix this of course!)
The explanation is that buttons are inline-element, and the text in the button determines the vertical alignment.
The default vertical alignment for inline elements is to place the bottom of characters on the base line of the text. If you look at the buttons in your example, you see that the bottom of the characters line up exactly.
If you add some text around the buttons, you see that the text of the buttons aligns with the text outside the buttons: http://jsfiddle.net/Guffa/q640e8sc/4/
If you specify a different verical alignment for the buttons, they will line up differently. If you for example use vertical-align: middle;, the buttons will line up at the center of the characters, so the edges of the buttons will line up: http://jsfiddle.net/Guffa/q640e8sc/5/
Another way to avoid that alignment is to make the buttons block elements, for example using float: left;. That makes the buttons line up, but it of course make the buttons react differently to surrounding elements: http://jsfiddle.net/Guffa/q640e8sc/6/
Use vertical-align:
button {
background-color: grey;
color: #fff;
border: none;
border-radius: 2px;
box-shadow: 1px 1px 0 0.8px #C0CBD1;
height: 30px;
margin: 0;
padding: 0;
position: relative;
width: 30px;
font: 500 16px/36px sans-serif;
display: inline-block;
vertical-align: top;
}
.special {
font-size: 30px;
display: inline-block;
vertical-align: middle;
}
<button>A</button>
<button>B</button>
<button class="special">C</button>
And to align the text in the middle, you may use line-height.
button {
background-color: grey;
color: #fff;
border: none;
border-radius: 2px;
box-shadow: 1px 1px 0 0.8px #C0CBD1;
height: 30px;
margin: 0;
padding: 0;
position: relative;
width: 30px;
font: 500 16px/36px sans-serif;
display: inline-block;
vertical-align: top;
line-height: 16px;
}
.special {
font-size: 30px;
display: inline-block;
vertical-align: middle;
line-height: 30px;
}
<button>A</button>
<button>B</button>
<button class="special">C</button>
<button>D</button>
<button>E</button>
I made a form and its input text.
HTML
<form action="/search" method="get" id="searchForm"><input type="text" id="searchText" name="q" value="{SearchQuery}" /></form>
CSS
#searchText {
text-align: center;
position: relative;
top: -2px;
padding-top: 5px;
width: 496px;
font: 2em "Myriad", Helvetica, sans-serif;
line-height: 1;
color: #222222;
border: 1px solid gray;
border-radius: 2em;
}
#searchForm {
}
And it works perfectly but looks like this:
Notice how the cursor is moved down and not centered vertically.
Then, when any text is entered, it corrects itself to the right size/ vertical position and becomes
(and even stays correct when text is deleted).
How can I make it also initialise correctly?
I've tried setting the
#searchForm { }
to vertical-align: center;
line-height: normal; or inherit
font-size: 1;
but to no effect.
Quick fix suggestion :
padding: 2px 0;
would solve your problem
#searchText {
text-align: center;
position: relative;
top: -2px;
padding: 2px 0; /* altered */
width: 496px;
font: 2em "Myriad", Helvetica, sans-serif;
line-height: -2em;
color: #222222;
border: 1px solid gray;
border-radius: 2em;
}
demo
why its happening
padding-top: 5px; is pushing down the cursor by 5px...since your are not balancing this padding from bottom cursor is forced to push down!
Apply equal padding on top and bottom like below. Also remove the top:-2px.
#searchText {
text-align: center;
position: relative;
padding-top: 5px;
padding-bottom: 5px;
width: 496px;
font: 2em "Myriad", Helvetica, sans-serif;
line-height: 1;
color: #222222;
border: 1px solid gray;
border-radius: 2em;
}
DEMO
I have this div wedged between two bars(other divs), though when I add text into the equation, the div gets repositioned down. It works as intended without the p element and its children. Here's a fiddle to demonstrate the issue: http://jsfiddle.net/57uSQ/
this is the HTML that is causing the problem:
<p>
<span class="name">DOLCE & GABBANA</span>
</br>
<span class="title">THE ONE</span>
</p>
And the correlating CSS:
.videoDesc {
display: inline-block;
border: 1px solid #000000;
border-right: 0px;
height: 200px;
width: 500px;
}
.videoDesc p {
display: inline-block;
margin: 0;
padding: 0;
}
.videoDesc .name {
display: inline-block;
padding: 0px;
}
.videoDesc .title {
display: inline-block;
padding: 0px;
}
.title {
font-family: Lekton;
font-size: 1.25em;
}
.name {
font-family: Oswald;
font-weight: lighter;
font-size: 2.5em;
letter-spacing: 10px;
padding-left: 5px;
}
You need to add vertical-align:top to .videoDesc:
.videoDesc {
display: inline-block;
border: 1px solid #000000;
border-right: 0px;
height: 200px;
width: 500px;
vertical-align:top;
}
jsFiddle example
The default vertical alignment is baseline, which is causing the behavior you see.