Can someone explain one part of the float behaviors? - css

Can someone explain it for me in simple words please?
A line box is next to a float when there exists a vertical position that satisfies all of these four conditions: (a) at or below the top of the line box, (b) at or above the bottom of the line box, (c) below the top margin edge of the float, and (d) above the bottom margin edge of the float.
Note: this means that floats with zero outer height or negative outer
height do not shorten line boxes.
Whats the meaning of the first paragraph?
What is the outer height? Is it margin?
It is from: CSS spec 2.1 > visual formatting model > section 9.5 Floats

What's the meaning of the first paragraph?
It's really just a precise definition of when a line of text is considered as being "next" to a float, basically when there is vertical overlap between them. When a line of text is considered to be next to a float, the line of text is shortened to avoid the float.
What is the outer height? Is it margin?
Yes, it's the distance from the upper edge of the top margin to the lower edge of the bottom margin. The important thing to remember in this context is that margins can be negative. So the lower edge of the bottom margin can be above the upper edge of the top margin, in which case the height is negative.
See below or http://jsfiddle.net/n0fobpqr/2/ for examples of how adjusting the bottom margin (and hence the outer height) affects the width of the lines of text.
body { font-size:20px; width: 300px; }
figure { float:left; }
img { padding-right: 10px; }
.one figure { margin:0; }
.two figure { margin:0 0 -60px 0; }
.three figure { margin:0 0 -110px 0; }
<div class="case one">
<figure>
<img src="http://placehold.it/100"/>
</figure>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p>
</div>
<hr/>
<div class="case two">
<figure>
<img src="http://placehold.it/100"/>
</figure>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p>
</div>
<hr/>
<div class="case three">
<figure>
<img src="http://placehold.it/100"/>
</figure>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p>
</div>

It means floats collapse to the next visible line. You can think of "lines" as boxes that may span across the entire width of the browser window. Floats have no impact on the height of these lines. The outer height is the entire height of the float which includes margins and borders.

Related

How do I add an image to a line of text without the white space under the image?

I'm trying to add an image within a line of text but the white space that is automatically added under the image is throwing off the alignment. The other answers to this question only seem to apply to images when they are not part of a line of text, so are not helpful.
The common answers seem to be
display: block, which doesn't work for my use because it adds a line break or vertical-align, which doesn't work for my use because it pushes down the next line of text.
Screenshots from wordpress:
Does anyone know another solution or workaround?
<style>img {
vertical-align: text-top;
margin-top: 3px
}
</style>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam (<img src="http://www.redacted.org/wp-content/uploads/2022/05/info-gray.png" alt="" />) quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat.
you might be able to put a height on your image to prevent it from pushing the text down. also add width: auto to prevent stretching or squishing of your image.
<style>
img {
vertical-align: text-top;
margin-top: 3px;
height: <the size of the text here>;
width: auto;
}
</style>

Word-break at nth word in css

I have a huge sentence as below:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud ullamco laboris nisi ut aliquip ex ea commodo consequat.
I want to break this huge sentence at every 6th word. so it should look as below:
Lorem ipsum dolor sit amet, consectetur
adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis
nostrud ullamco laboris nisi ut aliquip
ex ea commodo consequat.
I tried the following css rules:
word-break: break-all;
No impact. Next I tried:
overflow: visible;
width: 0px;
This is breaking every word to be in a line. any help on how i can word-break at 6th word?
As the other answer mentioned, CSS has no way of adding line breaks.
However, there's a lesser known unit in CSS: ch. Depending on the font you use, you might be able to roughly achieve what you want:
div {
width: 32.5ch;
}
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
If that doesn't work for your use case, a more reliable solution would be to manually add line breaks in your text and then use white-space: pre-wrap to display them, as described in this answer.
You'll probably want to use your second solution, but set the width to be wide enough for roughly 6 words.
overflow: visible;
width: 100px; /* 6 word width */
There's no way to break on 6 words in CSS, the alternative is adding <br /> after every 6th word in JS or some pre-processor.
I know you've mentioned you want it in CSS. But if JS is allowed then its easy without any guesses for width. So this will work even if the world length is big.
See the Snippet below:
var text = document.getElementById("text").innerText;
text = text.match(/(\S+ ){1,6}/g).join("<br>");
// You can change the number 6 to whatever you want.
document.getElementById("text").innerHTML = text;
<div id="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>

How can I solve this problem with CSS float left?

