Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizing div based on window width

I have kind of a weird question. On my page I have a main image of a planet in some heavy duty nebula. I have it set up so the min width is 1000px and max is 1500px. I have the sides fading out and this looks great with larger screens. What I'd like to try to do is when you're looking at it on a mobile device for example and it's cutting off the width at 1000 pixels, I'd like the image say 1300 pixels wide, centered and 150 pixels is cut off each side so you can't see the fade out at all, but is still able to then enlarge is the window's width becomes larger say on like a large iMac and that fade then becomes visible again once you pass that 1300 pixel width.

My initial thought was to do something with negative margins on either side, but I couldn't get this to work while keeping the max and mix widths.

This is that specific section of code from the page, though the html and css is right there for everyone to see, you can just use the fine command to find that div ID for any further looking.

<div style="position:relative;width:100%;">    <div id="help" style="       position:relative;       z-index:1;       height:100%;       min-width: 1000px;       max-width: 1500px;       margin: 0 auto;    ">       <img src="http://i.stack.imgur.com/tFshX.jpg" border="0" style="width:100%;">    </div> </div> 

Any thoughts on this, it's very close to working the way I'd like it to, just needs a small tweak.

like image 254
loriensleafs Avatar asked Apr 09 '12 15:04

loriensleafs


People also ask

What determines div size?

By default a div is a block element, it means its width will be 100% of its container width, and its height will expand to cover all of its children. In case its children has a larger width than the div 's container width, the div itself does not expand horizontally but its children spill outside its boundary instead.

Is a div 100% width by default?

auto automatically computes the width such that the total width of the div fits the parent, but setting 100% will force the content alone to 100%, meaning the padding etc. will stick out of the div, making it larger than the parent. so setting the 'width' to 'auto' would be better? Yes, but that's the default anyway.


1 Answers

Viewport units for CSS

1vw = 1% of viewport width 1vh = 1% of viewport height 

This way, you don't have to write many different media queries or javascript.

If you prefer JS

window.innerWidth; window.innerHeight; 
like image 143
atilkan Avatar answered Oct 07 '22 15:10

atilkan