adding a css class to a html wrapping element - html

I am creating a custom CSS class and i am not sure how to write the code.
The element uses a h2 font size for its heading and a standard paragraph font size for everything else.
I would like to code a custom size for the h2 headings.
my custom class name is hosting-post-slider and my code starts as follows...
.hosting-post-slider {
}
I am not sure how to refer to the h2 heading in my CSS in the above so i can customize h2 just for this element.

if you have some styling in a class such as
.hosting-post-slider {
color: red;
}
Then to make a single h2 tag have this style, simply do this in you HTML:
<h2 class="hosting-post-slider">The heading</h2>
If you want all h2's to have this style, either add that class to all h2 elements, OR change your css to target all the h2s, such as:
.h2 {
color: red;
}

The code below will reference only this instance of the h2 element. Any style you add to it will only be applied to this element.
The complete code will look like this:
.hosting-post-slider h2 {
font-size: 30px;
}
<div className="hosting-post-slider">
<h2>My header</h2>
<p>My paragraph</p>
</div>

Is this what you are looking for, this selects all h2 elements with your custom class
h2.hosting-post-slider{
}

Related

Is it possible to set all text in a div to the h1 style in without an h1 tag?

I have a div which contains the page's title. Is it possible to set all text to the h1 style without using tags?
For example:
<div id="title-div" style="h1">My Title</div>
Or maybe something like:
#title-div {
style: h1; //Imports all styles from h1
}
Is this kind of thing possible?
No.
Extensions to CSS such as SASS tend to have features like #mixin which can define a set of rules once and then apply them in different places but:
CSS itself has nothing like that
The styles have to be explicitly defined as a mixin
Find the css for your h1 tag and just add your div id (assuming you have control over that css and its not coming in from a 3rd party, in which case you will likely just have to copy it)
h1, #title-div {
// styles
}
You can use class or id to style your page's title.
CSS example for styling with "id":
#title-div {
//add your styling here
}
The other option is a "class", so in the "div" open tag, you should have "class" HML example:
<div class="title">
<h1>My Title</h1>
</div>
CSS example for class styling:
.title {
//add your styling here
}
Also, I think you should add in the div container h1 to understand and clear it.
You can use the default style of h1
-webkit-text-size-adjust: 100%;
line-height: 1.5;
color: #000!important;
box-sizing: inherit;
font-family: "Segoe UI",Arial,sans-serif;
font-weight: 400;
margin: 10px 0;
font-size: 42px;

Color of link within H1 tag doesn't change why?

I'm trying to change the color of a link within the h1 tag. Html code as follows:
<h1 class="redheadline">
Link text color here..
</h1>
The css code I'm trying to use is:
h1.redheadline {font-size: 1.75rem; color:red;}
The font size changes but the color of the link's text doesn't change. Where in the css code I have to add color? Thanks!
h1.headline and <h1 class="redheadline"> they are not the same class name.
Since its <a> element has a default color, it does not accept a color from its parent.
Chrome defaults:
To do this is to define the correct classes to override the default attributes of the element.
Returning to your question, we should define as h1.redheadline a { ... }.
You can run the code snippet.
h1.redheadline {
font-size: 1.75rem;
}
h1.redheadline a {
color: red;
}
<h1 class="redheadline">
Link text color here..
</h1>
For your second question:
h1.redheadline a {
font-size: 3rem;
color: red;
}
<h1 class="redheadline">
Link text color here.. Pure Heading Text Here...
</h1>
The class name that you have given to h1 tag in html code is redheadline, but you are trying to apply the style on h1.headline. Hence it is not applying the style correctly.
You need to use the correct classname.
h1.redheadline{font-size: 1.75rem; color:red;}
Your css classname does not match 'redheadline'.

:not() pseudo selector works incorrect with some tags

HTML:
<p>I`m p</p>
<a>I`m a</a>
<h2>I`m h2</h2>
CSS:
:not(p){
color:red;}
:not() pseudo class should select all the elements inside the HTML document, that aren`t "p", and give them red color, but when i run the code "p" is red too, just like all other elements.
here you need to specify color for all html elements. as there is no color set to elements, the color from your selector is getting set to all elements available.
Here is what you need to add in your style:
*{
color: black;/* the color you will want for all or p elements. */
}
Notorious Zet you can fix this error by giving the p element a class and then using the not pseudo selector
The HTML
<p class = "notRed"> This is a p element </p>
<h1>This is a h1 element</h1>
The CSS
p:not(notRed){
color: red /*This will apply to all p elements with the class of notRed*/
}
Need to specify the color of <p> tag first. See the example from w3schools.
p {
color: black;
}
:not(p) {
color: red;
}
<p> I am p </p>
I am a
<h1> I am h1 </h1>

Style (h and p) tags inside div. Only access inside <body>

I need to style h1, h2, h3... and p tags inside a div but I only have access to the content area.
If it where possible, this would be what I'd use:
<div style="h1{padding:0;}p{font-size:1.4em;color:#000}">
Is there a solution to do this ? Apart from adding the style to every element.
Thanks
Although HTML syntax restricts style elements to the head part, this requirement is not enforced in practice. It works inside body, too. You just need to take into account that the effects are global to the document. Thus, to limit the effect to elements inside a certain element, you need to use suitable selectors. Example (I have added a color setting because the effect of just padding: 0 as in the question in not noticeable: it equals the default):
<h1>Heading outside the div</h1>
<p>A paragraph outside the div.</p>
<div class=mydiv>
<style>
.mydiv h1 { padding: 0; color: green; }
.mydiv p { font-size: 1.4em; color: #000; }
</style>
<h1>Heading inside the div</h1>
<p>A paragraph inside the div.</p>
</div>
There isn't a good solution.
Style elements may only appear in the head.
Inline style only applies to the element the attribute appears on.
The closest you can come is to use JavaScript to dynamically modify the stylesheet.
You would be better fixing whatever problem is preventing you from modifying the head section.
To avoid unwanted changes inside divs i will be using to divs with 2 unique id's:red and green
If you want different style for specific divs:
<div id="red"><h1>red</h1><p>red</p>
<div id="green"><h1>green</h1><p>green</p>
body #red > h1,body #red >p{
color:red;
}
body #green > h1,body #green > p{
color:green;
}

How to apply css for a class within h2?

I have an HTML code as follows
<h2><a class="toc-backref" href="#id8">Debug</a></h2>
How can I apply css format to Debug which is inside <a> (which is inside <h2>) with a class of "toc-backref"?
You could put a span inside of the h2, but that's not necessary. If you want to style the <a> inside of the <h2>, just do something like:
h2 a {
// style here
}
Or you can give the link a class:
h2 a.toc-backref {
// style here
}
h2 .toc-backref
{
/* Any style here */
}
you can also try
<h2 class="toc-backref">Debug</h2>
your style will be in css
.toc-backref
{
//your style....
}