<div id="post">
<img src="image.png"/>
<div class="postbox">
some content here
</div>
</div>
How do i make `.postbox' expand it's width to the max possible with with respect to the width of the image or without the image?
#post{
width:569px;
overflow:hidden
}
#post img, #post .postbox{
float:left
}
I tried width:100% to .postbox but it's taking up the whole width.
http://jsfiddle.net/FTY4k/
.postbox { width:100% }
This will make the div the same width as the container
If it doesn't mess up your layout, you could float the image, but not the div.
#post{
width:569px;
overflow:hidden;
}
#post img{
float:left;
}
Using CSS float pulls the element out of the flow. In a sense, your img and div class="postbox" a no longer contained in the div id="post" even though it is still their parent in the DOM tree.
You can try setting those to inline-block instead of using float, or float them together withing an inner div and have a clear, all contained within id="post"
More details depends somewhat on your bigger picture of what you're trying to do in the layout.
For cases like these I like to use Nicole Sullivan's media object abstraction. Here's a fiddle (for reference).
HTML:
<div class="media">
<img src="http://placehold.it/100" alt="" />
<div class="bd">lorem dolor...</div>
</div>
CSS:
.media {margin:10px;}
.media, .bd {overflow:hidden; _overflow:visible; zoom:1;}
.media .img {float:left; margin-right: 10px;}
.media .img img {display:block;}
.media .imgExt {float:right; margin-left: 10px;}
Related
I have two div and they have some width. But I want each div to take full space as it's given in width property regardless of the content inside div s. Currently each div is taking space with respect to the content inside them and ignoring what is given in width property.
My HTML is given below,
<div style="margin-left:100px; display:inline; border-width:10px; width:30%; border- style:solid;">
fawad
</div>
<div style=" margin-left:10px; display:inline; border-width:10px; width:30%; border-style:solid">ali</div>
Just change your
display:inline;
to:
display: inline-block;
http://jsfiddle.net/osx76nbr/
You can use css property display:inline-block;.That is help to set div inline with block.
You need to apply a display block style. In this case I would apply
<div style="margin-left:100px; display:inline-block; border-width:10px; width:30%; border- style:solid;">
fawad
</div>
<div style=" margin-left:10px; display:inline-block; border-width:10px; width:30%; border-style:solid">ali</div>
Alternatively I would apply all of these settings to a style sheet and apply a class to the div instead of having inline styles like this.
.inline-div {
display: inline-block;
border: 10px solid;
width: 30%;
}
<div class="inline-div"></div>
Even better is I would look into a grid system much like the ones found in bootstrap or foundation. That way you can just apply the grid styles.
<div class="row">
<div class="col-4">
<div class="col-4">
<div class="col-4">
</div>
Only thing that was needed
display:inline-block;
That's it
Try adding
div { float:left }
That will make the divs respect the with property.
I know it's quite annoying, seems that vertical-align in CSS is common pitfall but I really need your help.
I want the text to be always be in "middle" vertical position left to the image (image test.gif has different dimensions)
I have such structure of html elements (unfortunately I can't change it due to system restrictions)
<div class="wrapper">
<div class="logo">
<img alt="some text" src="http://localhost/test.gif">
</div>
<div class="source">some text</div>
</div>
CSS:
.wrapper {clear: both;}
.logo {float: left;}
.source { float: left;line-height: 16px;}
Spent 1.5 hours and no luck. Tried to apply vertical align to different elements with different positioning, etc...Please help!
If you don't need to support IE7, try this:
.wrapper { display: table; }
.logo { display: table-cell; }
.source { display: table-cell; vertical-align: middle; }
Basically, tables give you the option to vertically align to the middle, and by making your divs act like a table, we can mimic that option.
A common issue but is explained well here: http://phrogz.net/css/vertical-align/
Key Points
Specify the parent container as position:relative or position:absolute
Specify a fixed height on the child container.
Set position:absolute and top:50% on the child container to move the top down to the middle of the parent.
Set margin-top:-yy where yy is half the height of the child container to offset the item up.
<style type="text/css">
#myoutercontainer { position:relative }
#myinnercontainer { position:absolute; top:50%; height:10em; margin-top:-5em }
</style>
...
<div id="myoutercontainer">
<div id="myinnercontainer">
<p>Hey look! I'm vertically centered!</p>
<p>How sweet is this?!</p>
</div>
</div>
Why does the parent div of the image have a few extra pixels at the bottom. How can I remove the pixels without hard code the parent div height.
http://jsfiddle.net/6x8Dm/
HTML
<div class="wrapper">
<div class="column">
<img src="http://www.lorempixel.com/200/200/" />
</div>
</div>
CSS
.wrapper {
width:200px;
margin:0 auto;
}
.column {
width:100%;
background:#cc0000;
}
img {
width:100%;
}
That space actually is a result of descender elements in fonts. You can get rid of it in a number of ways:
add a vertical-align:top rule to the image jsFiddle example
add font-size:0; to the containing div jsFiddle example
add display:block; to the image jsFiddle example
One way is by setting display:block on the img, causing it to fill the parent.
jsFiddle here - it works.
img {
width:100%;
display:block;
}
Alternatively, if you don't like that approach, you can also change the vertical alignment, as the default is baseline.
I have a div[div1] that is surrounding other elements. Inside the elements I have an image that is positioned absolute. Div1 and the elements have float left on them and are showing no height. Is there a way to have this so the main overall div1 has a height so other elements like footers can be floated below it.
HTML
<div class="div1">
<div> <img src="image1.jpg" /> </div>
<div> <img src="image2.jpg" /> </div>
<div> <img src="image3.jpg" /> </div>
</div>
CSS
.div1{
width:100%;
height:auto;
overflow:auto;
float:left;
}
.div1 div{
width:100%;
height:auto;
overflow:auto;
float:left;
}
.div1 div img{
width:100%;
height:auto;
position:absolute;
display:block;
float:left;
}
If you want div1 to have a height, then remove the position absolute from the images
.div1 div img{
width: 100%;
display: block;
float: left;
}
Since all your elements are floating, the div1 will have a height. Your images were positioned absolutely so it is taken out of the content flow. This is the same as your divs not having any content inside of it, so you don't get a height.
http://jsfiddle.net/QDYYw/3/
Update :
Make the first image not positioned absolutely and the rest can be positioned absolutely, so they still have the stacking effect you want and the container will have a height since 1 of your images is in the content flow.
<div class="div1">
<img src="image1.jpg" />
<img src="image2.jpg" />
<img src="image3.jpg" />
</div>
CSS
.div1 img:first-child {
position: static;
}
see http://jsfiddle.net/QDYYw/4/ for full code
That is because of the floats. A div is a block element, which always is 100%, so width: 100% is not needed. Also remove the display:block (div is block by default) and of you dont have a specific reason for the absolute, remove that too. And offcourse remove the floats.
If you want to keep everything, just give your divs position:relative;, absolute is always relative to the first relative element it finds
I know I'm missing something obvious here... the DIV has a fixed width, the image has a fixed width, this should be a snap...
<style>
.featured {margin:0 auto;}
</style>
<div style='width:300px;border:solid 1px red;'>
<img src='http://www.musicalads.co.uk/img/articles/image_64.jpg'
class='featured' style='width:186px;height:215px;'>
</div>
You can play with it here: http://jsfiddle.net/AHjAk/
You'll also need to add display: block; to the image if you plan on centering it using auto for its left/right margins
http://jsfiddle.net/AHjAk/1/
or try this simple text-align:center; the container
http://jsfiddle.net/AHjAk/2/
<div style='width:300px;border:solid 1px red;text-align:center'>
<img src='http://www.musicalads.co.uk/img/articles/image_64.jpg' class='featured'>
</div>
Try adding text-align:center to the parent gif that should center the image.
<style>
.featured {margin:0 auto;}
</style>
<div style='width:300px;border:solid 1px red;text-align:center'>
<img src='http://www.musicalads.co.uk/img/articles/image_64.jpg'
class='featured' style='width:186px;height:215px;'>
</div>
And if you want to center that parent div itself, your body needs a text-align:center.