Using predefined const strings - html

I'm using the following HTML code:
<div class="p-col-fixed" style="width:150px">First line:</div>
For column alignment, I have to use width:150px in many places on the same HTML page.
How can I use a #define? In CSS?

I think you are comparing the scripting language with one of the programming languages that use the #define directive.
In CSS, we have class selector for that (in case you need to use that property on multiple places in the HTML.Class selectors are defined by placing a (.) dot before the name of the class selector and are used by specifing them as a value to the class attribute.
E.g.
CSS
.cust-width
{
width:150px;
}
HTML
<div class="p-col-fixed cust-width">First line:</div>
Another feature is the "id" which is used to further refine the selection and add additional properties to the selected class.IDs are generally used in cases when the change is required in fewer classes/tags.IDs are defined using (#) before the name of ID selector in CSS
E.g.
CSS
#cust-width
{
width:150px;
}
HTML
<div class="p-col-fixed" id="cust-width">First line:</div>
For your use-case, classes are ideal.Provided that change is required in multiple parts of the HTML.

Html:
<div class="p-col-fixed define">First line:</div>
css:
.define
{
width:150px;
}

Why go in all that trouble? Simple solution is to create a CSS class and use it anywhere you need in your html file.
CSS
.width-150 {
width: 150px;
}
HTML
<div class="p-col-fixed width-150">First line: </div>

You can create a class in css and then add it in the div.
css file:
.width150 {
width:150px;
}
Then, in your html file add the class
<div class="p-col-fixed width150 " >First line:</div>

Use class if you want to add style to multiple elements.
Use id if you want to style only one element.
The best approach would be to add a class to the element first
Example : custom-width
HTML:
<div class="p-col-fixed custom-width">First line:</div>
CSS :
.custom-width
{
width:150px;
}

Related

Targetting a class with multiple classes on the parent

Here's the HTML,
<body class="home page page-id-13">
<div class="targetthis">
</div>
</body>
I want to target the class "targetthis" only when the body class has both "home" and "page" on it.
Try this
body.home.page .targetthis
by removing the spaces you are saying that you want an element with both classes, and by using the body, you are saying you are wanting the body element with both classes
If you only want targetthis as the direct child then you need to add the >
More info on multiple class / id selectors
Use both the classes in your target CSS selector like below.
body.home.page .targetthis
{
background-color:red;
}
DEMO
You could just write
firstClass.secondClass.thirdClass .childNodeClass
https://css-tricks.com/multiple-class-id-selectors/

Refering to 2 css classes from within a textarea

Is it possible/permissible to refer to 2 css classes from within a text area like below:
with section 1 and section2 being classes from a css file. Hope someone can advise. Thank you.
Yes you can, both in the style sheet and in your HTML, I've used div for demonstration purposes, but the same applies to textarea
.section1 {font-weight:bold;}
.section2 {font-style:italic;}
.section1.section2 {color:#f00;} /*Will only apply if both classes present*/
<div class="section1">I'm bold</div>
<div class="section2">I'm italic</div>
<div class="section1 section2">I'm bold, italic and red!</div>
In HTML seperate multiple classes in the attribute with a space. To join multiple classes in your CSS have no speration between them
What do you mean from within a text area? Do you mean something like the following...
<textarea class="section1 section2"></textarea>
Or do you mean CSS within the textarea itself? If so that's not possible because all you're typing into it is plain text. But you could target that text, for example...
textarea.section1 { }, textarea.section2 { } or textarea.section1, textarea.section2 { }
Yes! it is possible to refer any number of classes within any html tag.
Ex:<textarea class="section1 section2 section3"></textarea>
and please provide space to separate the different classes html does not recognise if you use semicolon to separate the classes.
You can write both the classes without comma like
<textarea class="section1 section2"></textarea>
Now provide CSS code in your CSS file seperately.

how to avoid the ID name in css file

is there any efficient way to scan the id & class name in css file? there are many css file in my web app and now i have to add some more css file. I often get stuck to define the ID & Class name which is already defined in another css file and it causes problem during testing.
I am really tired of keep changing the id & class name. can some one give me any tips to sort it out.
#Edit : suppose there are two css files in a web app old_1.css & old_2.css
old_1.css #id_1 {width:100%;height:100%; .... }
#id_2 { width:50%; height:50%; .... }
old_2.css #id_3 {width:70%;height:70%; .... }
#id_4 { width:30%; height:30%; .... }
Now i am creating a new css file new_1.css and by mistake i wrote the simmilar id of old css file. this is where i get stuck and i want to avoid to rewrite the same ID.
new_1.css #id_1 {width:80%;height:80%; .... } // this id is already declared in old css file
Rather than ensuring each CSS class name is unique, ensure that the CSS styles cannot clash by including parent elements in your CSS. This is better for structure:
HTML:
<div class="section1">
<div class="inner_div">
</div>
</div>
<div class="section2">
<div class="inner_div">
</div>
</div>
CSS:
.section1 {
}
.section1 .inner_div {
color:red;
}
.section2 {
}
.section2 .inner_div {
color:blue;
}
This will ensure that only div's with inner_div class will be given the style color:red where they are contained in a div with the class section1. Likewise, only div's with inner_div will be styled color:blue where they are contained in a div with the class section2.
Using this format should prevent you from ever having duplicate class names as you can define as far as you like, for example if I was applying a style to a span tag displaying the date for a news article I'd use:
.main_container .news .article .details span.date {}
This is a lot easier to read, and a lot less likely to be duplicated than:
.news_article_date {}
Otherwise, like #Ant has stated, use a good HTML editor software and do a Find on the classes and IDs used.
If refactoring the old CSS files are beyond the current scope of your work, you can simply override the old styles by making your selectors more specific. A good tutorial on CSS specificity is given here

What is id= name= .something #something?

In HTML are the attributes like
<input class="new" type="text" name="title" id="title2" />
and in CSS do I see
.something { ... }
#something { ... }
What is id= name= .something #something used for?
ID: unique identifier for the DOM element
Name: name to be used when submitting a form which is used as the data retrieval key
#something: reference to element with ID 'something'
.something: reference to element(s) with classname 'something'
These are some really basic concepts of HTML and CSS. You will probably want to read a basic HTML tutorial to find out more on the subject, especially the attributes section.
Id's and classnames are primary used for styling elements with CSS and adding behaviour to them with JavaScript. For example:
HTML:
<button id="foo">Click me to unleash the Unicorn</button>
CSS:
#foo {
border: 1px solid #ff0000;
font-weight: bold;
background: #000;
color: #fff;
}
JavaScript:
document.getElementById('foo').onclick = function() {
var img = document.createElement('img');
img.src = 'http://display.ubercomments.com/6/23672.jpg';
document.getElementsByTagName('body')[0].appendChild(img);
};
See also, this beautiful example (Unicorn included).
An id attribute is a unique identifier for the element within the DOM. It's unique in the sense that you cannot have more than one element with this ID contained within the document.
Styling an element based on ID is done using #something.
A name attribute is simply a non-unique name for this element. This is most commonly used in forms as the name that gets POST'd or GET'd through to the server side language.
.something is the style selector for the class= attribute on any element.
For instance, you could style the following element: <div class="testclass" name="testname" id="testid"></div> in any of the following 3 ways:
.testclass {
background-color: black;
}
#testid {
background-color: black;
}
div[name="testname"] {
background-color: black;
}
Remember, both a class and a name are NOT unique, so they can be used to style and define multiple elements.
The .something is a class, and the #something is an id.
the Name= attribute is commonly used in forms, and usually not used in CSS.
In other words, the following code:
<body class="thisisaclass">
<div id='thisisanid'></div>
<div class='thisisanotherclass'></div>
</body>
Would result in a CSS that looks like this:
.thisisaclass {..Code..}
.thisisaclass #thisisanid {..Code..}
.thisisanotherclass {...code...}
Classes are used for repeating stuff, for example if you want to use the same type of text formatting in several areas of your page - whereas ids only should appear once in the html code.
Check out HTMLDog to learn more, it's a great start :)
id="something" means ID. You can have it only once. It's CSS reference is #something. Also, by using #something at end of address, you can directly move browsers to that ID, without using JS.
name= is used while sending form. Using PHP, you can check that value by using $_REQUEST['title']. In other programming languages, there also are methods to get that value.
.something is class in CSS. It's used to style HTML elements with class="something"
class is multiple selector for example if you want many tables have same colors, back ground colors and font etc. You will define class.
In these tables if a specific table is to style in different way you will use id. Id can not be duplicated. And you can assign a same class to as many objects you want.
<style type="text/css">
.MyTable{
background-color:#ff00ff;
}
#centralTable{
background:color:red;
}
</style>
<div class="MyTable">Data </div>
<div class="MyTable"> </div>
<div class="MyTable" id ="centralTable"> Data</div>
<div class="MyTable"> Data</div>
<div class="MyTable">Data </div>
Remember classes are followed by period (.) and Ids (#) in Cascading Style Sheets.

How can I change my font color with html?

I'm making a web page where I want the color of the font to be red in a paragraph but I'm not sure how to do this.
I was using FrontPage for building web pages before so this HTML stuff is really new to me. What is the best way to do this?
<p style="color:red">Foo</p>
Or preferrably:
<p class="error">Foo</p>
Where "error" is defined in your stylesheet:
.error {
color: red;
}
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="hilight" might be more appropriate. ( Thanks to Grant Wagner for pointing that out )
Brief CSS Explanation :
Since most of the answers you're getting are all mentioning CSS, I'll add a small guide on how it works:
Where to put CSS
First of all, you need to know that CSS should be added inside the tags of your document. The tags used to define where the CSS is going to be are:
<style type="text/css"> <!-- Your CSS here --> </style>
This is called embedded CSS since it's inside the document. However, a better practice is to link "include it" directly from an external document by using the following tags:
<link href="file.css" media="all" rel="stylesheet" type="text/css"/>
Where file.css is the external file you want to include into the document.
The benefits of using the "link" tag is that you don't have to edit in-line CSS. So lets say if you have 10 HTML documents and you want to change the color of a font you just need to do it on the external CSS file.
This two ways of including CSS are the most recommended ways. However, there's one more way that's by doing in-line CSS adjustments, for example:
<[tag] style="<!-- CSS HERE -->"> Content </[tag]>
CSS General Structure
When you code write CSS, the first thing you need to know is what are classes and what are id's. Since I already mentioned what they do above I'm going to explain how to use them.
When you write CSS you first need to tell which elements you're going to "select", for example:
Lets say we have a "div" element with the class "basic" and we want it to have a black background color, a white font, and a gray border.
To do this we first need to "select" the element:
.[identifier] { }
Since we're using a class we use a "." in front of the identifier which in this case is: "basic", so it will look like this:
.basic { }
This is not the only way, because we're telling that ANY element that has the class "basic" will be selected, so lets say we JUST want the "div" elements. To do this we use:
[html-tag].[identifier] { }
So for our example it will look like this:
div.basic { }
Now we've selected the "div" with the class "body". Now we need to apply the visual style we wish. We do this inside the brackets :
div.basic {
background-color:black;
color:white;
border:1px solid gray;
}
With this, we just applied successfully a visual style to all "div" elements that have the "basic" class attached.
Remember this doesn't just apply for "class" it also applies for "id" but with a slight change, here an example of the final code but instead of a class we'll just say it's a "id"
#unique-basic {
background-color:black;
color:white;
border:1px solid gray;
}
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.
<style type="text/css">
.myCSS
{
color:red
}
</style>
<div class="myCSS">text</div>
<span class="myCSS">text</span>
<p class="myCSS">text</p>
<!-- table elements..... -->
<td class="myCSS">text</td>
<tr class="myCSS">text</tr>
<p style="color:red">Your Text here</p>
But as others have by now said in more and better words: Even better than the above would be to use classes or IDs and assign the CSS-attributes to that instead of using the inline style.