Merge multiple css rules of a class into a single class declaration? - css

I like it to separate different concerns of a class in css.
Example:
// Layout
.myElement {
width: 100px;
height: 100px;
margin: 10px;
padding: 5px;
}
// Chrome
.myElement {
background: red;
border: 2px solid green;
box-shadow: inset 0 0 10px black;
}
// Content
.myElement {
color: white;
font-size: 120%;
}
// Interaction
.myElement:hover {
background: black;
border: 10px dotted red;
}
Pretty fine so far. Personally I find that approach readable, nice to maintain and nice for development.
But is there any mechanism/tool available which would merge all rules of a class into one single class declaration before deployment automatically?
Desired result:
.myElement {
width: 100px;
height: 100px;
margin: 10px;
padding: 5px;
background: red;
border: 2px solid green;
box-shadow: inset 0 0 10px black;
color: white;
font-size: 120%;
}
.myElement:hover {
background: black;
border: 10px dotted red;
}
EDIT:
After reading the comments I felt my "artificial" example wasn't comprehensive enough. Below is a real world example written in the Stylus syntax but the results are the same as described above. I left the original example from above in place.
The next example exists as a single file which describes the appereance of a certain area and its contents which appears in various places on a site. It's like a microcosm for that area. As a consequence you can imagine there are more of these microcosms.
// Layout
.events
padding 2em
.eventItem
margin 0 0 4em
.eventCal
float left
padding 2.5em 0 0
.eventArticle
margin 0 0 0 6em
.eventHead
margin 0 0 1em
time
margin 0 2em 0 0
// Chrome
.eventCal
background url("../../assets/img/icon-events.png") no-repeat
.eventHead
border-bottom .5em solid $chimney
// Content
.eventItem
list-style-type none
a
color $beige
text-decoration none
h2
font-family $elsie
.eventCal
color $beige
font-family $elsie
font-weight bold
font-size 150%
.eventHead
font-size 75%
.eventDesc
font-size 90%
// Interaction
.eventItem
a:hover
color $chimney
text-decoration underline

My computer is sorta nagging me to reboot after a few straight days of coding , but basically I do stuff like that fairly regularly in my libraries. Basically what you can do to quickly mimmick this functionality is to grab one of the zillion coding examples for enumerating the CSSrules from the sheets.
Then in the enumeration function simply take the selectorText property and drop it into document.querySelector/querySelectorAll which will give you the elements matching the selectorText toss these , the selectorText and related cssText into a little {}/[] or whatever suits your fancy.
After the enumeration finishes do a document.createElement('style') and use the insertRule() to glue the styles together as you please.
Then simply grab the finished product from style.textContent and you have your merged stylesheet.
Being as the commonly available code for enumeration does the actual hard part , this is a nice lazy evenings coding task for most folks and it will do the job EXACTLY the way YOU want.

Related

Why are half of my <hr>s being styled differently than the others?

