Changing font and color of a text - html

Instead of creating a whole other id and ruleset, why can’t I just put multiple values (ex. font-family: cursive; color: blue) in a single ruleset? I tried it and it works and seems like a quicker way to do it. For example, if I want to change the font, color, and uppercase/lowercase of a title, can't I just put all those values into one ruleset?

The preferred way to do this is using Cascading Style Sheet (CSS). This allows you to edit the visual aspects of the site without having to deal much with the HTML code itself.
Explanation :
<[tag] style="[css]"> Content </[tag]>
Where [tag] can be anything. For example "p" (paragraph), "span", "div", "ul", "li".. etc.
and where [css] is any valid CSS. For example "color:red; font-size:15px; font-weight:bold"
The recommended way to add style to a html element is by assigning it a "class" (a identifier that can be repeated on the document) or a "id" a unique identifier that shall not be repeated in the document.
For example:
<[tag] id="element1" class="red"> Content </[tag]>
<[tag] id="element2" class="red"> Content </[tag]>
Where tag is any html valid tag. id is a unique arbitrary name and class is an arbitrary name that can be repeated.
Then in the CSS (inside the tags of your document):
<style type="text/css">
.red {
color:red;
}
#element1 {
background-color:black;
}
</style>
For this example and to keep it simple to new users I named the class "red". However class="red" isn't the best example of how to name . Better to name CSS classes after their semantic meaning, rather than the style(s) they implement. So class="error" or class="highlight" might be more appropriate. ( Thanks to Grant Wagner for pointing that out )
For a complete guide to CSS you can visit this link: http://www.w3schools.com/css/
Remember:
Keep your HTML Code clean and use CSS to modify ANY visual style that's needed. CSS is really powerful and it'll save you a lot of time.

Yes you can and it’s okay to do that.
Actually this is the right way!
so you create a ruleset with a specific selector, then you write all the properties that you wish and element to have (if the selector applies to the element)

Related

How can I simplify code in the body by working through the head?

New to this, bare with me please.
I've started having fun with HTML code doing offline documents. I just found out that I could easily change my font, in the head, by adding this:
<style type=text/css>
mkf { font-family:'Courier'; color:red; }
</style>
Then, as I go to add code to , every time I want to change the font of a select group of word with the addition of the color red, I just need to type
<mkf>words here</mkf>
Wonderful! It saves me so much time. But then I got to wonder, what if I wanted to add a link to a word. For example, instead of typing all of this out:
<mkf>Example1</mkf>
I would simply be able to parse whatever text I inputted between, let's say,
<linkandfont>Example1</linkandfont>,
which would basically create a link to the file "to_do_list.pdf".
I've tried to find a name or term for this so that I can study and learn more, but I have not found it yet.
Thank you.
Why are you not using classes instead? These achieve the same thing. For example;
<style>
.mkf {
font-family:'Courier';
color:red;
}
</style>
<div class="mkf">test</div>
However, to properly answer your question, what you want is ABSOLUTELY possible in HTML5 and CSS3. And I've used such methods in certain projects of mine (just know this isn't entirely Kocher or conventional).
mkf {
font-family:'Courier';
color:red;
}
<mkf>this is working</mkf>
As for making <linkToSomething>Click Here</linkToSomething> not as easy. You would definitely need JavaScript etc to handle all that. You won't achieve it in CSS and HTML alone.
You cannot. Only way to create a link in HTML is by typing description. You could also shorten by using JavaScript but that's not HTML.
The way you changed the color in head is part of styling or CSS, so you could give a class to a tag like <a class='redlink' href='.. and define that class in head like you did with mkf : .redlink {color:red} or if you want all your links to be red then you could give color to a in head style: a {color:red;}
By typing <mkf> in body I guess you created custom tag which is not part of standard HTML tags, more proper way would be using class to a standard tag like div or p.

How to select an specific element by a css class, but not other elements?

So I want to select the <div> element with the class .thisClass, but not any other elements with class of .thisClass:
<div class="thisClass"></div>
<p class="thisClass"></p>
CSS Selector by class name and tag: div.thisClass { ... }:
div.thisClass {
background-color: red;
}
<div class="thisClass">thisClass (div)</div>
<p class="thisClass">thisClass (p)</p>
But this is a bad way to write selectors:
Don’t qualify class rules with tag names
The previous concept also applies here. Though classes can be used many times on the same page, they are still more unique than a tag.
One convention you can use is to include the tag name in the class name. However, this may cost some flexibility; if design changes are made to the tag, the class names must be changed as well. (It’s best to choose strictly semantic names, as such flexibility is one of the aims of separate stylesheets.)
BAD
treecell.indented {…}
GOOD
.treecell-indented {…}
BEST
.hierarchy-deep {…}
Using JavaScript
document.querySelector('div.thisClass')
Using jQuery
$("div.thisClass")
Using CSS:
<style>
div.thisClass{}
</style>
The following code illustrates how to select the first class from the list of classes in both CSS and Javascript.
CSS
.thisClass:first-child {
/*css property*/
}
JAVASCRIPT:
var divElement = document.getElementsByClassName('thisClass')[0];

