Shrink container to image width - html

I am currently developing a website where the user should be able to scroll horizontally through a landscape with clickable info points on it.
The webite is required to be fully responsive and to ensure this I want to put my landscape image and the info points in a container with the exact equal size of the image.
HTML:
<div id="container-main">
<div id="landscape">
<img class="background" src="image.jpg" />
<div class="point" style="top: 24%; left: 7.5%;"></div>
<div class="point" style="top: 29%; left: 17.7%;"></div>
<div class="point" style="top: 77%; left: 39%;"></div>
<div class="point" style="top: 26%; left: 68%;"></div>
<div class="point" style="top: 70%; left: 80%;"></div>
</div>
</div>
CSS:
html {
height: 100%;
}
body {
height: 100%;
min-width: 100%;
margin: 0;
padding: 0;
}
#container-main {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow-y: hidden;
}
#landscape {
position: relative;
display: inline-block;
height: 100%;
}
#landscape > img.background {
height: 100%;
display: block;
}
.point {
position: absolute;
width: 50px;
height: 50px;
margin: -25px 0 0 -25px;
background-color: white;
border-radius: 5px;
cursor: pointer;
}
My CSS works completetly fine until you resize the height of the browser window, then the info points move to wrong places.. however when you refresh the resized website it is working properly again.
Try it: Fiddle (resize the output and then click "run" again.)
In my real project I am setting the container's width to the image's width using javascript but I would love to have a clean CSS solution.
I know there are similar questions around but none of the suggested solutions works out for me.
Thank you in advance!

use vw insted of %
Adjust the code below:
<div id="container-main">
<div id="landscape">
<img class="background" src="image.jpg" />
<div class="point" style="top: 24vw; left: 7.5vw;"></div>
<div class="point" style="top: 29vw; left: 17.7vw;"></div>
<div class="point" style="top: 77vw; left: 39vw;"></div>
<div class="point" style="top: 26vw; left: 68vw;"></div>
<div class="point" style="top: 70vw; left: 80vw;"></div>
</div>
</div>
<style>
html {
height: 100%;
}
body {
height: 100%;
min-width: 100%;
margin: 0;
padding: 0;
}
#container-main {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow-y: hidden;
}
#landscape {
position: relative;
display: inline-block;
height: 100%;
}
#landscape > img.background {
height: 100%;
display: block;
}
.point {
position: absolute;
width: 50px;
height: 50px;
margin: -25px 0 0 -25px;
background-color: white;
border-radius: 5px;
cursor: pointer;
}
</style>
.....................Another solution.........................................................
change css:
html {
height: 100%;
}
body {
height: 100%;
min-width: 100%;
margin: 0;
padding: 0;
}
#container-main {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow-y: hidden;
}
#landscape {
position: relative;
display: inline-block;
width: 100%;
}
#landscape > img.background {
height: 100%;
display: block;
}
.point {
position: absolute;
width: 50px;
height: 50px;
margin: -25px 0 0 -25px;
background-color: white;
border-radius: 5px;
cursor: pointer;
}
NB: See the responsive coding standard of this site:

First off, never use position:fixed; on css unless you want the div or container to stay at the same place even when you scroll.
Second, i would recommend to use jquery to make a fluid layout. What a fluid layout does is, it keeps the content of the page in the same place even when you resize the website. If you dont want to waste time on writing a long script file, i would personally recommend you to use Dreamweaver. Dreamweaver updated their scripts to give the users easy way to make a fluid layout just by clicking some buttons. It will auto generate the script file and css for you. And if you're a manual coder like myself, than simply use dreamweaver to generate a fluid layout and start coding manually.
Hope this helps :)

Related

How to set the children height on absolute position parent

