Fluid floated element next to fixed-width floated element - css

Here's my simple markup:
<div class="row">
<div class="col fixed">Short text</div>
<div class="col fluid">Longer text....</div>
</div>
And here's my CSS:
.row {
padding: 20px;
}
.row:after {
content: "";
display: block;
clear: both;
}
.col {
float: left;
}
.col.fixed {
width: 200px;
}
I thought the fluid text would appear next to the fixed column (to its right), and would then wrap down. This is not what actually happens. Instead, the whole .col.fluid drops under the other column, as if there were no floats.
The demo is here.
Why doesn't this work (isn't this how floats are supposed to work)? And what can I do to fix this?

One way to do this is by using this CSS:
.row {
background: rgba(0, 128, 0, .2);
padding: 20px;
display:table;
}
.col {
display:table-cell;
}
.col.fixed {
width: 200px;
}
jsFiddle example

To get the effect that you want, you need to place the floated-fixed box inside the fluid, block-level div.
See the following: http://jsfiddle.net/audetwebdesign/7cMQg/6/
<div class="row">
<div class="col fluid">
<div class="col fixed">Short text: to see the full effect,
type in a bit more text so that you can get a few lines
in the floated-fixed-width box.</div>
Lorem Ipsum is simply dummy text of the printing and typesetting
industry...
</div>
</div>
If you check the CSS, I added a right margin to the floated div and changed the background color to highlight the positioning.

If you'd like an effect similar to the first letter being larger than the rest of the text (found in many books & writings), try this:
<div>
<span class="fixed">Short text</span>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
CSS
.fixed {
width: 200px;
display:inline-block;
}
jsFiddle

Related

How to make text wrap before inner floating elements inside a container

How to make text wrap before inner floating elements inside a container?
Here is what I'm trying to do...
http://codepen.io/W3Max/pen/tKwqz
<div>
<div style="height:100px;width:300px;background-color:blue;float:left;"></div>
<div style="height:100px;float:left;word-wrap: break-word;background-color:green;">sdffds dgsdfgsdg sdfgsdfg sdfgsdfg ijhjkhkh lkjlk</div>
<div style="height:100px;width:300px;background-color:red;float:left;"></div>
</div>
I would like the text inside the green div (in the middle) to wrap before the row wraps when I resize the screen.
I would prefer to to support IE9. Is it possible without flexbox?
display:table is compatible with IE8+ and can achieve what you're looking for:
Forked pen.
.container {
display: table;
width: 100%;
/* if you don't want 100% width, set a max-width too */
word-wrap: break-word;
}
.container > div {
display: table-cell;
height: 100px;
}
.container > div:first-child, .container > div:last-child {
width: 300px;
}
<div class="container">
<div style="background-color:blue;"></div>
<div style="background-color:green;">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
<div style="background-color:red;"></div>
</div>
HTML
<div id="green">sdffds dgsdfgsdg sdfgsdfg sdfgsdfg ijhjkhkh lkjlk<div>
CSS
#green {padding:10px} (resize it) on green div .

How to position text in html with div tags?

This code:
<div id="columns">
text
</div>
<div id="columns">
text
</div>
<div id="columns">
text
</div>
Is coded with this css:
#columns {
width: 200px;
float: left;
margin-left: 20px;
margin-right: 20px;
}
The problem is that if I put any text below the three columns created, it just adds another column! I want the footer to be below these columns, but I can only do this so far by setting this:
footer {
/*height: 50px;*/
text-align: center;
position:absolute;
bottom:0;
}
And this just makes the page longer, i.e. puts a huge gap between this content and the footer.
Any solutions?
Thanks
Elements are floated left making document flow modified. Document flow needs to be reset right before writing footer. It can be done by setting property clear:both for the footer (in fact just after .columns are finished).
A working jsfiddle is here.
CSS:
footer{
clear: both;
}
Suggestion (outside scope of question):
You should change id="columns" to class="columns" as in valid html markup, id's should be unique. (thanks michael)
try this
demo
css
*{
margin:0;
padding:0;
}
#columns {
width: 200px;
float: left;
margin-left: 20px;
margin-right: 20px;
background-color:red;
}
.clearfix{
clear:both;
}
footer {
/*height: 50px;*/
text-align: center;
position:absolute;
border:1px solid red;
}
html
<div id="columns">
text
</div>
<div id="columns">
text
</div>
<div id="columns">
text
</div>
<div class="clearfix"></div>
<footer>footer</footer>
When using any floated items, the default behavior is for them not to count towards other content in that area, so they'd appear to one side of other content.
Of course, if you want to prevent that, the clear property will essentially continue below any floated items on one (or either) side before it.
footer {
height: 50px;
clear: both; /* can also use `clear: left` here */
}
try after removeing bottom:0; and put below html code after third column
<div id='footer'>
<p>Footer Content</p>
</div>
You need to use the css clear for your problem
like clear :both for id/class of you div
text
<div id="columns">
text
</div>
<div id="columns">
text
</div>
<div style="clear:both"></div>
<footer>footer</footer>