I have 3 divs inside a div. When I float them left, they line up side by side. I want to position them for example 500px from top using nth-child in CSS, but then they position themselves on top of each-other. How can solve this problem?
.wrapper{
content:"";
display:block;
clear:both;
}
.sevice p{
margin: 10px 0;
text-align: center;
}
.service{
float: left;
background-color: rgba(255,255,255,0.7);
width: 26%;
margin: 1%;
padding: 1%;
height: 200px;
position: absolute;
}
.service:nth-child(1){
top: 500px;
}
.service:nth-child(2){
top: 500px;
}
.service:nth-child(3){
top: 500px;
}
<div class="wrapper">
<div class="service">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
</p>
</div>
<div class="service">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
</p>
</div>
<div class="service">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
</p>
</div>
</div>
I think I understand what you are saying. You would like them to be positioned side by side, each with the same top value. Floating will position them side by side automatically (if there is room), but if you want to do that with absolute positioning, you need to set both the top and the left values of each element. If you don't set the left value, your elements will all be set all the way to the left. I've revised the snippet below to give an example (and clean up a couple of things).
The general rule is that when you use absolute positioning, you have to specify the x,y coordinates of one of the corners of the element, relative to the analogous corner of its container. To do that, you can use either top or bottom, along with either right or left. So, top and left will position the top left corner of your element relative to the top left corner of the container, bottom and left will do the same with the two bottom left corners, and so on.
If you don't specify, then the values will all be zero, and your element will be in the top left corner of your container. In your case, you didn't specify left or right, so all three elements were positioned at left: 0. In other words, they were all on top of each other as you observed.
/* Don't need any of this
.wrapper{
content:"";
display:block;
clear:both;
}
*/
.service p { /* You had ".sevice p," which is why you didn't get centering */
/* margin: 10px 0;
Don't really need margin with absolute positioning, just change top/left
values to make adjustments to position */
text-align: center;
}
.service {
/* float: left; don't need this any more */
top: 500px; /* put this here if it's the same for all of them */
background-color: rgba(255, 255, 255, 0.7);
width: 26%;
margin: 1%;
padding: 1%;
height: 200px;
position: absolute;
}
.service:nth-child(1) {
left: 0px;
/* here */
}
.service:nth-child(2) {
left: 27%;
/* here */
}
.service:nth-child(3) {
left: 54%;
/* and here */
}
<div class="wrapper">
<div class="service">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
</p>
</div>
<div class="service">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
</p>
</div>
<div class="service">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
</p>
</div>
</div>

CSS, UL/OL: Incorrect indent with custom counter

