Image inside div full width of screen - javascript

I need to have image inside div element which has width of 500px. But I need that this image is set to full width of screen. That means:
<div style='width:500px'>Some text <img full width of screen /> some other text </div>
But I need that image size is for full width of screen. That means that I don't need to have image only for 500px I need that this image is resizable of screen width. This img must be inside this div element. Thank you

Just set the position of your image to fixed,
the width and the height to 100%
and the top and left to 0.
It will then be fullscreen in your browser.
img{
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
In your example:
<div style='width:500px'>Some text
<img style="background: red; width: 100%;height: 100%; position: fixed;
left: 0;top:0" full width of screen />
some other text
</div>

Related

CSS crop image to fit screen width while keeping the original height of the image

I'm trying to display an image (6000px width, 300px height) at the end of the main-content like background image. The image should fit the width of the screen, but keep the origin height.
In other words somehow always crop the image at the center while the width of the x-crop is the screen width size and height crop is the origin height of the image.
I have made the image with 6000px width so that I can fit all the screens.
the code below does not work as expected its obvious, it just display the original img while scaling the height to keep the aspect ratio relative to the screen width.
newnewwaves.svg : http://svgshare.com/i/3DT.svg
how I want to be displayed: https://ibb.co/e80Ddw
HTML:
<div class="main-content">
...content
<div id="header-waves">
<img src="/assets/images/newnewwaves.svg" alt="">
</div>
</div>
CSS:
.main-content {
position: relative;
}
#header-waves {
position: absolute;
left: 0;
bottom: 0;
}
#header-waves img {
width: 100%;
height: auto;
display: block;
}
Try this:
.img-class {
width: 100%;
overflow: hidden;
display: flex;
justify-content: center;
object-fit: cover;
}
This will help in maintaining aspect ration by restricting the width and cropping the image to center it.
If that doesn't work, Please try this :
.img-class {
width: 100%;
height: 400px; /* Set your fixed Limit */
background-size: cover;
}
You could place the image in a container with width: 100% and set the overflow property to hidden.
Use flexbox to center the image within this container.
Note that this snippet is easier to test if you make it full screen and resize the browser...
#header-waves {
width: 100%;
overflow: hidden;
display: flex;
justify-content: center;
}
<div class="main-content">
<div id="header-waves">
<img src="https://placehold.it/6000x300" alt="">
</div>
</div>
You can do it using background: url(...), like the example..
.main-content
{
background: url('http://via.placeholder.com/6000x300');
background-size: auto 100%;
width:100%;
height:300px;
}
<div class="main-content">
...content
</div>

Width of image in html is not being changed

I have below code where I am trying to change the height and width of an image. For that, I gave height and width parameters in <img > tag.
Height of an image is being changed where as there is no affect on width. I want my image to be larger.
<div style="text-align: center; margin-top: 10px; width: 1000px; height:100px">
<img class="calIcons" src="#calLegend" alt="Legend" height="1000" width="100"/>
</div>
Image:
Dimensions 443x30
Width 443 pixels
Height 30 pixels
Bit depth 32
Make sure that the class calIcons does not manipulate the width or size.
Make width and height of image tag 100% and then adjust the width and height of the container div to whichever values you want. This works if the image is the only element in the div.
Instead of using the height and width attributes, to keep it clean you can try something like the following within your CSS file:
div > img{
width: 100%;
height: 100%;
}
Considering you have already set your dimension values within the parent div, you can just set the width and height to 100% for the child img.
Alternatively, if you don't want the image to stretch;
You can set it as the background-image of a div with a background-size of cover.
Like so (HTML):
<div class="parent-div"> <!-- the parent div with the set dimensions !-->
<div class="child-img"></div> <!-- the image !-->
</div>
CSS:
.parent-div{
width: 1000px;
height: 100px;
text-align: center;
margin-top: 10px;
}
.child-img{
width: 100%; /* set the dimensions to 1000px x 100px */
height: 100%;
background: no-repeat url('image/location.jpg');
background-size: cover; /* cover the whole div */
}
Replace your code with this. It will give you maximum dimensions of your image.
<div style="text-align: center; margin-top: 10px; width: 1000px; height:100px">
<img class="calIcons" src="#calLegend" alt="Legend" style= "width: 100%; height:100%"/>
</div>