I have problem with how to make children size following its parent. The case below.
HTML
<div class="video">
<div class="spot">
<img src="..." alt="">
<button>x</button>
</div>
</div>
CSS
.video {
width: 600px;
height: 500px;
background: #000;
position: relative;
}
.spot {
position: absolute;
max-height: 30px;
top: 0;
left: 0;
display: table;
margin: 0;
max-width: 100%;
/* width: 16%; only height can affect image on content*/
}
.spot img {
width: 100%;
height: 100%;
}
.spot button {
position: absolute;
top: 0;
left: 100%;
margin-left: -24px;
}
What I want to do is to make the image follow the spot height. Because if I set width (whatever size), the image will follow the spot width. Anyone know how to do this?
I also create jsfiddle https://jsfiddle.net/isatrio/yosfep6r/14/.
Your image is already following your .spot height. Check the border on spot. Or do you mean overflow? Or do you want your .spot to follow your .video?
console.log(".spot height: " + $(".spot").innerHeight());
console.log("img height: " + $(".spot img").height());
.video {
width: 600px;
height: 500px;
background: #000;
position: relative;
}
.spot {
border: red 2px solid;
position: absolute;
height: 40%;
/*width: 20%;*/
top: 0;
left: 0;
margin: 0;
padding: 0;
max-width: 100%;
overflow: hidden;
}
.spot img {
/*width: 150%;*/
height: 100%;
}
.spot button {
position: absolute;
top: 0;
left: 100%;
margin-left: -24px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="video">
<div class="spot">
<img src="https://creativeblossoming.files.wordpress.com/2013/10/navy-living-room.jpg" alt="">
<button>x</button>
</div>
</div>
You have set max-width instead of width. max-width will not set the width to the given value, it will just prevent the width from surpassing the given value.
.spot {
position: absolute;
height: 30px;
top: 0;
left: 0;
display: table;
margin: 0;
width: 100%;
}

HTML - Why z-index is not working?

I've tried for some hours and this is getting on my nerves. I'm working with bootstrap and the spin.js library. I'm trying to put a color layer over an img tag, but this simply doesn't works.
The code which I'm working on is this
The CSS code
.container-fluid {
position: relative;
padding: 0;
margin: 0;
}
.header{
position: relative;
max-height: 920px;
height: 100%;
}
.header_layer{
position: relative;
top: 0px;
left: 0px;
width: 100%;
height: 100px;
background-color: darkgrey;
z-index: 100;
}
.img_header{
position: relative;
top: 0px;
left: 0px;
width: 100%;
z-index: 99;
}
The HTML code:
<div class="container-fluid">
<div class="header col-md-12">
<div class="header_layer"></div>
<img src="http://placehold.it/350x150" class="img-responsive img_header">
</div>
</div>
However, thanks a lot.
As you give position:relative, and top/left : 0 so the elements do not overlap I guess you need position:absolute

CSS for one-page Website with 100%-element on top

I need to create a div-element, which has 100% width and height for the visible part of the screen. Below this there should be another div-element with variable height, which can only be seen, if the user scrolls down. It is like a one-page website...
JSFiddle: http://jsfiddle.net/sopk6vx3/
#main {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#overlay {
display: none;
opacity: 0.8;
width: 100%;
height: 100%;
background-color: #000;
position: fixed;
top: 0;
left: 0;
}
#overlay section {
z-index: 100;
position: absolute;
top: 0px;
left: 0px;
background-color: #FFF;
width: 94%;
height: 90%;
border-radius: 5px;
margin: 2% 3%;
}
<div id="main">
<header>Navigation</header>
<footer>Footer of main-element</footer>
</div>
<div id="tour">
Here is some tour-information about the product
</div>
<div id="overlay">
<section>Main Content</section>
</div>
Via click on a navigation element the #overlay will be fade in to show the content.
So how do I do the correct CSS for the #main and #tour element? As in the fiddle it doesn't work.
And could the overlay-css been optimized?

How to set responsively images on images to appear fixed using HTML+CSS+JS?