What is the difference between id and class in html/css [duplicate]

This question already has answers here:
Difference between ID and Class
(8 answers)
Closed 8 years ago.
I want to know what is the exact difference between id and class in html and css.
I read somewhere that ids are unique and there can be only one element with corresponding id, and classes can be applied to many elements.
But I think this is wrong I have a code in which I have applied a single id to two elements.
<link type="text/css" href="main.css" rel="stylesheet">
</head>
<body id="header">
<p id="header2">Poster</p>
<p id="header2">Hello World</p>
</body>
</html>
and here is my css code
p#header2{
background-color:white;
width:50%;
height:50%;
margin-left:25%;
margin-right:25%;
text-align:center;
font-size:50px;
}
Could anyone please explain me???
Here is the Explanation in detail.
Use a class when you want to consistently style multiple elements throughout the page/site. Use the ID when you have a single element on the page that will take the style. class is a type of item and id is the unique name of an item.
EDIT:
CSS doesn't care
Regarding CSS, there is nothing you can do with an ID that you can't do with a Class and vice versa. I remember when I was first learning CSS and I was having a problem, sometimes I would try and troubleshoot by switching around these values. Nope. CSS doesn't care.
Javascript cares
JavaScript people are already probably more in tune with the differences between classes and ID's. JavaScript depends on there being only one page element with any particular, or else the commonly used getElementById function wouldn't be dependable. For those familiar with jQuery, you know how easy it is to add and remove classes to page elements. It is a native and built in function of jQuery. Notice how no such function exists for ID's. It is not the responsibility of JavaScript to manipulate these values, it would cause more problems than it would be worth.
EDIT2:
The Documentation clearly shows that HTML required the ID attribute to be unique in a page:
This attribute assigns a name to an element. This name must be unique in a document. If you have several elements with the same ID, your HTML is not valid.
So in JavaScript, getElementById() should only ever return one element. You can't make it return multiple elements.
Well, you can have more than one element with the same ID, but you shouldn't - because the consequences of doing so is unpredictable, due to differences between browsers.
ID: Id name must have 1.
<link type="text/css" href="main.css" rel="stylesheet">
</head>
<body id="header"> // ID
<p id="header2">Poster</p>
<p id="header2">Hello World</p>
</body>
</html>
Class: have multi tag using the same class name. And you can apply style for all class have name is "name".
<p class = "name">abcdef</p>
<image class = "name">......</image>
It's right:
The id of an element should be unique in the entire dom.
The class can be applied to multiple elements.
In css if you use this syntax
.yourClass {
color: white;
}
every element with class="yourClass" have the style color: white.
The id HAVE to be unique for different reason. The thing first jump into my mind its the use of jquery where you identify dom elements with the ID
The id attribute is meant to be unique and should contain a single identifier. The class attribute may contain a space-separated list of class identifiers allowing for cumulative application of css rules.
This is notwithstanding to css engines that do not complain on double ids.
Simple - id is the one that can be used only once and it will be unique. Scripts mostly use id's but it doesn't mean that id will only be used with js and not css. You can used a id as selector to in css.
While classes when two or more divs have the same styling so use class instead of increasing lines of codes.

Which to use <div class="name"> or <div id="name">? [duplicate]