I don't know why, but when I try and style my horizontal rules in css, every other hr looks different. See below:
https://i.imgur.com/ltZdncM
When I increase the border size to 2px, the gap in between the top and bottom borders is filled in, but then half the horizontal rules are thicker than the others.
MY CODE:
hr {
border: 1px solid #07234f;
width: 300px;
}
<body>
<hr>
<hr>
<hr>
<hr>
</body>
I expect all the horizontal rules to look similar. Any idea why they look different? How can I fix this issue?
You're placing a border on all sides of the hr. But Chrome, for example, already has a user-agent stylesheet that sets an inset border style on hr's. I was able to replicate your issue at various px units.
One way to fix this issue is the following:
kill whatever border the user agent stylesheet may be implementing behind your back: border: 0
Then set a border along a single edge (top or bottom) and set it to the px value you'd like. (I chose border-top: 2px.)
Set an explicit height on the hr equal to the pixel value you chose for border-top to prevent (in this case) Chrome's user agent stylesheet from displaying border-style: inset on your element.
hr {
border: 0;
border-top: 2px solid #07234f;
height: 2px;
width: 300px;
}
<body>
<hr>
<hr>
<hr>
<hr>
</body>
Another approach might be to explicitly set the individual border properties yourself so as to avoid confusing Chrome about which border style to apply (it appears that even with border: 1px solid #000000, Chrome still insists on applying the inset styling).
hr {
border-width: 2px 0 0 0;
border-style: solid;
border-color: #07234f;
width: 300px;
}
UPDATE
One way to implement a double line would be to set a top and bottom border with an explicit height:
hr {
border-width: 2px 0 2px 0;
height: 4px;
border-style: solid;
border-color: #07234f;
width: 300px;
margin: 16px auto;
}
You could also use border-style: double like this:
hr {
border-width: 4px 0 0 0;
border-style: double;
border-color: #07234f;
width: 300px;
margin: 16px auto;
}
Also, here's some inspiration for your hr styling needs: https://css-tricks.com/simple-styles-for-horizontal-rules/
It's a bit dated, but may give you some ideas.
UPDATE 2
This works fairly well, though I do still see some aberrations at various zoom levels. Does the following work for you at normal zoom? Is it only when you manually zoom the viewport that you see aberrations?
It's a conundrum.
body {
background-color: lightblue;
}
hr {
border: 0;
width: 300px;
margin: 16px auto;
height: 2px;
background-image: url("data:image/svg+xml,%3Csvg width='300' height='2' viewBox='0 0 376 2' fill='none' xmlns='http://www.w3.org/2000/svg'%3E %3Cline y1='0.5' x2='300' y2='0.5' stroke='black' stroke-opacity='0.1'/%3E %3Cline y1='1.5' x2='300' y2='1.5' stroke='white' stroke-opacity='0.3'/%3E %3C/svg%3E ");
}

Multiple custom scrollbars

All I want to know is if it is possible to have multiple custom made -webkit-scrollbars on the same page.. I making some divs color specific, like one div has green text and images and another blue etc. So I would like to make a custom scrollbar for each div so it matches the color..
Q1: Is it possible?
Q2: If so, how would I do it?
I have thought about one solution, but I think it is a bit cumbersome. One solution may be to make each div containing an iframe and then create separate pages with the unique scrollbars, but I don't know if that is going to work either..
Of course you can - simply prepend the scrollbar pseudo-classes with your intended selectors, i.e.:
::-webkit-scrollbar-track {
background-color: #333;
}
/* Override styles for <div>s, for example */
div::-webkit-scrollbar-track {
background-color: #b13131;
}
I have made a simple example for you here - http://jsfiddle.net/teddyrised/Nsz93/
You can also apply these rules by id of the element. Let's say scroll bar of a div has to be styled which has an id "myDivId". Then you can do following. This way you can use different styles for scroll bars of different elements.
#myDivId::-webkit-scrollbar {
width: 12px;
}
#myDivId::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
border-radius: 10px;
}
#myDivId::-webkit-scrollbar-thumb {
border-radius: 10px;
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5);
}
http://jsfiddle.net/QcqBM/516/
It's possible using either a jquery plugin or simply styling the scrollbars w/ css. This can be done in webkit and ie.
::-webkit-scrollbar {
width: 12px;
}
::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
border-radius: 10px;
}
::-webkit-scrollbar-thumb {
border-radius: 10px;
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5);
}
http://jsfiddle.net/jeffpowrs/nEkPw/
http://css-tricks.com/custom-scrollbars-in-webkit/
Here's the solution
div::-webkit-scrollbar {
width: 0.5em;
border-radius: 5px;
}
div::-webkit-scrollbar-track {
box-shadow: inset 0 0 6px #f8fbff;
border-radius: 5px;
}
div::-webkit-scrollbar-thumb {
background-color: #9fa9bd;
border-radius: 5px;
}

Stylist Css Border Creation

