Picture:
.left {
border: 1px solid red;
}
textarea {
border: 1px solid blue;
}
.parent {
border: 1px solid green;
}
<div class='parent'>
<span class='left'>
<span>one</span>
<span>two</span>
</span>
<textarea></textarea>
</div>
Codepen
Why is my textarea higher up than its neighbor?
It's not.
Let me explain.
First, a bit of background:
Your span and textarea elements are (by default) inline elements.
Browsers normally provide a little bit of whitespace underneath inline elements for descenders.
To understand descenders...
Look at this line of text. Notice there are no letters that breach the baseline.
Now look at the following sentence:
By just crossing the bridge he probably got away.
Note the letters "y", "j", "p" and "g". These letters breach the baseline and are known in typography as "descenders".
[
Source: Wikipedia.org
The gap you're seeing is not margin or padding, but simply the browser providing room to accommodate these lowercase letters. In short, this is called baseline alignment.
baseline
The line upon which most letters "sit" and below which descenders extend.
[
Source: Wikipedia.org
So why, somebody might ask, does the browser provide this space for textarea, img, input and other inline elements, that don't need space for descenders?
Because the browser adjusts for the possibility that you may have text before or after one of those elements on the same line.
Now, to your image and code...
On first glance it clearly appears that the textarea is higher up than the span element. But if you take a closer look...
...you'll see they are perfectly aligned. Both span and textarea are providing space for descenders.
The borders you've added contribute to the appearance of misalignment because the textarea border wraps a clearly delineated box while excluding descender space, but the span border wraps the text and the descender space. If you remove the red border the misalignment is less pronounced.
In terms of a solution, here are two options:
Add vertical-align: bottom to the textarea rule, OR
Add display: block to the textarea rule.
Adam,
If you add the following to your existing css, you should get your desired results.
.left{
display:inline-block;
vertical-align: text-bottom;
}
textarea{
margin:0px;
vertical-align: text-bottom;
}
You can see a working example at the following url: https://jsfiddle.net/YOOOEE/z8pwpms6/
If you have two span elements, the high will be the same. Spans have display:inline; by default.
<span class="left">
<span>one</span>
<span>two</span>
</span>
<span class="right">
<span>one</span>
<span>two</span>
</span>
All browsers have defaults styles for textareas:
textarea {
-webkit-appearance: textarea;
background-color: white;
border: 1px solid;
border-image-source: initial;
border-image-slice: initial;
border-image-width: initial;
border-image-outset: initial;
border-image-repeat: initial;
-webkit-rtl-ordering: logical;
-webkit-user-select: text;
flex-direction: column;
resize: auto;
cursor: auto;
padding: 2px;
white-space: pre-wrap;
word-wrap: break-word;
}
input, textarea, keygen, select, button {
margin: 0em;
font: 13.3333px Arial;
text-rendering: auto;
color: initial;
letter-spacing: normal;
word-spacing: normal;
text-transform: none;
text-indent: 0px;
text-shadow: none;
display: inline-block;
text-align: start;
}
My solution:
.parent {
border: 1px solid green;
display: flex;
}
Related
I have a problem with text selection in Chrome. I have two spans styled as inline-blocks (same happens with divs). When I try to double click text inside one of the blocks all neighbor blocks are selected.
It can be solved by putting at least one space or newline between blocks. But that space will become visible and will break layout.
Demonstration (in Chrome 58):
Firefox works fine for both cases.
How to solve it without making mess out of the markup?
Source code:
span {
outline: 1px solid red;
display: inline-block;
min-width: 70px;
}
<span>Apple</span><span>Orange</span>
<br/>
<br/>
<span>Lemon</span> <span>Pear</span>
Instead of a normal space, you can use a Zero-width space:
Edit: ..or an element with font-size: 0 containing a normal space.
span {
outline: 1px solid red;
display: inline-block;
min-width: 70px;
}
<span>Apple</span><span>Orange</span>
<br/>
<br/>
<span>Lemon</span><span>Pear</span>
<br/>
<br/>
<span>Lemon2</span><i style="font-size:0;"> </i><span>Pear2</span>
I think I got it....
try adding this:
user-select: all;
so it would be this:
span {
outline: 1px solid red;
display: inline-block;
min-width: 70px;
user-select: all;
}
<span>Apple</span><span>Orange</span>
<br/>
<br/>
<span>Lemon</span> <span>Pear</span>
span {
outline: 1px solid red;
display: inline-block;
min-width: 70px;
}
<span>Apple</span><span>Orange</span>
<br/>
<span>Lemon</span> <span>Pear</span>
Lately I've been working with Japanese text, and I've found a rather annoying property. In Japanese, unlike English, glyphs do not extend below the text baseline. This example should show what I mean:
div {
font-size: 72pt;
display: inline-block;
text-decoration: underline;
border: 1px solid red;
margin: 10px;
text-align: center;
}
<div lang="ja">日本語</div>
<div lang="en">English</div>
Notice how the "g" in "English" extends below the underline, but none of the characters in 日本語 do. This is typical of Japanese text. Despite this, space is still reserved below the underline, and in fact on my screen the Japanese text reserves more space than the English text does. My question is this:
Is there a way to remove this space with CSS which is reliable across changing fonts and font sizes? I can see at least two possible approaches:
Remove the space below the baseline directly.
Move the baseline to be at the bottom of the containing box.
You need to reset the line-height so it's not bigger than 1. The initial value is normal which depends on the User Agent of the browser, on the font-family and on the font-size, but it's some number between 1 and 1.2 in general. Here's more information if you're interested.
div {
font-size: 72pt;
display: inline-block;
text-decoration: underline;
border: 1px solid red;
margin: 10px;
text-align: center;
line-height: 1;
}
<div lang="ja">日本語</div>
<div lang="en">English</div>
Just set the line height to the same size as the font size: line-height: 72pt. This normalizes the space that's taken for the font height. Of course you can take every value for the line height that you like, to adjust that space. More information to line-height at MDN.
div {
font-size: 72pt;
line-height: 72pt;
display: inline-block;
text-decoration: underline;
border: 1px solid red;
margin: 10px;
text-align: center;
}
<div lang="ja">日本語</div>
<div lang="en">English</div>
I noticed this issue caused by localisation typo - there was a space left in the end of the translation, which caused an unexpected effect. I've reproduced it in the following fiddle:
<style>
.label {
display: inline;
padding: .2em .6em .3em;
font-size: 75%;
font-weight: bold;
line-height: 1;
color: #ffffff;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
border-radius: .25em;
background-color: #d9534f;
text-transform: uppercase;
}
</style>
<div>
<p>Label with last character space</p>
<span class="label">Label </span>
<span>some text</span>
</div>
<div>
<p>Label without space</p>
<span class="label">Label</span>
<span>some text</span>
</div>
The label class is taken from the twitter bootstrap version 3.1.1.
Could you please help me to understand why span with a last character space sticks to the next one, but when space removed from inside span it is back to normal?
In HTML more white-space characters (\t, \n, , etc.) are replaced by a space (exactly one space). In the first code this space is inside span and there is no reason to render another one space after span.
In the second case, space is rendered correctly after span, because is the first.
Notice that the width of the label is increased when you have introduced the blank space for last character.
Yes, I see weird space, but I have added inline-block to the label. Now you don't see that weird space .
.label {
display: inline-block;
padding: .2em .6em .3em;
font-size: 75%;
font-weight: bold;
line-height: 1;
color: #ffffff;
text-align: center;
white-space: nowrap;
vertical-align: baseline;
border-radius: .25em;
background-color: #d9534f;
text-transform: uppercase;
}
http://jsfiddle.net/c8w6ex37/3/
I have a toolbar with buttons, some have text in them and others don't. For some reason I don't really understand, they don't vertically align.
Why? and how to fix it?
#tools{
border: 1px solid black;
}
#tools button{
border-width: 2px;
border-style: outset;
border-color: #ccc;
height: 24px;
width: 24px;
font-size: 9pt;
}
#tools button:active{
border-style: inset;
}
button.Bl{
font-weight: bold;
}
button.Bo{
font-style: italic;
}
button.B4{
background-color: #A00;
text-shadow: 0.15em 0.15em #2A0000;
}
button.Bc{
background-color: #F55;
text-shadow: 0.15em 0.15em #3F1515;
}
<div id="tools">
<button class="B4 pallete" title="§4 Dark Red"></button>
<button class="Bc pallete" title="§c Red"></button>
<button class="Bl pallete" title="§r Bold">B</button>
<button class="Bo pallete" title="§r Italic">I</button>
</div>
http://jsfiddle.net/hjo41wm2/
Vertical alignment of inline elements - why this works the way it does.
Suppose that we have the following HTML (similar to the above):
<div id="tools">
<button class="ExA pallete" title="Example Auto">E</button>
<button class="Ex0 pallete" title="Example Zero">E</button>
<button class="B4 pallete" title="§4 Dark Red"></button>
<button class="Bc pallete" title="§c Red"></button>
<button class="Bl pallete" title="§r Bold">B</button>
<button class="Bo pallete" title="§r Italic">I</button>
</div>
I added two more buttons to illustrate a few concepts.
Let's look at the following CSS rules:
#tools{ border: 1px solid black; }
button{
border-width: 2px;
border-style: outset;
border-color: #ccc;
height: 48px;
width: 48px;
font-size: 24pt;
vertical-align: baseline;
}
button:active{
border-style: inset;
}
button.Bl { font-weight: bold; }
button.Bo { font-style: italic; }
button.B4{
background-color: #A00;
text-shadow: 0.15em 0.15em #2A0000;
}
button.Bc{
background-color: #F55;
text-shadow: 0.15em 0.15em #3F1515;
height: auto;
}
button.ExA {}
button.Ex0 {
height: auto;
font-size: 0;
}
Here we have six inline elements, all buttons, forming a line box, shown below:
The browser will compute a height for each inline element and then use the vertical alignment property (baseline by default) to align them with respect to each other.
In the case of the first two boxes and the last two boxes, there is a character
content with a specified font-size, 24pt in this example (exept for the 0pt one, which I will explain shortly).
In this case, the 1st, 5th and 6th boxes behave as expected, the three letters are
aligned vertically to a common baseline.
The 3rd and 4th buttons do not have a character, so the height of the inline box
computes to zero (line-height only applies to text). In the 3rd button, the button
has a fixed height so the browser vertically aligns the element to the baseline such
that the half the height is above the baseline and half below. This is more obvious
if you set height: auto for the 4th button, which will shrink the element to
zero height (except for the borders) and we see that the 0+margin element aligns
with the common baseline.
To confirm the behavior, consider the 2nd button, which has a character, and height: auto
and font-size: 0. In this case, the zero font-size forces the inline box height to
shrink to zero, and the height shrinks to zero (and border widths).
So, a button with no text is equivalent to a button with text displayed with a zero
font height.
All of this behavior is implied by the CSS specification:
http://www.w3.org/TR/CSS2/visudet.html#line-height
You need to read the sections carefully to tease out the implications.
See demo at: http://jsfiddle.net/audetwebdesign/0jm8th00/
Add this two lines to #tools button
display: inline-block;
vertical-align: bottom;
In CSS:
Add vertical-align:top; inside #tools button{ }
I don't know why but a quick fix is to insert a non-breaking space:
<button class="B4 pallete" title="Dark Red"> </button>
although the CSS solutions would be the recommended way to go.
I'm over my head trying to figure out this one... I'm sure there's something to do with line-height, just couldn't figure it out yet.
Anyway, based on the solution proposed by RedEyedMonster, I've came up with a css approach to identify any empty content button, and add a white space inside it... It fixes the issue:
button:empty:after {
content: "\0000a0";
}
Updated Fiddle
EDIT
This discussion on Google Forum seems to be dealing with extra padding on button tag elements...
In above attachment, there is problem with last span(last word- "for"); it is actually going out of my gray region(this is actually div tag with class textframe).
I am really not getting the problem. The html code is also shown below. The borders for the <span> are shown because I just want to ask that, there is some gap present between two <span>, is it the reason which causing the last word problem? Then how to reduce this gap?
<div style="left:0pt; top:0pt; width:612pt; height:792pt; position:absolute;" class="page">
<div style="left:103.2pt; top:189pt; width:396pt; height:156pt; position:absolute; background-color:rgb(191,191,191); overflow:hidden; border:0px Solid rgb(0,0,0);" class="textFrame">
<p style="text-align: justify; padding-top: 0pt; padding-bottom: 0pt;">
<span class="ln" style="height: 14.4pt; width: 396pt;">
<span style="font-size: 12pt; font-family: F0; word-spacing: 45.9943pt; width: 197.802pt; letter-spacing: 0em; text-align: left; color: rgb(0, 0, 0); border: 0.1px solid red; display: inline-block;">Subject to the</span>
<span style="font-size: 24pt; font-family: F0; width: 64.9904pt; letter-spacing: 0em; text-align: left; color: rgb(0, 0, 0); border: 0.1px solid green; display: inline-block;">license</span>
<span style="font-size: 12pt; font-family: F0; word-spacing: 45.9943pt; width: 133.208pt; letter-spacing: 0em; text-align: right; color: rgb(0, 0, 0); border: 0.1px solid gold; display: inline-block;">terms for</span>
</span>
</p>
</div>
</div>
You have white-space: nowrap; set for span.ln this will stop it from wrapping when the content gets to the end of its parent element. It would stick out of the parent unless the parent has overflow: hidden; set, in which case the text would be obscured. Remove the white-space: nowrap; rule to allow the spans to wrap. You may also get weird layout issues as the inner spans are display: inline-block; which means they appear as a block element within some inline content, you may want to remove this too.
Unless the spans are actually next to each other in the source any white space will be rendered as a space between them whilst they are either display: inline; which is the default display for span elements or display: inline-block; as the inner spans are set up in this example (the style is visible in the style attribute of the element). To get rid of the space between elements with display: inline-block; set you need to remove the white-space (spaces, tabs and new lines) between the spans in the source.
Personally the thing that worked for me ,
I gave the parent element a "word-wrap: break-word" and the specified the width for the child span element.