I got a task where I have to rewrite an old Adobe Flex application in HTML, CSS, JavaScript.
It is an application for customers to demonstrate our product with several images (images on images, so let's say we have png files for background, house and stickfigures) and the images change based on interaction with the user.
For my example let's just have a background and two stickfigures.
The background should always take 100% of the width. As we resize the browser the stickfigures should stay always at the same place on the background.
At my first try
<style>
.container { position: relative; width: 100%; }
.my-background { position: absolute; left: 0; top: 0; width: 100%;}
.stickfigures { position: absolute; z-index: 10; }
.jane { left: 35%; top:25%; width: 40%; }
.james { left: 55%; top:55%; width: 15%; }
</style>
<div class="container">
<img src="background.png" class="my-background">
<img src="stickfig1.png" class="stickfigures jane">
<img src="stickfig2.png" class="stickfigures james">
</div>
Trying this way the top: xx%; doesn't seem to work, so I googled a bit and added height: 512px; to my container class. After that the 'top' worked, but as I resize the web browser the stickfigures move around on the background.
<style>
.container { position: relative; width: 100%; height: 512px,}
.my-background { position: absolute; left: 0; top: 0; width: 100%;}
.stickfigures { position: absolute; z-index: 10; }
.jane { left: 35%; top:25%; width: 40%; }
.james { left: 55%; top:55%; width: 15%; }
</style>
<div class="container">
<img src="background.png" class="my-background">
<img src="stickfig1.png" class="stickfigures jane">
<img src="stickfig2.png" class="stickfigures james">
</div>
Any help, clue, idea is really appreciated, please ask if something is not clear.
I made some changes in your exemple :
HTML
<div class="container">
<img src="background.png" class="my-background">
<img src="stickfig1.png" class="stickfigures jane">
<img src="stickfig2.png" class="stickfigures james">
</div>
CSS
.container {
position: relative;
width: 100%;
height: auto;
}
.my-background {
position: relative;
left: 0;
top: 0;
width: 100%;
height:auto
}
.stickfigures {
position: absolute;
z-index: 10;
}
.jane {
left: 5%;
top:20%;
width: 20%;
}
.james {
left: 60%;
top:50%;
width: 15%;
}
Please see the demo : JSFiddle

How to use 100% of the current window height (no more, no less)?

I am developing a web app that is supposed to have the feeling of a desktop app. I want my layout to make use of the entire browser window height (no more, no less). I've made an image that show the intended structure of the page.
Intended layout
In other words, I don't want the user to be able to scroll the entire page, but rather the different sections of the page. Similar to how most desktop applications work.
I've read everything about how to create columns with equal height etc., but non of the solutions I've found adds scrolling to the different sections instead of the entire page, and still makes use of the entire window height.
I hope someone has a solution to this, it would be awesome. I've been googling for hours.
If you are familiar with jQuery, please see http://layout.jquery-dev.net/demos.cfm .
They are also more plug ins like this out there.
This should get you 90% of the way there:
http://jsfiddle.net/aEdc7/
However, have you considered using a JavaScript framework such as Ext JS? Take a look at some of the demos: http://dev.sencha.com/deploy/ext-4.0.1/examples/
html, body {
margin: 0;
padding: 0
}
#container > div {
position: absolute
}
#header {
top: 0;
width: 100%;
background: cyan;
height: 40px
}
#nav {
top: 40px;
bottom: 0;
left: 0;
width: 150px;
background: #ccc;
overflow-y: auto
}
#content {
top: 60px;
bottom: 0;
left: 150px;
right: 0;
background: #eee;
overflow-y: auto
}
#error {
top: 40px;
left: 150px;
right: 0;
height: 20px;
background: #444
}
<div id="container">
<div id="header"></div>
<div id="nav"><br /></div>
<div id="error"></div>
<div id="content"></div>
</div>
There's a CSS trick that can be usefull in your case :
declare an element's position as absolute
set the top position at 0
set the bottom position at 0
The height of the element should not be defined, or defined to auto.
Than the element is full height of its parent.
this can probably help to build the layout you're looking for.
example code :
<html>
<head>
<style type="text/css">
.container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.header {
height: 40px;
background: #aabbff;
}
.contentLeft {
position: absolute;
top: 40px;
bottom: 20px;
left: 0;
width: 40%;
overflow: auto;
background: #eeeeff;
}
.contentRight {
position: absolute;
top: 40px;
bottom: 20px;
right: 0;
width: 60%;
overflow: auto;
background: #ddddff;
}
.bottom {
position: absolute;
bottom: 0;
left: 0;
right: 0;
border: 3px solid blue;
height: 14px;
background: #9999ff;
}
</style>
</head>
<body>
<div class="container">
<div class="header"></div>
<div class="contentLeft"></div>
<div class="contentRight"></div>
<div class="bottom"></div>
</div>
</body>
</html>
Ciao,
Nico