Two column dynamic layout

I have several boxes containing the following!
<div class="box">
<div class="image">
<img src="anything.png" /> <!-- The width is dynamic -->
</div>
<div class="box-text">
<h2>Some Title</h2>
<p>Lorem ipsum ...</p>
</div>
</div>
The problem is that the text floats to the left after the image. I want it to go straight down, as I've marked it in the following image:
This is the way it should look like:
I do not want to use tables or javascript. I can't use margin-left for the box-text, because the width of the image is dynamic.
Thanks for your contribution!
try using display: table for your box class and display: table-row for both image and box-text classes. Then align content using vertical-align: top on image and text.
Do not use tables, but make the <div>s act like the cells of a table, then make the inline content vertically aligned to the top:
​.box{display:table}
.image{display:table-cell;vertical-align:top;}
.box-text{display:table-cell;vertical-align:top;}
Check it here
Try this css:
.image { float: left; }
.box-text { float: left; }
Also you may want to stop floating elements after box div, so don't forget some clean up in the end:
<div style="clear:both"></div>
Here's an fiddle which shows how to acheive this layout: http://jsfiddle.net/X7j4P/
.box {
overflow:hidden;
}
.image {
float:left;
}
.image img {
width:300px;
}
.box-text {
width:300px;
float:left;
}
p {
margin-bottom:10px;
}

Make div of text only able to be as wide as its container

HTML
<div class="cont">
<div class="size" id="size1"></div>
<div class="text">Here is some textHere is some text Here is some text</div>
</div>
<div class="cont">
<div class="size" id="size2"></div>
<div class="text">Here is some textHere is some text Here is some text</div>
</div>​
CSS
.size {
background-color: red;
height: 100px;
width: 100px;
}
#size2 {
width: 200px;
}
.cont {
padding: 10px;
float: left;
}​
I need div.cont's widths to be the width of their contained div.size (in my actual example div.size is an image and its with will vary in each instance).
This isnt happening as div.text takes up more space than its container, how can I stop this and make the text wrap?
JS Fiddle
Deleted all the previous stuff as I have (after doing some digging) found an exact duplicate with working solution.
My answer was also incorrect (as the op then specified the image MUST be allowed to be variable)
The answer is found on this jsfiddle and is an exact duplicate of css - shrink a parent div to fit one child's width and constrain the width of the other child
//html
<div id="container">
<div id="child1"><img src="//www.google.com/logos/2012/Teachers_Day_Alt-2012-hp.jpg" width="300" height="116"></div>
<div id="child2">Lorem ipsum dolor sit amet.</div>
</div>
<br/>
<a id="longtext" href="#">lengthen/shorten text</a>
//css
#container{border:1px solid #f00;display:inline-block;margin:10px; display: table;}
#child1{border:1px solid #0f0;margin:10px; display: table-row; width: 1px;}
#child2{border:1px solid #00f;margin:10px; display: table-cell; width: 1px;}
img {border:1px solid #000;}
and basically it works using display:table-* (have a good read up)
'.size{ float:left;}'
let me know if this helps.
Expanding on Paul Sullivan's approach,
in your css:
.size {
...
display:block; /*assuming its an image - making sure its block level*/
...
}
.cont {
...
position:relative; /*add this to parent container if comming from cms*/
...
}
.text {
...
position:absolute;
top:100%; /*just to make sure content doesnt overlaps image*/
...
}
Just gives a plus point for getting content to stretch as wide as the image (plus padding)
Hope it helps,
Fiddle: http://jsfiddle.net/BKRsT/3/

CSS: Position a div vertical to the text height

I'm looking for a solution, to position a div-element (with a fixed width) near to a text, like in this example:
What is the most common solution by using CSS ?
I would do it something like this:
http://jsfiddle.net/Xfmdr/
.column { display: table-cell; }
.column:nth-of-type(1) { vertical-align: middle; }
#green { background: green; padding: 30px; margin: 10px;}
<div id="container">
<div id="left" class="column">
<div id="green">div</div>
</div>
<div id="right" class="column" >
<p>Lorem Ipsum </p>
</div>
</div>​
For reference, vertically aligning stuff is a pain in the nuts in CSS. See this very useful article on why I chose to display as table-cell for this use case. http://phrogz.net/css/vertical-align/index.html

Resources