I have an image inside a span tag, the span has a set width and height, and is set to overflow hidden. so it only reveals a small portion of the image. This works but the small portion of the image that is visible is the top left corner. I would like it to be the center of the image that is visible. I think I need to absolutely position the image, but the size of the image can vary though. Does anyone know how to do what I am trying to do?
Thanks!
Here is the HTML:
<div class="lightbox_images">
<h6>Alternate Views</h6>
<span>
<a href="http://www.kranichs.com/mothers_rings/mothers_rings_txt2.jpg" rel="lightbox[product_alternate_views]" title="This is my captions 1">
<img src="http://www.kranichs.com/mothers_rings/mothers_rings_txt2.jpg" />
</a>
</span>
<span>
<a href="https://www.kranichs.com/product_images/Simon-G#346_M_346_M.jpg" rel="lightbox[product_alternate_views]" title="This is my captions 2">
<img src="https://www.kranichs.com/product_images/Simon-G#346_M_346_M.jpg" />
</a>
</span>
<span>
<a href="http://www.kranichs.com/images/simong/sim_banner_01.jpg" rel="lightbox[product_alternate_views]" title="This is my captions 3">
<img src="http://www.kranichs.com/images/simong/sim_banner_01.jpg" />
</a>
</span>
<span>
<a href="http://www.kranichs.com/images/psu/psu_banner.jpg" rel="lightbox[product_alternate_views]" title="This is my captions 4">
<img src="http://www.kranichs.com/images/psu/psu_banner.jpg" />
</a>
</span>
</div>
Here is the CSS:
.lightbox_images{
background-color:#F9F9F9;
border:1px solid #F0F0F0;
}
.lightbox_images h6{
font-family:Verdana, Arial, Helvetica, sans-serif;
color:#333333;
font-size:14px;
font-weight:bold;
font-style:italic;
text-decoration:none;
margin:0px;
}
.lightbox_images span{
padding:5px;
padding-bottom:15px;
background-color:#DFDFDF;
margin:5px;
display:inline-block;
border:1px solid #CCC;
}
.lightbox_images a{
display:inline-block;
width:60px;
height:60px;
overflow:hidden;
position:relative;
}
.lightbox_images a img{
position:absolute;
left:-50%;
top:-50%;
}
.lightbox_images span:hover{
border:1px solid #BBB;
background-color:#CFCFCF;
}
As proposed in https://stackoverflow.com/a/14837947/2227298 by Billy Moat, there is a solution without knowing the image height and width.
Try it here: http://jsfiddle.net/LSKRy/
<div class="outer">
<div class="inner">
<img src="http://3.bp.blogspot.com/-zvTnqSbUAk8/Tm49IrDAVCI/AAAAAAAACv8/05Ood5LcjkE/s1600/Ferrari-458-Italia-Nighthawk-6.jpg" alt="" />
</div>
</div>
.outer {
width: 300px;
overflow: hidden;
}
.inner {
display: inline-block;
position: relative;
right: -50%;
}
img {
position: relative;
left: -50%;
}
Given this sort of HTML:
<span><img src="..." width="..." height="..." alt="..." /></span>
You could use CSS like this:
span {
position: relative;
display: block;
width: 50px; /* Change this */
height: 50px; /* Change this */
overflow: hidden;
border: 1px solid #000;
}
span img {
position: absolute;
left: -10px; /* Change this */
top: -10px; /* Change this */
}
You can then center the image based on its exact dimensions.
Alternatively, if you're able to modify the HTML, you could instead use something like this:
<div>
[name of picture]
</div>
Then, match it with this CSS:
div {
width: 50px;
height: 50px;
background: transparent url(...) center center no-repeat;
border: 1px solid #000;
}
div a {
display: block;
height: 100%;
text-indent: -9999em; /* Hides the link text */
}
In this case, the background will be automatically centered regardless of its dimensions, and it'll still be clickable.
This example, the images are at the center of the element, regardless of its size
HTML:
<div class="box">
<img src="example.jpg">
</div>
CSS:
div.box{
width: 100px;
height: 100px
overflow: hidden;
text-align: center;
}
div.box > img{
left: 50%;
margin-left: -100%;
position: relative;
width: auto !important;
height: 100px !important;
}
If the width and height of the image varies, I think the only way to do this is with javascript.
Style the image to left:50%; top:50%; and then, use javascript (image onload event maybe) to add margin-left:-imageWidth/2 px; margin-top:-imageHeight/2 px;
So basically you have
span img {
position: absolute;
left: 50%;
top: 50%;
}
and the following js
window.onload = function() {
var images = document.getElementsByTagName('img');
for(i=0; i<images.length; i++)
images[i].onload = centerImage(images[i]);
function centerImage(img) {
img.style.marginLeft = -(img.width/2) + "px";
img.style.marginTop = -(img.height/2) + "px";
}
}
PS. If you're using a javascript framework/library the code could simplify a bit, but I didn't make that assumption.
You can set the image as the background of the element and set x,y axis as in the following example:
#mySpan {
background-image: url(myimage.png);
background-repeat: no-repeat;
background-attachment:fixed;
background-position: -10 -10
}
Related
I have an ion slide box and it displays images. I want to add a button, ion-plus-round, to the bottom right side of the image. I don't want to add it to the bottom right of the slider, but rather to the bottom right side of the img only.
<ion-slide-box class="item-slide-box">
<ion-slide ng-repeat="image in vm.item.images" ng-cloak >
<img ng-src="{{image}}">
</ion-slide>
</ion-slide-box>
<a class="button icon ion-plus-round myButton"></a>
I have done the following but I am wondering how I can get the button to be float: right inside of the img:
.slider {
height: 60vh;
background-color: #76838f;
position: relative;
}
.slider-slide {
color: #000;
background-color: #76838f;
height: 100%;
}
.slider .slider-slide .img-ng {
display: block;
margin-left: auto;
margin-right: auto;
height: 100%;
max-width: 100%;
position: relative;
}
.slider .slider-pager {
bottom: 45px;
}
.myButton {
position: absolute;
bottom: 0px;
right: 0px;
}
codepan: http://codepen.io/anon/pen/gPYBpR
I have just edited you code, you can try this in code pane and check whether it is fulling your requirements.
Html code:
<ion-slide-box class="item-slide-box">
<ion-slide ng-cloak class="content">
<img class="img-ng" ng-src="http://geomorph.sourceforge.net/fourier/Bouboule256.jpg">
<a class="button icon ion-plus-round myButton"></a>
</ion-slide>
</ion-slide-box>
add some css line like:
.content{
position:relative;
}
.content a{
position:absolute;
bottom:5px;
right:5px;
}
Note:remove ".myButton" css from css and place button after the image source as shown in code,Hope this would exactly what you want.
For example if you really want the button exactly on the image just follow the example code and you can implement this code in your code.
HTML:
<div class="main">
<img src="http://placehold.it/182x121"/>
Can be BUTTON/LINK
</div>
CSS:
.main{
width: 182px; /*328 co je 1/3 - 20margin left*/
height: 121px;
line-height: 20px;
margin-top: 0px;
margin-left: 9px;
margin-right:0px;
display:inline-block;
position:relative;
}
.content a {
position:absolute;
bottom:5px;
right:5px;
background:blue;
color:#FFF;
}
above code will can help you give button on the image.
I have a header that I would like to keep centered inside a parent image div both horizontally and vertically at all times when the parent div does not have a fixed width and height but instead has a responsive width and height using Twitter Bootstrap 3.1's responsive columns.
HTML:
<div id ="firstholder" class= " col-sm-4 col-md-4 col-lg-4">
<a href="home.html" title="Home" class="imglink">
<div class="item1"><h1 class="slickfont1" >Home</h1>
</div><img src="/images/slide2.JPG" alt="City Lights Image" class="img-responsive" >
</a>
</div>
#firstholder {
display:inline-block;
float:left;
margin:0;
padding:0;
height:auto;
position:relative;
}
a.imglink {
background: #fff;
display: inline-block;
width:100%;
height:auto;
position:relative;
}
.item1 {
height:150px;
width: 150px;
margin: 0 auto;
position:absolute;
z-index:100;
vertical-align:middle;
}
.slickfont1 {
z-index:10;
color: #fff;
position:absolute;
font-family: 'Bowlby One SC', cursive;
font-size: 37px;
font-weight:lighter;
position: absolute;
text-shadow:0 0 0 transparent, -2px 2px 2px #1542bd;
}
Thanks :)
You can use this nice method by Chris Coyier at CSS Tricks, he uses percentages and CSS3's transform:translate to achieve what you need. Centering Percentage Width/Height Elements
Now as you're using Bootstrap, so you're to tweak it for yourself.
The code from Chris Coyier:
.center {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 40%;
height: 50%;
}
Better to go to CSSTricks (using above link) and study the whole Post (you'll also find the reason of using it and why its better than other methods of centring).
I hope this helped you.
I am not sure if I understood you right, but let's start from here:
CSS:
div.center {
display: table-cell;
width: 200px;
height: 200px;
vertical-align: middle;
text-align: center;
border: 1px solid #000;
background-color: #ccc;
}
div.inner {
margin: 0 auto;
}
HTML:
<div class="center">
<div class="inner">
<div class="title">
Title Here
</div>
</div>
</div>
Is this what you are trying to do? assuming the gray background is an image? SAMPLE HERE
I am trying to place a div with text on top of an image but for some reason it doesn't work. My code is:
<div id="pics">
<div class="inner">
<img src=".." class="pic" height="310" width="310">
<div class="cover">blah blah</div>
</div>
</div>
my CSS is:
#pics{
overflow:hidden;
display:block;
}
.inner a{
position:relative;
margin:3px;
padding:10px;
top:10px;
}
.inner{
position: relative;
display: inline-block;
}
.cover{
position: absolute;
background-color: black;
color: white;
left: 0;
right: 0;
bottom: 0;
top: 0px;
}
I have tried many things but it doesn't seem to work. I might have messed up my cs somewhere
That's because you're targetting an ID and not a class.
In other words, in the CSS you have the definition for an ID (#cover) and the HTML code has a class:
<div class="cover">blah blah</div>
Either change the HTML to have an ID:
<div id="cover">blah blah</div>
or change the CSS to target the class name:
.cover{
position: absolute;
background-color: black;
color: white;
width: 100%;
height: 100%;
border-style: solid 5px;
top: 0px;
}
UPDATE:
You are giving the .cover a width and height of 100%, but absolute positioned elements don't really "understand" that, so I suggest changing it to:
(place the left, bottom and right to the edges, this will fit the div as 100% width and height of the relative parent)
.cover{
position: absolute;
background-color: black;
color: white;
left: 0;
right: 0;
bottom: 0;
border-style: solid 5px;
top: 0px;
}
How about setting the picture as background via the background-image: attribute.
You then could make your div clickable with <div onclick="window.location="url";>
In detail your css would look like this:
.image {
width:310px;
height:310px;
background-image:url('pathtoimage');
}
I want to set the background image so that it appears in front of foreground image.
I tried the following code but it is not working as I expected:
.background-image
{
border :0px;
background-image: url(/images/icon.png);
z-index:1000;
}
.foreground-image
{
width:200px;
height:113px;
border :0px;
z-index:1;
}
<div>
<a href="#" class="background-image">
<img class="foreground-image" src="./images/pic1.jpg" />
</a>
</div>
I am using CSS2.
Here is one way to do it.
If this is your HTML:
<div>
<a href="#" class="background-image">
<img class="foreground-image" src="http://placekitten.com/200/100"/>
</a>
</div>
apply the following CSS rules:
div {
border: 1px dotted blue;
}
.background-image {
border: 1px dotted red;
background-image: url(http://placekitten.com/200/50);
background-position: center left;
background-repeat: repeat-x;
display: block;
width: 500px;
}
.foreground-image {
vertical-align: top;
width: 200px;
border: 0px;
position: relative;
z-index: -1;
}
Set position: relative on the image and throw the image deeper into the stacking order by using z-index: -1.
See demo at: http://jsfiddle.net/audetwebdesign/HUK28/
by removing the foreground-image.
.background-image {
border :0;
background-image: url(/images/icon.png);
width:200px;
height:113px;
display: block;
}
.foreground-image {/**/}
<div>
</div>
I have a situation where I have one div of fixed width, containing an image pulled from Twitter, and another div of variable width containing user text of variable length. What I want to achieve is something like the following:
I can do this well enough with a single div that has background-image and padding-left. But I want to be able to apply border-radius to the img element, which simply won't be possible with a background-image.
If I do text-align: center on the outer div, it gets me halfway there. Here's a DEMO and a screenshot:
But this obviously isn't fully what I want.
How can I accomplish this?
Ask and you shall receive — a simplified jsFiddle example:
As an added bonus, the text is vertically centered too!
HTML:
<div class="logo">
<div class="logo-container">
<img src="http://img.tweetimag.es/i/appsumo_b.png" />
</div>
<div class="logo-name">
AppSumo is a really really long title that continues down the page
</div>
</div>
CSS:
.logo {
background-color: #eee;
display: table-cell;
height: 100px;
position: relative;
text-align: center;
vertical-align: middle;
width: 600px;
}
.logo-container {
background-color: #fff;
border-radius: 10px;
left: 10px;
position: absolute;
top: 10px;
width: 75px;
}
.logo-name {
font: bold 28px/115% Helvetica, Arial, sans-serif;
padding-left: 85px;
}
Would it be something like this?
http://jsfiddle.net/uPPTM/6/
.logo {
width:80%;
margin:auto;
background-color: red;
}
.logo-container {
border: 1px solid gold;
width:73px;
height: 73px;
display: inline-block;
vertical-align:middle;
}
.logo-name {
display: inline-block;
}
You can float the image container (or image itself without the container) to the left, clearing anything the left... and then float the text to the left, clearing anything to the right.
.logo-container{
float:left;
clear:left;
}
.logo-name{
float:left;
clear:right;
}
You can adjust the distance of the text using margins.
.logo-name{
float:left;
clear:right;
margin-top:10px;
margin-left:5px;
}
Use absolute positioning with a left position to push the title text past the image.
http://jsfiddle.net/uPPTM/9/
.logo { width: 50px; }
.title {
position: absolute;
top: 0;
left: 50px;
font-size: 32px;
text-align: center;
}
img {
border: 1px solid gray;
border-radius: 15px;
}
<div class="logo">
<div class="logo-container">
<img src="http://img.tweetimag.es/i/appsumo_b.png">
</div>
<div class="logo-name">AppSumo</div>
</div>