Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop image shrinking when page is resized horizontally in CSS

I have a 1920x100 image which is inserted at the top of my page:

<img src="./Resources/Images/banner.jpeg" style="position:absolute; top:0px; left:0px; height:100px; width:100%;" name="top">

I'd like to have it so that when the page is resized horizontally, the image doesn't shrink with the page, but instead remains in the center of the screen, like this:

enter image description here

It seems like a simple thing that could be easily achieved, but I haven't found a solution.

I could put the image inside a div and set the left position of the div to be -(1920-pageWidth) / 2, but this would rely on JavaScript. I'm looking for a cleaner solution (if there is one), just to minimize complexity on the page, and maximize browser compatibility.

Is there any CSS property I could use to achieve this, or am I going the completely wrong way of achieving this?

Any help appreciated.

like image 922
Jack Greenhill Avatar asked May 18 '12 15:05

Jack Greenhill


People also ask

How do I stop my image from shrinking in CSS?

Just use the image as a background-image and use the background-position center 0 value to place it in the center top of the element.

How do I resize an image using CSS without losing the aspect ratio?

The Simple Solution Using CSSBy setting the width property to 100%, you are telling the image to take up all the horizontal space that is available. With the height property set to auto, your image's height changes proportionally with the width to ensure the aspect ratio is maintained.

How do I resize an image without distorting it CSS?

In that situation we can use CSS max-width or width to fit the image. Use max-width: 100% to limit the size but allow smaller image sizes, use width: 100% to always scale the image to fit the parent container width.

How do you resize an image proportionally in CSS?

Resize images with the CSS width and height properties Another way of resizing images is using the CSS width and height properties. Set the width property to a percentage value and the height to "auto". The image is going to be responsive (it will scale up and down).


2 Answers

You can add this css property to your image : min-width: 1280px

like image 65
gabitzish Avatar answered Oct 19 '22 18:10

gabitzish


With CSS

div {
  background: url(./Resources/Images/banner.jpeg) no-repeat scroll center top transparent;
  height: 100px;
}
like image 20
yunzen Avatar answered Oct 19 '22 17:10

yunzen