I came across this behavior recently when a client reported that some of the buttons on a page had vertically centered text while others did not.
As it turns out, buttons will vertically center text inside them but links won't. Example here: http://jsfiddle.net/valentin/7EjtD/
a, button{
height: 200px;
background-color: #ff6400;
color: #fff;
font-weight: bold;
display: inline-block;
vertical-align: top;
border: 0;
padding: 20px;
font-family: sans-serif;
text-decoration: none;
}
Is there any way to add this behavior to links as well outside of using line-height?
Button aligns to the middle because is its default behavior. Your fiddle is aligning top actually. To make it work you can wrap your elements on an display:table element, like a div. Then set the button and the link to be display:table-cell. Then your vertical-align will work. Like this:
<div class="wrapper">
LINK
<button>BUTTON</button>
</div>
And the css:
*{
box-sizing: border-box;
}
div.wrapper {
display:table;
}
a, button{
height: 200px;
background-color: #ff6400;
color: #fff;
font-weight: bold;
display: table-cell;
vertical-align: middle;
border: 0;
padding: 20px;
font-family: sans-serif;
text-decoration: none;
}
Buttons are inline-block elements, while anchors are just inline. You can use padding to achieve the same effect:
a
{
padding: 91px 20px; /* <---(height-fontSize)/2 */
height: auto;
}
JSFiddle
TableData (TD AKA cell) are pretty damn good at default text centering ;)
live demo
a{
display: table-cell;
vertical-align: middle;
}
for clean-code-sake i'd use a special class like:
a.buttonAlike{
Sorry folks! Didn't see he wanted to avoid line-height. Original post:
Add line-height equal to the height of your element. In this case:
line-height: 200px;
DEMO: JSFIDDLE
Related
I know both the left and right margins will work for display: inline and display-inline: block. But please clarify if top margin and text-align works for any of these. If yes, why?
Browsers treat inline elements differently when it comes to margins and padding. While you can add left and right margins/padding, you can not add them to the top or bottom of the element. This is because inline elements flow with the content on a page, the same way as a link or text. If you were able to set top/bottom margin/padding in inline elements, it would disrupt the flow of content.
As for text-align, this works on both inline-block, and inline elements. I have added a quick code example below showing text-align: center; on both display: inline; and display: inline-block; elements. This example also shows the top and bottom margins working for inline-block, and not working for inline.
.inline-block {
display: inline-block;
margin-top: 30px;
margin-bottom: 10px;
}
.inline {
display: inline;
margin-top: 30px;
margin-bottom: 10px;
}
/* Start demo styles (not required) */
* {
box-sizing: border-box;
}
html,body {
margin:0;
background: #95a5a6;
}
.container {
width:50%;
margin: 50px auto;
background: white;
padding: 10px 20px;
text-align: center;
font-family: arial;
font-size: 16px;
}
hr {
border: none;
height: 2px;
width: 100%;
background: black;
display: block;
}
/* End demo styles */
<div class="container">
<div class="inline-block">
Inline-Block Content
</div>
<hr>
<div class="inline">
Inline Content
</div>
</div>
What would be the easiest way to center align an inline-block element?
Ideally, I don't want to set a width to the elements. This way depending on the text inputted within the elements, the inline-block element will expand to the new width without having to change the width within the CSS. The inline-block elements should be centered on top of one another (not side by side), as well as the text within the element.
See code below or see on jsFiddle.
The current HTML:
<div>
<h2>Hello, John Doe.</h2>
<h2>Welcome and have a wonderful day.</h2>
</div>
The current SCSS:
#import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,600);
body {
margin: 0 auto;
background: rgba(51,51,51,1);
font-family: 'Open Sans', sans-serif;
}
div {
width: 100%;
height: auto;
margin: 15% 0;
text-align: center;
h2 {
margin: 0 auto;
padding: 10px;
text-align: center;
float: left;
clear: left;
display: inline-block;
&:first-child {
color: black;
background: rgba(255,255,255,1);
}
&:last-child {
color: white;
background: rgba(117,80,161,1);
}
}
}
Adding a br between the two elements and taking out the float: left/clear: left may be the easiest way; however, I was curious if there was another way going about this.
Like this? http://jsfiddle.net/bcL023ko/3/
Remove the float:left left and add margin: 0 auto to center the element. Or is it something else that your are looking for?
This is my code: http://jsfiddle.net/spadez/Z3he9/
I've been trying to align the green circle vertically within the white box, but I'm struggling on how it should be approached.
Using vertical-align: center; does nothing if applied to the circle.
Can anyone explain how it should be done please, in the most semantically correct way.
Remove the display:block attribute from the title class and that should do the trick.
vertical-align will not work with floated elements as floats are not within the normal 'flow' of the document. You can use vertical align with inline or inline-block elements.
.title{
display: inline-block;
vertical-align: middle;
}
Remove float: right; from .number.
As said by Dave Mroz, removing the display:block from .title should do the trick for you.
But in order to keep the .box element's original dimensions from you fiddle, you should also clear the floats after .title and .number.
Like this.
You need to change your CSS like this way
.box {
background-color: white;
padding: 30px;
margin-bottom: 30px;
border-radius:4px;
height:30px;
}
.title {color: rgb(15, 15, 15);
font-family: myriad-pro, Helvetica, sans-serif;
font-size: 24px;
margin-bottom: 20px;
display:block;
float:left;
}
Working Fiddle
I have the following CSS Code:
.blackbox {
background: black;
width: 17px;
height: 18px;
line-height: 18px;
text-align: center;
}
In my HTML file, I call the following.
<div class="blackbox">
10
</div>
<img src="icon-local.png">
<img src="icon-national.png">
How can I keep the box that is rendered via CSS call and the images called via img tag on the same horizontal line?
The goal is to create something that looks similar to this, but with the CSS box in front of the other icons. Example Image: Example Image
Thanks in advance!
Ken
You can use display:inline-block or display:table.
<div class="parent">
<div class="blackbox">10</div>
<img src="icon-local.png" />
<img src="icon-national.png" />
</div>
And the css
.parent div, .parent img {
display:inline-block;
*display:inline; /* IE7 hack */
zoom:1 /* IE7 */
vertical-align:middle;
}
Make your blackbox class inline or inline-block:
.blackbox {
display:inline-block;
background: black;
width: 17px;
height: 18px;
line-height: 18px;
text-align: center;
}
The default display for <div> elements are "block" (that is, it adds a line break before and after it) so you could just use:
.blackbox {
background: black;
width: 17px;
height: 18px;
line-height: 18px;
text-align: center;
display: inline;
}
Notice the display: inline;
Also, display: inline-block; is also useful in many scenarios.
You need to define the div as display:inline-block and float your images to the left so that you have the same result as your example image. Floating your images can be avoided if you change your HTML and have the images before the div. See this demo: http://jsfiddle.net/G5Q4k/1/
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/