CSS center image in a clipped parent div
http://codepen.io/gcyrillus/pen/BdtEj
use text-align , line-height , vertical-align and negative margin. img virtually reduced to zero, will center itself.
.wrapper {
width:300px;
text-align:center;
line-height:300px;
height:300px;
border:solid;
margin:2em auto;
overflow:visible; /* let's see what we slice off */
}
img {margin:-100%;vertical-align:middle;
/* to show whats been cut off */
position:relative;
z-index:-1;
}
For horizontal only :
.wrapper {
width:300px;
text-align:center;
border:solid;
margin:2em auto;
overflow:hidden
}
img {
margin:0 -100%;
vertical-align:middle;
}
How to place image inside of a parent div properly
I achieved this by absolute
positioning and ::before
selector. Also I added border radius to the image with a max width.
HTML:
<div class="mainInfo">
<div class="circle">
<img src="https://picsum.photos/200" />
</div>
</div>
CSS:
.mainInfo{
background-color: #fff;
border: .01rem round #e0e1ed;
border-radius: 20px;
color: #585CA1;
width:100%;
height:5em;
box-shadow: 0px 0px 17px -5px rgba(0,0,0,0.75);
margin-top: 3em;
display: flex;
align-items: center;
justify-content: center;
}
.circle {
position: relative;
width: 8em;
height: 8em;
background: #fff;
border-radius: 50%;
box-shadow: 0px 0px 17px -5px rgba(0,0,0,0.65);
}
.circle:before{
position: absolute;
content: "";
width: 15em;
height: 5em;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: #fff;
}
.circle img {
position: absolute;
max-width: 85%;
border-radius: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 200;
}
Link to the Demo
center IMG/ inside a DIV with CSS
You can make it work if you wrap another element around the image:
<div class="outer">
<div class="inner"><img src="" alt="Sample Image" /></div>
</div>
And the following CSS:
.outer {
width: 300px;
border: 1px solid red;
overflow: hidden;
*position: relative;
}
.inner {
float: left;
position: relative;
left: 50%;
}
img {
display: block;
position: relative;
left: -50%;
}
The position: relative
on the .outer
is marked with *
so it only applies to IE6/7. You could move it to a conditional IE stylesheet if that's what you prefer, or remove the *
altogether. It's needed to avoid the now relatively positioned children from overflowing.
How to make an image center (vertically & horizontally) inside a bigger div
Personally, I'd place it as the background image within the div, the CSS for that being:
#demo {
background: url(bg_apple_little.gif) no-repeat center center;
height: 200px;
width: 200px;
}
(Assumes a div with id="demo"
as you are already specifying height
and width
adding a background
shouldn't be an issue)
Let the browser take the strain.
center and clip image inside div
Try this:
.Image1 {
background: url(images/bg.jpg) no-repeat center center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
How to center an image within a div (both vertical and horizontal) and cut off excess?
Hopefully this helps you with your query. If you have any questions, drop a comment and I'll try to amend my post to suit your needs.
.preview { background: red; width: 200px; max-height: 100px; display: flex; align-items: center; justify-content: center; overflow: hidden;}
.preview:hover { overflow: visible;}
.preview img { width: 150px;}
<div class="preview"> <img src="http://placehold.it/150x150"></div>
CSS: Make image fill parent but not change parent size
UPDATE:
The method I was using (With flexbox) did not do what I wanted finally so I decided to use css grid and it did it perfectly!
Here is the new css (same html):
.ImagesContainer {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: .25rem;
width: 100%;
}
.ImagesContainer img {
width: 100%;
height: 100%;
}
.ImagesContainer a {
padding: 5px;
}
Thank you to everyone, your answers helped me understand css better!
center image in div with overflow hidden
You should make the container relative and give it a height as well and you're done.
http://jsfiddle.net/jaap/wjw83/4/
.main { width: 300px; margin: 0 auto; overflow: hidden; position: relative; height: 200px;}
img.absolute { left: 50%; margin-left: -200px; position: absolute;}
<div class="main"> <img class="absolute" src="http://via.placeholder.com/400x200/A44/EED?text=Hello" alt="Sample Image" /></div><br /><img src="http://via.placeholder.com/400x200/A44/EED?text=Hello" alt="Sample Image" />
Related Topics
Is Using @Import Declarations a Bad Practice
How to Select a Radio Button in CSS
CSS @Supports (::Pseudo-Element)
Text Wrapping Incorrectly for Radio Button
CSS - Smooth Button Gradient Color Transition on Hover
Has Anyone Created a 3D Website That Works on a 3D Monitor
CSS Selector to Check That Attribute Does Not Contain Both Values
Add Additional Box-Shadow to an Element Where Existing Shadow Is Unknown
How to Make an Element with Fixed Position Relative to Its Parent, Not The Whole Screen
Broke Page Styles of Vue.Js App (Webpack Template) When Live Changing It in Chrome Devtools
Google Font Not Working with Bootstrap
CSS3 Element with Opacity:0 (Invisible) Responds to Mouse Events
Gulp-Sass Work Around for Load_Path Support
CSS Transform and Transition on Hover Do Not Work After Animation-Fill-Mode: Forwards
Systematically Resolve Conflicting Styles in CSS
Margin Does Not Push Down Another Margin
Bootstrap - Spacing Between Form-Inline Elements
Should I Locally Store CSS Generated by The Google Web Fonts API