Center image in DIV and hide the overflowing - css

This is kind of an unique case. I am trying to achieve the following:
Square images should be 100% 100%. Basically fill out the entire DIV (which is squared)
All images should fill out the entire height of the DIV.
All images should be aligned center
If they overflow the X-axis, they should overflow and hide
https://jsfiddle.net/cxnyLxfn/2/
.image-container {
position: relative;
width: 300px;
height: 300px;
display: inline-block;
margin: 20px;
border: 1px solid black;
float: left;
overflow: hidden;
}
.image-container img {
height: 100%;
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
}
I almost achieved this, except for centering the images no matter what. The Google logo should be centered and the rest look fine as they are. How do I achieve my four requirements?

I usually use a flex approach to this:
.image-container {
display: flex;
width: 300px;
height: 300px;
border: 1px solid black;
overflow: hidden;
position: relative;
align-items: center;
justify-content: center;
}
img {
display: block;
height: 100%;
}
<div class="image-container">
<img src="https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png" />
</div>
<div class="image-container">
<img src="https://i.kinja-img.com/gawker-media/image/upload/s--pEKSmwzm--/c_scale,fl_progressive,q_80,w_800/1414228815325188681.jpg" />
</div>
<div class="image-container">
<img src="http://www.zevendesign.com/wp-content/uploads/2015/04/google-logo-progress-270x480.jpg" />
</div>
<div class="image-container">
<img src="https://cdn.pixabay.com/photo/2015/10/31/12/56/google-1015752_960_720.png" />
</div>

put css for img
img {
height: 100%;
position: absolute;
left: 50%;
transform: translate(-50%,0);
}

Try this:
img {
height: 100%;
position: absolute;
left: 50%;
top: 50%;
transform:translate(-50%, -50%);
}

Using object fit property...
fiddle
.image-container {
width: 300px;
height: 300px;
display: inline-block;
margin: 20px;
border: 1px solid black;
float: left;
}
img {
object-fit: contain;
height: 100%;
width: 100%;
}
<div class="image-container">
<img src="https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png" />
</div>
<div class="image-container">
<img src="https://i.kinja-img.com/gawker-media/image/upload/s--pEKSmwzm--/c_scale,fl_progressive,q_80,w_800/1414228815325188681.jpg" />
</div>
<div class="image-container">
<img src="http://www.zevendesign.com/wp-content/uploads/2015/04/google-logo-progress-270x480.jpg" />
</div>
<div class="image-container">
<img src="https://cdn.pixabay.com/photo/2015/10/31/12/56/google-1015752_960_720.png" />
</div>

