CSS float issue - css-float

I have a page where there are two images on top which are floated to left and right as below :
.floatLeft{ float:left;}
.floatRight{ float:right; }
<div>
<img src="firstImage" class="floatLeft"/>
<img src="secondImage" class="floatRight"/>
<!-- wright here -->
</div>
Issue is when i write anything after images (be it in div or a plain text ) and if it goes beyong page width ,
the text after maximum width shifts downwords entirely after images . That is text doesn't wrap inbetween two images
What i understand about float is text should "FLOW" in between two images (unless we use clear which "clears" area around images ) , but its not working like that.
I want to have a text in between two images.
Any help is higly appreciated as a I have done a lot of efforts but it is not working.

Logically, you have 3 blocks of data here, so I suggest using 3 floating divs with set width:
.floatLeft{ float:left}
.div1{width:X%}
.div2{width:Y%}
.div3{width:Z%}
.overflow{overflow: hidden} /* this is optional */
<div class="overflow">
<div class="floatLeft div1">
<img src="firstImage"/>
</div">
<div class="floatLeft div2">
<!-- text here -->
</div">
<div class="floatLeft div3"><!-- or make it float right if you want -->
<img src="secondImage"/>
</div">
</div>
If you want your images a fixed width, try using CSS calc:
.div1{width:100px}
.div2{width:-moz-calc(100%-300px);-webkit-calc(100%-300px);calc(100%-300px);} /* not supported in some browsers */
.div3{width:200px}
But honestly I recommend to go classic way and use a table here:
.table {width:100%;border-collapse:collapse}
.table td {padding:0;margin:0}
.block1 {width:X%} /* or width:Xpx */
.block3 {width:Z%} /* or width:Zpx */
/* do not touch block2 here */
<table class='table'>
<tr>
<td class='block1'>
<img src="firstImage"/>
</td>
<td class='block2'>
<!-- text here -->
</td>
<td class='block3'>
<img src="secondImage"/>
</td>
</tr>
</table>
Remember to wrap your content in "blocks", it always helps when you mark up.

I guess this is the answer that you need jsfiddle
I will explain a little about.
in your div add a class of imgcontainer
in your css, add the following
.imgcontainer{
position:relative;
}
you can add the width of the div, in jsfiddle I set it as 150px. and each of the image is 50px. there you add paragraph with some text

Try this way
<div>
<img src="firstImage" class="floatLeft"/>
<p class="floatLeft">Some textgoes here</p>
<img src="secondImage" class="floatRight"/>
</div>
but give proper width

Another variation is using a CSS version of the table approach proposed by igorpavlov. You have to replace the table/tr/td by divs and to adapt the CSS as proposed here :
.table {width:100%; display: table;}
div{ display: table-cell; vertical-align: top;}
.block1 {width:33%} /* or width:Xpx */
.block3 {width:33%} /* or width:Zpx */
img {width: 100%; height: auto;}
<div class='table'>
<div class='block1'>
<img src="firstImage"/>
</div>
<div class='block2'>
<!-- text here -->
</div>
<div class='block3'>
<img src="secondImage"/>
</div>
</div>
It provides better semantics and works IE8+!

Related

img fixed width when making float in IE 8

I have a problem with fixed image width only in Internet Explorer (I have IE8)
First the image is not appear at all when padding is not defined
Second when i specify padding:5px; for the img it appears like this
Note that I can't set a special width for image container div because
below is my code
HTML:
<div class="block_div">
<div>
<div class="img_about">
<img src="test_img.jpg" alt="test_img" width="150" />
</div>
<div class="img_about">
about: "Adapted from Betty Crocker".
</div>
</div>
<div class="clear"></div>
</div>
CSS:
.img_about{float:left; }
.img_about img{padding:5px; }
and if i delete the float from .img_and_text dev it looks normal width:150
try this:
.img_about img { width:150px; height:150px; }
Using inline width tags is only helpfull for e-mail clients these days afaik...

how can I clip an <img> inside a <blockquote>

