If you are working with an image <img>
can use the vertical-align:middle
, but in this case you need to pay attention to the line-height
of the element containing the image, it must have the same element size.
*Image-oriented
HTML
<div id="container">
<img src="http://a.disquscdn.com/next/7b3d8738/assets/img/noavatar92.png" alt="avatar disqus">
</div>
CSS
#container{
width: 200px;
height: 200px;
line-height: 200px;
text-align: center;
background: #f00;
}
#container img{
vertical-align: middle;
}
see for jsfiddle
*Method with knowledge of height
If you have the possibility to define a height
for this element can do using the code quoted in an article of Smashing Magazine
HTML
<div>
magix!
</div>
CSS
div{
background: red;
bottom: 0;
height: 100px;
left: 0;
margin: auto;
position: absolute;
top: 0;
right: 0;
width: 100px;
}
Javascript solution with jQuery
Now if your focus is another element you can use jQuery, using logic you will leave the distance of your element with 50% of the edges, then control the margin
.
see for jsfiddle
Semi-duplicate? Horizontally it is easy, vertically see http://answall.com/questions/141/howto center vertically, see vertically
– bfavaretto
It’s not perfectly clear what you’re asking! There is a universe of ways to center elements on the screen via CSS, but each way is considered an appropriate solution to a specific problem. The way you’re asking the question, there’s no assertive answer.
– Zuul
@Zuul who knows is worth giving a general in the forms and their weaknesses and strengths or uses.
– Alexandre Marcondes
@Kazzkiq posted an answer answering exactly what you want.
– Paulo Roberto Rosa