&extend showing no matches when element exists - html

I am using less css for a basic website when i was trying to use &extend of less its showing no matches element when the element do exists.
I created a dummy environment same problem exists. Here is my code
style.less
#import 'style1.less';
#colorgreen: #00ff00;
#colorred: #ff0000;
#fontsize: 20px;
#bsolid: solid;
#bdash: dashed;
.container {
border: 1px#bsolid #ccc;
padding: 10px;
.myclass {
border: 1px#bdash #ccc;
padding: 10px;
h1 {
color: #colorred;
font-size: #fontsize;
}
p {
color: #colorred;
font-size: #fontsize;
}
}
}
.extend {
border: 1px solid #ccc;
padding: 10px;
h2 {
color: green
}
p {
&: extend(h2);
font-style: italic;
}
}
<div class="container">
<div class="myclass">
<h1>Second Heading</h1>
<p>LESS enables customizable, manageable and reusable style sheet for web site.</p>
</div>
<div class="importClass">
<p>This is imported CSS.</p>
</div>
<div class="extend">
<h2>First in extend</h2>
<p>Second in extend</p>
</div>
</div>

As the official Less Website indicates, the Less compiler looks at the compiled CSS selector when processing extend.
Essentially the extend looks at the compiled css, not the original less.
So, when trying to extend a nested selector, we should provide the full selector in the extend and not just the inner selector. In the example in question, the compiled CSS selector would be .extend h2 and so when the extend statement has only h2 provided as input, it wouldn't match and not output anything.
So, the below would compile successfully and work as per expectation.
.extend{
border: 1px solid #ccc;
padding: 10px;
h2{
color: green
}
p{
&:extend(.extend h2);
font-style: italic;
}
}
When compiled, it would result in the below CSS:
.extend {
border: 1px solid #ccc;
padding: 10px;
}
.extend h2,
.extend p {
color: green;
}
.extend p {
font-style: italic;
}

Related

CSS Inheriting or Sharing Properties (Class Inheritance IN CSS)

So I looked at how CSS "shares" its properties here : https://stackoverflow.com/a/199719/13707884
This was written in 2008 so I figured maybe CSS does allow for inheritance now & sure enough there is an article on it here :
https://coderwall.com/p/lqjd1w/css-class-inheritance-in-css
So I am trying out the code as mentioned in the link on "coderwall":
[class*=“row-“] {
border: 5px solid lightskyblue;
}
.row-1 {
color: blue;
}
.row-2 {
color: darkred;
}
<div class="row-1">AAA</div>
<div class="row-2">AAA</div>
However, this does not seem to be working. What could I be doing wrong here?
Your quotation marks in your first selector are invalid.
[class*="row-"] {
border: 5px solid lightskyblue;
}
.row-1 {
color: blue;
}
.row-2 {
color: darkred;
}
<div class="row-1">AAA</div>
<div class="row-2">AAA</div>

Best way to extend a SASS class with inner selector (subclass)

Im styling the following HTML to display messages:
<div className="message">
<div className="message_label">
A message
</div>
</div>
Using the following SCSS class:
.message {
margin: 10px;
border-radius: 10px;
border: 3px solid lightblue;
&_label {
color: #444;
padding: 5px;
}
}
Following BEM, I want to create another modificator version for error messages:
<div className="message--error">
<div className="message_label">
This is an error!
</div>
</div>
This version will just change the previous colors to red so I want to extend the previous SCSS class:
.message {
margin: 10px;
border-radius: 10px;
border: 3px solid lightblue;
&_label {
color: #444;
padding: 5px;
}
&--error {
#extend .message;
border: 3px solid red;
&_label {
color: red; // This is not working
}
}
}
But the selector message_label is not working, since its an inner selector and the #extend doesnt affect it, as explained in SCSS Docs. Whats the best way to extend a class including inner selector?
You can check the DEMO here.
The reason this isn't working is because all #extend does is share some css properties across different classes. So in this case, I would expect it to create a selector like:
.message, .message--error {
margin: 10px;
border-radius: 10px;
border: 3px solid lightblue;
}
.message_label {
color: #444;
padding: 5px;
}
.message--error {
border: 3px solid red;
}
.message--error_label {
color: red;
}
You can see at the bottom of the above css how your color: red might actually end up in a style sheet.
Finally, please do not format your scss like this. It would be a nightmare to maintain and understand.
Answer
My suggestion to answer your question is to just use .message and .message--error on the same element, similar to how Bootstrap does things

How make a single CSS class to make two text lines, like Title and Subtitle?