I am trying to limit the horizontal size of whatever I load in a blockquote (I do not control that content in the blockquote so I know it's going to screw up my layout). Here is a reduced testcase of what the page layout looks like:
<html>
<body>
<div style="width: 800px; background-color: #ff0000;">
<div style="display: table;">
<div style="display: table-row;">
<blockquote style="overflow:hidden;">
<div style="width: 1000px; height: 400px;background-color: #00ff00;"></div>
</blockquote>
</div>
</div>
</div>
</body>
</html>
The size of the blockquote is implicitely set through a width on one of its parent elements and a table container is used to layout the blockquote itself.
Now, if you try to copy/paste the above html into a test.html file, you will see the entire inner div displayed as blue, beyond the boundaries of the red background. I would like to make sure it gets cliped at the background boundaries.
The question is then: is there a way to do this without changing the structure of the html layout and without having to set again an explicit width on the blockquote itself (I cannot do the latter because I do not know the real size of the blockquote in the real layout because there are other elements within the outer div that take an unknown amount of horizontal space) ?
EDIT
Earlier, I naively tried the following. I added an extra column in the table to illustrate the fact that I really do not know how much space the other elements in the table will suck up.
<html>
<body>
<div style="width: 800px; background-color: #ff0000;">
<div style="display: table;">
<div style="display: table-row;">
<div style="display: table-cell;">
<blockquote style="overflow:hidden;" id="inner">
<div style="width: 1000px; height: 400px;background-color: #00ff00;"></div>
</blockquote>
</div>
<div style="display: table-cell;">
<div style="width:100px; background-color:#0000ff; height: 300px;"></div>
</div>
</div>
</div>
</div>
<script>
var width = document.getElementById('inner').parentNode.offsetWidth;
console.log(width);
</script>
</body>
</html>
In order for overflow: hidden; to work you have to set the dimensions of the element explicitly. So, yes, sorry, you have to specify the width on blockquote.
If you are allowed to, you can use javascript to determine the width of the parent element which has a set width and then set the width of the blockquote accordingly.
Here's an example of how that might work, using your current markup:
var root = document.getElementsByTagName('div')[0],
block = document.getElementsByTagName('blockquote'),
i;
for (i = 0; i < block.length; i += 1) {
block[i].style.maxWidth = root.style.width;
}
Demo
However, this would be vastly improved if you were to give the div that's got the set width a class name or id. Check out this fiddle to see how that would work.

Creating complex div structure using CSS

I'm attempting to create a complex div structure using CSS.
I want it to be made up of four columns. On the left is just a list of images. On the right is the complex div structure that I can't figure out a way to create. There should be two large vertical boxes containing various details. In-between these vertical boxes are any number of horizontal boxes.
My problem is that I cannot work out how to create this div structure in a way that 'scales', i.e. there could be any number of horizontal boxes between the two vertical boxes.
This is the div structure I was attempting to use:
<div class="result">
<div class="detail_1">
<p>Detail 1</p>
</div>
<div class="details">
<p>Details</p>
</div>
<div class="details">
<p>Details</p>
</div>
<div class="detail_2">
<p>Detail 2</p>
</div>
</div>
Any help would be greatly appreciated!
EDIT: I have fixed this problem by just using tables. Thanks for the replies.
Update 2
Your question is: How to make the price & flight_number div the same height as the parent div (container)..
1) Use the technique described here: http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks
2) update your CSS so that the flight number and the price are vertical aligned in the middle of their div.
I think that mine HTML structure is better then yours because it's more clear and easier to work with.
So based on my HTML structure: The parent container (flight_info) is stretchend as long as the content inside (the table with the rows will be the longest). the div's flight_number and price are also the total height of the parent container thanks of the technique described in step 1 above. The extra CSS (step 2) will align the price and flight number nicely in the middle.
OLD
<ul id="flights">
<li>
<ul class="images">
<li><img src="img1" alt="your image" /></li>
<li><img src="img2" alt="your image 2" /></li>
</ul>
<div class="flight_info" id="flight_EK49">
<div class="flight_number">
EK49
</div>
<table>
<thead>
<th>date</th>
<th>from</th>
<th>to</th>
</thead>
<tbody>
<tr>
<td>1/1/2013</td>
<td>departure airfield</td>
<td>destination airfield</td>
</tr>
...
</tbody>
</table>
<div class="price">
€999,99
</div>
</div>
</li>
// duplicate the above for a new flight..
</ul>
And for the CSS style (you must do the rest on your own because this is just an example. I didn't test any of the code):
<style>
#flights .images {
float: left;
width: 250px;
}
.flight_info {
float: left;
width: 700px;
}
.flight_info .flight_number,
.flight_info .price {
float: left;
width: 150px;
}
.flight_info .price {
float: right;
}
.flight_info table {
float: left;
width: 400px;
}
</style>
I think you will get the idea.
EDIT 1
Changed all the position absolutes to floats because it easier with the li's automatic heights.
I also added the leg images of the flight as well, but as I mentioned, you have to do the rest yourself ;)

