I previously asked this qn:How to represent fractions in html. It was about writing the numerator and denominator in html and the solution provided allows me to write fraction in the proper format.
However, I can't write text beside it. If I write text beside it, it appears together at the top or bottom. It appears below it. I want the text to appear beside it.
The code is here:
.fraction {
position: relative;
width: 1em;
font-size: 2em;
}
.numerator {
border-bottom: 2px solid black;
text-align: center;
}
.denominator {
border-right: 2px solid black;
width: 0.75em;
text-align: center;
}
<div class="fraction">
<div class="numerator">3</div>
<div class="denominator">8</div>
</div>
The text appears like this:
Is there any solution? Any solution?
Set .fraction class display property as inline-block.
I guess you also will have to fiddle with its vertical-align prop.
add these css -
.fraction {
display: inline-block;
vertical-align: top;
}
working fiddle - http://jsfiddle.net/et2Lan5k/
Add div word and set CSS
CSS:
.fraction {
position: relative;
width: 1em;
font-size: 2em;
}
.numerator {
border-bottom: 2px solid black;
text-align: center;
}
.denominator {
width: 0.75em;
text-align: center;
}
.word{
position:absolute;
top:0px;
line-height:2.5em;
left:1em;
}
HTML:
<div class="fraction">
<div class="numerator">3</div>
<div class="denominator">8</div>
<div class="word">HI</div>
</div>
https://jsfiddle.net/e9nc6gna/1/
This can be achieved with just a few lines of code :
Wrap the numerator and denominator within <span> tags.
Wrap them again within a container <span>.
Then declare the container inline-block, so that it stays on a single line.
Declare the vertical-align: middle property for the container.
Add a border at the bottom of the numerator (or at the top of the denominator) so that it looks like a fraction.
Use this code :
* {
font-size: 50px;
}
.container {
vertical-align: middle;
display: inline-block;
}
.numerator {
border-bottom: 1px solid;
}
33<span class="container"><span class="numerator">1</span><br><span>3</span></span>
Also, do not put line breaks in between these tags, otherwise, there would be unnecessary spaces within the fraction, since the <span> tags are declared as inline elements.
Use this code if you wish.
<style type="text/css">
frac {
display: inline-block;
position: relative;
vertical-align: middle;
letter-spacing: 0.001em;
text-align: center;
}
frac num { /*Numerator*/
display: block;
padding: 0.01em;
}
frac den { /*Denominator*/
border-top: thin solid black;
/* Above line is for the division line. */
}
</style>
<p>33<frac><num>1</num><den>3</den></frac> sss</p>
If you want to change font-size, then add a font-size tag inside the frac tag.
Related
I have one div inside that i have one label. But I am getting some space above the div. I can't remove that space any how.
Don't know what happens there.
Question: Why I am getting that space above div? How can I remove that space?
My code:
For display issue proper I had put color and border.
* {
padding: 0;
margin: 0;
}
body {
background: green;
}
label {
border: 1px solid black;
font-size: 10px;
}
<div>
<label>Some text</label>
</div>
I have tried many things but, didn't get any solution.
label is a inline element therefore add display:inline-block/block or vertical-align:top
* {
padding: 0;
margin: 0;
}
div {
font-size: 0;
/* fix inline-block gap - not needed when using block only*/
background: red
}
label {
border: 1px solid black;
font-size: 10px;
}
.l1 {
display: inline-block
}
.l2 {
display: block;
width: 9%
}
.l3 {
vertical-align: top;
}
<div>
<label class="l1">Some text</label>
</div>
<div>
<label class="l2">Some text 2</label>
</div>
<div>
<label class="l3">Some text 3</label>
</div>
Try to add vertical-align: top; for your label
* {
padding: 0;
margin: 0;
}
body {
background: green;
}
label {
vertical-align: top;
border: 1px solid black;
font-size: 10px;
}
<div>
<label>Some text</label>
</div>
This behaviour is caused because the div element has a browser-dependent default size applied to it. When it contains an element with a smaller font-size, the contained element will be placed on the baseline of the div which results in the space. To solve it, add a matching font size to the div:
div {
font-size: 10px;
}
It is coming because of the default line-height of the labels. You need to use reset.css or similar (like normalize.css) to clear the browsers default styling.
Here are some helpful reference, download and simply add them above your style sheet.
http://meyerweb.com/eric/tools/css/reset/reset.css
or
https://necolas.github.io/normalize.css/3.0.3/normalize.css
Looks like the line-height in the div is too high.
This does the trick:
div {
line-height: 10px;
}
(Of course you should reference the div with a class or id).
Inline element like span also behaves the same.
Add a float:left for the label
* {
padding: 0;
margin: 0;
}
body {
background: green;
}
label {
border: 1px solid black;
font-size: 10px;
float: left;
}
<div>
<label>Some text</label>
</div>
I'm having trouble keeping two nested inline-blocks aligned without specifying widths. I can get the behavior I want using tables but would prefer to use simpler markup. Here's the basic markup:
<div class="error">
<i></i>
<div class="message">Ruh oh</div>
</div>
Here is the basic css:
.error {
border: 2px solid red;
padding: 8px 10px;
}
i {
display: inline-block;
width: 45px;
vertical-align: middle;
}
.message {
display: inline-block;
vertical-align: middle;
}
Here are the requirements:
.error can be any width (usually 100%)
i will be fixed width (usually 45px)
.message will fill the remaining width of the parent .error
both i and .message will be vertically aligned in the middle
.message cannot wrap under i
no javascript
Here is a fiddle showing a good line (short error), a bad line (longer error messages wrap below the i) and a working example with tables (but I don't want tables). Please enlighten me!
http://jsfiddle.net/3m2db1hw/
you need to use display:table to parent Div and display:table-cell to children i.e. <i> and <div>
.error {
border: 2px solid red;
padding: 8px 10px;
display:table;
}
i {
width: 45px;
vertical-align: middle;
display:table-cell !important;
}
.message {
display: table-cell;
vertical-align: middle;
}
here is edited jsfiddle
http://jsfiddle.net/3m2db1hw/1/
Check out this fiddle
http://jsfiddle.net/9S4zc/2/
How come this looks different in firefox vs chrome (the text is not aligned the same)
How do I ge the text in the inner:before element to be vertically aligned, preferably without line-height?
The dom looks like
<div class="middle">
<div class="inner"> Small text </div>
</div>
The css looks like
.middle {
display: inline-block;
border: 1px solid black;
height: 150px;
vertical-align:middle;
}
.middle:before {
content: '';
height: 100%;
display: inline-block;
vertical-align:middle;
}
.inner {
display:inline-block;
vertical-align: middle;
font-size: 25px;
/* height: 30px; */
text-align:center;
}
.inner:before {
content: "Big Text";
font-size: 50px;
display:inline-block;
margin-right: 20px;
vertical-align: middle;
border: 1px solid red;
height: 90px;
}
The inner:before pseudo element is same as using a DIV element with display: inline-block; property.
Generally in CSS we use display: table-cell; property to have the content aligned vertically center or the CSS Flex-box for modern browsers, but in this case the display property is set to inline-block, which leaves no choice other than line-height(which you don't want to use) or some other hacks to push the content in middle.
As of my understanding, there is no another option for this. I am curious to know if anyone has a better explanation.
Although you prefer not to use line-height it seems to be the best solution here.
Just replace height: 90px; with line-height: 90px; and everything is vertically centered. (Unless of coarse the big text is multi-line text - in which case line-height won't work)
UPDATED FIDDLE
.inner:before {
content: "Big Text";
font-size: 50px;
display:inline-block;
margin-right: 20px;
vertical-align: middle;
border: 1px solid red;
line-height: 90px; /* <--- */
}
Problem
So I'm creating a simple navigation menu which contains a div of a tags. Currently it looks like this:
The follow are my HTML and CSS:
HTML
<div id="tabcontent-container">
<div class="tabcontent-menu">
WLAN Jumpstart
Mobility
Guest Access Jumpstart
</div>
</div>
The CSS
#tabcontent-container { padding: 15px 0px; position: relative; text-align: center; border-radius: 25px; -webkit-border-radius: 25px; }
.tabcontent-menu {}
.tabcontent-menu a { text-decoration: none; color: white; font-size: 30px; border-right: 1px solid white; line-height: 33px; padding: 0 22px; display: inline-block; width: 200px; height: 70px; vertical-align: top; }
.tabcontent-menu a:last-child { border:none; }
.tabcontent-menu a:hover { color:#000; }
Working example on Jsfiddle.net
The Question
I'm wondering if there is an easier way to align the middle "Mobility" a tag to the middle. The other two links look fine because they are double line. I purposely made them double line for a reason, and now just need the middle one to middle align some how.
Any suggestions?
You can use vertical-align: middle to adjust the position vertically. Since that only works on table cells, set display: table-cell for the .tabcontent-menu a
http://jsfiddle.net/H9VHs/8/
I usually accomplish something like this by varying the line-height.
.tabcontent-menu a.midline {
line-height: 64px;
}
See it here: http://jsfiddle.net/PZVnq/
Documentation/Further Reading
CSS line-height on MDN - https://developer.mozilla.org/en/CSS/line-height
Lauri Raittilan on Vertical centering with CSS - http://www.student.oulu.fi/~laurirai/www/css/middle/
Vertical centering with CSS on vanseodesign.com - http://www.vanseodesign.com/css/vertical-centering/
I am trying to center align an image that is wrapped in a <span>, but I am having trouble doing so. I have uploaded my CSS and HTML to jsfiddle: http://jsfiddle.net/7nHhu/1/
I am trying to get the image to center align itself with the content in a "block" style (ie. all text above and below it, not wrapped to the left or right)
Any help would be greatly appreciated.
.imgframe {
border: 1px solid #EAEAEA;
display: inline-block;
margin: 8px;
}
.imgframe img {
border: 1px solid #FFFFFF;
margin: 0;
background: #F6F6F6;
padding: 8px;
-moz-box-shadow: 2px 2px 5px #CCCCCC;
-webkit-box-shadow: 2px 2px 5px #CCCCCC;
}
<span class="imgframe centerimg"><img src="http://i48.tinypic.com/31368e9.jpg" /></span>
I think it's more appropriate to use text-align for centering text rather than images. You could center an image by setting left and right margin auto.
img {
display: block;
margin-left: auto;
margin-right: auto;
height: auto;
padding-top: 10px; //margin-top doesn't work
}
Demo
Just make image wrapper block level element and text-align:center; it.
FIDDLE
or wrap it in another element if needed;
FIDDLE
In .imgframe, add width: 100%;
Given your requirements, to keep the .imgframe element in-line, to avoid it taking up the full width of the enclosing element, and working without adding wrapping elements to your mark-up, the following works:
body {
text-align: center;
}
body p {
text-align: left;
}
JS Fiddle demo.
This would, probably, be less intrusive if you had the elements from your Fiddle wrapped in a specific, target-able, element; rather than the body, as the method, above, requires you to reset the text-align for all elements contained within the body. So, personally, I'd use:
<div id="contentWrapper">
<p>...</p>
<span class="imgframe">
<img src="..." />
</span>
<p>...</p>
</div>
And:
#contentWrapper {
text-align: center;
}
#contentWrapper p {
text-align: left;
}
Just in order to minimise the amount of work required to tidy up afterwards.
span {position: absolute; top:0; left: 0; width: 100%; text-align: center;}
img {width:yourimagewidth; heigth: width:yourimageheigth}