I new to CSS/CSS3 and I read in many places different way to build CSS files. Some people all tags in the same elements and some people divide elements and then use different classes in the HTML code.
eg:
// css
h1 { font: normal 20px Arial; color: black; margin: 1em 0; padding:0; border-bottom: solid 0.1em #ddd; }
h2 { font: normal 16px Arial; color: black; margin: 1em 0; padding:0; border-bottom: solid 0.1em #ddd; }
so in the HTML they just have to put and that's it. If you need to change the border color then you have to change ALL tags that has the border-bottom.
OR
h1 { font: normal 20px Arial; }
h2 { font: normal 16px Arial; }
.colorBlack { color: black; }
.headers { margin: 1em 0; padding:0; }
.borderBottom { border-bottom: solid 0.1em #ddd; }
and in the HTML you use:
<h1 class="black headers borderBottom">h1</h1>
Very easy but everytime you have to put all the CSS you need
Is there any Best Practices on how to build CSS? Which way is better for performance or loading time?
I recommend to use:
h1, h2 {
color: black;
margin: 1em 0;
padding: 0;
border-bottom: solid 0.1em #ddd;
}
h1 {
font: normal 20px Arial;
}
h2 {
font: normal 16px Arial;
}
The best practice: Keep your code readable. Readability is not achieved by separating a style definition in several useless classes.
Usually, you want the h1 and h2 tags to have similar styles. For that reason, I've grouped the styles. When you want another property value for a certain element, you can add another CSS definition. When the selector has a higher specificity, the new declaration will override the previous one(s).
I think both technics are useful. Personally I often put two or more classes when I need to separate one element from another (example last in row element should not contain margin-right) my code looks like:
HTML:
<div class="images-row">
<div class="image-item">...</div>
<div class="image-item">...</div>
<div class="image-item">...</div>
<div class="image-item last-in-row">...</div>
</div>
CSS:
.image-item {
border:1px solid red;
margin-right:20px;
width:200px;
height:200px;
}
.image-item.last-in-row {
margin-right:0;
}
(supported well in IE > 7 and other good browsers). This solution keeps the code quite clean and also makes me write less (I don't need to rewrite all the styles for the last element or add separate selectors). jQuery element handling is less confiusing (I need only one selector to match all .image-items).
Related
I'm making a blog and I am using a free template to manage my fron-end part, but now I want to add one class called code_block. Every time I write an article and I want to add a
<p class="code_block"> some code</p>
and the code piece to be displayed in a similiar to how this last code is displayed here in stackoverflow.
I went to the END CSS file entered:
.code_block{
color: #933 !important;
border: 5px solid red;
}
didn't work, tried adding the css directly in the html, didn't work, tried adding manualy the css while in chromium web tool, didn't work what is happening ?!
source: https://github.com/martin-varbanov96/summer-2016/tree/master/Pitonia/Django/mysql_blog/blog
EDIT:
made it more specific:
.ar
ticle ul li p .code_block{
color: #933 !important;
border: 5px solid red;
}
Still not working I think priority is not the problem here.
ID has got more priority. Remove the color from here. Or you can override.
#body.home .body div p {
color: #ffffff;
display: block;
font-family: Arial;
font-size: 18px;
font-weight: normal;
line-height: 24px;
margin: 0 auto;
padding: 0;
text-align: center;
width: 780px;
}
You can override it like this...
#body.home .body div p.code_block{
color: #933;
border: 5px solid red;
}
This is more of a debate than a question but I feel that there isn't a lot on the internet that covers this topic.
For example foundation comes with hundreds of !important tags for things that in my eyes do not need them:
.text-center { text-align: center !important; }
There is loads of css that is simular to this which in my point of view is bad practise and the question I'd like to answer is why do css frameworks use them at all? Bootstrap and Foundation are two main css frameworks that both use them.
I've always been told that using important tags in css is very bad practise and should only be used for IE.
If you write your own CSS you have the freedom to add more specific rules whenever needed:
.center { text-align: center; }
.foobar { text-align: left; }
.foobar.center { text-align: center; }
However, the CSS framework cannot predict how you will arrange your HTML. So it is easier to do !important instead of generating thousands of combinations of more specific rules. Example:
.center { text-align: center; }
.foobar { text-align: left; }
.barbaz { text-align: right; }
/*
* assuming .center must be centered regardless of other rules and
* !important must be avoided at the same time, we need to do this
*/
.foobar.center { text-align: center; }
.barbaz.center { text-align: center; }
.foobar.barbaz.center { text-align: center; }
Is because you can have in your code st. like this:
<style>
#aside p {text-align: right;}
.text-center {text-align: center} /* without important text will be aligned to right */
</style>
<div id="aside">
<p>right-aligned text</p>
<p class="text-center">centered text</p>
</div>
http://jsfiddle.net/v1v4jaas/
In this case without inportant the text will be aligned to right. With important, the second paragraph will be centered.
Class has only a low priority against id, etc.
Using !important in your CSS usually means, the classes you have written do
not have a proper hierarchy.
The !important rule overrides a particular property. But should be used only when one is helpless and it has to be overridden.
Best practice would be to use !important only in utility classes alone.
eg. Say you have a button which u want to look similar throughout your application
.btn {
margin-bottom: 0;
text-align: center;
vertical-align: middle;
touch-action: manipulation;
cursor: pointer;
border: 1px solid transparent;
padding: 6px 12px;
font-size: 11px;
border-radius: 4px;
}
The above definition for .btn holds true unless it is not wrapped by any other class which could override the .btn formatting which we expect to be same throughout the application. But once it is wrapped by other class like below, the final style would be totally different from your expectations.
<p class="format-paragraph">
<button type="submit" name="save" class="btn"> Submit</button>
</p>
Now, to make your .btn class super strong and to be treated with respect by the ones wrapping it, change the .btn definition to:
.btn {
margin-bottom: 0 !important;
text-align: center !important;
vertical-align: middle !important;
touch-action: manipulation !important;
cursor: pointer !important;
border: 1px solid transparent !important;
padding: 6px 12px !important;
font-size: 11px !important;
border-radius: 4px !important;
}
This definition would be sufficient to make your button look similar throughout the application.
The .text-center { text-align: center !important; } class mentioned in question is nothing but a utility here.
Know more on !important precedence here.
SASS inserts unexpected CSS on production server and messes up my a:hover
This is fragment of my application.css:
pre {
background-color: #eee;
padding: 10px;
font-size: 11px; }
a {
color: #000000;
}
a:visited {
color: #666666;
}
a:hover {
border-bottom: none;
}
div {
&.field, &.actions {
margin-bottom: 10px; } }
however on production server firefox reports following css:
pre {
background-color: #EEEEEE;
font-size: 11px;
padding: 10px;
}
a {
color: #000000;
}
a:visited {
color: #666666;
}
a:hover {
border-bottom: 1px solid #777777;
}
div.field, div.actions {
margin-bottom: 10px;
}
on my development machine firefox shows following CSS:
a:hover {
border-bottom: medium none;
}
I use Rails 3.2.13 and I have never seen problem like this. I have wasted whole afternoon trying to find a solution. This problem breaks my home page and makes it look very unprofessional.
First, border-bottom is a shorthand property. It combines border-bottom-width, border-bottom-color and border-bottom-style. The values of those properties can appear in any order. Any of them can be omitted.
none is a value from border-bottom-style.
Second, what Firefox shows in its inspection feature, is not exaclty your CSS. It shows what it treats the CSS like. border-bottom: medium none; means that you changed the style of border to none, while the width remains as medium (apparently, it was inherited).
To see the actual CSS, open the actual CSS file and look inside. It will also let you view media query wrappers.
Third, to remove bottom border, use border-bottom: 0;. That will be treated as border-bottom-width: 0;, which effectively removes the border.
I am currently trying to make a custom underline with border-bottom. However, currently the underline is going all the way of my block-element (whole page).
I’d prefer to have it being only 50px longer than my headline (however the text is flexible and I do not know the length).
Can I do this without adding another <span> tag within the <h2> somehow? I do not wannt to add a <span> element to each <h2> just to change my design.
Current HTML is:
<h1>My title</h1>
CSS:
h1 {
font-size: 18px;
color: #b62525;
border-bottom: 2px solid #c68181;
}
Is it possible to adjust the border-bottom length to my text length? (e.g. behave like inline element for border, but like block for newlines, padding and margin)
Using display: inline-block works, the only caveat being that the content after the <h1> tag must be the full width of the container element. The other solutions here also assume this. You can also use display: inline (supported by older browsers), but inline-block allows for setting of explicit widths, should you need it.
Here's a JSFiddle
CSS
h1
{
display: inline-block;
padding-right: 50px;
border-bottom: 1px dotted #888;
}
Inline or floating methods can be problematic if you're unable to compensate for them in other rules. One alternative is to use display:table
h1
{
display:table;
border-bottom:1px solid black;
padding-right:50px;
}
You can use
h1 {
font-size: 18px;
color: #b62525;
border-bottom: 2px solid #c68181;
float: left;
padding-right: 50px
}
Simply add one more property in css like this :
h1 {
display:inline;
font-size: 18px;
color: #b62525;
border-bottom: 2px solid #c68181;
}
I have a simple border style say:
.border
{
/*content*/
}
I want several other classes to inherit this border style. Can this be done in CSS only?
Or do I need to specify it in HTML also?
I want:
.actionArea : .border
{
/*content */
}
Or can this only be done in HTML like:
<div class="actionArea border"/>
It would be very annoying if the latter is only possible.
Update
This works good enough, but still is a bit ugly:
.actionArea, .otherArea, .anotherArea
{
/*border specification */
}
.actionArea
{
/*area specification/*
}
.otherArea
{
/*area specification/*
}
(..)
You will need to use a CSS framework such as LESS for such a thing.
You may use sass . Probably it is the nesting feature you want to use http://sass-lang.com/#nesting
table.hl {
margin: 2em 0;
td.ln {
text-align: right;
}
}
li {
font: {
family: serif;
weight: bold;
size: 1.2em;
}
}
Or as Oded said you can use LESS . LESS is having some interesting feature one of them is mixins . This is not exactly inheritance but it gives you has-a relationship in css
Example copied from LESS
.bordered {
border-top: dotted 1px black;
border-bottom: solid 2px black;
}
#menu a {
color: #111;
.bordered;
}
.post a {
color: red;
.bordered;
}