CSS: How to scale an image from the center instead of top-left
Just replace width: 400px;
with transform: scale(2,2)
on :hover
.
img {
width: 100%; max-width: 100%;
}
div {
position: absolute; left: 20%; top: 20%;
width: 250px;
transition: all 2s ease-in-out;
}
div:hover {
transform: scale(2,2)
}
<div>
<a href="http://photobucket.com/images/cat" target="_blank">
<img src="http://i583.photobucket.com/albums/ss278/campipr/coolcat.gif" border="0" alt="cat photo: cat coolcat.gif"/>
</a>
</div>
CSS: center and scale up or down image while preserving aspect ratio
You can drop the inner img element and do like this.
This will work no matter image proportions.
.container {
margin: 10px;
width: 115px;
height: 115px;
border: 1px solid red;
background: url(http://i.imgur.com/H9lpVkZ.jpg) no-repeat center center;
background-size: contain;
}
<div class='container'>
</div>
<div class='container' style='width:50px;height:100px;line-height:100px'>
</div>
<div class='container' style='width:140px;height:70px;line-height:70px'>
</div>
This one should scale up
<div class='container' style='width:350px;height:350px;line-height:350px'>
</div>
css/html, how to scale and center a logo image
The img
tag is okay. The center
tag is obsolete since about 1867. But you can easily use text-align: center
as a style on the parent element of the logo.
So with a piece of HTML like this:
<div class="logo"><img src="..."></div>
and a piece of CSS like this:
.logo {
text-align: center;
}
.logo img {
width: 50%;
}
You should have a centered logo of about half the width of the page.
There is no shame in using the img
tag, and in fact, if you want to scale, then it's very convenient to have this. An image will automatically scale its height relatively according to its width. There is no easy way to get the same effect using just a background image.
Alternatively, you can make the logo a background. But then you'll have to specify the height of the container, because it doesn't have any content at all and will otherwise collapse:
.logo {
text-align: center;
background-color: red;
background-image: url('http://jsfiddle.net/img/logo.png');
background-size: contain;
background-position: 50% 0;
background-repeat: no-repeat;
height: 100px;
}
http://jsfiddle.net/cwj2b/
Transform: scale an image from top to bottom
By default, an element transforms with it's center point as the origin. So in this case it will scale from the center on out. You can change this by setting transform-origin
, like you did.
Simple example:
div {
margin: 3em auto;
width: 50px;
height: 50px;
background: red;
}
div:hover {
transform: scale(1.9);
}
.d2 {
transform-origin: top;
}
<div></div>
<hr />
<div class="d2"></div>
Related Topics
How to Use CSS to Replace or Change Text
Absolute Positioning Inside Relative Positioning
@Font-Face Won't Load via Https in Ie
Are CSS Stylesheets Loaded Asynchronously
Why Is This Non-Float Margin Collapsing with a Float
How to Make a Twitter Bootstrap Modal Slide from the Side or Bottom Instead of Sliding Down
Twitter Bootstrap: Center Text on Progress Bar
How to Create Zebra Stripes on HTML Table Without Using JavaScript and Even/Odd Classes Generation
How to Create Multiple Box-Shadow Values in Less CSS
Keep Div Height Relevant to Aspect Ratio
Keep Footer with Variable Height on Bottom
Dynamically Define a Variable in Less CSS
Cannot Get Bootstrap 4 Horizontal Form to Work as Required
CSS Alternative for Display:Box for Ie and Opera
Bootstrap - Do We Have to Use Rows and Columns