The tansform:translate() will do the magic. Here is an working example-
.image-container {
position: relative;
width: 300px;
height: 300px;
display: inline-block;
margin: 20px;
border: 1px solid black;
float: left;
overflow: hidden;
}
img {
height: 100%;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<div class="image-container">
<img src="https://assets-cdn.github.com/images/modules/logos_page/GitHub-Mark.png" />
</div>
<div class="image-container">
<img src="https://i.kinja-img.com/gawker-media/image/upload/s--pEKSmwzm--/c_scale,fl_progressive,q_80,w_800/1414228815325188681.jpg" />
</div>
<div class="image-container">
<img src="http://www.zevendesign.com/wp-content/uploads/2015/04/google-logo-progress-270x480.jpg" />
</div>
<div class="image-container">
<img src="https://cdn.pixabay.com/photo/2015/10/31/12/56/google-1015752_960_720.png" />
</div>

Related

CSS position: sticky; bottom: 0 not sticking to bottom [duplicate]

This question already has answers here:
Why bottom:0 doesn't work with position:sticky?
(2 answers)
If you specify `bottom: 0` for position: sticky, why is it doing something different from the specs?
(3 answers)
Closed 11 months ago.
I can't get #up-arrow to stick to the bottom of .container.
.container {
height: 100vh;
width: 100vh;
background-color: yellow;
}
#up-arrow {
width: 45px;
height: 45px;
border: 2px solid #23ADF8;
border-radius: 23.5px;
background-color: #23ADF8;
position: -webkit-sticky;
position: sticky;
bottom: 0;
}
#up-arrow:hover {
background-color: white;
}
#up-arrow:hover img {
filter: invert(63%) sepia(35%) saturate(5648%) hue-rotate(174deg) brightness(102%) contrast(95%);
}
<div class="container">
<div id="up-arrow">
<a href="#top">
<img src="https://mandoemedia.com/wp-content/uploads/2022/03/up.svg">
</a>
</div>
</div>
The element #up-arrow will not stick to the bottom of the container with the current layout.
Because, sticky element is positioned according to the normal flow of the document, and then offset relative to its nearest scrolling ancestor and containing block (nearest block-level ancestor).
Here the element #up-arrow is at the top of the container, hence the element will not be able to stick to bottom on scroll.
Add some content on top of #up-arrow to see sticky working.
Sample Implementation
.container {
min-height: 100vh;
width: 100vh;
background-color: yellow;
}
#up-arrow {
width: 45px;
height: 45px;
border: 2px solid #23ADF8;
border-radius: 23.5px;
background-color: #23ADF8;
position: -webkit-sticky;
position: sticky;
bottom: 0;
}
#up-arrow:hover {
background-color: white;
}
#up-arrow:hover img {
filter: invert(63%) sepia(35%) saturate(5648%) hue-rotate(174deg) brightness(102%) contrast(95%);
}
#an-element {
height: 100vh;
}
<div class="container">
<div id="an-element"></div>
<div id="up-arrow">
<a href="#top">
<img src="https://mandoemedia.com/wp-content/uploads/2022/03/up.svg">
</a>
</div>
</div>
OR
use position: relative; parent and position: absolute; child
Working Fiddle
.container {
height: 100vh;
width: 100vh;
background-color: yellow;
position: relative;
}
#up-arrow {
width: 45px;
height: 45px;
border: 2px solid #23ADF8;
border-radius: 23.5px;
background-color: #23ADF8;
position: absolute;
bottom: 0;
}
#up-arrow:hover {
background-color: white;
}
#up-arrow:hover img {
filter: invert(63%) sepia(35%) saturate(5648%) hue-rotate(174deg) brightness(102%) contrast(95%);
}
<div class="container">
<div id="up-arrow">
<a href="#top">
<img src="https://mandoemedia.com/wp-content/uploads/2022/03/up.svg">
</a>
</div>
</div>
as you haven't wrote any other content sitcky position may not work fine.
please review below stuff and you might get what you want,
.container {
height: 300vh;
width: 100vh;
background-color: yellow;
}
#up-arrow {
position: -webkit-sticky;
position: sticky;
bottom: 0;
width: 45px;
height: 45px;
border: 2px solid #23ADF8;
border-radius: 23.5px;
background-color: #23ADF8;
}
#up-arrow:hover {
background-color: white;
}
#up-arrow:hover img {
filter: invert(63%) sepia(35%) saturate(5648%) hue-rotate(174deg) brightness(102%) contrast(95%);
}
<div class="container">
<div style="background: red;">Scroll</div>
<div style="height: 300px; background: orange;"></div>
<div class="sticky">Sticky Section</div>
<div style="background: pink;">Scroll</div>
<div style="height: 300px; background: green;"></div>
<div class="sticky">Sticky Section</div>
<div style="background: red;">Scroll</div>
<div style="height: 300px; background: orange;"></div>
<div class="sticky">Sticky Section</div>
<div id="up-arrow">
<a href="#top">
<img src="https://mandoemedia.com/wp-content/uploads/2022/03/up.svg">
</a>
</div>
<div style="background: pink;">Scroll</div>
<div style="height: 300px; background: green;"></div>
<div class="sticky">Sticky Section</div>
</div>
Note: I have made sticky at bottom position and for your understanding how bottom positioned sticky works I've also added some stuff below sticky positioned content.
Whenever content below sticky position renders position of screen will be initial(normal).
References: https://www.w3schools.com/howto/howto_css_sticky_element.asp

Move image some pixels with object-position

