how to make container responsive when device change? - html

I have created a div, and give it an initial size, css as below, I want it can responsive in different devices.
.main {
font-size: 14px;
line-height: 1.4;
font-family: museo_sans,Helvetica Neue,Helvetica,Arial,Lucida Grande,sans-serif;
width: 30em;
height: 40em;
background-color: tomato;
border: solid 1px black;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto
}
demo result
What I want is make it responsive, when view in different device.
Similar as a component in Airtasker website, in the browser, it has its initial size (width and heigth) enter image description here
but when viewed on different device, or just zoom out browser, it will auto-scale to fit the screen
enter image description here
I know the solution may be very simple, but I just don't understand how to do it.

You can achieve it in many ways, and two most common would probably be:
setting its width to some percentage value (like 80%) and some fixed max-width value. This way it will take 80% of the screen on smaller devices, but won't be bigger than the fixed value you specified on bigger screens;
.modal {
display: block;
width: 80%;
max-width: 50rem;
height: 30rem;
background-color: lightgray;
margin: 0 auto;
}
<div class="container">
<div class="modal"></div>
</div>
using media-queries, where you will have exact control over how elements behave under certain conditions (screen size most commonly).

Related

How to set dimensions of an element to fill only the initial window/viewport size?

I'm trying to make an element maintain only to the initial 100% height of the viewport. So anytime the user resize their windows, the height shouldn't be responsive.
Am i suppose to write javascript to get the initial viewport height and then create those deliver those dimensions to the css? Anybody can show me how to do that?
* {
margin: 0;
padding: 0;
}
.top {
height: 100vh;
width: 100%;
background-color: yellow;
}
.bottom {
height: 300px;
width: 100%;
background-color: red;
}
class top is the div i want to maintain its initial 100% viewport size.
I don't think you need a javascript for that. The height: 100vh; from your code will do just fine.

Image Viewer - toggle between thumbnail and full size

One code sample code sample show a image at its original size with scrollbars while the other proportionally scales the image to fit inside it parent.
I want to initially show the image proportionally resized and after the user does something (click, hover, etc...) the image is shown at full size with scrollbars. When the user stops doing an action the image returns.
Essentially what I want to do is toggle between the two states without messing up the page's layout.
My images can be both landscape and portrait in nature with dimensions of up to 5184 pixels to show detail, although most are cropped to 2500 to 4100.
Restrictions:
No scripts of any kind are permitted - they will be stripped out.
No <a> links permitted - they will be stripped.
No active content of any kind - will be stripped out.
I cannot insert the actual widths and heights of images into the <img> tag as I am using a program to generate a html template and it does not have access to those dimensions.
I'd like the divisions in which the images are seen to resize to the user's screen, thus the 96vh code above (not sure if this is the right technique).
So far I have tried using various schemes using divisions with a hidden checkbox toggle and have tried using a <ul> list, but I can't seem to get everything to work correctly. I typically can get one version of the image to work, but it typically breaks how the other version of the image is viewed or worse yet, it messes up the page layout.
Show image at full size with scrollbars inside division:
<center>
<div class="gsimagewrapper">
<img class="gsimage" src="http://anthology.vastserve.com/kimtechto-1476773165-95808.jpg">
</div>
<div class="gsimagewrapper">
<img class="gsimage" src="http://anthology.vastserve.com/kimtechto-1476773167-95809.jpg">
</div>
</center>
.gsimagewrapper {
position: relative;
width: 96vw;
height: 96vh;
overflow: auto;
margin: 1vh 0px;
padding: 0px;
box-sizing: border-box;
}
.gsimage {
max-width: none;
height: auto;
}
Show scaled down version of image inside division:
.gsimagewrapper {
position: relative;
width: 96vw;
height: 96vh;
overflow: auto;
margin: 1vh 0px;
padding: 0px;
box-sizing: border-box;
}
.gsimage {
width: 100%;
height: auto;
}
Inspired by this answer: Can I have an onclick effect in CSS?
You can try to use a checkbox as a button. This is the JSFiddle: https://jsfiddle.net/sxpgvj6z/
HTML
<center>
<div class="gsimagewrapper">
<input type="checkbox" id="btnControl"/>
<label class="btn" for="btnControl">
<img class="gsimage" src="http://anthology.vastserve.com/kimtechto-1476773165-95808.jpg">
</label>
</div>
</center>
CSS
#btnControl { display: none; }
.gsimagewrapper { position: relative; width: 96vw; height: 96vh; overflow: auto; margin: 1vh 0px; padding: 0px; box-sizing: border-box; }
.gsimage { max-width: none; height: auto; }
#btnControl:checked + label > img {
width: 100%;
height: auto;
}

