CSS - Displaying images in a div in a line - html

I have 3 images inside a div and i want them to be displayed in a single line. The output thats coming from my current code is that 2 images are one a single line and the other one at the bottom.
Any help would be appreciated!
My Code of HTML:
<div id="slider_container">
<div class="slider">
<img id="1" src="Kid Playing Guitar_big.jpg" border="0" style="float:left;" />
<img id="2" src="University Students_big.jpg" border="0" style="float:left;" />
<img id="3" src="Business Man_big.jpg" border="0" style="float:left;" />
</div>
<div class="slider_thumbnails">
<img id="1" src="Kid Playing Guitar.jpg" border="0" />
<img id="2" src="University Students.jpg" border="0" />
<img id="3" src="Business Man.jpg" border="0" />
</div>
</div>
My code for CSS:
#CHARSET "UTF-8";
#slider_container{
height: 360px;
width: 720px;
}
.slider{
width: 720px;
height: 360px;
overflow: hidden;
background-image: url(loading.gif);
background-repeat: no-repeat;
background-position: center;
}
.slider img{
width: 480px;
height: 360px;
display: none;
}
.slider_thumbnails{
width: 720px;
display:inline-block;
}
.slider_thumbnails img{
display: inline-block;
}
Please explain the code after you post your answer.

.slider_thumbnails{
width: 720px;
display:inline-block;
white-space:nowrap;
}

I think the problem is the total width of your images equals the width of your container. Try changing the width of your images to 235.
It works on your jsfiddle.

What about a float:left;?
.slider img{
float: left;
position: relative;
}
For float, width is very important. So don't forget to measure 'em up and define the appropriate width calculated after margin, padding.

.slider img{
width: 480px;
height: 360px;
display: none;
}
.slider_thumbnails{
width: 720px;
display:inline-block;
}
.slider_thumbnails img{
display: inline-block;
}

Related

Center image in any viewport without stretching

