subscript and superscript for the same element - html

Is there any way to add both subscript and and superscript to the same element? If I do
Sample Text<sub>Sub</sub><sup>Sup</sup>
the superscript appears after the subscript. One I idea I had is to do something like:
<table>
<tr>
<td rowspan='2' valign='center'>Sample Text</td>
<td>Sup</td>
</tr>
<tr>
<td>Sub</td>
</tr>
</table>
It seems to do the job but is quite ugly. Any better ideas?
Thanks!

This one is similar to CyberDudes approach, additionally it indents the following text depending on the width of sub and sup:
http://jsfiddle.net/jwFec/1/
Some text <span class="supsub"><sup>sup</sup><sub>sub</sub></span> followed by other text.<br />
<style>
.supsub {
display: inline-block;
}
.supsub sup,
.supsub sub {
position: relative;
display: block;
font-size: .5em;
line-height: 1.2;
}
.supsub sub {
top: .3em;
}
</style>

I'm no CSS guru but you could try something along the lines of http://jsfiddle.net/TKxv8/1/
There are a lot of hardcoded values and the effects on other elements around may only be found afterwards but it's a good place to start.
Sample Text
<span class='supsub'>
<sup class='superscript'>Sup</sup>
<sub class='subscript'>Sub</sub>
</span>
.supsub {position: absolute}
.subscript {color: green; display:block; position:relative; left:2px; top: -5px}
.superscript {color: red; display:block; position:relative; left:2px; top: -5px}

I use the following:
.supsub {
display: inline-flex;
flex-direction: column;
justify-content: space-between;
vertical-align: middle;
}
<span class="supsub">
<span>sup</span>
<span>sub</span>
</span>

Well, you can specify position of sup relative to Sample Text's right border.
http://jsfiddle.net/a754h/

Here's a clean solution. Create two CSS classes:
.nobr {
white-space: nowrap;
}
.supsub {
display: inline-block;
margin: -9em 0;
vertical-align: -0.55em;
line-height: 1.35em;
font-size: 70%;
text-align: left;
}
You might already have the "nobr" class as a <nobr> replacement. Now to express the molecular formula for sulfate, use the "supsub" class as follows:
<span class="nobr">SO<span class="supsub">2-<br />4</span></span>
That is, enclose your superscript/subscript within the "supsub" class, and put a <br /> between them. If you like your superscripts/subscripts a bit larger or smaller, then adjust the font size and then tinker with the vertical-align and line-height. The -9em in the margin setting is to keep the superscripts/subscripts from adding to the height of the line containing them; any big value will do.

On addition to the solution of CyberDude here there is a example that you can do using sup and sub tags and css with flexbox.
<div class="expression">
Sample Text
<span class='supsub'>
<sup class='superscript'>Sup</sup>
<sub class='subscript'>Sub</sub>
</span>
</div>
.expression {
display:flex;
align-items: center;
}
.supsub {
display: flex;
flex-direction: column;
margin-left: 2px;
}
.subscript {
color: green;
display:flex;
}
.superscript {
color: red;
display:flex;
}
You can see and test this here JsFiddle

It can be done very simply by using inline style as well. Specifically: style='position: relative; left: -0.4em;'
So an example would be: u<sub>i</sub><sup style='position: relative; left: -0.4em;'>T</sup>
One can vary the position by changing the value after left. It doesn't reproduce here, but it works fine otherwise.

Related

Text outside of span, offset from span outline