How do i expand first div the same width as second div width

I have a page with a couple of divs, sort of like this:
<div id="content">
<div id="topDIV" style="background-color: #0000C9; position:absolute; width:100%; top:0px; left:0px; height:44px;">
<table cellpadding="0" cellspacing="0" border="0" style="height:44px; width:100%;">
<tr><td>header div<td></tr>
</table>
</div>
<div id="mainTableDIV" style="background-color: #f00; padding: 10px; position:absolute; top:44px; left:0px;">
<table cellspacing=0 cellpadding=0 border=0 style="width:1400px;height:800px">
<tr><td><td></tr>
</table>
</div>
</div>
So second div (mainTableDIV) populates via AJAX request and width is set to approx 1400px depends on data.
so when i shrink browser less that this width i see horizontal scrollbar. when i scroll to the right first div (topDIV) which is a header div is not expanding to the same width as second div.
How can i make first header div to be always the same width as second width?
Wrap them in a div tag of their own
<div id='content'>
<div id="div_one" style="width:100%;">
<table width=100%...>
...
</table>
</div>
<div id="div_two" style="width:100%;>
<div id="data_div"></div>
</div>
</div>
First, you do not need width:100% when dealing with a div (as long as there is not a float). A div will AUTO FILL in the area that its placed in. So If content 2 exceeds the size of content 1, as long as the overall content container does not have a RESTRICTION on it, in terms of width, the content 1 will grow to meet content 2.
You've got some quotes in strange places, try fixing those and it should work. Besides that, it's a bit clearer if you instead use CSS to set the styles of these divs:
HTML:
<div id="div_one" class="divider"></div>
<div id="div_two" class="divider"></div>
CSS:
.class {
width: 100%;
}
Is this what you're looking for?

center div within 100% width div

I am trying the Blueprint CSS framework, and am having a hard time figuring out how to do the overall layout.
It seems Blueprint (as far as I have understood it so far) makes you use a set page width at 950px. I guess you could change that with some modification, but in any case there has to be some width, so that's fine. The problem is, even if I want the main content of the page to be 950px wide, I want 100% wide headers and footers.
So I have placed a header and a footer outside the main "container" div that's 950px wide. I set the header div to 100%. And then I have a "headerContent" div inside it (containing menu, logo, etc), which has a 950px width (span-24 in Blueprint terms). But I want the headerContent div to be centered within the header div.
I have always used the "margin: 0 auto" trick to do this, but for some reason it doesn't work at all now.
Here's the html:
<div id="header" class="blueheader">
<div id="headerContent" class="span-24">
<div id="logo" class="span-6">
<a href="/">
<img src="/images/expertinfo.png" width="230" height="62" />
</a>
</div>
<div id="menucontainer" class="span-14"><ul id="menu"><li>
<a href='/Services/Index'>TJÄNSTER</a></li>
<li>
<a href='/About/References'>KUNDER</a></li>
<li>
<a href='/About'>OM OSS</a></li>
<li>
<a href='/About/Contact'>KONTAKT</a></li>
</ul></div>
<div id="logindisplay" class="span-2">
Logga in
</div>
</div>
</div>
And here's the css for header and headercontent:
#headerContent
{
overflow: auto;
zoom: 1;
margin: 0 auto;
}
#header
{
width: 100%;
position: relative;
margin-bottom: 0px;
color: #000;
margin-bottom: 0px;
overflow: auto;
zoom: 1;
}
The overflow and zoom part is just another trick I read about to avoid having to use empty divs to clear containing divs, and I tried without them with no luck, so they have nothing to do with the problem.
Any ideas what I'm doing wrong?
You need to set a width the the #headerContent because without it defaults to width:100% if you place a 950px width to the div, you should be fine.
Found the answer: you shouldn't use span-24 on the headerContent apparently in the Blueprint framework, but rather the container class. Here's what worked:
<div id="header" class="blueheader">
<div id="headerContent" class="container">
<div id="logo" class="span-6">
<a href="#Url.Action("Index", "Home")">
<img src="/images/expertinfo.png" width="230" height="62" />
</a>
</div>
<div id="menucontainer" class="span-14">#Html.Raw(Html.Menu())</div>
<div id="logindisplay" class="span-2">
#Html.Partial("_LogOnPartial")
</div>
</div>
</div>
I cannot say I understand exactly why it didn't work before, and that worries me, because I am trying this framework to simplify layout, but this made it harder to understand. As far as I could see it should have worked with the first code too...

Resources