I have 3 different box sizes in which I need to display an image. The image should take the entire width and height of the box but should not stretch. It can crop and center the image.
Here is an example:
https://jsfiddle.net/y1zn0mxy/
If you see the 3 different size, you will see that it works in the first and second case but not in the last one. It would work in the last one if i swap the size of the image tag to:
width: 100%;
height: auto;
but then it will not work in the first two.
Any other way to achieve this?
You can simply achieve the desired effect by inserting your image as background image instead of an <img /> tag. The advantage is, you don't need the image tags and the CSS applied to them. Just use background-size: cover; to always fit the image into the viewport. This way you have much less code and can control the image by the CSS background property.
.img {
background-image: url(http://i.kinja-img.com/gawker-media/image/upload/jour87ix9aoikm1zpjct.jpg);
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}
<div style="width:300px; height:250px; margin: 30px; background: black; float: left;">
<div class="img box" style="padding:0; width: inherit; height: inherit;"></div>
</div>
<div style="width:300px; height:500px; margin: 30px; background: black; float: left;">
<div class="img box" style="padding:0; width: inherit; height: inherit;"></div>
</div>
<div style="width:500px; height:200px; margin: 30px; background: black; float: left;">
<div class="img box" style="padding:0; width: inherit; height: inherit;"></div>
</div>
Create a new class for the last image (I called it landscape2 in my jsfiddle) as the last image is the only one with width value higher than the height value. Then add this:
.landscape2 {
width:100%;
}
https://jsfiddle.net/y1zn0mxy/2/
It's normal behaviour. It's normal behaviour. You can't set both axis to 100% because your image will be stretched. Why not add additional class for horizontal landscape: https://jsfiddle.net/y1zn0mxy/1/ ?
If you don't need <img ... /> you can replace it by css properties:
background
background-position
background-size: cover
Here you can see reproduce: https://jsfiddle.net/y1zn0mxy/3/
Besides Andreas good answer, you have a new way to handle this.
Just can achieve just the same functionatily of backgroound-size: cover in an image using object-fit.
It isn't as widely suported (no suport in IE/Edge) but there is a polyfill available
img {
position: absolute;
top: -100%;
right: -100%;
bottom: -100%;
left: -100%;
margin: auto;
width: 100%;
height: 100%;
object-fit: cover;
}
.box {
position: relative;
overflow: hidden;
}
<div style="width:300px; height:250px; margin: 30px; background: black; float: left;">
<div class="img box" style="padding:0; width: inherit; height: inherit;">
<img src="http://i.kinja-img.com/gawker-media/image/upload/jour87ix9aoikm1zpjct.jpg" class="landscape" />
</div>
</div>
<div style="width:300px; height:500px; margin: 30px; background: black; float: left;">
<div class="img box" style="padding:0; width: inherit; height: inherit;">
<img src="http://i.kinja-img.com/gawker-media/image/upload/jour87ix9aoikm1zpjct.jpg" class="landscape" />
</div>
</div>
<div style="width:500px; height:200px; margin: 30px; background: black; float: left;">
<div class="img box" style="padding:0; width: inherit; height: inherit;">
<img src="http://i.kinja-img.com/gawker-media/image/upload/jour87ix9aoikm1zpjct.jpg" class="landscape" />
</div>
</div>

Align two images side by side

I want to arrange the two images in my HTML page side by side.
I want the images to stay side by side even if the page size changes.
I also want the second image to span the entire header of the page ie. all the space left after the first image. The images here are of different size.
For now, I have arranged two images side by side, but when I change the size of the page, the image wraps and comes in the next line after the first image.
Here is my code sample and the CSS:
.header {
display: inline-block;
margin-left: auto;
margin-right: auto;
height: 120px;
}
<img class="header" src="http://www.placehold.it/160X120" style="float: left;" alt="CCM Logo">
<img class="header" src="http://www.placehold.it/543X120/0000FF" alt="CCM Banner">
Here is a Fiddle.
Use white-space: nowrap to prevent wrapping.
.header {
margin: 0 auto; max-width: 800px; /*centering header*/
height: 120px; position: relative; /*scale header images to 120px*/
white-space: nowrap; /*keep one-line*/
overflow: hidden; /*hide excess images parts on small screens*/
}
.header>img { height: 100%;}
<body>
<div class="header">
<img src="http://www.www8-hp.com/in/en/images/T-GE-healthcare-logo__153x115--C-tcm188-1616301--CT-tcm188-1237012-32.jpg" alt="CCM Logo">
<img src="http://blu-alliance.com/wp-content/uploads/2013/10/healthcare-banner2.jpg" alt="CCM Banner">
</div>
</body>
.header {
display: inline-block;
margin-left: auto;
margin-right: auto;
max-width: 50%;
height:120px;
}
HTML:
<body>
<img class="header" src="http://www.www8-hp.com/in/en/images/T-GE-healthcare-logo__153x115--C-tcm188-1616301--CT-tcm188-1237012-32.jpg" style="float: left;" alt="CCM Logo">
<img class="header" src="http://blu-alliance.com/wp-content/uploads/2013/10/healthcare-banner2.jpg" alt="CCM Banner">
</body>
Give style
.header {
display: block;
float:left;
height: 120px;
}
to both images
Apply below style:
.header {
display: inline-block;
height: 120px;
width: 50%;
}
Try with max-width Demo
.header {
max-width:50%;
}
Try this -
<div class="imgclass">
<div class="img1">
your img here
</div>
</div>
<div class="imgclass">
<div class="img2">
your img here
</div>
</div>
On your css file or between <style>here</style> this -
.imgclass {
display: table-cell;
}

how to space 4 images equally within a div?

i have 4 social media buttons in a div and i want to space them equally but i can't figure out how to?
CSS
.socialbuttonstop {
height: 150px;
width: 35px;
margin-left: 915px;
position: absolute;
}
HTML
<div class="header">
<div class="headercontent">
<div class="socialbuttonstop">
<img src="Images/facebooksmall.png" />
<img src="Images/twittersmall.png" />
<img src="Images/googlesmall.png" />
<img src="Images/linkedinsmall.png" />
</div>
</div>
</div>
I would place a div around the images and place the height of the divs to 25%.
HTML
<div class="header">
<div class="headercontent">
<div class="socialbuttonstop">
<div class="social_btn">
<img src="Images/facebooksmall.png"/>
</div>
<div class="social_btn">
<img src="Images/twittersmall.png"/>
</div>
<div class="social_btn">
<img src="Images/googlesmall.png"/>
</div>
<div class="social_btn">
<img src="Images/linkedinsmall.png"/>
</div>
</div>
</div>
</div>
CSS
.socialbuttonstop {
height: 150px;
width: 35px;
margin-left: 915px;
position: absolute;
}
.social_btn {
height: 25%;
}
If your container div is a fixed width here's what I usually do, assuming 48x48 icons:
HTML
<div id="social">
<img id="twitter" />
<img id="facebook" />
<img id="linkedin" />
</div>
CSS
#social {
width: 154px;
height: 48px;
}
#social img {
width: 48px;
height: 48px;
float: left;
margin-right: 5px;
background-image: url('icons.png');
background-position: 0px 0px;
}
#social img:last-child{
margin-right: 0px;
}
#social img#twitter {
background-position: -48px 0px;
}
#social img#facebook {
background-position: -96px 0px;
}
Then make a PNG file with just the icons without any padding
I only can think of the use of padding:
HTML:
<div>
<img class="imagePadding" src="Images/twittersmall.png"/>
<img class="imagePadding" src="Images/twittersmall.png"/>
<img class="imagePadding" src="Images/googlesmall.png"/>
<img class="imagePadding" src="Images/linkedinsmall.png"/>
</div>
CSS:
.imagePadding
{
padding: 10px;
}
For vertically centering the block please check the following fiddle http://jsfiddle.net/L4su9/3/
.socialbuttonstop
{
height: 150px;
width: 35px;
position: absolute;
top:50%;
margin:-75px 0 0 0;
/* negate top pixels =-"total height/2" */
background:#000;
}
Semantically you should have the HTML set up using an unordered list:
<ul>
<li class="facebook"><span>Facebook</span></li>
<li class="linkedin"><a href="#"><span>Linked In</span></li>
</ul>
CSS:
ul {
height: 150px;
width: 35px;
margin-left: 915px;
position: absolute;
display: block;
}
ul li {
display: block;
width: 35px;
height: 35px;
}
ul li a {
display: block;
background-image: url(facebookSmall.png) no-repeat center;
}
ul li a span {
display: none;
}
Quick explanation. Basically the unordered list tells the browser or someone who is blind that it is a list - which it is. It is a list of social media buttons.
The anchors allows the user to click and go to your Facebook/Linked In page while the span tags enable you to provide helpful text to Google/search engines and those who are blind.
Of course, you CAN still you use the original HTML code that you have but then you should at the least apply alt attributes to the images and consider linking them with parent anchors.
I think this is more than enough information to get you started. I don't think it's fair for me (or anyone else) to give you the complete code. That's the beauty of coding! Problem solving.