Is it possible to create a border like the flowing image with css? Any hints will be appreciated
#sidebar h4, #sidebar-alt h4 {
background:url('images/widget-title-bg.png');
color: #333333;
font-size: 22px;
font-family: Arial, sans-serif;
font-weight: normal;
margin: 0 0 10px 0;
padding: 7px 0px 11px 0px;
}
EDIT: Made some changes according to your comments. Try:
<h1 id="progress">
<i></i>Recent Posts
</h1>​
#progress {
display: block;
max-width: 200px;
min-width: 150px;
position: relative;
margin: 50px auto 0;
padding: 0 3px;
border-bottom: 10px solid #ECECEC;
font: bold 26px 'Dancing Script', cursive;
}
#progress i {
display: block;
position: absolute;
width: .8em;
height: 10px;
left: 0;
bottom: -10px;
background-color: #4287F4;
}​
http://jsfiddle.net/userdude/z45QJ/4/
I'm not a big fan of the position manipulation, but all browsers should support and display this nearly identically, the only possible problem being the font's displa may be slightly differently in different browsers. However, IE7-9 should interpret everything else just fine.
Too bad the whole wuuurld isn't on WebKit:
<div id="progress"></div>​
#progress {
width: 300px;
height: 10px;
border: none;
background-color: #ECECEC;
border-left: solid #4287F4;
box-shadow:inset 2px 0 white;
-webkit-animation: slide 10s linear infinite;
}
#-webkit-keyframes slide {
from {
border-left-width: 0;
width: 300px;
} to {
border-left-width: 300px;
width: 0;
}
}​
http://jsfiddle.net/userdude/z45QJ/1
It could be adjusted to go both ways. However, it only works on WebKit browsers (Chrome, Safari [?]). If that's ok, let me know and I'll add the return trip.
There are four ways to do it. I demonstrate four ways in this JSFiddle, and here are some explanations.
If you're not sure, just use Method B.
Method A
Method A has the advantage that it's the most compatible but the disadvantage that it requires extra HTML. Basically, you're giving an outer div the blue border and an inner div the white border. Your HTML will look something like this:
<div class="methodA">
<div class="container">
Method A
</div>
</div>
Your CSS will look like this:
.methodA {
border-left: 10px solid blue;
}
.methodA .container {
height: 100%;
border-left: 10px solid white;
}
Method B
Method B has the advantage that there's no extra HTML, but the disadvantage is that it won't work in IE before version 9.
.methodB {
border-left: 10px solid blue;
-webkit-box-shadow: inset 10px 0 white;
-moz-box-shadow: inset 10px 0 white;
box-shadow: inset 10px 0 white;
}
You can mitigate IE's compatibility issues using CSS3 PIE, which makes box shadows behave in Internet Explorer (along with other CSS3 features).
Methods C and D
This JSFiddle shows two other methods, which I won't describe in as much detail, but...
Method C makes the blue border a shadow. As a result, it can "cover" other elements and it also changes the size of the element. I don't love this solution, but it might work for you. It also suffers the compatibility issues of Method B.
Method D puts two divs inside of the element: one for the blue border and one for the right border.
it is not really complicate and no extra HTML is needed.
h4:after {
display:block;
content: '';
height:4px;
width: 1px;
border:0px solid #ececec;
border-left-width: 10px;
border-left-color:#4287F4;
border-right-width: 90px;
}​
http://jsfiddle.net/N27CH/
Check this link Visit
(http://jsfiddle.net/qD4zd/1/).
See if it helps. This tells you about the application of gradient. See how it is done.
Also why not use directly the images that you want as the border.
Check out for "Gradient" in Css. This might answer your question.
I studied some usage of "canvas" tag in HTML5. That is preety much informative about gradient specification and is also more readable than the traditionl HTML4. So for this question i also want to request the questioner to look at the "canvas" tag in HTML5. check the link below.
Link: http://html5center.sourceforge.net/Using-Unprefixed-CSS3-Gradients-in-Modern-Browsers
Link: http://www.sendesignz.com/index.php/web-development/111-how-to-create-gradient-and-shadow-effect-in-html5-canvas
Second link is more awesome. Cheers.:)

Placing text on a CSS trapezoid

I have inherited a legacy app for a rewrite and have run across a curious problem. There is tabular data displayed on the page where the title of the table is within a trapezoidal shape that resembles a manila envelope tab. At the bottom of such tables, there is usually a button row that is the same shape as the table title but rotated 180°. Currently, this effect is being pulled off by using a square image with a white triangle in one half on a transparent background as a background image in the corner of a rectangular block to achieve the look of a trapezoid. However, this technique is prone to flickering when the page is refreshed.
As an exercise, I have tried to see if I can replace this with a pure CSS technique. I found this link to different shapes in CSS and have emulated the trapezoid to look as I need. I am able to place the table title text within a trapezoid correctly. However, when I need the look of the 180° rotated trapezoid, I am unable to get the text to place within the shape. My code is included below and here is a jsFiddle showing what I have accomplished so far. I understand that the text shows below the rotated trapezoid because the height is set to 0 and I'm using border-top to build the shape. Is there anything I can do to get this to work correctly?
Please keep in mind that I need this to display in IE8 (and possibly also IE8 in compatibility mode -- IE7). Also, I'd like to keep additional HTML elements to a minimum because I want to keep this as semantic as possible. I know I can place a span inside the div and absolutely position that span so that it displays the text within the shape, but when I do that I have to manually set a width on the trapezoid and when the width can vary from button row to button row, I'd rather not go down that path.
Thanks.
HTML:
<div class="trap">Title Text</div>
<div class="trap180">Button Row</div>​
CSS:
.trap {
color: black;
font: normal bold 13px Arial;
border-bottom: 27px solid #F00;
border-right: 27px solid transparent;
height: 0px;
float: left;
line-height: 27px;
padding: 0 4px;
}
.trap180 {
clear: both;
color: black;
font: normal bold 13px Arial;
border-top: 27px solid #F00;
border-left: 27px solid transparent;
height: 0px;
float: right;
margin: 20px 0 0 0;
line-height: 27px;
padding: 0px 4px 0;
}​
It's possible with pseudo-elements. But I don't have access to those old browsers to test.
.trap, .trap180 {
color: black;
font: normal bold 13px Arial;
float: left;
line-height: 30px;
height: 30px;
padding: 0 4px;
background: salmon;
position: relative;
}
.trap180 {
float: right;
margin: 20px 0 0 0;
}
.trap:after,.trap180:after {
content: '';
position: absolute;
height: 0px;
width: 40px;
top: 0;
}
.trap:after {
right: -30px;
border-bottom: 30px solid salmon;
border-right: 30px solid transparent;
z-index: -10;
}
.trap180:after {
left: -30px;
border-top: 30px solid salmon;
border-left: 30px solid transparent;
z-index: -10;
}
​
Demo
Frankly, if you need to still support IE7, I would just use images or allow a little graceful degradation.

