vertical align img - css

I have a div called images_box which has a width of 277px. Within that div I have 9 images in it, just stored as <a> tags. I've got the images to float left and line up quite nicely within my div, but I would like the images to align vertically, as some are portrait and others are landscape. I know I can do this if I enclose each image in a div, but the plugin I use to launch the gallery won't recognise which image is being fired, so i need to enclose them as just within the <a> tags.
This is the code I have, if someone can help me just align the images horizontally and vertically. I don't want the images to be skewed.
#images_box a {
float: left;
padding: 9px;
width: 70px;
height: 70px;
text-align: center;
vertical-align: middle;
display: table-cell;
}
my data
<div id="images_box">
<a class="fancybox" rel="gallery1" href="http://farm8.staticflickr.com/7069/7060779347_fbee5aae15_b.jpg" title="morning after[explored] (mariosworld343)">
<img src="http://farm8.staticflickr.com/7069/7060779347_fbee5aae15_m.jpg" alt="" />
</a>
<a class="fancybox" rel="gallery1" href="http://farm8.staticflickr.com/7234/7047458501_46a2203733_b.jpg" title="Self confined... (TVidhya)">
<img src="http://farm8.staticflickr.com/7234/7047458501_46a2203733_m.jpg" alt="" />
</a>
<a class="fancybox" rel="gallery1" href="http://farm8.staticflickr.com/7053/6918451990_20fa76f338_b.jpg" title="kleiner schrittmacher (KatjaGiersig)">
<img src="http://farm8.staticflickr.com/7053/6918451990_20fa76f338_m.jpg" alt="" />
</a>
<a class="fancybox" rel="gallery1" href="http://farm8.staticflickr.com/7121/7059981833_abe404f4a0_b.jpg" title="(caro diario.)">
<img src="http://farm8.staticflickr.com/7121/7059981833_abe404f4a0_m.jpg" alt="" />
</a>
</div>

I think i figured out what you want. float: left to get the images side by side is not necessary.
#images_box {
background: #eee;
overflow: hidden; /* this div will get the height of the tallest element inside it */
white-space: nowrap; /* prevent line-breaks */
}
#images_box a {
padding:9px;
display: inline-block; /* required to apply vertical-align as expected */
vertical-align: middle;
}​
Works in:
Internet Explorer 6+
and modern browsers
Live demo: http://jsfiddle.net/vjDVp/1/

Basically, it seems like you're looking for:
#images a {
padding: 9px;
width: 70px;
height: 70px;
text-align: center;
vertical-align: middle;
display: table-cell;
}
Where you can tweak the vertical-align to top or middle, as needed. There's no need for floats. The dimensions you've described in your original question are off, so apologies if I've misunderstood.
Note also that display: table-cell is not IE6/7-friendly, if that matters.

Set the css to the images, not the a tags. Give them a classname (to make sure your other images are not affected) and apply the code you provided in your question to that (img.myclass). Or, at least the vertical-align.
Also, what are the sizes of the images? Do they all have the same width, same height or a certain size depending on whether it's a landscape or portrait image?
Oh and I don't know why, but text-align seems to work on img tags too sometimes.

Related

Making <img> responsive when within <a>tag?

