Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make image wider than parent <div> while preserving vertical space?

Tags:

html

css

If you have a parent div of fixed width, is it possible to make a child <img> wider than the parent, while constraining proportions, while also preserving vertical space?

While position:absolute; can break the image out of the normal document flow, I don't want to remove the vertical space the image retains. If I did, any content that appears after the image would be pushed upwards, and as a result, it would appear behind the image.

Here's an example fiddle.

Image one represents the vertical behavior I want. Obviously, if the image were horizontally larger, the height should increase proportionally. Image two represents the horizontal behavior I would want. However, this does not preserve vertical space. I experimented with negative margins, but couldn't get anything viable working.

Assuming unknown image dimensions, is the desired effect possible without JavaScript?

like image 703
Nathanael Avatar asked Aug 22 '14 18:08

Nathanael


People also ask

How do I make my div wider than my parents?

- First set the width of the extended-content-container div to 500%. This will make the div you are trying to extend 5 time wider than the center column it lives inside. This can be adjusted up or down depending on the size of the center column you are working with.

How do I make an image occupy a whole div?

To auto-resize an image or a video to fit in a div container use object-fit property. It is used to specify how an image or video fits in the container. object-fit property: This property is used to specify how an image or video resize and fit the container.

How do I stretch an image in HTML?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.

How do you make a div not bigger than a parent?

You can simply use the CSS display property with the value inline-block to make a <div> not larger than its contents (i.e. only expand to as wide as its contents).


3 Answers

Try with this:

#wrapper img.two {
    display: block;
    margin: 30px 0;
    width: 100vw;
    margin-left: 50%;
    -webkit-transform: translateX(-50%);
    -moz-transform: translateX(-50%);
    -ms-transform: translateX(-50%);
    -o-transform: translateX(-50%);
    transform: translateX(-50%);
}

width: 100vw will make image stretch to screen width, margin-left: 50% will center left border of image to the center of the wrapper, and then transform: translateX(-50%) will move image 50% of its width to the left.

You can set image width to whatever you want. It will stay centered and it will have margins at the top and bottom.

demo: http://jsfiddle.net/7cjr6jff/

like image 163
Bodzio Avatar answered Oct 21 '22 10:10

Bodzio


Once browser support catches up, you could consider using calc to apply a negative margin.

DEMO using calc()

html {
  font-size: 10px; /* Set a base value for easy math, or for sane defaults */
}

body {
  background-color: #FC9;
  margin: 0;
}

#wrapper {
  font-size: 1.9rem; /* Or whatever */
  background-color: #FFF;
  margin: 0 auto;
  padding: 3rem;
  width: 400px;
}

#wrapper img.one {
  display: block;
  margin: 3rem 0;
  width: 100%;
}

#wrapper img.two {
  display: block;
  width: 100vw;
  /* 
   * Math break: 
   *   viewport width: 100vw
   *   #wrapper width: 400px
   *   remainder: (100vw - 400px)
   *   want to shift the image by half of the remainder, so: (100vw - 400px) / 2
   *   and apply it as a negative margin: -1 * (100vw - 400px) / 2)
   */
  margin: 3rem 0;
  margin-left: -webkit-calc(-1 * (100vw - 400px) / 2);
  margin-left: calc(-1 * (100vw - 400px) / 2);
}

If your requirements for a fixed-width #wrapper element aren't firm, you could actually use vw for the whole thing, as in:

#wrapper {
  ...
  width: 40vw;
}

Which simplifies the margin-left declaration to use hardcoded vw units, which has better browser support:

#wrapper img.two {
  display: block;
  width: 100vw;
  /* 
   * Math break: 
   *   viewport width: 100vw
   *   #wrapper width: 40vw
   *   remainder: 100vw - 40vw = 60vw
   *   want to shift the image by half of the remainder, so: 60vw / 2 = 30vw
   *   and apply it as a negative margin: -30vw
   */
  margin: 3rem 0 3rem -30vw;
}

DEMO of straight-up vw

In that case, if this design needs to support screen widths below which the 40vw of your content wrapper looks gross, you'll either need to use a media query to assign a higher % of the viewport width to #wrapper, or to degrade the design so that it uses different layout calculations and doesn't use the content-spanning image.

like image 2
Palpatim Avatar answered Oct 21 '22 10:10

Palpatim


by this code you don't need to change margin from top or bottom

<style>
.wrapper
{
width: 400px;
margin:auto;
background:#e6e6e6;
}

.wrapper img
{
width:120%;
clear:left;
margin-left:-10%;
}
</style>

<div class="wrapper">

Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu
<img src="http://wallpoper.com/images/00/31/09/01/pokemon-pikachu_00310901.jpg" />
Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu Pikachu
</div> 
like image 1
Ankit Agrawal Avatar answered Oct 21 '22 11:10

Ankit Agrawal