Parent div not wrapping image inside it - html

i'm working with a website.i have a problem with the divs
<div class="parent">
<div class="child" style="display:inline-block; z-ndex:1000;">
<img src="" style="max-width:100%; max-height:100%"/>
</div>
</div>
html:
.parent {
clear: both;
float: left;
padding:0 2%;
width: 98%;
padding:0 1%;
display: block;
height:557px;
}
I have my html like this, i want my child div to wrap around the image so i gave it inline-block. but if the image is large, i'm getting the image out of the parent div.
Is there any way that it get fixed in the parent div.
Any help please .

you should set some style attributes .i.e. height and width of outer[parent] div so that contents get bounded by it.
<div class="parent" style="height: 80px; width: 80px;">
<div class="child" style="display:inline-block;">
<img src="" style="max-width:100%; max-height:100%"/>
</div>
</div>

Try to set some height and width to your parent and Child Div.
Sample code which has image size larger than parent Div
<div class="parent" style="background-color: pink; height: 100px; width: 100px;">
<div style="display: inline-block; background-color: black; height: 80px; width: 80px;" class="child">
<img style="max-width:100%; max-height:100%" src="../somepath.JPG">
</div>
</div>

Mention the fixed height and width in pixel to parent DIV .Image will fix in the parent DIV
Take a look Fiddle. Actually image size 200*200 px ,parent DIV size 100*100 px .Now it will fix
HTML :
<div class="parent">
<div class="child" style="display:inline-block; z-ndex:1000;">
<img src="http://www.jonathanjeter.com/images/Square_200x200.png" style="max-width:100%; max-height:100%"/>
</div>
</div>
CSS :
.parent
{
clear: both;
float: left;
padding:0 2%;
width: 100px;
padding:0 1%;
display: block;
height: 100px;
}

Now your .parent div has width:98%, As it is in % the div will attain a width 98% of the current viewport or browser window you used to view the site irrespective of the content inside..
So even if your image inside .parent div is larger, div will get only the 98% of window.
1.) if you want your image in its original size and the parent should wrapped around, you can go with this:
remove width:98% from .parent and remove max-width from image.
2.) If you are looking to resize the image with respect to the width attained by .parent
You can use the same code you are using now.

Related

Auto-resizing div and its content

I am trying to achieve auto resizing in my div parent block. I would like my image to resize to an exact div block width or even better if text would resize image and changes div block width so that page look neat.
<body style="margin: auto;max-width: 800px">
<div class="messages" style="color: #002b55;max-width:100%;height: auto;font-size: 11px">
<img src=cid:header.png alt="header">
<div style="margin-top: 7px"><b>Lieber Nutzer,</b></div>
for example if the image is bigger than text i would like my image to resize to the longest line
You can have images and divs resize using display: flex; Also you can have your text change size using vw instead of px such as font-size:5vw; which is in relation to the size of the view window.
Check out what I did here. And here is the jsfillde for you to play around with the window size. https://jsfiddle.net/dcxp0uqe/
.messages {
width 50%;
height 50%;
display: flex;
font-size:5vw;
}
.messages * {
display: flex;
width: 50%;
height: 50%;
}
<body style="margin: auto;max-width: 800px">
<div class="messages" style="">
<img src="https://w3schools.com/html/img_girl.jpg" alt="header">
<div style="margin-top: 7px"><b>Lieber Nutzer,</b></div>
</div>

making image fill html/css

I'm aiming for a page with a collage of 4 photos;
a large one on the left taking up 50% width of the page and 100% of the height.
3 smaller photos, each taking up 50% of the width of the page but 33% of the height.
I'm running into problems with the larger image though,
my current CSS is;
.container {
width: 50%;
height: 100%;
overflow: hidden;
-size: cover;
}
and my html;
<div class="container">
<img height="100%" width="100%" src="jpg"></img>
</div>
the image's 50% width is fine but the height is only 50%.
It's a large image(4k) and my aim was to have overflow:hidden so that it fills the container but it's not working.
How could I do this?
Edit:
I'm aiming for it to be similar to this website:
http://www.masitupungato.com/
Suggested Solution
In fact, it is the easiest solution
Use two different divs, one for the left side and the other for the right side.
The left side div takes the half of the container width, and contains a image
The right side div takes the half of the container width, and contains 3 different divs, each one takes 33% of this right div height, and contains an image.
Use the CSS below:
.container {
width: 250px;
height: 250px;
margin: auto;
}
#left {
width: 50%;
float: left;
height: 100%;
}
#right {
width: 50%;
float: left;
height: 100%;
}
.right-inner{
width: 100%;
height: 33%;
}
.left-inner {
width: 100%;
height: 100%;
}
Expected output
Check it out.
You might change the height of the biggest image to fit the window of the device. Try to to set its height to "100vh", maybe it is what you were looking for.
you can use display:flex; property for this purpose. it will resolve your issue dynamically.
<div class="wrapper">
<div class="big-image">
<img src="http://static.independent.co.uk/s3fs-public/styles/story_medium/public/thumbnails/image/2013/07/31/10/A-striped-field-mouse-(Apod.jpg" alt="">
</div>
<div class="small-image-wrapper">
<div class="image">
<img src="http://static.independent.co.uk/s3fs-public/styles/story_medium/public/thumbnails/image/2013/07/31/10/A-striped-field-mouse-(Apod.jpg" alt="">
</div>
<div class="image">
<img src="http://static.independent.co.uk/s3fs-public/styles/story_medium/public/thumbnails/image/2013/07/31/10/A-striped-field-mouse-(Apod.jpg" alt="">
</div>
<div class="image">
<img src="http://static.independent.co.uk/s3fs-public/styles/story_medium/public/thumbnails/image/2013/07/31/10/A-striped-field-mouse-(Apod.jpg" alt="">
</div>
here is the fiddle https://jsfiddle.net/d95hw8fq/