I am trying to "move" an image inside a div which is adjusted with object-fit: cover; and object-position: center;.
My question is: Can I move the center-positioned image a few pixels to the right without to remove the object-* from my css?
.myDiv {
width: 150px;
height: 150px;
margin: 0 15px;
}
.myDiv.before img {
width: 100%;
height: 100%;
}
.myDiv.after img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
.general {
display: flex;
}
<div class="general">
<div class="myDiv before">
<img src="https://via.placeholder.com/400x250">
</div>
<div class="myDiv after">
<img src="https://via.placeholder.com/400x250">
</div>
</div>
I would like to NOT resize the image as in .after, but moving the image a few px right/left in the div. Is there a way to do it?
Yes, simply adjust the object-position value. The property works the same way as background-position
The object-position property determines the alignment of the replaced element inside its box. The <position> value type (which is also used for background-position) is defined in [CSS-VALUES-3], and is resolved using the concrete object size as the object area and the content box as the positioning area. ref
Related question: Using percentage values with background-position on a linear-gradient
.myDiv {
width: 150px;
height: 150px;
margin: 0 15px;
}
.myDiv.before img {
width: 100%;
height: 100%;
}
.myDiv.after img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
.myDiv.after.moved img {
object-position:calc(50% + 10px) 50%; /* the fix is here ! */
}
.general {
display: flex;
}
<div class="general">
<div class="myDiv before">
<img src="https://via.placeholder.com/400x250">
</div>
<div class="myDiv after">
<img src="https://via.placeholder.com/400x250">
</div>
</div>
<div class="general">
<div class="myDiv before">
<img src="https://via.placeholder.com/400x250">
</div>
<div class="myDiv after moved">
<img src="https://via.placeholder.com/400x250">
</div>
</div>
You can move the images by setting left or right css attribute on the relative position as follows on <img> tag.
.myDiv {
width: 150px;
height: 150px;
margin: 0 15px;
}
.myDiv.before img {
width: 100%;
height: 100%;
position: relative;
left: 10px;
}
.myDiv.after img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
position: relative;
right: 10px;
}
.general {
display: flex;
}
<div class="general">
<div class="myDiv before">
<img src="https://via.placeholder.com/400x250">
</div>
<div class="myDiv after">
<img src="https://via.placeholder.com/400x250">
</div>
</div>
Do it with padding.
.myDiv {
width: 150px;
height: 150px;
margin: 0 15px;
/* Move the image left or right */
padding-left: 10px;
/* padding-right: 10px */
box-sizing: border-box;
background: blue;
}
.myDiv.before img {
width: 100%;
height: 100%;
}
.myDiv.after img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
.general {
display: flex;
}
<div class="general">
<div class="myDiv before">
<img src="https://via.placeholder.com/400x250">
</div>
<div class="myDiv after">
<img src="https://via.placeholder.com/400x250">
</div>
</div>
Right now your image dimension is not changing... Please have a look.
.myDiv {
width: 150px;
height: 150px;
margin: 0 15px;
}
.myDiv.before {
position: relative;
left: 20px;
}
.myDiv.before img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
.myDiv.after img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
.general {
display: flex;
}
<div class="general">
<div class="myDiv before">
<img src="https://via.placeholder.com/400x250">
</div>
<div class="myDiv after">
<img src="https://via.placeholder.com/400x250">
</div>
</div>
Please have a look...do you need like that way?
You could just move the img inside your div like this:
.myDiv img { position: relative; left: 10px; //or top, bottom, right }
.myDiv {
width: 150px;
height: 150px;
margin: 0 15px;
/* just for demo purpose */
background-color: green;
}
.myDiv.before img {
width: 100%;
height: 100%;
}
.myDiv.after img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
.general {
display: flex;
}
.myDiv img {
position: relative;
left: 10px;
}
<div class="general">
<div class="myDiv before">
<img src="https://via.placeholder.com/400x250">
</div>
<div class="myDiv after">
<img src="https://via.placeholder.com/400x250">
</div>
</div>

Centering an image with display:table-cell

I want to center-align and bottom-align an <img> in a container block. The <img> needs to be constrained to the container dimensions, so max-width:100% and max-height:100% are set on it. Also I need to align a label to the top-right corner of the <img>, which makes things more complicated.
I have a working version using display:inline-block and line-height (below):
.block {
line-height: 236px;
text-align: center;
width: 236px;
height: 236px;
background: rgba(255, 0, 0, .3);
}
.flex {
background: rgba(255, 0, 0, .3);
display: inline-block;
vertical-align: bottom;
position: relative;
line-height: 18px;
}
img {
display: block;
max-width: 236px;
max-height: 236px;
width: auto;
height: auto;
}
label {
position: absolute;
top: 0;
right: 0;
}
<div class="block">
<div class="flex">
<img src="http://i.imgur.com/x7q4E80.png" />
<label>X</label>
</div>
</div>
<br>
<!-- 374 × 187 -->
<div class="block">
<div class="flex">
<img src="http://i.imgur.com/dBy1WKI.png" />
<label>X</label>
</div>
</div>
View on Codepen
However this requires a reset on the line-height of the label. Is it possible to use a different method like display:table-cell to center and bottom-align the content?
My closest attempt is below:
.block {
display: table-cell;
vertical-align: bottom;
width: 236px;
height: 236px;
background: rgba(255, 0, 0, .3);
}
.flex {
background: rgba(255, 0, 0, .3);
display: table-cell;
vertical-align: bottom;
position: relative;
}
img {
display: block;
max-width: 236px;
max-height: 236px;
width: auto;
height: auto;
}
label {
position: absolute;
top: 0;
right: 0;
}
<div class="block">
<div class="flex">
<img src="http://i.imgur.com/x7q4E80.png" />
<label>X</label>
</div>
</div>
<br>
<!-- 374 × 187 -->
<div class="block">
<div class="flex">
<img src="http://i.imgur.com/dBy1WKI.png" />
<label>X</label>
</div>
</div>
View on Codepen
You can simplify your code same as like below.
.block1 {
background-color: red;
width: 100px;
height: 100px;
position: relative;
}
.block2 {
background-color: red;
width: 100px;
height: 100px;
position: relative;
margin-top: 10px;
}
.block1 a {
width: 20px;
height: 20px;
vertical-align: middle;
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
.block1 a label {
position: absolute;
top: 4px;
right: 0;
line-height: 0;
}
.block2 a {
width: 20px;
height: 20px;
vertical-align: middle;
margin: auto;
position: absolute;
left: 0;
bottom: 0;
right: 0;
}
.block2 a label {
position: absolute;
top: 4px;
right: 0;
line-height: 0;
}
<div class="block1">
<div class="ctr-algn">
<a href="#">
<img src=".../images">
<label>x</label>
</a>
</div>
</div>
<div class="block2">
<div class="ctr-algn">
<a href="#">
<img src=".../images">
<label>x</label>
</a>
</div>
</div>
I hope you solve your issue.

How to make a grid reponsive?

I have created a website and i am unable to make the tiled layout responsive. I am a beginner and i have no idea how to make my website responsive. Any help will be appreciated. The css and html is given below. Plus the background is repeating itself 3 times while i have added no repeat property in css.
HTML
<div class="grid">
<div class="tile hvr-reveal " id="tile1">
<div style="text-align: center;">
<div class="img-with-text ">
<p>
<strong>
Contact Us
</strong>
<img src="homepage images/file242.png" alt="sometext" width="64" height="64" id="img0"/>
</p>
</div>
</div>
</div>
<div class="tile hvr-reveal" id="tile2">
<div style="text-align: center;">
<div class="img-with-text">
<p>
<strong>
Products
</strong>
<img src="homepage images/shopping145.png" alt="sometext" width="64" height="64" id="img"/>
</p>
</div>
</div>
</div>
<div class="tile hvr-reveal " id="tile3">
<div style="text-align: center;">
<div class="img-with-text">
<p>
<strong>
Partners
</strong>
<img src="homepage images/celebration19.png" alt="sometext" width="64" height="64" id="img2"/>
</p>
</div>
</div>
</div>
<div class="tile hvr-reveal" id="tile4">
<div style="text-align: center;">
<div class="img-with-text">
<p>
<strong>
About Us
</strong>
<img src="homepage images/men16.png" alt="sometext" width="64" height="64" id="img1"/>
</p>
</div>
</div>
</div>
</div>
CSS
body {
font-family: chewy;
color: #ffffff;
background-image: url(pictures%20for%20web/bg3.jpg) ;
background-position: center center;
background-attachment: fixed;
background-size: cover;
**strong text**background-repeat: no-repeat;
webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
}
.grid {
width: 1140px;
height: 650px;
margin: auto;
}
.tile {
position: absolute;
width: 200px;
left: 451px;
top: 152px;
height: 152px;
box-sizing: border-box;
}
#tile1 {
top: 407px;
left: 754px;
width: 338px;
height: 196px;
margin-left: 2px;
margin-right: 2px;
background-color: #ff3300;
}
#tile2 {
margin-left: 2px;
margin-right: 2px;
top: 100px;
left: 980px;
width: 148px;
height: 154px;
background-color: #008000;
}
#tile3 {
top: 255px;
left: 523px;
width: 200px;
height: 150px;
background-color: #660066 ;
margin-left: 2px;
margin-right: 2px;
}
#tile4 {
top: 255px;
left: 118px;
width: 200px;
height: 150px;
background-color: #990000;
margin-left: 2px;
margin-right: 2px;
}
I'm guessing you want something like this:
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
.container {
position: relative;
top: 0;
left: 0;
width: 90%;
height: auto;
margin-left: 5%;
background-color: #444;
display: block;
text-align: center;
}
.box {
border: solid 1px #000;
height: 200px;
width: 200px;
display: inline-block;
margin: 25px auto;
}
.box:nth-of-type(1){
background: yellow;
}
.box:nth-of-type(2){
background: red;
}
.box:nth-of-type(3){
background: blue;
}
.box:nth-of-type(4){
background: green;
}
Which will keep the boxes aligned and depending on the width of the browser, shifts them.
http://codepen.io/DB1500/pen/JGqaBZ

