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;
}
Related
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).
Why does this using the img tag force a white border? I've tried border:0; with but a white border is still there in the picture. This does not happen when you use div though. And is there anyway to get rid of it? See my example below:
div {
background: url("https://www.w3schools.com/html/pic_trulli.jpg");
width: 100px;
height: 150px;
padding: 10px;
}
.image {
background: url("https://www.w3schools.com/html/pic_trulli.jpg");
width: 100px;
height: 150px;
padding: 10px;
}
<img class="image">
<br>
<div></div>
The answer is simple, you are wrongly using the img tag because:
src
The image URL. Mandatory for the element
And
If an error occurs while loading or rendering an image, and an onerror event handler has been set on the error event, that event handler will get called. This can happen in a number of situations, including:
The src attribute is empty ("") or null.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img
So you are falling into the error case where the browser will decide to output something related to the error that you cannot really control.
If you add alt you will get another output:
div {
background: url("https://www.w3schools.com/html/pic_trulli.jpg");
width: 100px;
height: 150px;
padding: 10px;
}
.image {
background: url("https://www.w3schools.com/html/pic_trulli.jpg");
width: 100px;
height: 150px;
padding: 10px;
}
<img class="image" alt="image">
<br>
<div></div>
I don't see the purpose of doing this with img but if you insist on placing a background image inside consider having a valid url of an image. Even an empty SVG will do it
div {
background: url("https://www.w3schools.com/html/pic_trulli.jpg");
width: 100px;
height: 150px;
padding: 10px;
}
.image {
background: url("https://www.w3schools.com/html/pic_trulli.jpg");
width: 100px;
height: 150px;
padding: 10px;
}
<img class="image" src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'></svg>" alt="image">
<br>
<div></div>
Why would you want to set a background image for an img element? For an img element, you could just use the src attribute instead to set its image. (I fail to see the need for setting an image's background image.)
So for rendering images, I would personally:
Use an img element and set the (required!) src attribute. This would be my preference if the image is part of the web page's (dynamic) functional/informational content. It would be included when copying/pasting data from the web site.
Use a div element with a CSS background image. This would be my preference if the image is part of the web page's (static) design. It would be ignored when copying/pasting data from the web site.
.background-image {
background: url("https://www.w3schools.com/html/pic_trulli.jpg");
width: 100px;
height: 150px;
padding: 10px;
box-sizing: border-box; /* do not size by content, but by border */
}
.image {
object-fit: none; /* do not resize image */
object-position: left top; /* do not center image */
width: 100px;
height: 150px;
}
<img class="image" src="https://www.w3schools.com/html/pic_trulli.jpg">
<br>
<div class="background-image"></div>
Instead of using border: 0;, use border: none;.
You can also try using box-sizing: border-box; to set the padding and margin inside of the element so that the total width/height is the exact total instead of having to add borders, padding, and margin to the equation.
https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing#:~:text=border%2Dbox%20tells%20the%20browser,to%20absorb%20that%20extra%20width.
border-box tells the browser to account for any border and padding in
the values you specify for an element's width and height. If you set
an element's width to 100 pixels, that 100 pixels will include any
border or padding you added, and the content box will shrink to absorb
that extra width.
I upload image from user and display it on my page. I have also a canvas that I used to mark lines on image. I want to have image displayed in the same place as canvas which is center. Right now image image display starts from center. The problem doesn't exist on Firefox and Chrome but only on Safari. I use also materialize.css in this project.
How can I display image in exact center?
<style>
.image {
position: absolute;
z-index: 1;
}
.container {
display: inline-block;
margin: 0 auto;
position: relative;
width: 100%;
}
.canvas {
position: relative;
z-index: 20;
}
</style>
<div class="container">
<img
width="400"
height="400"
class="image"
/>
<canvas width="400" height="400" class="canvas" />
</div>
Red thing is my image
There are many ways to center an image. In your case, I'd recommend using display: block; margin: 0 auto; and giving it a max-width: 400px; width: 100%; so the image always maintain the aspect ratio and doesn't get bigger than what you want.
If you use width: 400px, when it reaches devices with a small width (phones) it will make the page scroll which is bad UX. Furthermore, you usually don't need the height: 400px. When you add an actual image, let the image fill the height automatically.
Also, avoid putting styles on the markup. All styles should be in the CSS for organization.
Look into learning the basics of HTML/CSS through a course. It doesn't really matter whether you use Bootstrap, Materialize, Bulma, etc. It all comes down to the basics.
.image {
display: block;
margin: 0 auto;
max-width: 400px;
width: 100%;
height: 400px;
border: 1px solid lightgray;
}
Check code on codepen here.
I have an image nested inside a series of <div> tags. I have the image in height: 100%; and width: auto;. When I resize the height of my browser, the image changes its size but somehow the parent div doesn't change in IE and Edge (fine for all other browsers).
Here is the normal render on chrome when you resize your browser:
Here is the problem in IE when you resize:
Or like this:
I'm trying to resize the parent in relation with the image's width. The parent container just doesn't resize. Here is my <div> structure:
body,
html {
margin: 0;
padding: 0;
background-color: #fff;
height: 100%;
}
#photo-gallery {
position: relative;
height: 100%;
padding: 100px;
box-sizing: border-box;
}
.project {
position: relative;
display: inline-block;
height: 100%;
max-height: 600px;
border: solid 18px #333;
box-sizing: border-box;
}
img {
height: 100%;
box-sizing: border-box;
}
<div id="photo-gallery">
<div class="project">
<img src="img/landscape1.jpg">
</div>
</div>
Here is my CodePen (open in IE or Edge)
Right now I got it working with JavaScript by detecting the width of the image and giving it to the immediate parent, but that's really far from ideal... am I missing something? The strange thing is, if you resize your window and then reload the page, the image shows with the right size/proportions until you resize the browser again.
I'm trying to make a specific clickable location on my image. The image size adjusts dynamically to the size of the browser, so I need the clickable box to do that as well. I have created a box to contain my image (not sure if that is necessary) and thought that if I made a box (a.resume) within that container, it would adjust relative to the image. However, the box seem to be creating outside of the container, off-screen to the right.
Below is the CSS:
#banner {
width: 100%;
margin-bottom: auto;
margin-top: auto;
margin-left: auto;
margin-right: auto;
}
img.banner {
max-width: 100%;
height: auto;
width: auto;
}
a.resume {
top: 20%;
left: 35px;
width: 60%;
height: 28%;
position: relative;
background-color: black; /* to see where my box is */
display:block;
}
Below is the HTML:
<body>
<div id="banner">
<img src="banner.png" class="banner" />
<a href="banner1.png" class="resume" />
</div>
</body>
Also, I would appreciate it if you can let me know if I can simplify anything in my CSS or HTML. I'm new to this and I might be over complicating them.
You'll need to use the HTML map tag. You can learn about it here