How does this produce unequal height divs?

demo [Firefox & IE]
HTML:
<div class="content">
<div class="img">
<div class="divs">
<img src="path.jpg" alt="" />
</div>
<div class="divs">
<img src="path.jpg" alt="" />
</div>
<div class="divs">
<img src="path.jpg" alt="" />
</div>
<div class="divs">
<img src="path.jpg" alt="" />
</div>
</div>
</div>
CSS:
html, body {
height: 100%;
}
.content {
/* height: 100%; */
position: relative;
width: 100%;
}
.img {
height: 100%;
margin-left: 0.5%;
max-width: 100%;
width: 100%;
}
.content .divs {
border: 1px solid #b64024;
float: left;
height: 25.5%;
margin: 1% 0.5% 0;
overflow: hidden;
width: 20%;
}
.content .divs img {
height: 100%;
width: 100%;
}
If you uncomment the height: 100%; from .content then you'll see the equal height divs there with images.
So, my question is:
How the .divss' height is calculated without having parent div height .img -> .content in percentage calculation for inherited div ?
Here's your problem in a nutshell:
Demo: http://jsfiddle.net/a75ekc0e/9/
.content {
height:400px;
position:relative;
}
.content div {
float:left;
background:yellow; border:1px solid blue;
width:20%; height:25%;
margin:1em;
}
That simplified demo shows the content 'working'. The key parts are that:
Your divs have a percentage height.
The 'containing block' of the divs has an explicit height.
Super-important: Note that the position:relative causes .content to be the 'containing' for its children; without this declaration it will be the body that is used for positioning.
…and thus, the divs can calculate their height based on their positioning.
If you remove the height from the .content then this element has no explicit height, and its height is taken from its content. When you do that with the above demo and Run it, you see the divs collapse to have no height at all, because they cannot calculate the percentage of something that will be determined later on.
In your demo they get their height from their varying-height image content, which causes them to vary. The height:25.5% is completely ignored because the parent has no explicit height.
Per the specification of the CSS height property:
The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to 'auto'.
For more details, read the section "Calculating Heights and Margins" in the CSS specification.
This might not be an answer.
Though, struggling a lot, I found the key reason behind this in short is due to different height of images in original size.
Let see the demo with equal height original image size:
demo
But still amazed of behavior of height of images just due to original image sizes and it also varies in different browsers.

CSS nested div height 100% doesn't work

I hope that someone can help me about this problem. I have structure like this
<div id="Div1" style="height:auto">
<div id="Div2" style="height:100%;">
<div id="Div3" style="min-height:100%;"></div>
<div id="Div4" style="height:100%;"></div>
</div>
</div>
Also I put in my css file
html,body
{
height: 100%;
}
Problem is that neither Div3 nor Div4 have the expected height of 100%, I check size of first two divs with firbug and they are ok, filling the whole screen.
Sorry for my bad English, I hope that you understand my question :)
Have a look at this. When using a percentage, each div will be affected by the height of it's parent.
In this example the html,body has a height of 100% and the percentage of each div child is then relative to it's parent. Note how each div is half the size of it's parent div, each step it shrinks by half the size.
Updated with all percentage example
Simple example
HTML
<div>
<div>
<div>
<div></div>
</div>
</div>
</div>
CSS
html, body {
height: 100%;
}
div {
height: 100%;
background: #F00;
}
div div {
background: #FF0;
height: 50%;
}
div div div {
background: #000;
height: 50%;
}
div div div div {
background: #F30;
height: 50%;
}
First of all write height in inline style
<div id="Div4" height:100%;"></div>
change to
<div id="Div4" style="height:100%;"></div>
The key is to set the height of the div with id "Div1" to something other than "auto". Try 100% or a specific value like this
<div id="Div1" style="height:100%;">
<div id="Div2" style="height:100%;">
<div id="Div3" style="min-height:100%;"></div>
<div id="Div4" height:100%;"></div>
</div>
</div>

Make div only as wide as sibling

I have an image within a parent div. I also want to have some text underneath the image within that parent div, but I only want the width of that text div to be as large as the image.
<div class="parent">
<div class="child">
<div class="image-container">
<img src="..." />
</div>
<div class="text">
...
</div>
</div>
</div>
Here is a jsfiddle that illustrates my problem:
jsfiddle
How can I resolve this? I can't put the text inside the same div as the image because the image is cut off using a max-height css.
Is this what you were after? Can you use jquery?
$('.child').width($('.image-container').width());
http://jsfiddle.net/YRYZA/
I simplified your markup and css a little bit. You can keep them in the same parent. use position absolute for the text and add position relative to its parent. that way it will take the parent's width. and the parent's width will be set by whatever size the image is, hence the text will be the same width as the image at the end of the day.
html:
<div class="parent">
<div class="image-container">
<img src="http://lorempixel.com/400/600/" />
<div class="text">
text
</div>
</div>
</div>
CSS:
.parent {
width: 700px;
}
.image-container {
background: green;
float:left;
position: relative;
}
div.text {
background: green;
position: absolute;
width:100%;
left:0;
}
jsfiddle
Do this:
.child{ position: relative; }
.text{ position: absolute; left: 0px; right: 0px; }
Then .child div would be as wide as the image (not influenced by .text width) and .text would fit in the space.
JSFiddle: jsfiddle.net/8hV2E/12