How important is CSS property order? [duplicate]

This question already has answers here:
Is there a specific order for CSS properties?
(6 answers)
Closed 2 years ago.
Does the order of properties in a CSS declaration have an effect on the rendering of the HTML?
This is really a trickier question than I first thought. My first reaction was:
Within a CSS rule (also called “rule set”), the order of declarations (of the form property: value) is immaterial. So foo { color: red; background: white; } is equivalent to foo { background: white; color: red; }. There is no separate statement about this in specifications (I suppose); it follows from the lack of any statement that would make the order significant.
On second thought, we could also have
* { font: 100% Calibri; font-weight: bold; }
What happens if you switch the order? In browsers, it does have an effect. I’m not sure how this should be interpreted in terms of CSS specification, but browsers apparently, and naturally, process the declarations sequentially. So the rule makes the text bold, but if you change the order,
* { font-weight: bold; font: 100% Calibri; }
then things change. The font shorthand sets font-weight to its initial value normal, so the text is not bold.
On third thought, there are many methods and tricks based on the order. So yes, the order of declarations is significant.
The impact of the order of rules is a completely different issue.
Apparently the order does not have any direct impact on the result.
The subject has been mentioned here: http://css-tricks.com/new-poll-how-order-css-properties/
It does have an impact according to here: http://css-tricks.com/ordering-css3-properties/
And here is another trend: http://perishablepress.com/obsessive-css-code-formatting-patterns-and-trends/
Final verdict: Arrange the way you judge best, it will work.
Mainly 5 Types Of CSS Property Orde:
Randomly
Grouped By
Alphabetical
By Line Length
CSS Properties by Importance
1. Randomly
.module {
border: 1px solid #ccc;
width: 25%;
padding: 20px;
position: relative;
min-height: 100px;
z-index: 1;
border-radius: 20px;
}
2. Grouped By
.module {
width: 25%;
min-height: 100px;
padding: 20px;
border: 1px solid #ccc;
border-radius: 20px;
position: relative;
z-index: 1;
}
3. Alphabetical
.module {
border-radius: 20px;
border: 1px solid #ccc;
min-height: 100px;
padding: 20px;
position: relative;
width: 25%;
z-index: 1;
}
4. By Line Length
.module {
border: 1px solid #ccc;
border-radius: 20px;
position: relative;
min-height: 100px;
padding: 20px;
z-index: 1;
width: 25%;
}
Here is Refrence URL: https://css-tricks.com/new-poll-how-order-css-properties/
5. Ordering CSS Properties by Importance
But Best Way To Ordering CSS Properties by Importance
Layout Properties (position, float, clear, display)
Box Model Properties (width, height, margin, padding)
Visual Properties (color, background, border, box-shadow)
Typography Properties (font-size, font-family, text-align,
text-transform)
Misc Properties (cursor, overflow, z-index)
Example:
.button {
display:inline-block;
margin:1em 0;
padding:1em 4em;
color:#fff;
background:#196e76;
border:0.25em solid #196e76;
box-shadow:inset 0.25em 0.25em 0.5em rgba(0,0,0,0.3),
0.5em 0.5em 0 #444;
font-size:3em;
font-family:Avenir, Helvetica, Arial, sans-serif;
text-align:center;
text-transform:uppercase;
text-decoration:none;
}
In normally no need or rule to order of properties in a CSS declaration. I wrote order like my requirements of design and browser response, but in firebug tool it arrange sin alphabet order.
The best method of order is
1. box model = width ->height->float
2. font related = font-size -> text-decoration, font-family
3. background images = width, height, border, image
Then only browser allocate space for each element faster in my experience.
Some persons order like below types:
Random
Alphabetical
Grouped by Type
By properties Length
There are some guidelines. But, at the end of the day it comes down to browser implementation. Try not to rely on a certain order always working, because it won't. Inside a declaration, a statement tends to override a previous statement.

Resources