I've created this simple html :
there are 2 SPANs here.
one should be beneath the other by clearing the float.
The bbb element has float:left
And I used (on the aaa element ) , Facebook's "clearfix" CSS which is :
.clearfix:before { content: ""; display: table; }
.clearfix:after { content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;}
.clearfix { zoom: 1; }
This is a valid way ( as facebook do) and as described here
However it works in FireFox : (look at the above picture)
But in chrome (v 24) it doesn't .
What am I missing ?
Your jsbin example has TWO typos/syntax errors and the clearfix code is NOT correct.
<span class=" clearfix"> you have a space before clearfix
<span class='fll'>bbbbb </span> you have single quotes instead of double quotes.
Change your html to:
<div class="clearfix">aaa </div>
<div class="fll">bbbbb </div>
and your CSS to:
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.clearfix { display: inline-table; }
* html .clearfix { height: 1%; }
.clearfix { display: block; }
and it works (all browsers,IE6-7 included) http://jsbin.com/ukaxav/19/
.clearfix { display: block; }
Isn't it a "bit" more clean?
add display:inline-block; property to the parent div.
Code:
<div style="height: 100%; border: 1px solid blue;display: inline-block;">
<span class=" clearfix" style="">aaa </span>
<span class="fll">bbbbb </span>
</div>
Clearfix is intended to clear floats. meaning, on an element that contains float.
Not for clearing previous floats.So in your question, you are not putting the clearfix on the correct place, or misunderstand the principle.
Add a width: 100%; in your fll
so,
.clearfix:before {
content: "";
display: table; }
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden; }
.clearfix { zoom: 1; }
.fll
{
float:left;
width: 100%;
}
Related
My problem is that when the class is-sticky is added to my menu, my ::before and ::after on logo are not necessary anymore. I'm not the biggest hero with Jquery and can't fix it with a online search.
the div
<div id='Top_bar' class='is-sticky'>
<div class='container>
<div class='top_bar_left>
::before
<div class='logo>
::after
</div>
</div>
</div>
</div>
My scss
.logo {
background: #1A2741;
padding: 0 50px;
width: 13%;
margin: 0 !important;
#logo {
margin-left: 39%;
}
&::before {
content: ' ';
background-image: url(../uploads/RH-Beelmerk.svg);
height: 100px;
width: 50px;
position: absolute;
padding: 50px;
z-index: 2;
top: -85%;
left: 1%;
transition: top 2s;
}
&:hover::before {
top: -50%;
}
&::after {
content: '';
background: #1A2741;
height: 110px;
width: 50px;
display: block;
transform: rotate(10deg);
position: absolute;
left: 15.5%;
top: -6%;
z-index: 0;
border-right: solid 4px #FF8496;
}
}
You can either unset their content (content: unset;) or turn off their display (display: none).
For instance, here's unsetting the content (which was originally posted as an answer by doğukan but then deleted for some reason; since this answer was accepted, I've added that here and marked the post Community Wiki):
.is-sticky::before, .is-sticky::after {
content: unset;
}
Depending on the selector adding them, you may need to make that more specific, but that's the general idea.
Example:
setInterval(() => {
document.querySelector(".target").classList.toggle("is-sticky");
}, 800);
.target::before {
content: 'before';
}
.target::after {
content: 'after';
}
.is-sticky::before, .is-sticky::after {
content: unset;
}
<div class="target"> text </div>
Or turning the display of the content off instead:
.is-sticky::before, .is-sticky::after {
display: none;
}
Example:
setInterval(() => {
document.querySelector(".target").classList.toggle("is-sticky");
}, 800);
.target::before {
content: 'before';
}
.target::after {
content: 'after';
}
.is-sticky::before, .is-sticky::after {
display: none;
}
<div class="target"> text </div>
Put the condition with 'is-sticky' and unset the :before and :after children. Then you can just toggle the 'is-sticky' class on the logo. And keep in mind that :before and :after are children of your class-element, not elements outside of your class-element.
.logo{
&.is-sticky{
&:before, &:after{
content: none;
}
}
}
How about using the CSS :not test, that way you don't get the pseudo elements if is-sticky is set.
See MDN:
The :not() CSS pseudo-class represents elements that do not match a list of selectors. Since it prevents specific items from being selected, it is known as the negation pseudo-class.
e.g. &:not('.is-sticky')::after instead of &::after
That way you don't need any extra entries in the CSS.
This is the website I am developing right now: in-nabavi.com. There is nothing in it but a few blocks. As you see there is an extra white space at the end of the document that I really have no idea why it is in there.
This is the firebug result
As you see the area with red line around it is not included in the HTML area.
I also tried the following
body, html{
margin:0;
height: 100%;
}
but it didn't work again.
Thanks.
You have used content: "." inside clearfix class for setting layout.
Use content: ""; only.
.clearfix:after {
visibility: hidden;
display: block;
content: "";
clear: both;
height: 0;
}
There is a .clearfix:aftercss class in which there is a content:"."
Remove that.
Or override it, so that it does not hamper anything else.
.clearfix:after {
display: block;
height: 0;
clear: both;
visibility: hidden;
content: close-quote;
}
You can try this....
.panels-flexible-1 .panels-flexible-row-last{position: absolute;}
Just curious.. I was going through the code bootstrap.css and saw the following..
.container {
margin-right: auto;
margin-left: auto;
}
.container:before,
.container:after {
display: table;
content: " ";
}
.container:after {
clear: both;
}
.container:before,
.container:after {
display: table;
content: " ";
}
.container:after {
clear: both;
}
It seems that a portion of the code is repeating and does the same thing... even if you take it out of the css code specifically the following:
.container:before,
.container:after {
display: table;
content: " ";
}
.container:after {
clear: both;
}
it seems that all works fine and dandy.
Is it an error or oversight?
It was a bug, but was fixed a month ago. There is some debate on whether it was a Bootstrap bug, or a bug in their CSS compiler, LESS. Discussion on bugtrackers for LESS, Bootstrap.
Details
Bootstrap CSS is compiled from Less, and this particular section of the code comes from grid.less, using mixins from mixins.less.
Figuring out why it's duplicating requires going down a couple layers. The relevant Less code is here:
// Set the container width, and override it for fixed navbars in media queries
.container {
.container-fixed();
#media (min-width: #screen-sm) {
width: #container-sm;
}
#media (min-width: #screen-md) {
width: #container-md;
}
#media (min-width: #screen-lg-min) {
width: #container-lg;
}
}
Which uses the mixin .container-fixed():
// Centered container element
.container-fixed() {
margin-right: auto;
margin-left: auto;
padding-left: (#grid-gutter-width / 2);
padding-right: (#grid-gutter-width / 2);
.clearfix();
}
Which in turn uses the mixin .clearfix():
.clearfix() {
&:before,
&:after {
content: " "; // 1
display: table; // 2
}
&:after {
clear: both;
}
}
EDIT: After some further digging, the bug was that they had inadvertently defined the .clearfix() macro twice - once in mixins.less and then again in utilities.less. You can see it in action at less2css.org with this LESS code:
.clearfix() {
&:before,
&:after {
content: " "; // 1
display: table; // 2
}
&:after {
clear: both;
}
}
.container {
.clearfix();
}
.clearfix {
.clearfix();
}
I don't know enough LESS to say for sure, but it seems that the second .clearfix is being treated as some sort of mixin, and is looping. If you switch the LESS version ("Options" button at less2css.org) to 1.3.1 or earlier, you get a recursion error - it seems LESS added some code to stop the infinite recursion, but some duplicates still get generated.
Further notes: this bug affected everything that had a .clearfix() mixin - I say that in past tense, because they recently fixed this bug, namely by switching to a different way of using the mixins.
TL;DR: Yes, it was a bug, but they fixed it a month ago. There is some discussion of whether it was a LESS bug or a Bootstrap bug on LESS's bugtracker and back over on Bootstrap.
Is it possible to implement vertical layout with CSS only, and not with HTML elements?
I have a list of divs inside one div. By default the next element is right to the last, when there's no place on right, it is placed below.
I'd like to achieve the same with CSS style settings. Is it possible?
By CSS-only I mean, we have div and its children, and do not add anything special such as:
line-breaking elements ( <br/>, <div style="clear:both;"/> )
UL tags
tables (yes, still used, f.g. JSF almost exclusively based on them)
So:
<div id="menu">
Page 1
Page 2
Page 3
</div>
And CSS implementing vertical layout:
#menu { ??? }
#menu a { ??? }
Is there a ??? that I could use to achieve what I want?
Display anchor tags as block elements.
#menu a {
display: block;
}
Do you mean something like this?
http://jsfiddle.net/7Y9jS/
#menu {
width: 300px;
}
#menu a {
display: block;
background: #ccc;
color: #000;
padding: 10px 0;
text-align: center;
margin-bottom: 2px;
}
<div id="menu">
Page 1
Page 2
Page 3
</div>
set display block to a
#menu a {
display: block;
}
use float left
#menu a {
float:left;
}
and then add the class group to your #menu
.group:before,
.group:after {
content: "";
display: table;
}
.group:after {
clear: both;
}
.group {
zoom: 1; /* For IE 6/7 (trigger hasLayout) */
}
As a part of learning CSS (& practically applying it — by creating simple themes), today I wanted to know some proper ways of clearing floats in CSS.
I wanted to see how Twitter does it, so I downloaded Bootstrap, went through the bootstrap.css file, and found what I was looking for (I found two code blocks):
.clearfix {
*zoom: 1;
}
.clearfix:before, .clearfix:after {
display: table;
content: "";
}
.clearfix:after {
clear: both;
}
&
.container {
margin-left: auto;
margin-right: auto;
*zoom: 1;
}
.container:before, .container:after {
display: table;
content: "";
}
.container:after {
clear: both;
}
I immediately tried it out, and that specific part of my code looked like so:
<p class="sample-preview">
<span class="sample-preview">PREVIEW</span>
<em>This is italicized aka emphasized</em>, and so is <em>this</em>.<br />
<strong>This is bold aka strong emphasis</strong>, and so is <strong>this</strong>.<br />
Use <strong><em>italics and bold together</em></strong> if you <strong><em>have to</em></strong>.
</p>
+
p.sample-preview {
border: 1px solid #FFCCC9;
background: #FFEBE9;
outline: 2px solid #FFEBE9;
padding: 10px;
}
span.sample-preview {
display: inline-block;
float: right;
margin:0;
font-weight: bold;
font-size: 12px;
background: #FFCCC9;
padding: 2px 5px;
}
.sample-preview {
margin-left: auto;
margin-right: auto;
*zoom: 1;
}
.sample-preview:before, .sample-preview:after {
display: table;
content: "";
}
.sample-preview:after {
clear: both;
}
Although I am not entirely sure, I think this code is causing a weird bug on the page I tried it. Why do I think so? Everything seemed fine when I removed display: table; from the code using Firebug.
You can take a look at the page here and the bug is — the first pink box is taller than the content. What am I doing wrong?
The issue is that you're also clearing the floated menu to the right.
There's two solutions for that:
the usual is to float your content area itself to the left. This means that everything inside it is in a different float context. Your clear will only affect the elements inside of it.
another trick that works is specifying overflow: hidden on your sample-preview paragraph. This is probably easier to do. Specifying the overflow property on an element (but not set to visible) causes it to behave like a float container.
Cfr: http://www.brunildo.org/test/clear.html, http://webdesignerwall.com/tutorials/css-clearing-floats-with-overflow
I should also note that with this overflow trick, you don't need the clearfix at all.