How to wrap div around image? - html

I trying to wrap image inside a div, but the div gets expanded by the parent div.
<div id="parent" style="display: inline-block; position: relative; text-align:center">
<div id="wrapper">
<img>
</div>
<p>Some text that expands the wrapper div more than image width</p>
</div>
The text field expands the parent div to be wider than the image and this also expands the wrapper div. How can I make the wrapper div to be exactly the same size as the img?
img nor the wrapper have no margin or padding, but yet wrapper div is wider than the image.

Use display:inline-block on #wrappper
DEMO

You need to set display:inline-block on wraper element
<div id="parent" style="display: inline-block; position: relative; text-align:center">
<div id="wrapper" style="display: inline-block;">
<img src="https://cdn2.iconfinder.com/data/icons/picol-vector/32/source_code-128.png" />
</div>
<p>Some text that expands the wrapper div more than image width</p>
</div>
If you want to keep p size same as image you need use some JavaScript or jQuery
e.g.
$('#parent').css({
maxWidth: $('img').width()
});
Fiddle

Related

div inside a div 0 height

I created a div id="A" and div id="B" and placed the latter div inside div A. Div A has a background-color attribute, but the inside div shows up as its own separate div without the A's background-color. I thought a div within a div would take the attributes of parent div. Instead, div A shows up with 0 height on the browser. The divs below follow the same structure and are just named differently: 'AboutPictures'(A) and 'ya'(B)
<div id="AboutPictures">
<div id="ya">
<img style='border:5px solid #F00' src="http://41.media.tumblr.com/3ad1ef80a08560a7e5f6be2b31f13c2/tumblr_n5wto2Ukmf1txjmgjo1_1280.jpg">
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<figure>
<figcaption>Hello I am Edward</figcaption>
</figure>
</div>
</div>
CSS:
#AboutPictures {
background-color: rgb(0,200,255);
height:100%;
}
#ya{
float:right;
}
#ya figure{
float:right;
}
Since your #ya div is floated, the parent div just sees empty content. You need to clear the #AboutPictures div, by applying the clear: both style. See: What is a clearfix?
Working example:
<div id="AboutPictures">
<div id="ya">
<img style='border:5px solid #F00' src="https://placehold.it/350x150">
<div style="clear: both;"></div>
<figure>
<figcaption>Hello I am Edward</figcaption>
</figure>
</div>
<div style="clear: both;"></div>
</div>
Hey there are different possibilities to solve this:
you could add a float: right / left; to your outer div (#AboutPictures)
or you could set it to display: inline-block;
See this fiddle: https://jsfiddle.net/8j2zpfdg/
Another SO answer as reference: How to make div not larger than its contents?

Align image to right of text in div

I'm sure it's an easy question but I can't figure it out.
I have this code basically
<div>
<img />
<p></p>
</div>
<div>
<img />
<p></p>
</div>
I want the image to align to the right of the text within the div. Everything I've tried so far takes the image out of the normal page flow making the div not respect the height of the image. I can't just explicitly state the height of the div because the image inside is dynamic and only of set width.
Here's what the page looks like.
How do I align the image to the right of the div and still have the div respect the height of the image.
Basically your div must have overflow:hidden, look example:
<div style="overflow:hidden">
<img style="float:right; width:100px; height:100px"/>
<div>text text text text text text text text</div>
</div>
<div style="overflow:hidden">
<img style="float:right; width:100px; height:100px"/>
<div>text text text text text text text text</div>
</div>
Alternatively you can put <div style="clear:both"></div> after each div
In css set the overflow of the div that has to respect the height of the image to auto.
div {
overflow: auto;
}
It is because of the float on the image. In order to fix this, you may use a method called clearfix on the container element: div.infobox
.infoBox:after {
content: "";
display: table;
clear: both;
}
Fiddle here.

Overlaying an item on top of something with display: table-cell and min-height

When using display: table-cell it's not possible to give that element position:relative. So in order to overlay the entire element with something (e.g a translucent div) you first have to wrap the contents in a div with position:relative and then put the overlay inside there.
But when I set a minimum height on the wrapper too then the overlay doesn't extend to the full height and I can't seem to find a way to make the wrapper's contents height-aware. Is there away, using just css, to have a 100% height overlay on top of a div with display:table-cell and minimum height?
<div style="table-row">
<div style="table-cell; min-height: 100px">
<div style="position: relative; height: 100%;"> // this refuses to extend to 100% height when the table row forces the cell height to grow
<p> content </p>
<div style="position:absolute; top:0;left:0;height:100%;width:100%"> // ... therefore so does this
</div>
</div>
</div>
<div style="table-cell; height: 150px"></div> // forces the entire row to grow in height
</div>

how to wrap an absolute div around 3 floating divs

Basically I have a container set to absolute positioning, for which I CAN'T set a width or height for... so it needs to wrap around the content automatically.
However, inside the absolute div, are 3 divs that are set to "float: left", so that they will stack up next to eachother.
Once I set the parent to be absolute positioned, the 3 inside divs jumps down, and the parent div, doesn't wrap around them.
Is it possible at all? So that I can wrap an absolute div, around 3 floating ones (next to one another)
apply overflow:hidden to parent div
Make sure you are using a clear element following your floats (withing your abs position div)
Here is the Fiddle for it
CSS:
.left{
float:left
}
.clearL{
height:1px;
margin-bottom:-1px;
clear:left;
}
#wrapper
{
padding:5px;
background-color:#e37c00;
}
​
HTML:
<div id="wrapper">
<div id="divOne" class="left">
<p>Some content goes here...</p>
</div>
<div id="divTwo" class="left">
<p>Some content goes here...</p>
</div>
<div id="divThree" class="left">
<p>Some content goes here...</p>
</div>
<div class="clearL">
</div>
<div/>
​
This will do the trick:
div.wrapper { /* outer-most div */
... /* other styles */
overflow:auto;
}
I use this often, works great inin all modern browsers.
Cheers

Background color for div with child divs

<html>
<head>
<style type="text/css">
div
{
background-color:#ccc;
}
</style>
</head>
<body>
<div>
<div style="float: left;">This is a text inside a div element.</div>
<div style="float: right;">We are still in the div element.</div>
</div>
</body>
</html>
Why isnt the background color showing up in between those 2 divs?
When you float elements you should provide the width of the floated elements. Otherwise you may encounter unexpected behaviors accross different browsers.
Check this tutorial, there is good info on floating in css. [link is dead]
Basically, if you provide an overflow:hidden; to the container div and provide width to the floated elements, your problem will be solved.
<div style="overflow: hidden;">
<div style="float:left; width: 300px;">Some text</div>
<div style="float:right; width: 300px;">Some text</div>
</div>
Similarly, you can add another div wherever you want to normalize the flow ike this:
<div>
<div style="float:left; width: 300px;">Some text</div>
<div style="float:right; width: 300px;">Some text</div>
<div style="clear:both;"></div>
<div>This div will be at the same place
as if the previous elements are not floated</div>
</div>
Both will work :)
EDIT
Another method which I use frequently in these days is to float the first element and set a margin-left to the following element. For instance:
<div>
<div style="float: left; width: 300px;">Some text</div>
<div style="margin-left: 300px;">Some text</div>
<div style="clear: both;"></div>
</div>
The advantage of this method is that the following element (the second div in this case) does not need a fixed width. Plus, you may skip the third div (clear: both;). It's optional. I just add it in case that the floated div is longer in height than the second div since if you don't add it the parent div will always get the height of the second div.
Just set the container div to overflow: hidden;.
If you set elements to float they won't be in the normal 'flow' of the document anymore.
div { background: #ccc; overflow: hidden; }
And you didn't even made a freehand circle ;)
A floating element doesn't affect the size of the parent, unless the parent specifically contain the children using the overflow style.
Your outer div has the same background colors as the child divs, but the height of the parent is zero, so you don't see its background.
It's because both the divs are floated so the containing divhas no height. If you were to add a third child div whic wasn't a float, give it a height of 0 and clear:both you should see the background colour appear.
The white space you are showing is a body part and you set the background color to the div but not in the body. That is the reason the body part is empty.
To color the empty part you should add following code:
<html>
<head>
<style type="text/css">
div
{
background-color:#ccc;
}
body{
background-color:#ccc;
}
</style>
</head>
<body>
<div>
<div style="float: left;">This is a text inside a div element.</div>
<div style="float: right;">We are still in the div element.</div>
</div>
</body>
</html>
You can change the body background color by changing the background color in body style.