Align image inside DIV horizontally and vertically - html

I am trying to align image inside DIV horizontally and vertically. Problem is that I tried several methods and none of them worked for me.
This is code that I am using:
CSS
img{
max-width:100%;
vertical-align: middle;
}
#slika {
float: center;
height: 126px;
width: 111px;
text-align: center;
}
HTML
<div id="slika">
<img src="images/2105602.png" width="auto" height="auto" alt="2105602.png">
</div>
jsfiddle: HERE
Can soemone share his thoughts with me? I can't find solution. It always stays aligned at top.

JSFiddle - DEMO
img {
max-width:100%;
top: 50%;
position: relative;
transform: translateY(-50%);
}
#slika {
height: 126px;
width: 111px;
text-align: center;
border: 1px #000 solid;
}

You can do it by adding a margin of 50% and then a top of -(imageheight/2)
img{
max-width:100%;
vertical-align: middle;
margin-top:50%;
position:relative;
top:-37px;
}
#slika {
float: left;
height: 126px;
width: 111px;
text-align: center;
border:1px solid red;
}
fiddle here: http://jsfiddle.net/dduygx0x/2/

Here is my soluton for your problem using the common table - table-cell way:
I wrapped you image in a new div:
<div id="slika">
<div class="img-wrapper">
<img ....>
</div>
</div>
and altered the CSS:
img{
max-width:100%;
}
.img-wrapper{
display: table-cell;
vertical-align: middle;
}
#slika {
display: table;
float: left;
height: 126px;
width: 111px;
text-align: center;
vertical-align: middle;
}
img{
max-width:100%;
}
.img-wrapper{
display: table-cell;
vertical-align: middle;
}
#slika {
border: 1px solid black;
display: table;
float: left;
height: 126px;
width: 111px;
text-align: center;
vertical-align: middle;
}
<div id="slika">
<div class="img-wrapper">
<img src="http://tommyvirtualnikatalog.com.hr/images/akcija/prehrana/2105602.png" width="auto" height="auto" alt="2105602.png">
</div>
</div>
The benefit of this solution ist that it ist absolut dynamical an can easely made responsive!!

instead of positioning it with hard-coded properties (which would change depending on the image) or using the transform property which wont work in older browsers you can simply wrap the image in a p and set the line-height to match the height of the box
p{
line-height: 126px;
margin: 0;
padding: 0;
}
JSFIDDLE

A solution I've often used is adding an empty span element next to the image, with
#slika span {
display: inline-block;
height: 100%;
vertical-align: middle;
}
The idea is that vertical-align is relative to its siblings, therefore a lone element has nothing to work with. The upside of this method is that its completely dynamic, no fixed pixels, works in older environments ( make sure to test that it meets your lowest-end requirements ) and does not affect the container div. The downside would be the extra html.

Related

css using float: right with vertical-align: middle

I have 2 divs (in a wrapper) that I am using to display a photograph and the title of the photograph side by side and vertical-align as middle.
The issue that I am having is that when I attempt to float the photograph to the right and float the title to the left, the title and photograph are no longer vertical-aligned as middle.
It seems that when I use float, the vertical-align becomes top.
How do I add the floats and keep the vertical-align as middle? I have spent 2 hours trying all I know. I hope someone can point out what I have done wrong.
Here is my code:
<div class="live_preview_top_header_wrapper">
<div class="live_preview_top_header_title">Title</div>
<div class="live_preview_top_header_photograph">
<img id="id_name_details_photograph_01_live_preview" src="{{ name_details_photograph_url }}" />
</div>
</div>
.live_preview_top_header_wrapper {
width: 100%;
background-color: #ffaaaa;
vertical-align: top;
}
.live_preview_top_header_title {
display: inline-block;
background-color: #aaaaff;
vertical-align: middle;
float: left;
}
.live_preview_top_header_photograph {
display: inline-block;
background-color: #cc0033;
vertical-align: middle;
float: right;
}
Try centering with Line-Height in the parent Div, and using vertical-align: middle; on the content eg image and title.
Example
.live_preview_top_header_wrapper {
width: 100%;
background-color: #ffaaaa;
line-height: 200px;
}
.live_preview_top_header_wrapper {
width: 100%;
background-color: #ffaaaa;
line-height: 200px;
}
.live_preview_top_header_photograph {
display: inline-block;
background-color: #cc0033;
float: right;
line-height: 200px;
}
.live_preview_top_header_photograph img {
vertical-align: middle;
}

Vertical centering text/images in divs not working

