I've been battling this a few hours. The text in this span is mysteriously aligned to the top of the span. Here is a screenshot from Firebug:
And here are my related CSS blocks:
.skills {
overflow:hidden;
height:100%;
}
.skills li{
border-bottom:1px dotted black;
position:relative;
width:200px;
height:18px;
margin-left:13px;
}
.skills li span{
display:inline-block;
position:relative;
background:white;
bottom:0px;
height:100%;
padding:0 5px;
}
Here is the HTML:
<h4 class="main-heading"><span>Data Exchange</span></h4>
<ul class="skills">
<li>
<span>SOAP/Axis2</h4>
</li>
</ul>
Can you tell why this is aligned to the top? I want it in the center.
And here is the jsFiddle, where the same code results it in text being in the center. Does that mean that CSS elements higher in the hierarchy may be causing it?
...where the same code results it in text being in the center. Does
that mean that CSS elements higher in the hierarchy may be causing it?
I imagine that an ancestor in your actual stylesheet has the line-height set to less than 18px. You can look at the calculated line height for that element in your actual stylesheet to see what value was being applied.
The default value for line-height is roughly 1.2x (depends on browser).
Set the line-height to be equal to the non-padded height of the containing element to vertically align a single line of text (in this case, 18px).
Example fiddle: http://jsfiddle.net/4vq42/
No line-height. Make it the same as the height, either 18px or 100%.
.skills li span{
display:inline-block;
position:relative;
background:white;
bottom:0px;
height:100%;
line-height:18px;
padding:0 5px;
}
Try adding line-height: 18px to .skills li span CSS.
Edit: Just realised Tim Medora already said this. Ignore me.
Setting line-height to the value of your element's height is the simplest way to vertically align text.
.skills li {
height:18px;
line-height:18px;
}
Related
I'm building a menu where every dropdown item has the width of its content. To manage that I had to make the items inline-blocks, but that means they all end up on the same line. Is there a way to make them all on their line without using margins, OR is there a way to have blocks have the width of their content?
Thanks
sample menu item HTML:
<div class="menuItemWrap">
resources
<div class="menuItemContentWrap">
articles
web reviews
bookstore
powerpoint
schwab forms
blunck forms
</div>
</div>
CSS:
.menuItemWrap{
display:inline-block;
float:left;
}
.menuItemHead{
padding:14px 22px;
margin: 0px;
border:0px;
background:#2389DD;
}
.menuItemContentWrap{
position:absolute;
display:none;
width:100px;
background:#A6CFD1;
}
.menuItemContentItem{
display:block;
padding: 8px 14px;
background: red;
color:#333;
position:relative;
float:left;
}
float removes the width and height of an element. In order to make the elements appear on separate line use the clear property:
float: left;
clear: left;
Quote from MDN:
The clear CSS property specifies whether an element can be next to
floating elements that precede it or must be moved down (cleared)
below them.
Demo
I have the following layout:
<ul>
<li>Text
<div class="task"></div>
</li>
</ul>
See fiddle. I am having problems aligning the text Text. Now it is on the same line as the div. I would like to align (vertically) the Text to the middle of the div. I have tried nesting the Text inside a span and giving the span margin-bottom, padding-bottom, but nothing seems to work.
Beware of spacing issues with inline-block - http://robertnyman.com/2010/02/24/css-display-inline-block-why-it-rocks-and-why-it-sucks/
http://jsfiddle.net/6AxbH/1/
.task {
display:inline-block;
vertical-align: middle;
}
.task {
height:20px;
width:20px;
background-color:red;
display:inline-block;
text-align:center;
float:none;
}
ul li {
text-align:center;
width:50px;
display:block;
list-style:none;
}
I just set a width on the LI to match what the size of the box was (then reduced the size of the box to make sure it centered) and applied display:block;text-align:center; to center everything.
I am making a navigation bar and I want all of the links in it to be 30px high. When I set this - height:20px; (20 pixels because there is 5px padding all around) - the height just stays normal. My full code for the a's is:
#header a {
height:20px;
background-color:#666666;
color:white;
text-decoration:none;
border:none;
padding:5px;
}
#header a:hover {
background-color:#CCCCCC;
color:black;
}
JSFiddle: http://jsfiddle.net/Lcupj/
The height property does not apply to elements that are display: inline and <a> elements are display: inline by default.
Set a different value for the display property (such as inline-block) or use line-height instead.
You can also use a padding on A tag, and use line-height either.
example :
a {
padding : 9px;
line-height : 20px;
}
Bye.
height won't apply to inline blocks.
Use display: inline-block in your CSS to make the element behave like an inline element but with some of the block element properties.
use display: inline-block;
#header a {
height:30px;
background-color:#666666;
color:white;
text-decoration:none;
border:none;
padding:5px;
display: inline-block;
}
DEMO
you can also try the following
#header a {
position:absolute;
margin:0;
padding:0;
height:20px;
background-color:#666666;
color:white;
text-decoration:none;
border:none;
padding:5px;
}
sometimes you have to manually set the height or padding of a given element, or the element would be squashed to fit inside the other other element. I also use position:absolute this gives 100% control over your elements position, size etc.
http://jsfiddle.net/Lcupj/
A is an inline element, you can't use height on it. If you want to do so you must first change it into a block element. You can do so with display: block; however this will put your element on a new "line" on your screen, if you want it to stay like an inline element, you should use display: inline-block; Or you could use line-height.
Add to the a rule display:inline-block in CSS
basicly I have li elements which contain a small icon an a text, i want to horizontally and vertically center those elements together. However i dont get it working, since Im not really experienced in CSS.
I already managed to vertically center the elements using two spans and line-height. But Im failing with the horizontal centering, since text align doesnt work for spans, and float middle doesnt exist.
You guys have some ideas??
here my code:
HTML:
echo "<li style=\"background-color:#f7f7f7; color:#2EA620;\"><span class=\"image\"><img src=\"Images/List/Teams/" . $temp . ".png\" /></span><span class=\"text\">".$obj_teams->Name."</span></li>";
CSS:
li {
width:173px;
height:30px;
line-height:30px;
font:"Myriad Pro";
font-size:14px;
padding-left:10px;
padding-right:10px;
border-bottom:1px solid;
border-left:1px solid;
border-right:1px solid;
border-color:#CCC;
}
.image {
float:left;
text
}
.text {
float:left;
color:#2EA620;
line-height:30px;
}
Here an Image of how it looks like now. (If marked the elements which i want to center)
You could use a text-align rule:
li.center {
text-align: center;
}
Take a look at this fiddle for a live example:
http://jsfiddle.net/fX9jp/1/
Your specific problem is that you are floating the image and span left but then want them centered. Remove the float left on both then you can put text-align: center on your li elements.
li {
width:173px;
height:30px;
line-height:30px;
font:"Myriad Pro";
font-size:14px;
padding-left:10px;
padding-right:10px;
border-bottom:1px solid;
border-left:1px solid;
border-right:1px solid;
border-color:#CCC;
text-align:center;
}
.text {
color:#2EA620;
line-height:30px;
}
Try applying text-align: center; to the parent (or any ancestor) element of your span since a span is an inline element (assuming .text is your span).
Or try making the span a block element and setting its left and right margins to auto:
.text {
display: block;
margin: 0 auto;
}
Inline elements like spans respond to text-align on their ancestor elements, whereas block elements like divs respond to setting margins to auto. Note that text contained inside of an element will respond to text-align that is placed on that element (since the text is technically a "text node" inside of that element).
http://jsfiddle.net/PZ5AZ/
Please advise me what to do to make text Send Vertical align middle .also please advise that these problems not came in future what can i do ?
As has been previously said vertical alignment is not really supported on anything that isn't a table cell.
But if you are just trying to center a single line of text you could use line-height. If you set the line-height to the same as the height of the element and remove any padding then the text will display in the middle of the element, just as if it is vertically aligned.
So on your example the following would work (if you remove the default styles first):
line-height:28px;
height:28px;
padding:0px;
But if the text wraps to more than one line this solution won't work, the text will suddenly become very spaced.
For a more general solution it is best to use javascript to dynamically work out the padding required for the particular element.
You can't vertically align text outside of tables so there are two options:
You play with the padding of the parent element to achieve the illusion of v-aligned text. As illustrated by Mr Long.
or
You make the parent element position:relative; and the child element absolute:
<div id='container'>
<div id='txt'>My Text</div>
</div>
#container{
position:relative;
}
#txt{
position:absolute; left:0px; top:50%;
margin-top:10px; /* half the height of the text element */
}
/* hint: for scaling attributes use %'s */
I think the first option is the simplest in your case.
Good luck Bro!
W.
if you like to center the text inside the div vertically and perhaps horizontally you can try this
#container{
position:relative;
width:200px;
height:300px;
border:1px solid #CCCCCC;
}
#txt{
position:absolute;
width:150px;
height:50px;
top:50%; left:50%;
margin-top:-25px; /* 1/2 of height */
margin-left:-75px;/* 1/2 of width */
border:1px solid #FF0000;
}
<div id="container">
<div id="txt">My Text</div>
</div>
Try this: padding: 0px 0px 4px 0px;
Add this to clear default button padding in Mozilla:
button::-moz-focus-inner {
border:0;
padding-top:0;
}