I'm building a "We the People" website section inspired by the U.S. Constitution, and I'm listing names of "signers" supporting my project. The names have different sizes, and I'm trying to get them to show up nicely on the page. Strangely, the font is consistently offset from the spans containing the names. Here's what I have:
As you can see, the names overlap, which I'd like to avoid. What I find odd is that the text is outside of the span outlines:
The same is true of all the names. Here's my HTML:
<div id="names">
<span class="order ten CalifornyaA-Bold" id="o3">Eric So</span>
<span class="order twenty-five CalifornyaB-Bold" id="o5">Sierra Hansen</span>
<span class="order ten CalifornyaB-Bold" id="o6">Eleanor Collier</span>
...
<span class="order twenty-five CalifornyaC-Bold" id="o69">Maeve McCarty</span>
</div>
... and CSS:
#names {
text-align: center;
padding-bottom: 10%;
}
.hancock, .five-hundred, .two-hundred, .one-hundred, .fifty, .twenty-five, .ten {
line-height: 1.5em;
white-space: nowrap;
}
.hancock {
font-size: 5.5em;
}
.five-hundred {
font-size: 4em;
}
.two-hundred {
font-size: 3.4em;
}
.one-hundred {
font-size: 2.8em;
}
.fifty {
font-size: 2.2em;
}
.twenty-five {
font-size: 1.6em;
}
.ten {
font-size: 1em;
}
Any help would be very much appreciated!
its your line-height since they are inline elements an no block elements, try modifying the value:
.hancock, .five-hundred, .two-hundred, .one-hundred, .fifty, .twenty-five, .ten {
line-height: 110%;//percent would be good
white-space: nowrap;
}
or add individual CSS rules for each one, instead of all being set to1.5em
Edit:
Remember to set a font-size to the parent element since you are using em, check how i wrapped a text and added a font-size in em in the page of the font and its not overlapping:
you can use
span {line-height: 1.2;}
It seems that display: inline-block and then setting margin-top works fairly well. I think that's a bit of a hack solution, but it more or less works.

How to place 3 blocks (span, span, input) in one line with vertical align?

I have html something like this http://jsfiddle.net/nLt9unxa/5/ and I want to place 3 block .number__label, .text__label, and .from__input in one line. .form__input must be align to the right side of form and all 3 elements must be vertical align in one line. How to do this? And I don't want use display: table-cell
And also if you know very good tutorial or book about alignment, where described all possible alignment and receipts how to do it, like cheatsheet, please share link.
you forgot to put : after max-width and min-width in .number__label
DEMO
.number__label {
display: inline-block;
background-color: #ffffff;
border: solid 1px;
max-width:20%;
min-width:20%;
}
Use vertical-align: middle (or top, or bottom). Here is the fiddle: http://jsfiddle.net/1ddewjxd/
.class
{
vertical-align: middle;
}
to align elements to right set the parent element to text-align: right, and the child elements to text-align: left. You could also float: right, but that can complicate things.
.item__label {
text-align: right;
}
.number__label, text__label, form__input {
text-align: left;
}
Run this code snippet to check whether all your requirements are done or not? also check fiddle
Check CSS Layout or learn from W3School
form {
width:70%;
background-color: #dddddd;
font-size: 20px;
}
.itme__label {
display: block;
}
.form__item {
display: block;
padding: 3px 5px;
}
.number__label {
display: inline-block;
background-color: #ffffff;
border: solid 1px;
max-width 20%;
min-width 20%;
}
.text__label {
display: inline-block;
background-color: #888888;
max-width: 50%;
}
.form__input {
display: block;
min-width: 20%;
max-width: 20%;
font-size: 1em;
margin-left:120px;
}
<form>
<div class="form__item">
<p>
<label class="item__label">
<span class="number__label">
01 12 31 23 123 2452 34534 5345
</span>
<span class="text__label">
text label long long long very long long for two or more lines ong very long long for two or more linesong very long long for two or more lines
</span>
<input type="text" class="form__input" value="input text">
</input>
</label>
</p>
<div class="errors">
<p class="error">
some error
</p>
</div>
</div>
</form>

How to get text and line in a straight line in html

How can I get a line, text and then a line in a straight line?
code. Here is the jsfiddle of my html. I use inline property to make them appear in a straight line. But they do not change.
How to do so that they appear like
---------------------- About Me ---------------------
(^^dotted line above should actually be single line.)
Use this -
#about_me1 hr, #about_me1 h3{
display: inline-block;
vertical-align: middle;
}
Here's updated Fiddle
Try this and have a look to display:inline-block in style
<header id="about_me">
<div id="about_me1">
<hr size="5" align="left" color="black" style="display:inline-block;width:30%;">
<h3 style="display:inline;">About Me</h3>
<hr id = "line" size="5" align="left" width="30%" color="black" style="display:inline-block;width:30%;">
</div>
</header>
Use only one element to show border, which will work in every resolution and reusable:
<header id="about_me">
<div id="about_me1">
<h3><span>About Me</span></h3>
</div>
</header>
#about_me1 {
border-top: 2px solid #FF0000;
position: relative;
margin-top:15px;
}
h3 {
position: absolute;
top: -18px;
text-align:center;
width:100%;
padding:0;
margin:0px;
}
h3 span {
background:#fff;
display: inline-block;
padding: 5px;
}
Demo

Setting the height of a span element to 100%