Make img grow to max width/height regardless of container and maintain aspect ratio

I have an <img> which is positioned absolutely within a <div>. The <div> is 100px by 100px.
The <img>, when clicked, should grow to a known max width or height, say 500px by 700px. If the <img> is smaller, say 300px by 250px, then that's as large as it should get. Aspect ratio should be maintained.
Setting height and width to 100% just makes the <img> the size of the containing <div>.
I'm looking for either a css or vanilla javascript solution. (I don't require the code for a click animation, just a static end result so I can apply the concept)
Set the max-width of the image as 100%.
Height should be set to auto.
Solution 1 : When the image can grow up to its parent div's width:
.wrapper {
width: 500px;
height: 500px;
border: 1px solid red;
position: absolute;
}
.wrapper img {
/*width:100%;*/
height: auto;
max-width: 100%;
}
<div class="wrapper">
<img src="https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
</div>
Solution 2 : Initially the image width will be restricted by the max-width property to stay within the thumbnail size.
Onclick of image, remove the thumbnail class, so that it naturally over flows the parent to its original size
.wrapper {
width: 100px;
height: 100px;
border: 1px solid red;
/*position: absolute;*/
}
.wrapper img.thumbnail {
/*width:100%;*/
height: auto;
max-width: 100%;
}
<div class="wrapper">
Initially
<img class="thumbnail" src="https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
</div>
<div class="wrapper">
On click
<img src="https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
</div>

Fit my tabs height in a container

Currently I have a few navigation <nav> tags. I have place the nav tag within a container with 100% width. But when I set the container height by a certain percentage the <ul> & <li> tag in the nav when click got cut off instead of scale down. How do I go about doing it?
here's my css code;
#container
{
position: absolute;
top: 30px;
left: 50px;
width: 100%;
height: 50%;
background-color: #000000;
}
below is my source code.
http://jsfiddle.net/eMLTB/107/
Instead of
height: 50%
can you just use
min-height: 50%
as this implies that the min-height will be 50% but it will not restrict it in case container has huge content that will be more that 50% height of the container
DEMO
http://jsfiddle.net/eMLTB/109/
Percentage is applied to whatever default height the browser sets for the page.
So If by 50% you mean the #container to be 50% of the page height then you must have this as well:
html, body {
height:100%;
min-height:100%;
}

Image 100% of a div

How can I make an image 100% of a div ?
I have a 300x300 image and I want to make it the full size of a bigger div. (the size of this div can change, so I have to specify somewhere 100% of it)
Is there a solution in CSS or Javascript ?
try this:
<img src="test.jpg" alt="test" id="bg" />
img#bg {
position:fixed;
top:0;
left:0;
width:100%;
height:100%;
}
css3 also supports this:
#testbg{
background: url(bgimage.jpg) no-repeat;
background-size: 100%;
}
Just assign the CSS style width: 100% in the image tag to have it cover the whole space of its parent container.
Example or jsFiddle:
<div style="width: 500px;">
<img src="yourPic.png" style="width: 100%" />
</div>
<div style="width:450px;height:450px;">
<img src="photo.png" style="width:100%;height:100%;"/>
</div>
Try this: http://jsfiddle.net/XUZV5/
<div style="height:100px;width:300px;">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/Mars-Schiaparelli.jpg/280px-Mars-Schiaparelli.jpg" style="width:100%;height:100px;"/>
</div>​
The CSS below will scale your image and fill 100% of the width of your div
#imgId {
width: 100%;
height: auto;
}
However if you actually want to fill the entire div by stretching the image use
#imgId {
width: 100%;
height: 100%;
}
One other useful tip is when your width is specified as a percentage and your image is square, as yours is.
HTML
<div class="container">
<img src="sample.jpg">
<div style="clear:both;"></div>
</div>
CSS
​.container {
position: relative;
width: 50%;
height: 0;
// % padding is calculated as % of width rather than height
// so height will equal 50%
padding-bottom: 50%;
}
img {
position: relative;
width: 100%;
// image is square so as long as width is 100% then height will be the same.
height: auto;
}
html, body {
height: 100%;
width: 100%;
}
The above means the image will always resize to fit the parent div exactly.
Fiddle here

Categories

Resources