I have been using the following css to make my images responsive
img{
max-width: 100%;
height: auto;
}
However it doesn't seem to work when the img is within an <a> tag ie.
<img class="fbicon" src="images/fbicon.png" alt="main">
Why is this and what could be a way around it?
Here is the complete code - (it is responsive on the fiddle but not on the site):
https://jsfiddle.net/bLchqb9u/
use width insted of max-width , find the working fiddel : https://jsfiddle.net/5n4rarrL/
The 100% always applies to the value of the parent element. By default <a> doesn't have a 100% width (it's would just be as big as it's content). You would have to change the behaviour, like this:
<img class="fbicon" src="images/fbicon.png" alt="main">
Demo here:
<div style="width: 300px">
<a href="#" style="max-width:100%">
<img style="width:100%;" src="https://upload.wikimedia.org/wikipedia/commons/1/17/Gustave_Caillebotte_-_Paris_Street%3B_Rainy_Day_-_Google_Art_Project.jpg" alt="main">
</a>
</div>
A percentage in max-width is resolved with respect to the width of the containing block.
Then, the only case where your code may not work is
If the containing block's width depends on this element's width, then
the resulting layout is undefined in CSS 2.1.
That should only happen when the width of the containing block is calculated with the shrink-to-fit algorithm. For example, floats, absolutely positioned or inline-blocks with width: auto.
div {
float: left; /* Shrink-to-fit width, depends on the content */
}
img {
max-width: 100%; /* Depends on the containing block */
height: auto;
}
<div>
<a href="#">
<img src="http://lorempixel.com/1000/200/" />
</a>
</div>
The solution is preventing the containing flock from depending on the content. Make sure it has an explicit width.
div {
float: left;
width: 100%; /* No longer depends on the content */
}
img {
max-width: 100%; /* Depends on the containing block */
height: auto;
}
<div>
<a href="#">
<img src="http://lorempixel.com/1000/200/">
</a>
</div>
You should make the <a> element a block container.
Like this:
a {
display: block;
}
img{
width: 100%;
height: auto;
}
<img class="fbicon" src="https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2_1x.png" alt="main">
This way the <a> tag will behave as a container and the image will stretch to the size of that.
Tried display and width suggestions, but still the the images were not responsive for some reason (while another img was), even though it worked on jfiddle. Finally, the new srcset came to the rescue,
<img class="fbicon" src="images/fbiconlarge.png"
srcset="images/fbiconlarge.png 1380w,
images/fbiconlarge.png 640w,
images/fbiconlarge.png 320w"
However must say, now its a bit too responsive - ending up too small on the smallest screen. Will post a separate Q. THanks #Oriol, Hasan, Hans, CodeiSir

Inline block challenges and suggestions for the layout

I keep reading articles that floats are outdated and that using inline-block solves problems such as having to use clearfix and a few more. These articles go on to justify inline-block by showing the same example: three squares that are aligned middle. In trying to use inline-block to create a navbar, I come across many problems. My navbar layout looks like such:
<nav id="main-nav" class="navbar">
<div class="logo">
<!-- image -->
</div><!--
--><div class="navbar-header"><!--
--><button type="button" class="navbar-toggle closed">
<span class="sr-only">Toggle navigation</span>
<i class="fa fa-bars"></i>
</button>
</div>
<div class="navbar-collapse navbar-sidebar">
<ul class="navbar-nav">
<!-- list-items -->
</ul>
</div>
</nav>
In order to align the logo left and the navbar-toggle button right, I had to use text-align justify and some special markup, which I find just as obtrusive as clearfix (Align two inline-blocks left and right on same line):
.header {
text-align: justify;
/* ie 7*/
*width: 100%;
*-ms-text-justify: distribute-all-lines;
*text-justify: distribute-all-lines;
}
.header:after{
content: '';
display: inline-block;
width: 100%;
height: 0;
font-size:0;
line-height:0;
}
.logo {
display: inline-block;
vertical-align: top;
}
.navbar-header {
display: inline-block;
vertical-align: middle;
}
My navbar is very similar to Bootstrap's. At small screen sizes, I want my navbar-toggle button to be centered in the navbar area. Vertical align: middle, however, would align this button to the middle my logo, which will be shorter or taller than the navbar, and which I also want aligned to the top of the navbar. Inline-block doesn't allow me to vertically align my content to the parent container, which seems to make it a non-viable option in many layouts. Is there some sort of solution where I can align my content to the container, rather than the sibling elements? I've been experimenting with setting different line heights and vertical-aligns.
If you have followed the comments above, there are many question being asked. I'll try to summaries most of it.
For display:inline-block, the vertical-algin property only affects the position of the element itself, and relative to the position of the siblings (the tallest sibling especially).
Percentage height like height:100%, only works with fixed height of parent container, or all percentage height that is set all the way back to <html> tag. But excluding positioned (relative, absolute etc.) elements, and viewport units vh, and maybe some other cases.
For display:table-cell, the vertical-algin property affects all the child elements, again excluding some positioned ones.
I think CSS table is easiest way to get your desired layout done in this case. Since you can easily have both vertical and horizontal alignments set on it. Here is a simplified workaround.
JsFiddle Demo
.nav {
border: 1px solid red;
display: table;
width: 100%;
}
.nav > div {
display: table-cell;
vertical-align: middle;
}
.logo img {
display: block;
}
.menu {
text-align: right;
}
.menu span {
border: 1px solid blue;
}
<div class="nav">
<div class="logo">
<img src="//dummyimage.com/50"/>
</div>
<div class="menu">
<span>Menu</span>
</div>
</div>

