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.
Related
Ive been fiddling with a lot of informationm ive found here to overlay a transparent TV PNG over a youtube video, and have succesfully gotten it to work on desktop. However I cannot get it to align correctly when viewed on mobile devices (which will be primary viewership). Is there a way I can force different CSS values depending on the device used?
#panel {
position: relative;
width: auto;
height: 625px;
overflow: hidden;
}
#panel-tv {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: url('images/tvoverlay.png') no-repeat center;
pointer-events: none;
z-index: 10;
}
#panel-content-overlay {
position: absolute;
top: 80px;
left: 24%;
width: 720px;
height: 405px;
z-index: 9;
background-color: #000;
}
#embed-container {
position: relative;
padding-bottom: 56.25%;
padding-top: 30px;
height: 0;
overflow: hidden;
max-width: 100%;
height: auto;
}
<div id="panel">
<div id="panel-tv"></div>
<div id="panel-content-overlay">
<div id="panel-content">
<div id="embed-container">
<div style="width: 683px; " class="wp-video">
<iframe width="710" height="399" src="https://www.youtube.com/embed/ZI2dbyNn8PI?rel=0&controls=0&showinfo=0" frameborder="0" allowfullscreen></iframe></div>
</div>
</div>
It is live currently at http://nickosteel.com for your reference.
Regards
One problem I could see is that you have the tv image larger than it needs to be, meaning it becomes a bit harder. I would do the following changes.
Container (id="panel")
make this max-width to after your tv image and center it with
margin: 0 auto;
TV image
cut it so it had as much to the left as the right, so the panel width to the right, should be as much "transparent" to the left. That makes it much easier to align
use it as an image (<img />) instead of a background, that way it will become responsive and have it's natural height (and width).
set width width: 100%; (this is for the responsive part)
Video
make it responsive, look at this post for that (note you don't need any of the javascript, just html/css)
center it
with this you are good to go for a responsive solution. It would be a bit easier to give you the changes in css and html, but for that you need to have the tv image at the right size to begin with. Hope this makes sense!
Just an opinion of mine, use classes for styling and id for javascript targets.
I would just like to know how to resize an image width to fit the browser width, The image is basically my header image that i want to fit the screen width.
I would thereafter need to place a div on the image. I have the following at the moment but nothing seems to work.
#container {
position: relative;
}
#divWithin {
position: relative;
top: 20%;
left: 20%;
padding: 5px;
background-color: white;
}
#imgWithin{
width: 100vw;
height: 100vh;
left: 0;
}
<div id="container">
<img id="imgWithin" src="~/images/image(2).png" style="height:325px; margin-top: 75px;" />
<div id="divWithin">Testing</div>
</div>
Any help or ideas would be gladly appreciated
What I am trying to achieve is displayed in an image below:
With 1 being : The image that I want displayed across the screen width (fit screen width)
and with 2 being : The div that I would want to place upon the image
To make a image responsive You need to use a class like this:
.responsive {
width: 100%;
height: auto;
}
If you need more details about responsive images this link should help https://www.w3schools.com/howto/howto_css_image_responsive.asp
Try changing your css to this:
html, body {
width: 100%;
}
#container {
width: 100%;
position: relative;
}
#imgWithin {
width: 100%;
}
#divWithin {
position: absolute;
top: 20%;
left: 20%;
padding: 5px;
background-color: white;
}
This will make the image the full width of the browser window with the text overlaid on top.
You are going to warp the image with a fixed height in your html though. If you provide a link to an image mocking up what you are trying to achieve I might be able to help you further
Why don't you use background: url()?
so new html now is:
<div id="container">
<div id="divWithin">Testing</div>
</div>
and css:
#container {
background: url("Your image url") no-repeat center center;
background-size: cover;
}
learn more about background and background-size
what ever media query you use put every where
CSS:-
.container{
padding: unset;
width:auto;
}
i am expecting inside container id is your image this works perfectly fine in every screen if you face any problem ping me
I'm a beginner in HTML coding and I'm trying to display just a part of an image. I'm displaying the image this way:
<img id="theImg" style="width:100%;" src="https://'myimage.jpg'" />
but I really don't know how to display just bottom left quarter of the image. It is even possible without making a new picture with the cropped image?
If you know the size of your image, you can put it into a container which has half the width and height of the image and use position: absolute; and the settings shown below:
.container {
width: 300px;
height: 200px;
overflow: hidden;
position: relative;
}
.container img {
position: absolute;
bottom: 0;
left: 0;
}
<div class="container">
<img src="http://placehold.it/600x400/fa0" />
</div>
You can just use a div element that has a background image and then just apply a few css changes to that div like so:
#theImg {
height: 200px;
width: 200px;
display: block;
background-image: url('https://myimage.jpg');
background-position: bottom left;
}
JsFiddle: https://jsfiddle.net/kekwdy2L/3/
Use background-image with background-position:
#my-image {
background-image: url('https://i0.wp.com/lovecuteanimals.objects.cdn.dream.io/wp-content/uploads/2016/01/Cute-Netherland-Dwarf-Rabbit.jpg?w=1160');
background-position: -220px -80px;
display: inline-block;
width: 200px;
height: 200px;
}
<div id="my-image"></div>
<style>
div {
height: height you want;
width: width you want;
background-image:url("image you want");
</style>
<div class="div"></div>
If you know the size of the image in pixels, you can use a css clip.
Note, clip is officially deprecated in css specification, however its replacement clip-path currently has very low browser support.
Another way of achieving crop is placing the <img> tag within a <div> as shown in this answer.
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;
}
I'm trying to center an element of a precise size on a background that is built specifically for it, in order to make it appear seamless.
In the following code, #window represents the browser's window size in pixels (change it to anything). #background obviously refers to the background image I'll be using, and #positionMe is the object I want to fit on the background. I want the background to always be centered in the browser even if the window is resized, and I want the kitten to always be centered in the black box on the background.
As you can see below, the problem is that the background isn't centered on the viewport to begin with; it's centered based on total width of the browser. And when you resize the screen, it doesn't adjust accordingly.
HTML:
<div id="window">
<div id="background">
<img id="positionMe" src="http://cs421018.vk.me/v421018778/74bc/NearuIJQIck.jpg" />
</div>
</div>
CSS:
#window {
background-color: red;
width: 1280px;
height: 720px;
}
#background {
background: url('http://i.imgur.com/xzDclz5.jpg') no-repeat;
width: 500px;
height: 500px;
margin: 0 auto;
}
#positionMe {
position: relative;
top: 174px;
left: 154px;
}
This Fiddle demonstrates my issue.
Using a combination of display:table-cell and vertical-align:center will center your image vertically. In addition, you can simply use text-align:center to center your image horizontally.
http://jsfiddle.net/reinmcha/XtQ37/10/
Might need to do a little adjusting to keep the background div centered. So, we add another div and set to display:table. The "table cell" will fill the whole thing. Now we center the table with margin: 0 auto.
Final Product:
http://jsfiddle.net/reinmcha/XtQ37/20/
Might need to do some updating to get the image to center perfectly with the border (since it has width...)
Here's my go at it.
I hope you are aware there are tons of articles on this topic. Search around. You'll find your answer :)
You basically have two options, one would be using a div to display an image and making the image a centered background like so:
<div id="window">
<div id="background">
<div id="centerMe"></div>
</div>
</div>
with css:
#centerMe {
width: 100%;
height: 100%;
background: url('http://cs421018.vk.me/v421018778/74bc/NearuIJQIck.jpg') no-repeat center;
}
or for a pure css solution:
#background {
background: url('http://i.imgur.com/xzDclz5.jpg') no-repeat;
width: 500px;
height: 500px;
margin: 0 auto;
text-align: center;
}
#background:before {
content: "";
display: inline-block;
height: 100%;
vertical-align: middle;
}
#centerMe {
vertical-align: middle;
}