I have a javascript code and it needs to place images in different heights in a certein div according to the code. However, I also need the images to be centered. I searched the web and found different solutions such as using display:inline-block instead of float, but then the different heights don't work. Nothing I found seems to do the trick.
Here's the Javascript code that generates the final tags, margin_top is the variable that has the height difference needed.
var added_tags = ('<img style="margin-top:'+ margin_top.toString() +'px; float:left; margin-left:5px;" src="[Image source]" /> '+'<a></a>');
You can't center floated elements. display: inline-block on the to-be-centered elements and text-align: center on their container is the way to go.
Concerning vertical alignment of different heights, you can use vertical-align: middle or top or bottom on the inline-block elements, whatever you need.
.wrap {
text-align: center;
}
.wrap>img {
display: inline-block;
vertical-align: middle;
}
<div class="wrap">
<img src="http://placehold.it/100x150/fa0">
<img src="http://placehold.it/80x70/a0f">
<img src="http://placehold.it/200x180/af0">
<img src="http://placehold.it/50x120/f7a">
</div>
OR
.wrap {
text-align: center;
}
.wrap>img {
display: inline-block;
vertical-align: top;
}
<div class="wrap">
<img src="http://placehold.it/100x150/fa0">
<img src="http://placehold.it/80x70/a0f">
<img src="http://placehold.it/200x180/af0">
<img src="http://placehold.it/50x120/f7a">
</div>
OR
.wrap {
text-align: center;
}
.wrap>img {
display: inline-block;
vertical-align: bottom;
}
<div class="wrap">
<img src="http://placehold.it/100x150/fa0">
<img src="http://placehold.it/80x70/a0f">
<img src="http://placehold.it/200x180/af0">
<img src="http://placehold.it/50x120/f7a">
</div>
if you must use floated element the desired result can be acheived:
section {
padding:20px;
margin:20px;
border:1px solid silver;
box-sizing:border-box;
}
span {
float:right;
}
section.centre span {
margin-right:50%;
transform:translateX(50%);
}
<section>
<span>ALPHA</span>
<br style="clear:both;" />
</section>
<section class="centre">
<span>BRAVO</span>
<br style="clear:both;" />
</section>
Related
After trying a lot of suggestions on how to solve the following problem, I think it is time to ask for direct help.
I am trying to exactly align a logo (picture) to its following text (heading). All the solutions I tried before have shown an asymmetrical placement (cp. attached picture).
I hope somebody can help me on this. Thank you!
My actual output.
I want to have both red line segments of the same length, in other words: center the text relative to the picture.
And here is a code snippet:
<div class="jumbotron">
<img src="~/Content/images/logo.png" alt="logo" width="50" height="50"/>
<h2 style="display:inline">Tool XY</h2>
</div>
First method
Add vertical-align:middle;margin:0; to h2
.jumbotron h2, .jumbotron img{
display: inline;
vertical-align:middle;
margin:0;
}
.jumbotron img, .jumbotron h2{
display: inline;
vertical-align:middle;
margin:0;
}
<div class="jumbotron">
<img src="https://i.stack.imgur.com/a2I5h.png" alt="logo" width="50" height="50"/>
<h2 style="">Tool XY</h2>
</div>
Second Method
Add to this style to jumbotron
.jumbotron{
display: flex;
align-items: center;
}
.jumbotron{
display: flex;
align-items: center;
}
<div class="jumbotron">
<img src="https://i.stack.imgur.com/a2I5h.png" alt="logo" width="50" height="50"/>
<h2>Tool XY</h2>
</div>
You can use vertical-align: middle; in img tag to vertical center text
.jumbotron img {
display: inline-block;
vertical-align: middle;
}
<div class="jumbotron">
<img src="https://via.placeholder.com/150" alt="logo" width="50" height="50"/>
<h2 style="display:inline">Tool XY</h2>
</div>
Related: Use CSS to reorder DIVs
In my case, my HTML looks more like this:
<div class="container">
<div class="gallery">
<div class="image-wrap"> stuff </div>
<div class="thumbnails"> stuff </div>
</div>
<div class="info"> stuff </div>
</div>
I want .thumbnails and .info to switch places visually, but without affecting the styles or position of anything else. The all the html (and most of the css) inside .gallery is generated by a plugin that I can't edit.
This is what you can assume about the styling:
.thumbnails {
width: 100%;
height: 100px;
}
.info {
width: 100%;
min-height: 125px;
}
I considered using absolute positioning, but .info has a variable height because it has variable content length.
I'd prefer pure a CSS solution, but I'm open to jQuery/JS solutions if necessary.
If .info has a known height, then you can trick this using the float properties and behavior:
.thumbnails {
float:left;
clear:left;
width:100%;
}
.gallery:before {
content:'';
float:left;
height:130px;/* this should be the height and margins used by .info .... but js do not access pseudo element */
}
.info {
height:100px;
display:flex;
border:solid tomato;
justify-content:center;
align-items:center;
background:turquoise;
color:white;
font-size:2em;
}
<div class="container">
<div class="gallery">
<div class="image-wrap"> image-wrap </div>
<div class="thumbnails"> thumbnails </div>
</div>
<div class="info"> info </div>
</div>
codepen to play with
You can use flexbox to reorder flex items, and display: contents to make all the elements participate in the same flex formatting context.
.container {
display: flex;
flex-direction: column;
}
.gallery {
display: contents;
}
.thumbnails {
height: 100px;
order: 1;
}
.info {
min-height: 125px;
}
<div class="container">
<div class="gallery">
<div class="image-wrap"> image-wrap </div>
<div class="thumbnails"> thumbnails </div>
</div>
<div class="info"> info </div>
</div>
Currently, display: contents is only supported by Firefox.
Right, so I don't think you'll be able to achieve this just with css, because you want to actually change the structure... Javascript definitely will be required here:
(function($) {
$(function() {
$(".container").each(function() {
var container = $(this);
var gallery = container.children(".gallery");
container.children(".info").appendTo(gallery);
gallery.children(".thumbnails").appendTo(container);
});
});
})(jQuery);
Hope this helps!
I'm using Jquery slide panels for some images. The problem I'm having is when the slide panel shows it moves the image next to it down. I do not want this to happen. Is there a way to show the slider panel but not move the image next to it? There are questions similar to this but there are no answers to them on the site.
Here's the jsfiddle without the image files:
https://jsfiddle.net/amyspod/q2obknwt/
Here's my code:
<div class="images">
<div class="image1">
<img class="myImg" id="heroimage" src="heros website.jpg" alt="www.heros.com" width="300" height="200">
<div class="panel" id="hero">PSD to responsive website</div>
</div>
<div class="image2">
<img class="myImg" id="oakimage" src="oak website.jpg" alt="www.oak.com" width="300" height="200">
<div class="panel" id="oak">PSD to responsive website 2</div>
</div>
</div>
.myImg {
border-radius: 5px;
cursor: pointer;
display: inline-block;
margin-top: 40px;
}
/*slider panels*/
.image1, .image2{
display:inline-block;
margin-left: 10%;
}
.panel {
padding: 10px;
text-align: center;
background-color: white;
font-size: 20px;
display: none;
}
$(document).ready(function(){
function slidepanel(x,y){
$(x).mouseenter(function(){
$(y).slideToggle("slow");
});
$(x).mouseleave(function(){
$(y).slideToggle("slow");
});
}
slidepanel("#oakimage", "#oak");
slidepanel("#heroimage", "#hero");
});
Inline (or inline-block) elements align to the baseline. Try this:
.image1,
.image2 {
...
vertical-align: top;
}
Demo
Also note that CSS classes, by design, are intended to be reusable. I see no reason to have two of them in this case, and they should have semantic values that describe their use or function.
I have a div-container, which has one main image and optional multiple smaller images: http://jsfiddle.net/h5kc8ybm/
The multiple smaller images are generated dynamically, so there can be just 1 or 10 of them. On my JFiddle you can see, that the images are just displayed in one single row.
What I want to achieve is, that there are filled up 'by colomns':
First image on top next to the main image (like shown in this example)
Second image below that (not right of it, like in the example)
Third image right of first image (top)
Fourth image below third image
...and so on.
Is it possible to do that just with CSS?
Update
To avoid misunderstanding: All smaller images should be positioned right of the main image. But these small images should be displayed in two rows, filled up from first row to second row.
The main div-element will never change its height, but only its width.
Example
HTML
<div class="tnwrapper">
<div class="tn">
<img src="http://placehold.it/96x96" alt="" class="thumbnail">
</div>
<div class="tn">
<img src="http://placehold.it/96x96" alt="" class="thumbnail child">
</div>
<div class="tn">
<img src="http://placehold.it/96x96" alt="" class="thumbnail child">
</div>
</div>
LESS
.tnwrapper {
background-color: #f5f5f5;
padding: 5px 5px 5px 9px;
border-radius: 4px;
display: inline-block;
.tn {
display: inline-block;
vertical-align:top;
position: relative;
margin-right: 5px;
.thumbnail {
display: block;
padding: 4px;
margin-bottom: 20px;
line-height: 1.42857143;
background-color: #fff;
border: 1px solid #ddd;
border-radius: 4px;
}
.thumbnail.child {
width: 40px;
}
}
}
I was able to do this with the following steps:
wrap the smaller children in a div and make it position:relative
apply position:absolute on even items and reposition them
float them left
http://jsfiddle.net/0neukb08/
The downside of this approach is that it hardcodes the image's size in the "reposition" step
Additionally, the reason I chose not to use flex-box here was this issue with growing its width (I also didn't like the highest voted answer), but flexbox is a good option if you know the container's width in advance.
You probably can do this by
Rotate the container -90deg and reflect it:
.tnwrapper {
...
transform: rotate(-90deg) scaleX(-1);
}
then apply the reverse transformation for the thumbnails:
.tnwrapper .tn {
...
transform: rotate(90deg) scaleX(-1);
}
JSFiddle: http://jsfiddle.net/h5kc8ybm/1/
Note though that the height limit of the container is now width, not height (because it was rotated -90deg.
CSS flexbox styling should do the trick:
.tnwrapper {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 200px;
}
.tn:first-child {
height: 192px;
width: 192px;
}
<div class="tnwrapper">
<div class="tn">
<img src="http://placehold.it/96x96" alt="" class="thumbnail">
</div>
<div class="tn">
<img src="http://placehold.it/96x96" alt="" class="thumbnail child">
</div>
<div class="tn">
<img src="http://placehold.it/96x96" alt="" class="thumbnail child">
</div>
<div class="tn">
<img src="http://placehold.it/96x96" alt="" class="thumbnail child">
</div>
<div class="tn">
<img src="http://placehold.it/96x96" alt="" class="thumbnail child">
</div>
<div class="tn">
<img src="http://placehold.it/96x96" alt="" class="thumbnail child">
</div>
</div>
EDIT: Sorry, the above snippet doesn't quite answer the question after all. This snippet places each subsequent image in left-to-right then top-to-bottom order, rather than top-to-bottom then left-to-right order as the question asked. I think adding a div around the first image would be the cleanest way to accomplish what you want.
I'm not quite clear on the order of the thumbnails but I think you wanta column format for those.
I that case wrap the main image and the thumbnails in separate divs and then flexbox can do the rest.
.wrap {
display: flex;
margin: 1em auto;
height: 280px;
}
.hero {
padding: 10px;
}
.sidekicks {
flex: 1;
padding: 10px;
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-content: flex-start;
}
.sidekicks .item {
width: 96px;
height: 96px;
margin: 10px;
background: lightblue;
line-height: 96px;
text-align: center;
}
<div class="wrap">
<div class="hero">
<img src="http://lorempixel.com/image_output/city-h-c-240-250-5.jpg" alt="" />
</div>
<div class="sidekicks">
<div class="item">Item1</div>
<div class="item">Item2</div>
<div class="item">Item3</div>
<div class="item">Item4</div>
<div class="item">Item5</div>
<div class="item">Item6</div>
<div class="item">Item7</div>
<div class="item">Item8</div>
</div>
</div>
Codepen Demo
This is solution with flexbox and since you said that height of main-div wont change this should work http://jsfiddle.net/h5kc8ybm/13/
CSS
.tnwrapper {
background-color: #000;
padding: 5px 5px 5px 9px;
border-radius: 4px;
display: -webkit-flex;
display: flex;
}
.child-images {
display: -webkit-flex;
display: flex;
-webkit-flex-direction: column;
flex-direction: column;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
margin: 0 10px;
height: 170px;
}
.tnwrapper .tn .thumbnail {
padding: 4px;
margin: 10px;
line-height: 1.42857143;
background-color: #fff;
border: 1px solid #ddd;
border-radius: 4px;
}
.child-images .tn img {
width: 40px;
}
So ive seen some jquery hover effects, but none that can do the multiple choices hover.
Basically i have 5 t-shirt color choices, that when each one is hovered over, it should pop up where the current (green t-shirt) is located.
Heres link - http://musclefire.com/26.php
Note: there will also be other t-shirt styles on this page as well, so not sure if this will be too much code/complex for this to work properly.
p.s. - whoever nails it and gets perfect code, i'll send out a free tee to ya!
thanks much!
I'm pretty sure this is what you're looking for:
Demo: http://plnkr.co/edit/jiNxcw9nHQD5gV4vvwlj?p=preview
HTML
<div id="main">
<img class="active" src="http://musclefire.com/gear/muskull-green.png" />
<img src="http://musclefire.com/gear/muskull-red.png" />
<img src="http://musclefire.com/gear/muskull-blue.png" />
<img src="http://musclefire.com/gear/muskull-charcoal.png" />
<img src="http://musclefire.com/gear/muskull-yellow.png" />
</div>
<div id="thumbs">
<img src="http://musclefire.com/gear/muskull-green.png" />
<img src="http://musclefire.com/gear/muskull-red.png" />
<img src="http://musclefire.com/gear/muskull-blue.png" />
<img src="http://musclefire.com/gear/muskull-charcoal.png" />
<img src="http://musclefire.com/gear/muskull-yellow.png" />
</div>
CSS
#main {
border: solid 1px #eee;
text-align: center;
}
#thumbs {
border: solid 1px #eee;
text-align: center;
}
#main img {
width: 300px;
display: none;
}
#main img.active {
display: inline-block;
}
#thumbs img {
width: 50px;
height: auto;
}
jQuery
$(function(){
$('#thumbs img').bind('hover', function(){
var which = $(this).attr('src');
$("#main img:visible").hide();
$('#main img[src="' + which +'"]').stop().fadeIn(800);
});
});
All I know you cannot do this with css. So you should use javascript or jquery for this.
The only way to do this with CSS is if the element to affect is either a descendent or an adjacent sibling.
For example:
.parent:hover .child{}
.child{}
<div class="parent">
<div class="child"></div>
</div>
You can do what you wanted with jquery,
Live demo : http://jsfiddle.net/XUM8R/