Been trying to vertically center 3 divs within another div and no matter what I try it doesn't work.
Please take a look at my example here:
http://jsfiddle.net/d6sLpxvc/
My html:
<body class="mainBackground">
<header class="wrap">
<div class="menuIcon">Menu Icon</div>
<div class="pageTitle">Good morning, John Doe</div>
<div class="logo">Logo</div>
</div>
</header>
</body>
My CSS:
.wrap {
height: 41px;
background-color: gray;
width: calc(100% - 34px);
margin: 17px auto;
}
.menuIcon {
display: table-cell;
float: left;
vertical-align: middle;
}
.logo {
float: right;
}
.pageTitle {
text-align: center;
vertical-align: middle;
}
The menu icon will actually be a small SVG icon with a fixed size of about ~28px. The logo on the right hand side also has a fixed size which has roughly double the height of the menu icon. I've used placed holder text for those images. The text in the middle MUST be centered horizontally as well within the browser canvas (not the div it's in).
The only thing that I absolutely need to do is use as little hardcoded pixel sizes as possible. So my end design must follow the psd's to the pixel but must be coded in percentages wherever possible. I'd like to stick with using width: calc(100% - 34px) if possible as well since this lets me use math for determining the div size. This means no using px to vertically center - I need to use percentages to vertically center these items in the div because they will change in the future and I cannot go back to adjust them to make sure they are always centered if the height is different(like menu icon or logo). Must work with IE9 - dont care about other browsers.
Would appreciate any help greatly!
You can't use display: table-cell; without display: table; and display: table-row;, plus table cells can't float. this is updated fiddle, is this what you are looking for? More about centering could be found at css-tricks.com
HTML
<header class="wrap">
<div class="row">
<div class="menuIcon">Menu Icon</div>
<div class="pageTitle">Good morning, John Doe</div>
<div class="logo">Logo</div>
</div>
</header>
CSS
.wrap {
height: 41px;
background-color: gray;
width: calc(100% - 34px);
margin: 17px auto;
display: table;
}
.row {
display: table-row;
}
.menuIcon {
display: table-cell;
vertical-align: middle;
}
.logo {
display: table-cell;
vertical-align: middle;
}
.pageTitle {
text-align: center;
vertical-align: middle;
display: table-cell;
}
You just need to apply line-height to the DIV's that are inside .wrap. The line-height property property value should be equal to to the height of .wrap for maximum result.
You can add line-height to them individually but the block of code below will do just that.
.wrap > div {
line-height: 41px;
display: inline;
margin: 0 10px 0 50px;
}
.wrap {
height: 41px;
background-color: gray;
width: calc(100% - 34px);
margin: 17px auto;
margin-top: 60px; /* Just for my presentation */
}
.wrap > div {
line-height: 41px;
display: inline-block;
margin: 0 10px 0 50px; /* Adjust to suit your need */
}
.menuIcon {
display: table-cell;
float: left;
vertical-align: middle;
}
.logo {
float: right;
}
.pageTitle {
text-align: center;
vertical-align: middle;
}
<header class="wrap">
<div class="menuIcon">Menu Icon</div>
<div class="pageTitle">Good morning, John Doe</div>
<div class="logo">Logo</div>
</header>
try this:
.wrap {
height: 41px;
background-color: gray;
text-align:center;
width: calc(100% - 34px);
margin: 17px auto;
}
.menuIcon {
float: left;
line-height: 41px;
}
.logo {
float: right;
line-height: 41px;
}
.pageTitle {
text-align: center;
display:inline-block;
line-height: 41px;
}
<body class="mainBackground">
<header class="wrap">
<div class="menuIcon">Menu Icon</div>
<div class="pageTitle">Good morning, John Doe</div>
<div class="logo">Logo</div>
</div>
</header>
</body>
Look at this question: Vertical alignment of elements in a div
This fiddle is especially helpful: http://jsfiddle.net/techsin/FAwku/1/
Taken from a solution in that fiddle provided by a comment to the linked question:
<div class="top6">
<div class="in6">6</div>
<div class="in6"></div>
<div class="in6"></div>
</div>
.top6 {background-color: blue; height: 100px; display:flex; align-items: center;}
.in6 {background-color: yellow; width: 30px; height: 30px; margin-right: 5px; }

Vertically center text in a box without knowing how many lines