Mobile auto scaling of images

I have a basic static S.P.A. that I've gotten styled the way I want when developing on a laptop. When I test on a mobile viewport and scroll up or down the images/content resize. I believe that is built into the chrome mobile browser but I'm wondering if there may be a way to disable or to style around this? A few of the sections of content utilize full background images with text overlaid on top. When the user scrolls and the image resizes it pushes the content into the next section vs. having a clean ending point. I know it's possible because I use mobile pages all the time that don't have this issue. I just don't know how to do it.
.landingImage {
width: 100vw;
height: 100vh;
background-image: url('https://upload.wikimedia.org/wikipedia/commons/6/6f/Black_gram.jpg');
background-position: right;
background-size: cover;
margin-left: auto;
margin-right: auto ;
}
.titleContainer {
position: absolute;
width: 85vw;
height: 75vh;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
border: 2px solid white;
font-family: 'Open Sans Condensed', sans-serif;
font-size: 16vw;
color: white;
text-shadow: 1px 1px black;
}
.titleText {
margin-top: 10px;
margin-left: 10px;
}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<div class="landingImage">
<div class="titleContainer">
<h1 class="titleText">SHIFT<br>WORKS<br>BICYCLE<br>OPERATIONS</h1>
</div>
</div>
One issue I can see is that the landingImage and titleContainer divs have their heights set relative to the viewport width, e.g.
.landingImage {
...
height: 100vh;
...
}
This means that if the viewport is say 400px wide, the .landingImage div will be 400px high, and this may not be enough.
One option is to use #media queries in you CSS and refine the height of these divs based on the viewport width. e.g.
#media screen and (max-width: 480px) {
.landingImage {
height: 200vh;
}
}
Another possibility is that in your existing CSS you add rule so that the div doesn't shrink below a certain minimum, e.g.
.landingImage {
...
min-height: 800px
...
}
You can set any number of #media media statements and refine the settings to match the specifics of your page.
Good luck!

HTML/CSS bakcground around page and image peeping out of the page

I'm trying to solve the problem showed on the picture.
I have a site of a width of 980px (margin:auto) centered in the middle of the page and I need surrounding background of a width of 400px each side. But when user narrows the width of the browser the background shouldn't affect horizontal bar (only the width of the page itself 980px)
Plus there's an image which is placed 80% in the main page and the rest outside of the page. I also want this piece of the image (20%) not to affect the horizontal bar when user narrows the width of the window.
THANKS!!!
image:
http://tinypic.com/r/ri58io/8
code: link to fiddlehttps://jsfiddle.net/c0ro66s4/
The thing with this design is that the 'background' boxes have a fix width. When the fill the rest of remaining width (next to the content) no scrollbar appears (at least, not in Firefox).
So what I've did is made a media query. When the screen size is bigger than (980+400+400=) 1780px the boxes will have their normal width. As soon as the screensize becomes under the 1780px, the width will be set to auto and we use the 'left' position, which makes them fill the screen and won't show the scrollbars.
I made the testcase in JSFiddle with half the sizes (otherwise it wouldn't fit on my screen).
<div id="content">Content</div>
<div id="bgLeft"> </div>
<div id="bgRight"> </div>
body, html { margin: 0; padding: 0; min-height: 100%; }
#content {
background: lightblue;
position: absolute;
width: 490px;
height: 100%;
left: 50%;
margin-left: -245px;
}
#bgLeft {
background: lightgreen;
position: absolute;
width: 200px;
right: 50%;
margin-right: 245px;
height: 100%;
}
#bgRight {
background: lightgreen;
position: absolute;
width: 200px;
left: 50%;
margin-left: 245px;
height: 100%;
}
#media screen and ( max-width: 890px ) {
#bgLeft {
width: auto;
left: 0;
}
#bgRight {
width: auto;
right: 0;
}
}
Set the image as background in both boxes and a align the one in the left box on the right and the one in the right box on the left.
DEMO
EDIT
Added your background-images: DEMO 2