I'm currently building a theme / style for a piece of software.
Currently, the code looks like such:
http://jsfiddle.net/afseW/1/
The relevant code is:
body div[type*=privmsg] .sender {
font-weight: 700;
width:134px;
text-shadow: #fff 0px 1px;
background-color: #eee;
min-height:22px;
border-right: 1px solid #dcdcdc;
padding-right:5px;
text-align:right;
display:inline-block;
overflow: auto;
}
Note that in fiddle, for some reason, the text is collapsing onto the second line, whereas in the client, the image looks like this:
Granted, a span is not meant to be a block, hence I've given it the property of: display: inline-block;
But how do I get the height to inherit the parent p block?
I changed DOM structure. See the inline style. In the first div (.message) I prefer a better solution adding a .clearfix class, see this.
<div class="message" type="privmsg" style="overflow: auto;">
<div class="sender-cont" style="width: 30%; float: left;">
<span class="sender" ondblclick="Textual.nicknameDoubleClicked()" oncontextmenu="Textual.openStandardNicknameContextualMenu()" type="myself" nick="shamil" colornumber="20">+shamil</span>
</div>
<div style="width: 70%; float: left;">
Welcome to <span class="channel" ondblclick="Textual.channelNameDoubleClicked()" oncontextmenu="Textual.openChannelNameContextualMenu()">#textual-testing</span>! This channel is for the users of the Textual IRC Client to test scripts and do other activities in an unregulated environment. — <span class="inline_nickname" ondblclick="Textual.inlineNicknameDoubleClicked()" oncontextmenu="Textual.openInlineNicknameContextualMenu()" colornumber="3">milky</span>'s law states: "On IRC, after a user has executed a command that outputs interesting information to a channel (i.e. /sysinfo), then there will be at least two users that do the same."
</div>
</div>
Hope this helps!
Since the spans are a set width, probably the easiest thing to do here is just make the span have a absolute position.
body div[type*=privmsg] .sender,
body div[type*=action] .sender {
position: absolute;
top: 0;
bottom: 0;
left: 0;
...
}
Then add padding to the parent element:
body span.message {
position: relative;
padding-left: 140px;
...
}
http://jsfiddle.net/afseW/3/
PS: please provide a trimmed down version in jsfiddle next time, the html and css here is pretty epic.

Align an anchor to the right

Consider the following:
<a>a</a><a>b</a>
How can I align the second anchor (b) to the right?
PS: float is an abuse in this situation. It's not made for this and it causes some problems, so I need other more reasonable solution.
Just do this:
style="float:right"
Like:
<div>
Equipment
Model
</div>
http://jsfiddle.net/umerqureshi/0jx7kf1L/
You'd need separate containers.
<p>
<span>
<a>Left</a>
</span>
<span class="align-right">
<a>Right</a>
</span>
</p>
p {font-size:0; /* Fixes inline block spacing */ }
span { width:50%; display:inline-block; }
span.align-right { text-align:right; }
span a { font-size:16px; }
JSFiddle example.
Try this CSS,
Using CSS3 nth-child()
a:nth-child(2) {
display: inline-block;
text-align: right;
width: 100%;
}
Demo: http://jsbin.com/opexim/3/edit
Note: nth-child is a CSS3 and won't work on older browsers like IE6, 7 and 8
Support for old browsers
Set class to second <a> anchor element and apply the CSS.
<a>a</a><a class="right">b</a>
a.right {
display: inline-block;
text-align: right;
width: 100%;
}
Maybe you can make something like this: <a>a</a><a class="right">b</a>
And CSS like this:
a.right {
position: absolute;
right: 0;
}
Try and use :nth-child():
a:nth-child(2) {
display: inline-block;
text-align: right;
width: 100%;
}
I don’t know if this works for the older browsers.
Assign a class or id to the 'b' containing anchor and give margin-left:100% to it.
For example:
.second{margin-left:100%;}
or else
a:nth-child(2){margin-left:100%;}
or else
you can also do like mentioned below:
css
a:nth-child(1){display:inline-block;width:50%;text-align:left;float:left;}
a:nth-child(2), .second{display:inline-block;width:50%;text-align:right;}
Working Fiddle
You may try the below code:
<a>a</a><a align="right">b</a>
<a>a</a><a style="text-align:right">b</a>
<div class="mydiv">
<a class ="mylink"> test </a>
</div>
.mydiv {
text-align: left;
}
You must enter your styles for the 'a' tag algin give to 'div'.