This is driving me crazy I just don't understand why this piece of simple css to vertically center an element in a div doesn't work as expected.
this is the html:
<div class="header-a-wrapper" style="
line-height: 48px;
height: 48px;
background: red;
display: block;
text-align: center;
">
<a href="/user/5659186348163072" class="right" style="
background: blue;
line-height: normal;
display: inline-block;
vertical-align: middle;
float: none;
height: 20px;
">medical salamander</a>
</div>
the inner element does not get centered vertically but I really think it should
here is an html with the two elements:
http://alephz.com/test.html
and this is the CRAZY part. here is a jsfiddle with the same html and over there it works! tested on the same chrome/win7!
http://jsfiddle.net/pkrsdqkb/
Very weird, but if you want to solve it, you add to 'a':
position: relative;
top: 50%;
transform: translateY(-50%);
Remove
vertical-align: middle;
float: none;
One option to play nicely with vertical-align: middle is to use display: table and display: table-cell.
The wrapper gets display: table and width: 100%
Wrap the links in a div which will act as a "table cell" with display: table-cell
vertical-align: middle will now work as you expect it to.
Compatibility: display: table is good for IE 8 + and modern browsers everywhere.
Example:
.header-a-wrapper {
background: red;
display: table;
text-align: center;
height: 100px;
width: 100%;
}
.vertical {
display: table-cell;
vertical-align: middle;
}
.right {
background: blue;
display: block;
margin: 2px 0;
}
<div class="header-a-wrapper">
<div class="vertical">
medical salamander
medical salamander
</div>
</div>
Old answer
There is a lot of redundant CSS.
The vertical center is applied through: line-height: 48px.
Leave that on the wrapper and remove all the positioning CSS properties on a.right.
Example:
.header-a-wrapper {
line-height: 48px;
background: red;
display: block;
text-align: center;
}
.right {
background: blue;
}
<div class="header-a-wrapper">
medical salamander
</div>

CSS - Class is moving out of position

i should make something like:
But it actualy looks like:
As u can see on images "left" class is moving out of its possition with no reason when i add image or text there, and i am 100% sure that text or image is smaller then width / height of block so i really dont know how to continue and fix this.
My HTML:
<div id="sub-content">
<span class="left">
<img src="images/foto.png">
<span class="topic-name"> Název topicu nebo článku </span>
</span>
<span class="middle"></span>
<span class="right"></span>
</div>
My CSS:
#sub-content{
margin-left: 20px;
margin-top: 0px;
background-image: url("images/sub_content_bg.png");
background-repeat: repeat-x;
height: 145px;
width: 987px;
display:inline-block;
}
#sub-content .left{
width: 326px;
height: 145px;
display: inline-block;
}
#sub-content .left img{
width: 122px;
height: 121px;
display: inline-block;
margin-top: 0px;
margin-left: 5px;
}
#sub-content .left .topic-name{
width: 150px;
margin-bottom: 130px;
margin-left: 10px;
display: inline-block;
vertical-align: top;
text-decoration: underline;
font-weight: 14px;
}
#sub-content .middle{
background-color: orange;
width: 326px;
height: 145px;
display: inline-block;
}
#sub-content .right{
background-color: yellow;
width: 326px;
height: 145px;
display: inline-block;
}
Please can somebody help me to style this or tell me what i am doing wrong?
p.s. Live preview can be also find on: funedit.com/andurit/new/
If you add overflow:hidden to the .left div, it will align correctly.
See: JSFiddle
(I also replaced the inner span with div since div shouldn't occur inside span).
Try to add a position propery to your CSS file. In #sub-content, add position:static;. This should make the elements render in order as they appear in the document.

Inline-block element shifts to bottom if previous element contains an image

http://jsfiddle.net/dsUnc/
However when I replace an img tag with text - elements are positioned like expected (next to each other with the same height).
Happens on all browsers.
How to fix it?
float: left is not an option
HTML:
<div id='main'>
<div id='first'>
<img src='https://www.google.ru/images/icons/product/chrome-48.png' height='30'>
</div>
<div id='second'>Text</div>
</div>
CSS:
div {
border: 1px solid gray;
height: 30px;
}
#first {
display: inline-block;
height: 30px;
}
#second {
display: inline-block;
height: 30px;
}
Add vertical-align: top to do it without float.
jsFiddle
#first {
display: inline-block;
height: 30px;
vertical-align: top;
}
#second {
display: inline-block;
height: 30px;
vertical-align: top;
}
Make the container with relative position and the div you want to position with absolute position with top:0;
http://jsfiddle.net/uqAGt/
Just add float: left or vertical-align: top to your child divs:
Demo: http://jsfiddle.net/dsUnc/2/
Demo: http://jsfiddle.net/dsUnc/5/