This question already has answers here:
What's the difference between an id and a class?
(17 answers)
Closed 9 years ago.
I am learning HTML. Can someone please tell me what is the difference between class and id and when to use one over the other? They seem to do the same thing
<!DOCTYPE HTML>
<html>
<head>
<style>
#mycolor1 {color: red;}
.mycolor2 {color: red;}
</style>
</head>
<body>
<div id="mycolor1"> hello world </div>
<div class="mycolor2"> hello world </div>
</body>
</html>
They do not do the same thing.id is used to target a specific element, classname can be used to target multiple elements.
Example:
<div id="mycolor1" class="mycolor2"> hello world </div>
<div class="mycolor2"> hello world2 </div>
<div class="mycolor2"> hello world3 </div>
Now, you can refer all the divs with classname mycolor2 at once using
.mycolor2{ color: red } //for example - in css
This would set all nodes with class mycolor2 to red.
However, if you want to set specifically mycolor1 to blue , you can target it specifically like this:
#mycolor1{ color: blue; }
Read the spec for the attributes and for CSS.
id must be unique. class does not have to be
id has higher (highest!) specificity in CSS
Elements can have multiple non-ordinal classes (separated by spaces), but only one id
It is faster to select an element by it's ID when querying the DOM
id can be used as an anchor target (using the fragment of the request) for any element. name only works with anchors (<a>)
Classes should be used when you have multiple similar elements.
Ex: Many div's displaying song lyrics, you could assign them a class of lyrics since they are all similar.
ID's must be unique! They are used to target specific elements
Ex: An input for a users email could have the ID txtEmail -- No other element should have this ID.
The object itself will not change. The main difference between these 2 keyword is the use:
The ID is usually single in the page
The class can have one or many occurences
In the CSS or Javascript files:
The ID will be accessed by the character #
The class will be accessed by the character .
To put it simnply: id is unique to just one element in the whole HTML document, but class can be added to numerous elements.
Also, ID properties have priority over class properties.
ids and classes are especially useful if you plan on using javascript or any of its frameworks.
class is used when u want to set properties for a group of elements, but id can be set for only one element.
ID's must be unique (only be given to one element in the DOM at a time), whereas classes don't have to be. You've already discovered the CSS . class and # ID prefixes, so that's pretty much it.
ID provides a unique indentifier for the element, in case you want to manipulate it in JavaScript. The class attribute can be used to treat a group of HTML elements the same, particularly in regards to fonts, colors and other style properties...
ID is suitable for the elements which appears only once
Like
Logo sidebar container
And Class is suitable for the elements which has same UI but they can be appear more than once.
Like
.feed in the #feeds Container
the Id selector is used when referring to a single unique element.
The Class selector referrers a group of elements.
For example, if you have a multiple buttons that look the same, you should use class="mybutton" to have consistent styling.
In terms of performance:
CSS selectors are matched from right to left.
Therefore, .myClass should be "faster" than #myID because it misses out testing.
The performance speed is negligible for normally sized pages and you will probably never notice a difference so it's mostly just about convention.
Here is more info on why css is browsers match css selectors from right to left

What do square brackets in class names mean?

I saw here square brackets that are used in class names:
<input class="validate[required,custom[onlyLetter],length[0,100]]" name="firstname" type="text" />
What do these square brackets mean?
The square brackets are used as an attribute selector, to select all elements that have a certain attribute value. In other words, they detect attribute presence.
Example 1:
img[alt="picName"] {width:100px;}
would affect only
<img src="picName.png" alt="picName" />
in your code, and won't affect
<img src="picName.png" alt="picName2" />
Example 2:
The following affects all elements with title attribute specified:
[title] {border:1px dotted #333;}
Example 3:
This CSS
p[class~="fancy"]
will affect the following html
<p class="fancy">Hello</p>
<p class="very fancy">Hola</p>
<p class="fancy maybe">Aloha</p>
but won't affect this:
<p class="fancy-fancy">Privet</p>
Example 4:
[lang|="en"]
will affect elements with lang attribute, which is hyphen-separated list of words beginning with “en”, like
<div lang="en">Tere</div>
<div lang="en-gb">GutenTag</div>
Examples 5, 6, 7:(CSS3)
The following attribute selector affects link elements whose href attribute value starts with the string “http:”.
a[href^="http:"]
The following attribute selector affects image elements whose src attribute values ends with the string “.png”.
img[src$=".png"]
The following attribute selector affects any input element whose name attribute value contains the string “choice”.
input[name*="choice"]
That is most likely used by some sort of validator or validation library. The class here means that validate this field denoted by validate keyword and then:
required it is required field
custom validation type; allow only letters
length should be between 0 to 100 chars
Well, this information is used by the jQuery validation library you posted the link to :)
Apart from the use-case / example given by the OP for brackets in class names, there is also another use case which Harry Roberts proposed (and later stopped proposing) in his blog a while back: grouping related classes in your markup where the square brackets could be used to group
two or more related class attributes to make them easier to notice
when scanning an HTML file
...
and that looks something like this:
<div class="[ foo foo--bar ] baz">
where:
There must be more than one ‘set’ of classes.
One ‘set’ must contain more than one class.
He also noted that adding the brackets is completely valid according to the html5 spec
There are no […] restrictions on the tokens authors can use in the
class attribute…
Just to reiterate:
The brackets in the class attributes - while being valid CSS class names are not actually meant to be used in the CSS1 - but rather to help readability in the HTML.
Notes:
1
Although technically, they can be used when escaped,
.\[ {
color: red;
}
<div class="[">Hi there</div>
Nothing. Brackets are a legal character for class names with no special meaning whatsoever.
In standard HTML, they have no particular meaning. It's just more text.
To the jQuery Validation plugin, they do.
Example:
[what-ever] {
color: red;
}
<p what-ever>Hello</p>
This will color Hello red. You can use square-bracket as class names
There is no particular rule within a class name. In your example they are almost certainly being used by a JavaScript validation framework. This is because in HTML you can't simply add your own attributes to tags, therefore the validation framework is exploiting the fact that CSS class names can contain such characters in order to 'store' the validation rules within the class name. There won't actually be any corresponding class in the style-sheet - this is just a trick to work around the limitations of HTML.