My code currently looks like this:
.data-title {
font-weight: bold;
}
.data-content {
color: gray;
}
<p class="data-title">
Title
</p>
<p class="data-content">
Content
</p>
The result is how I intended it to look:
However, I would like to make the following two changes:
Decrease the margin between the elements.
Have a single class on a parent element instead of the two classes on the paragraphs that automatically identifies the first line and second line and applies the current styles.
<div class="my-class">
<p>Title</p>
<p>Content</p>
</div>
You can use the :first-child selector to select the first element, and then combine this with the + * selecter to get the element following it. To decrease the margin, simply set margin: 0 (or whatever value you see fit) to all paragraph elements in your div (or you could add them to only the ones you already selected, depending on whether there are any other paragraphs that should retain their margin).
.my-class span {
display: block;
}
.my-class :first-child {
font-weight: bold;
}
.my-class :first-child + * {
color: gray;
}
<div class="my-class">
<span>
Title
</span>
<span>
Content
</span>
</div>
If i am right then you are asking for this may be:-
1st method:-
You can use span tag
p {
color: blue;
font-size: 40px;
margin-bottom: 10px
}
p span {
color: red;
display: block;
font-size: 15px;
}
<p>Title<span>Content</span></p>
2nd Method :-
div.main p {
color: blue;
font-size: 40px;
margin-bottom: 0px
}
div.main p+p {
color: red;
font-size: 20px;
margin-top: 0px
}
<div class="main">
<p>Title</p>
<p>Content</p>
</div>
3rd Method:-
div.main p {
color: blue;
font-size: 40px;
margin-bottom: 0px
}
div.main p:nth-child(2) {
color: red;
font-size: 20px;
margin-top: 0px
}
<div class="main">
<p>Title</p>
<p>Content</p>
</div>
Make sure the p-element in the title block loses its margin.
After that: style all elements by their own html class.
<div class="title_block">
<p>Title</p>
<span>Subtitle</span>
</div>
<style>
.title_block p {
margin: 0;
font-size: 28px;
font-weight: bold;
}
.title_block span {
font-size: 20px;
}
</style>

Different styles for tags with the same name <h2>

I think this is a real silly question.
But I really can not find an answer.
I need to give all my h2 tags the border-top element, except my h2 tag that is in my <header>.
Now it gives it a big line there and it is going across my header.
I really don't know how to solve this issue that seems so easy.
body {
background-color: AntiqueWhite;
max-width: 800px;
font-family: "Arial", Helvetica, sans-serif;
color: black;
}
h2, h3 {
font-style: italic;
color: darkblue;
}
h3 {
border-top:1px solid #999; padding-top:10px;
}
header {
background-image: url(pics/bg.jpg);
height: 140px;
background-position: center;
}
You just need to be specific.
header h2 {
border-top:none;
}
You can override the <h2>'s style and set the border back to it's default, like so:
h2 {
border-top: 1px solid white;
}
header h2 {
border-top: initial;
}
Or, and this is the approach I recommend, have your border-top in a class. Then, only instances of <h2> that have that class will have a border:
h2.with_border {
border-top: 1px solid white;
}
And then in your HTML:
<h2 class="with_border">This has a border!</h2>
<h2>This does not have a border!</h2>
CSS lets you compose selectors that are more complex; the simplest way of doing this is the descendant selector, which will work for your question. You can write this:
header h2 { }
to target any h2 that is a child (direct or not) of a header. For example:
h2 { border-bottom: 1px solid blue; }
header h2 { border-bottom: none; }
<header>
<h2>No border here!</h2>
</header>
<h2>But here there is</h2>
You should also do some reading up on how css works; there are all kinds of ways to select elements, including classes and attributes. Which one you use will depend on what you're trying to achieve and how specific you need to be.
I have created a JSFiddle for you.
HTML
<h2>Header One</h2>
<h2>Header Two</h2>
<h2>Header Three</h2>
<header>
<h2>Header Four</h2>
</header>
CSS
h2{
border-top: 1px solid green;
}
header h2{
border-top: none;
}
Hope this helps.

Why won't my CSS classes load?

My PHP looks like this (it's in a while loop) :
print"<li>
<a class='topcategory' href='index.php?category=$encode'>$category[name]</a>
</li>";
My CSS looks like this:
.topcategory {
display: block;
float: right;
height: 20px;
padding: 10px 20px 0 20px;
border: none;
font-size: 85%;
font-weight: bold;
color:#333;
text-decoration:none;
text-align:center;
}
But it doesn't affect my <a> tag at all, why?
(later edit) ---> Weird, it seems it spontaneously started working after 10 minutes of just looking on the code and refreshing even though i didn't do anything
All my examples assume HTML of...
<li>
Example
</li>
When you are writing your CSS, the order of the style rules matters, so if you have rules like this...
.topcategory {
color: red;
}
a {
color: blue;
}
CSS follows your order and the rule for "a" will override your rule for ".topcategory".
You can solve this in a few ways, but the easiest is to declare rules in order of generality / specificality, so declare your most general rules first, then declare rules in a more specific way later...
In this example, we start by declaring really general tag names, then slightly more specific tag names, then very specific tag names, then class names and finally ids. Using this order means that the cascading nature of CSS will apply the most specific rule to your element.
body {
color: Silver;
}
div {
color: Orange;
}
p {
color: Yellow;
}
a {
color: Blue;
}
.topcategory {
color: red;
}
.topcategory span {
color: black;
}
#someid {
color: Aqua;
}
#someid span {
color: Green;
}
You can override the cascade by using !important - but it is much easier to maintain if you declare your rules in the right order instead.
It's not weird at all. It's the browser cache. It is very common with CSS files, so from now on, be sure you check that first.
li a.topcategory {
display: block;
float: right;
height: 20px;
padding: 10px 20px 0 20px;
border: none;
font-size: 85%;
font-weight: bold;
color:#333;
text-decoration:none;
text-align:center;
}
If you have defined another rule for a tags elsewhere in your CSS, then this rule will overwrite those and force the priority of this class over a tags with the class topcategory.
Try this
print "<li>
<a class='topcategory' href='index.php?category=".$encode."'>".$category['name']."</a>
</li>";
<style>
.topcategory {
display: block !important;
float: right !important;
height: 20px !important;
padding: 10px 20px 0 20px !important;
border: none !important;
font-size: 85% !important;
font-weight: bold !important;
color:#333 !important;
text-decoration:none !important;
text-align:center !important;
}
</style>