CSS display:table content height

I am trying to make a number of columns the same height, and have decided to go down the display:table CSS route.
<div class="header" style="display: table; width: 100%; background-color: yellow">
<div class="title" style="font-size: 30px; display: table-cell;">Navigation Title</div>
<div class="navigation" style="display: table-cell;">
<a class="navigation-link" style="background-color: red">Home</a>
<a class="navigation-link">About</a>
<a class="navigation-link">Contact</a>
</div>
</div>
I would like the navigation-links to the the full height of the header table (so as to add background-color to them), but the navigation seems to have some padding automatically added to the top and bottom. How would i set the height of navigation, and navigation-links to be the height of the header table.
I have tried using height:100% in various places but that did not seem to work (I am probably missing something). Here is a diagram to show what i mean:
Try to play with display: inline-block;, vertical-align: top;, padding-top and height of your navigation links:
.navigation {
...
vertical-align: top;
}
.navigation-link {
...
display: inline-block;
height: 100%;
padding: 7px 5px;
}
Demo: http://jsfiddle.net/y8AF5/
This seems to solve your problem : DEMO
CSS
.navigation > a {
display:inline-block;
border:1px solid #CCC;
line-height:2.5em;
}
What you have done so far is styling the classes but no styling was done for a tag.
Now, the trick is to change the display type of a and using line-height to provide appropriate spacing of full height!!!

Cross-browser solution to keep Floating css bullets positioned relative to a fluid Div?

I have a set of css bullets that come along with the Orbit javascript slider by Zurb Foundation. The bullets have a float:left that is necessary so that they line up horizontally when new slides are introduced. Removing the float stacks them vertically....
I need to position the bullets "to the pixel" vertically on my page sadly, and I've had trouble with various browsers, having to use browser-specific css. I turned to jqueryUI to position my bullets based on the Div above it, but there are a couple issues.
The bullets need to be positioned in the center below the slider...and the slider fluidly resizes with the page. JqueryUI can position the bullets on page load, but on resize the positioning gets screwed up due to the float on the bullets...
Just wondering if anybody can think of a solution off the top of their head...I've been stumped for a while lol. Thanks for any help in advance!
I created a basic jsfiddle with 2 divs that should demonstrate the situation. http://jsfiddle.net/KRTYd/2/
<div id="slider">
<img src="http://www.hdwallpaperspot.com/wp-content/uploads/2013/02/nature-background-wallpaper.jpg" style="width:100%;"></img>
</div>
<div id="bullets"></div>
Specifically the html is this:
<ul data-orbit>
<li>
<img src="../img/demos/demo1.jpg" />
<div class="orbit-caption">...</div>
</li>
<li>
<img src="../img/demos/demo2.jpg" />
<div class="orbit-caption">...</div>
</li>
<li>
<img src="../img/demos/demo3.jpg" />
<div class="orbit-caption">...</div>
</li>
</ul>
and the bullet css looks like this:
.orbit-bullets {
overflow: hidden;
position: absolute;
}
.orbit-bullets li {
display: block;
width: 18px;
height: 18px;
background: #ffffff;
float: left;
margin-right: 6px;
border: solid 3px #666666;
-webkit-border-radius: 1000px;
border-radius: 1000px; }
And due to multiple slides indicated in the html, multiple css bullets are generated by javascript.
.orbit_bullets{
text-align:center;
list-style:none;
width:100%;
display:block;
}
.orbit_bullets li {
width: 18px;
height: 18px;
background: #ffffff;
display:inline-block;
margin-right: 6px;
border: solid 3px #666666;
-webkit-border-radius: 1000px;
border-radius: 1000px;
}
Looks familiar to me, i think i already answered this question.
Anyway, #bullet {margin:auto;} instead flotting will do it.
http://jsfiddle.net/yAgdT/
But i do not believe that will do it for the slider once you have many img to slide.
#bullets should become parents of bullets
http://jsfiddle.net/yAgdT/1/
These are supposition, since we have no idea what's gonna be sliding and what you wanna do with the bullets :) .
maybe something more usefull, on the way to become a slider maybe : http://jsfiddle.net/yAgdT/2/
Else, here's a slider i played with where i produce bullets with box-shadow. it runs without javascript. http://dabblet.com/gist/4323453 it's not an anser, but html structure might interest you . if you wish to relay on CSS selectors.