Unwanted space inside of DIVs

I´m trying to place a slided image in the center if the page. I have it almost done, the thing is that inner each piece of the image I have a small space, like if it has some little padding (which it hasn't), does anybody sees something wrong in the code?
<style type="text/css">
html, body, #wrapper, images {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px;
border: 0px;
background-color: #000;
}
img {
margin: 0px;
padding: 0px;
border: 0px;
}
.center {
width: 800px;
height: 600px;
position: absolute;
left: 50%;
top: 50%;
margin-left: -400px;
margin-top: -300px;
}
.center_mini {
margin: 0px;
padding: 0px;
border: 0px;
}
.center_mini_float {
float: left;
margin: 0px;
padding: 0px;
border: 0px;
}
</style>
<div class="center">
<div class="center_mini">
<img src="images/background_01.png" width="800" height="144" alt="bg">
<div class="center_mini">
<div class="center_mini_float">
<img src="images/background_02.png" width="503" height="456" alt="bg">
</div>
<div class="center_mini_float">
<div class="center_mini">
<div class="center_mini">
<img src="images/background_03.png" width="246" height="89" alt="bg">
</div>
<div class="center_mini">
<img src="images/background_05.png" width="246" height="106" alt="bg">
</div>
<div class="center_mini">
<img src="images/background_06.png" width="246" height="102" alt="bg">
</div>
<div class="center_mini">
<img src="images/background_07.png" width="246" height="159" alt="bg">
</div>
</div>
</div>
<div class="center_mini_float">
<img src="images/background_04.png" width="51" height="456" alt="bg">
</div>
</div>
</div>
<!--<img src="images/background.jpg" width="800" height="600" alt="bg">-->
</div>
Try adding:
img { display: block; }
The problem is that IMG tags have a natural DISPLAY value of "INLINE". This causes extra whitespace to appear around the image in certain situations.
Depending on your layout needs, try
img { display: block; }
or
img { display:inline-block; }
If your images are otherwise working the way you want, inline-block will cause the least amount of thrash.
More info:
http://www.w3schools.com/cssref/pr_class_display.asp
http://www.tequilafish.com/2009/04/29/css-removing-extra-space-underneath-an-image/
I think the better solution will be using
img { vertical-align: middle; }
This way you won't alternate the default browser image display. Also, make sure the image container has line-height: 100%, that could be causing problems too.
Images have display:inline; by default, that's what's causing the whitespace between your images. You can do three things to prevent this:
float:left;
or
display:inline-block;
or
display:block;

