This question already has answers here:
Horizontal center dynamic image in div with absolute position
(2 answers)
Closed 8 years ago.
I want to have a page which displays an image as large as possible but with the whole image in view, without changing the proportions of the image, and centred on the page (i.e. full height with borders either side, or full width with borders top and bottom). Is there a way to do this purely in CSS? I've tried various combinations of width/height/min-width/min-height properties and can't get it to display as I'd like.
I also want to be able to overlay a link that consists of a div with a background image, which changes on hover, the code for which is below. I want this to be positioned at the top right of the image:
HTML:
<div class="imagecontainer">
<img src='gallery/images/<?php echo $image;?>' max-height="100%" width="100%"/>
<div class="backbuttoncontainer">
<a class="gallerybackbutton" href="gallery/index.htm"></a>
</div>
</div>
CSS:
.imagecontainer{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
.backbuttoncontainer{
text-align:right;
margin-top: 10px;
margin-right: 10px;
height: 38px;
width: 100%;
display: block;
}
.gallerybackbutton{
background: url('icons/back_to_gallery.png') bottom;
display: inline-block;
height: 28px;
}
.gallerybackbutton:hover{
background-position: 0 0;
}
perhaps this is what you want : JSFIDDLE, here's how:
create a container for the image in any dimension you want (200x200 or 400x200 or 200x400 ), then put the image inside the container, with style
vertical-align: middle // to make your image centered vertically, but it's relative to the inline element, so if you don't have any text or set line-height for it's siblings, it doesn't work
max-width: 200px; // or your container width
max-height: 200px; // or your container height
// this style is used for my example with the container max-width and max-height set to 200px
then in the container add this style
text-align: center; // to make it centered horizontally
width: 200px; // for example
height: 200px; // for example
border: 1px solid red; // only to show the container in this example
line-height: 200px; // this will make the inline element in the center from 200px, or so
or another method by using pseudo element in this JSFIDDLE, you need to make a container and the image that going to be centered :
<div class="block">
<img src="http://cdn.wonderfulengineering.com/wp-content/uploads/2014/07/background-wallpapers-7.jpg" class="centered" />
</div>
add pseudo element to the container block
.block:before {
content:'';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; // to remove the spacing that's created by inline-block
}
and add style for the image that's going to be centered
.centered {
display: inline-block;
vertical-align: middle;
max-width: 100%;
max-height: 100%;
}
here's the explanation from css tricks: centering in the unknown
To use margin: auto you have to have a static width.
But your Image is a pixel image and will have that static width.
Just set your width of image and margin to auto:
.backbuttoncontainer{
margin:auto;
height: 38px;
width: 100px;
}
set image as background and try the following code
class-name{
background:url('1.jpg') no-repeat center center fixed ;
width:100%;
height:auto;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size:cover;
background-size: cover;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='1.jpg',sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='1.jpg',sizingMethod='scale')";
Related
I am trying to display a div within a background-image. I have tried to do it in the same way that I would do it using an img but it does not work:
WITHIN IMG
HTML
<img class="scale" src="../../../assets/images/van-landing-2.JPG">
CSS
.scale {
max-width: 100%;
height: auto;
width: auto\9;
/* ie8 */
}
This is perfectly working. It shows an image that takes 100% of my screen and the height that should have to be proportional.
Photo
Nevertheless, when I try the same using a div it does not work. It displays nothing to cause it does not set a specific height, so It shows nothing.
WITHIN DIV
HTML
<div class="i-van"></div>
CSS
.i-van {
background-image: url("../../../assets/images/van-landing.JPG");
max-width: 100%;
height: auto;
width: auto\9;
/* ie8 */
}
Photo
How could I do it? I have tried using min-height and it shows it but just the minimum height. I would like to show it all.
The background-image property is used to add a background to an element. This means that the content with in that element is what dictates its size. Additionally, height:auto is interpreted by the elements content.
What you can try is to use height:100% providing that the parent elements also have defined height values. This will stretch the element to the tallest height and scale your background image accordingly.
If you are looking to display the image at the exact size or aspect ratio of the image itself, then the element will need to be defined with the exact width and height of the image.
From a semantics perspective however, you should decide if the image you are displaying is part of the content of your page or a decorative part of the design. In general, you should use a <img /> tag for images that are content and background-image for images that are not.
You can learn more about background-image here
You have to declare a valid height for your background div. As Maneesh has said height:auto takes the element content's height.
They key is to specify a height in vh or px.
After that you can easily place your text div inside it and set it
around with either flexbox or position absolute
Check the snippets! :)
CODE WITH FLEXBOX
body {
margin: 0;
}
.i-van {
display: flex;
align-items: center;
justify-content: center;
background-image: url(https://www.planwallpaper.com/static/images/3865967-wallpaper-full-hd_XNgM7er.jpg);
background-size: cover;
background-position: center;
max-width: 100%;
height: 100vh;
}
#div-inside {
font-size: 100px;
color: white;
}
<div class="i-van">
<div id="div-inside">
HELLO
</div>
</div>
CODE WITH POSITION ABSOLUTE
body {
margin: 0;
}
.i-van {
position: relative;
background-image: url(https://www.planwallpaper.com/static/images/3865967-wallpaper-full-hd_XNgM7er.jpg);
background-size: cover;
background-position: center;
max-width: 100%;
height: 100vh;
}
#div-inside {
position: absolute;
top: 50%;
/* position the top edge of the element at the middle of the parent */
left: 50%;
/* position the left edge of the element at the middle of the parent */
transform: translate(-50%, -50%);
/* This is a shorthand of translateX(-50%) and translateY(-50%) */
font-size: 100px;
color: white;
}
<div class="i-van">
<div id="div-inside">
HELLO
</div>
</div>
I have a problem in the content inside of my container. Because I already set some method to get the height of the background image equal to my container. My problem is when I put a content inside my container it detects my invisible image that I set.
Here's my code:
<div class="parent">
<div class="hidden-image">
<img src="http://placehold.it/350x350" />
</div>
<h1>
text
</h1>
</div>
CSS:
.parent {
border: 1px solid red;
width: 350px;
height: auto;
position: relative;
display: flex;
align-items: center;
justify-content: center;
background: url('http://placehold.it/350x350');
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
}
.parent img {
visibility: hidden;
width: 100%;
height: auto;
}
If you check my fiddle, the text moved to the right because I think that it is because of the image I set.
fiddle:
https://jsfiddle.net/kdu9691c/
This is because visibility: hidden doesn't remove the element from flow. It still takes up the same space it normally would, it's just invisible. You might try using position: absolute on either the image or the text div; or if the idea is just to keep the div a certain proportion to match the image, google css padded box aspect ratio for some neat workarounds. You also have the option of background-size: cover, which may be helpful depending on your particular constraints.
I have a vector img that appears, with the correct dimensions, in a responsive div-box using this css:
.col1 > div {
width:100%;
height:25%;
position: relative;
}
.thisIMGcontainer {
height: 100%;
width: 100%;
position: absolute;
display: table;
}
.thisIMG {
background-image: url('../img/thisIMG.png');
background-size: contain;
background-repeat: no-repeat;
}
For this HTML:
<div class="col1">
<div class="box1">
<div class="thisIMGcontainer"><div class="thisIMG"></div></div>
</div><!--/box1-->
</div>
I have tried multiple combinations of CSS to get the img aligned in the center, but it either remains aligned to the left, or it randomly disappears!
display: table-caption; or display: inline-block; and margin: 0 auto;
You image div has no width, so it takes all available width - 100%. If you want an image to be centered, you either have to use <img> tag instead of using a background image, or you have to set a width in .thisIMG class.
I'm trying to center an element of a precise size on a background that is built specifically for it, in order to make it appear seamless.
In the following code, #window represents the browser's window size in pixels (change it to anything). #background obviously refers to the background image I'll be using, and #positionMe is the object I want to fit on the background. I want the background to always be centered in the browser even if the window is resized, and I want the kitten to always be centered in the black box on the background.
As you can see below, the problem is that the background isn't centered on the viewport to begin with; it's centered based on total width of the browser. And when you resize the screen, it doesn't adjust accordingly.
HTML:
<div id="window">
<div id="background">
<img id="positionMe" src="http://cs421018.vk.me/v421018778/74bc/NearuIJQIck.jpg" />
</div>
</div>
CSS:
#window {
background-color: red;
width: 1280px;
height: 720px;
}
#background {
background: url('http://i.imgur.com/xzDclz5.jpg') no-repeat;
width: 500px;
height: 500px;
margin: 0 auto;
}
#positionMe {
position: relative;
top: 174px;
left: 154px;
}
This Fiddle demonstrates my issue.
Using a combination of display:table-cell and vertical-align:center will center your image vertically. In addition, you can simply use text-align:center to center your image horizontally.
http://jsfiddle.net/reinmcha/XtQ37/10/
Might need to do a little adjusting to keep the background div centered. So, we add another div and set to display:table. The "table cell" will fill the whole thing. Now we center the table with margin: 0 auto.
Final Product:
http://jsfiddle.net/reinmcha/XtQ37/20/
Might need to do some updating to get the image to center perfectly with the border (since it has width...)
Here's my go at it.
I hope you are aware there are tons of articles on this topic. Search around. You'll find your answer :)
You basically have two options, one would be using a div to display an image and making the image a centered background like so:
<div id="window">
<div id="background">
<div id="centerMe"></div>
</div>
</div>
with css:
#centerMe {
width: 100%;
height: 100%;
background: url('http://cs421018.vk.me/v421018778/74bc/NearuIJQIck.jpg') no-repeat center;
}
or for a pure css solution:
#background {
background: url('http://i.imgur.com/xzDclz5.jpg') no-repeat;
width: 500px;
height: 500px;
margin: 0 auto;
text-align: center;
}
#background:before {
content: "";
display: inline-block;
height: 100%;
vertical-align: middle;
}
#centerMe {
vertical-align: middle;
}
I want to position an image into a center square and fill in the remaining space with colour. With the below code though, my image is on top and the text "test" is right below the image. I want the text to be outside the 120x120 square.
I tried the below code:
CSS (included in head):
.img-container {
width: 120px;
height: 120px;
display: inline-block;
overflow: hidden;
background-color:#000;
}
.img-container img {
width: 100%;
height: 100%;
}
HTML:
<a class="img-container" href="http://google.com"><img src="http://couponcoupon.biz/image/logo/landmsupply.com.jpg" /> Test </a>
Look at this article: http://coding.smashingmagazine.com/2013/08/09/absolute-horizontal-vertical-centering-css/
It has a great breakdown of absoulte center:
.Absolute-Center {
margin: auto;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
A fiddle with it applied to your example: http://jsfiddle.net/bhlaird/XgNXa/
I also moved the text down with a negative bottom: setting. I'm not wild about it, I'd add a wrapper div around the img-container a to have the text below it, but I didn't want to change your markup too much.
.img-container span {
position:absolute;
bottom: -20px;
left:0;right:0;
}
Wrap your text in a <span> then use CSS to move it around
Make sure your .img-container has position:relative; set and remove overflow:hidden;, then do this
.img-container span {
position:absolute;
left:125px;
your text will be wherever you want it.
As for the image being in the center. If it is a dynamic image (changing all the time) You will have to either experiment with display:table-cell or look into some javascript. If the image is the same one every time, you can just position it inside your img-container with position:absolute, top:45px or wherever center is.
First off, there's a typo in your example
height: 100%px;
this must be either 100% or 100px, but not both.
You can use background-image and background-position to center the image
CSS:
.img-container {
width: 120px;
height: 120px;
display: inline-block;
background-image: url(http://couponcoupon.biz/image/logo/landmsupply.com.jpg);
background-repeat: no-repeat;
background-position: center center;
background-color:#000;
}
To move the text below the square, remove overflow: hidden and add a margin to a wrapper around the text
HTML:
<a class="img-container" href="http://google.com">
<div class="center">Test</div>
</a>
CSS:
.img-container .center {
margin: 120px 45px;
}
Complete JSFiddle