responsive inline block enquiry

I need some advice on this issue i'm having. In the jsfiddle below, I'm trying to create a responsive grid layout. The issue with what i have is, i would like the text to be in the middle of each individual grid. I've tried bumping it using margin-top but instead of the images stacking onto each other while resizing, the images are overlapping each other. The end result desired will be to have the text aligned center onto the image and have no gaps on all sides of the grid when resizing according to various screen resolution.
Link: http://jsfiddle.net/kelvinchow/VaDS9/
<style type="text/css">
#wrapper {
display: block;
width: 100%;
height: auto;
background: green;
}
.box {
display: inline-block;
float: left;
width: 50%;
height: auto;
vertical-align: baseline;
background: red;
}
.box-img img {
width: 100% !important;
height: auto;
}
.box-title {
display: block;
background: grey;
height: 25px;
font-size: 20px;
font-family: helvetica, san serif;
color: blue;
text-align: center;
margin-top: -100px;
}
</style>
<div id="wrapper">
<div class="box">
<div class="box-img">
<img src="http://airinlee.com/wp-content/uploads/2013/06/thumbnail6.png">
</div>
<p class="box-title">howdy</p>
</div>
<div class="box">
<div class="box-img">
<img src="http://airinlee.com/wp-content/uploads/2013/06/thumbnail6.png">
</div>
<p class="box-title">howdy</p>
</div>
<div class="box">
<div class="box-img">
<img src="http://airinlee.com/wp-content/uploads/2013/06/thumbnail6.png">
</div>
<p class="box-title">howdy</p>
</div>
<div class="box">
<div class="box-img">
<img src="http://airinlee.com/wp-content/uploads/2013/06/thumbnail6.png">
</div>
<p class="box-title">howdy</p>
</div>
</div>
You'll get this:
Fiddle here: http://jsbin.com/osazav/1.
With this markup:
<body>
<div id="tl" class="box">
<p class="box-title">howdy</p>
</div>
<div id="tr" class="box">
<p class="box-title">howdy</p>
</div>
<div id="bl" class="box">
<p class="box-title">howdy</p>
</div>
<div id="br" class="box">
<p class="box-title">howdy</p>
</div>
</body>
And this css:
div.box {
background: url('http://airinlee.com/wp-content/uploads/2013/06/thumbnail6.png');
position: absolute;
height: 50%;
width: 50%;
background-size: cover;
background-position: center;
}
div.box p.box-title {
color: red;
background-color: black;
padding: 5px;
position: absolute;
margin: -10px -20px;
top: 50%;
left: 50%;
}
div.box#tl { top: 0%; left: 0%; }
div.box#tr { top: 0%; left: 50%; }
div.box#bl { top: 50%; left: 0%; }
div.box#br { top: 50%; left: 50%; }

Resources