HTML Layout : Continuous images

I have a series of images, which I want to make them showing in a row, i.e.
[img][img][img][img][img][img][img][img][img][img][img][img][img][img][img]
I want the overflow part will be hidden.
My current HTML is as follow:
<div id="gallery">
<img src="http://www.o2h.com.hk/images/apple_gala.JPG" class="gallery_img" />
<img src="http://upload.wikimedia.org/wikipedia/commons/1/15/Red_Apple.jpg" class="gallery_img" />
<img src="http://www.o2h.com.hk/images/apple_gala.JPG" class="gallery_img" />
<img src="http://upload.wikimedia.org/wikipedia/commons/1/15/Red_Apple.jpg" class="gallery_img" />
<img src="http://www.o2h.com.hk/images/apple_gala.JPG" class="gallery_img" />
<img src="http://upload.wikimedia.org/wikipedia/commons/1/15/Red_Apple.jpg" class="gallery_img" />
<img src="http://www.o2h.com.hk/images/apple_gala.JPG" class="gallery_img" />
<img src="http://upload.wikimedia.org/wikipedia/commons/1/15/Red_Apple.jpg" class="gallery_img" />
<img src="http://www.o2h.com.hk/images/apple_gala.JPG" class="gallery_img" />
</div>
and this is the CSS:
#gallery {
width: 100%;
height: 200px;
overflow: hidden;
}
#gallery .gallery_img {
width: auto;
height: 200px;
}
Here is the jsFiddle. I would like to show half image in the edge of the screen, like this:
However, I can only manage to show full images only. How should I modify the HTML / CSS codes ?
Just use overwidth in percentage, and a mask div:
DEMO
#gallery {
width: 200%; /* this is the trick */
}
#gallery img { /* and no need to add class="gallery_img" to IMGs anymore */
height: 200px;
}
#wrapper {
height: 200px;
overflow: hidden;
}
<div id="wrapper">
<div id="gallery">
<img src="http://www.o2h.com.hk/images/apple_gala.JPG" />
<img src="http://www.o2h.com.hk/images/apple_gala.JPG" />
<img src="http://www.o2h.com.hk/images/apple_gala.JPG" />
<img src="http://www.o2h.com.hk/images/apple_gala.JPG" />
<img src="http://www.o2h.com.hk/images/apple_gala.JPG" />
<img src="http://www.o2h.com.hk/images/apple_gala.JPG" />
<img src="http://www.o2h.com.hk/images/apple_gala.JPG" />
<img src="http://www.o2h.com.hk/images/apple_gala.JPG" />
</div>
</div>
EDIT
I'll add the simplest solution too (posted by #RupaliShinde), because it shows the correct use of the white-space attribute, and appearently it is hard to reproduce (reading the comments):
#gallery {
width: 100%;
overflow: hidden;
white-space: nowrap; /* this is the trick */
}
#gallery img {
height: 200px;
}
DEMO
Have a look at this fiddle - http://jsfiddle.net/jQkHp/
I added a wrapper div around gallery:
#gallery {
position: absolute;
left: 0;
top: 0;
width: 1200px;
height: 200px;
overflow: hidden;
}
#gallery .gallery_img {
width: auto;
height: 200px;
}
#wrapper {
position: relative;
width: 100%;
height: 200px;
overflow: hidden;
}
You will need to use some javascript to set the width of the gallery div for this to work properly (I'm only guessing at the width at the moment).
Edit - added some js to calculate the width of gallery based on the images inside it http://jsfiddle.net/jQkHp/1/
var galleryWidth = 0;
$('#gallery > img').each(function(){
galleryWidth += $(this).width();
});
$('#gallery').css('width', galleryWidth);
Just add this in CSS
<style>
#gallery {
width: 100%;
height: 200px;
overflow: hidden;
white-space: nowrap;
}
#gallery img {
width: auto;
height: 200px;
}
</style>
Let rest of code be as it is.
I would suggest putting the images in an unordered list within your containing div, with each image in it's own LI and then set them to display: inline-block in the css.
You'll need some way of scrolling through the images, of course, for which you could use some jquery.