clearing nested floats

I'm creating a tiled grid of images much like an image gallery with a grid of thumbnails, and I need the images to wrap onto the next row after 3 images. So I'm floating a bunch of divs that each contain an image and then clearing the float manually after three images.
The problem is that I'm working within a rather convoluted existing template that already makes use of float to arrange everything. Clearing the float in my grid scrambles the entire page, presumably because it's clearing every float in page so far. What can I do?
I'm clearing the float using by inserting a blank div. ie:
<div style='clear:right'>
Might one of the other methods for clearing floats work better?
Create a suitable container div (if
you don't already have one)
Set a restrictive width on the
container div - equalling the same
that 3 images takes up.
Allow all images to float - they'll
automatically wrap.
Set the container div with
'overflow: hidden', which will clear
the floats for you.
A simplified version might look like this:
<style>
div#container {
overflow: hidden;
width: 300px;
}
div#container img {
float: left;
width: 100px;
}
</style>
<div id="container">
<img src="" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
<img src="" />
</div>
If your markup is like so:
div
img
img
img
row break
img
img
img
...
Then you need to add this after every three blocks:
<br class="clear" />
But if your markup is like this:
div
div
img
img
img
div
img
img
img
...
..then you just need to apply the following .clear class to your inner DIVs.
Either way, add this to your stylesheet:
.clear:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
* html .clear { height: 1%; }
*:first-child+html .clear { min-height: 1px; }
You can use this class for all other elements that contain floats.
If IE >= 8 support is fine for you, you might want to consider using display: table instead of floats. Since you want to display a grid, this the more appropriate way of doing it.
http://www.quirksmode.org/css/display.html#table
I'd try to use display: inline-block; to style the elements containing each image.
Example of HTML code for one container:
<style>
.figure {
display: inline-block;
}
</style>
<div class="figure">
<img src="littlepony.jpg" alt="My cute little pony" width="13" height="37" />
</div>
Now there a few pitfalls using this with IE6, IE7 and Firefox 2:
IE 6 and 7 will only style inline elements that have hasLayout triggered, I mean you'll see inline-block behavior if you do this:
<!--[if lte IE 7]>
.figure {
display: inline;
zoom: 1; /* triggering hasLayout */
}
<![endif]-->
Firefox 2 doesn't understand display: inline-block; so you'll have to precede the latter by another display instruction:
.figure {
display: -moz-inline-stack;
display: inline-block;
}
now Firefox 2 is going to annoy you a bit. First, you must have only one child element in your .figure element, otherwise the children would ... stack. So if you have a legend under your image, insert a div between div.figure and img+p
<div>
<img src="littlepony.jpg" alt="Photo of my cute little pony" width="13" height="37" />
<p>Second child of .figure>div and not .figure</div>
</div>
</div>
Second (still only in Fx2), you'll notice from time to time that you can't anymore neither select text inside the -moz-inline-stack'ed element nor click on links it could contain. The fix is to position the added div relatively:
.figure div {
position: relative;
}
Of course the conditional comment for IE6/7 must occur after regular CSS, otherwise it'll be of little help.
And finally, if no elegant solution works for you, use a TABLE. A simple table with only td and no th. If it occurs that:
IE6 and 7 don't like display: table-sth
your CMS causes problems to what would otherwise work fine on another site
Firefox 3 support for inline-block is of no help
than yes it's better for everybody that you use a table and no half-solution causing problems to half your users ;)

Resources