HTML / CSS Display fixed ratio images based on the height of the page

There is probably a relentlessly simple solution to this but I've been chasing my tail for a while so I've come to ask those wiser and smarter than me.
I've got a website for a personal project I'm making which displays images within a lightbox. See image:
The header area (red) is fixed height.
I want the images (yellow) to sit within a light box (green) which also has a caption. Crucially the images displayed need to retain their aspect ratio, 5:4, and fill the remaining height left below the header (bar a small margin top and bottom).
There's probably a really simple, elegant solution out there but I've not found it.
Any help gratefully received.
EDIT ---
Here's a fiddle of what I'm trying to do: http://jsfiddle.net/qh2V8/
Even this isn't right as I've had to put a fixed width in to even try and get it to work.
CSS:
#header{
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 145px;
background-color: #F00;
}
#overlayBg {
position: fixed;
top: 155px;
bottom: 20px;
padding: 8px;
padding-bottom: 30px;
left: 0px;
right: 0px;
margin-left: auto;
margin-right: auto;
background: #FF0;
width: 400px;
}
#overlayContainer img {
position: relative;
height: 100%;
width: 100%;
}
#overlayBg p {
position: relative;
padding-top: 8px;
padding-bottom: 10px;
font-family: 'Josefin Sans', sans-serif;
font-weight: 600;
font-size: 14px;
}
HTML:
<div id="header"></div>
<div id="overlayBg">
<div id="overlayContainer">
<img src="http://i.imgur.com/u9VIg60.jpg" />
</div>
<p>Caption</p>
</div>
The image size need to be set through scripting, unless the images are a fixed constant size. The following link is of good help to your problem: Change image size with JavaScript
I'm pretty sure that you can get the original size of the image through yourImg.Style.Height and yourImg.Style.Width, and then make the calculations required to make it a 5:4 picture..
Here's where I got to.
There are fixed ratio solutions if you are basing the size of the element on width, using :before and padding-top. There's a good write up here.
There is a fixed ratio solution if you are basing the size of the element on height, however the height must be a % of the height of the screen. Written up here in another Stackoverflow question:
Resize a Div Based on Height but Retain Aspect Ratio (almost got it) Strange Reload bug
If you have a fixed pixel size header or footer and need an element to expand to fill the exact size remaining you can't do it with just HTML and CSS.
Here's a codepen.io of where I got to:
http://codepen.io/niazipan/pen/ydkGt
JS to set the image height, and CSS to style everything else around it. Code below:
HTML
<div id="overlayBg">
<div id="overlayContainer">
<img src="http://i.imgur.com/u9VIg60.jpg" id="yourImgId" />
</div>
<p>Caption</p>
</div>
CSS
html, body {
height: 100%;
text-align: center;
}
#header{
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 50px;
background-color: #F00;
}
#overlayBg {
position: fixed;
top: 55px;
padding: 8px;
margin-left: auto;
margin-right: auto;
background: #FF0;
position: relative;
text-align: left;
}
#overlayContainer {
height: 100% !important;
width: 100%;
}
#overlayBg p {
font-family: 'Josefin Sans', sans-serif;
font-weight: 600;
font-size: 14px;
margin-top: 5px;
margin-bottom: 5px;
}
JS
var size = window.innerHeight - 120;
document.getElementById('yourImgId').style.height = size + 'px';
document.getElementById('overlayBg').style.width = size * 1.25 +'px';