Update: this problem has found a very satisfactory solution, but in production side effects popped up which I describe in this thread.
So, I'm using a custom counter in my OLs to get numbering like "1 - 1.1 - 1.1.1"
Works fine.
But when I do this, the indentation of the LI is wrong. The text aligns with the left edge of the number, not with the right edge (like standard OLs do).
Edit: To get the numbers layouted the way I want, I had to mess with the standard paddings/margins of the OL.
Now the text aligns with the left edge of the number, not with the right edge (like standard OLs do).
I've tried numerous things, but somehow I can't seem to control the left edge of the LI content.
Also, this feature apparently isn't used terribly often, so web searches didn't yield any hints :-(
Does anybody have an idea what I've been missing?
Below, you find both the CSS and the HTML, and I have put a test case into this cssdesk: http://cssdesk.com/EzPBG
CSS:
ol.wrong {
margin-left: 0;
padding-left: 20px;
counter-reset: counter_level1;
list-style: none outside none;
display: block;
line-height: 18px;
width: 500px;
}
ol.wrong li {
position: relative;
list-style: none;
margin-right:20px;
}
ol.wrong li:before {
display: inline-block;
position: relative;
content: counter(counter_level1) ". ";
counter-increment: counter_level1;
font-weight: bold;
width: 20px;
}
ol.wrong ol {
counter-reset: counter_level2;
}
ol.wrong ol li {
margin-right:0px;
}
ol.wrong ol li:before {
width: 40px;
margin-left: 20px;
content: counter(counter_level1, decimal) "." counter(counter_level2, decimal) ". ";
counter-increment: counter_level2;
}
HTML
<ol class="wrong">
<li>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
<ol>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</li>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</li>
</ol>
</li>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
<ol>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</li>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</li>
</ol>
</li>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</li>
<li>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</li>
</ol>
Here is one approach:
ol.wrong {
margin-left: 0;
padding-left: 20px;
counter-reset: counter_level1;
list-style: none outside none;
display: block;
line-height: 18px;
width: 500px;
}
ol.wrong li {
position: relative;
list-style: none;
margin-right:20px;
padding-left: 20px; /* create some space for the counter label */
outline: 1px dotted blue;
}
ol.wrong li:before {
display: inline-block; /* block would also work */
position: absolute; /* move this out of the way of the text*/
left: 0; /* move the counter labe into the space from the padding */
content: counter(counter_level1) ". ";
counter-increment: counter_level1;
font-weight: bold;
width: 20px;
}
and you can check the code at: http://jsfiddle.net/audetwebdesign/wsmnJ/
The pseudo-element trick is quite useful, and a good choice in this application.
Start by adding some padding to the left for ol.wrong li, this will create some white space for placing your label.
In your pseudo-element styling, ol.wrong li:before, use position: absolute to remove the label out of the way of the text and position it left: 0. The display type can be either block or inline-block.
You then follow suit for the inner, nested ol.
Just created padding to the left equal in width to the width that you need for your counter/label element.
I agree with Marc Audet, interesting CSS, however by dispensing with HTML's natural way to deal with nested lists you've created your own little world to contend with. As far as I understand it there are three possible ways to deal with this:
Firstly, go back to the standard native way to deal with nested lists as you have with your "standard indentation" list.
Secondly, add something like this to the pseudo-element...
ol.wrong li:before {
float:left;
height:80px;
}
...The floating of the pseudo-element kicks the rest of the LI to the right, however setting the height to a fixed value isn't very flexible unless you can guarantee that all LIs will be the same height (alternatively you can set several heights and choose whichever suits each particular LI... again, though, rather clunky).
Finally, taking the above idea and adding some javascript to deal with changing the height of the pseudo-element on the fly... if this is even possible.
Daniela, I'd think the simple solution is to use positive and negative positioning. the LI is moved to the right (+20px) whereas the counter is moved to the left (-20px). I think it's easier to check this fiddle:
http://fiddle.jshell.net/Gbf6u/

Wrapping text & block elements around images

I know it's easy enough to wrap text around images by floating the image right or left & then putting your text after it, but what I am wanting to do is wrap other elements around it as well, such as div's.
I tried to set my div to inline & this worked fine, however once I added other divs inside that div it still looked fine, but when looking at it in Firebug the little blue line that shows the element you are hovering over in the code extended over the image as well & when I attempted to add padding to the container div it didn't work & you could see that was because the padding was added right at the end.
I ended up getting it to look ok but adding padding to the image, however it still doesn't seem the right way to go about it seeing as Firebug doesn't like it & I am worried about compatibility issues.
Here is an image of what I am trying to do.. the gray area is where I want the text/elements to wrap & the brown is the image.
Here is some example code: (This example is the not wrapping version)
<div class="main">
<img src="../images/work/example.png" width="275" height="233" class="screenshot" alt="Example" />
<div class="details">
<div class="about">
<div class="title">
About:
</div>
<div class="info">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
<!-- Info Ends -->
</div>
<!-- About Ends -->
</div>
<!-- Details Ends -->
<div class="contentClear"></div>
</div>
<!-- Main Ends -->
Example CSS:
#content .wrapper .left .main {
padding-top: 20px;
width: 550px;
}
#content .wrapper .left .main .screenshot {
float: right;
border: 1px solid #c0c0c0;
width: 275px;
}
#content .wrapper .left .main .details {
width: 263px;
padding-right: 10px;
}
#content .wrapper .left .main .details .title {
color: #0F5688;
font-size: 1.8em;
font-family: arial;
font-weight: bold;
}
#content .wrapper .left .main .details .info {
margin-top: 6px;
font-size: 1.3em;
font-family: Arial;
color: #636363;
line-height: 1.6;
}
Here is an image showing the issue FireBug has with it (from the JSFiddle example), as I say it looks fine on the browser, but seeing as the firebug bar extends all the way over the image I was worried that may cause problems..
Yes, the correct way to move something to one side and have stuff wrap around it is to float the element.
In the example below (simplified from your code), adding padding to the floated image works just fine.
CSS:
.main .screenshot {
float: right;
border: 1px solid #c0c0c0;
padding: 5px;
}
.main .title{
font-size: 140%;
}
HTML:
<div class="main">
<img src="img/png" width="150" height="117" class="screenshot" alt="Example" />
<div class="details">
<div class="about">
<div class="title">About:</div>
<div class="info">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
</div>
</div>
</div>
Demo jsFiddle

Resources