-1

I tried to solve this but I'm still blocked on an error like this lol. I don't understand why my CSS does not apply to my HTML elements.

a{
 text-decoration: none;
}

.test{
 text-decoration: none;
}
<a href="#" class="test">
         <div id="blue-card" class="card h-150">
           <div class="card-body">
            <p class="info-card"><strong><?php echo $_SESSION["_nbruser"] ?></strong></p>
            <p class="sub-info-card">Utilisateurs actifs</p>
           </div>
         </div>
        </a>

I first tried with only the balise in CSS and after it doesn't work I tried with the "test" class. But it still doesn't work.

The other CSS of my page work. It is only on my balise ..

if anyone have an idea on how to solve my problem pls!

Thanks,

EternalHour
  • 8,308
  • 6
  • 38
  • 57
max keller
  • 13
  • 5
  • Where does your css live? Is it defined under ` – Mr T Feb 25 '20 at 16:24
  • hello, I import my css like this : ``` ``` and yes it work because my css work on my other class etc.. – max keller Feb 25 '20 at 16:26
  • 1
    check in your browser console if this properties are overwritten by someting else – Sfili_81 Feb 25 '20 at 16:29
  • 1
    It doesn't make any sense :) have you got any css with higher specificity (or `!important`) which overrides your link? At this point I would suggest you to check developer tools and see what styles are inherited, the problem is not recreatable as it stands -> https://jsfiddle.net/p2so0feb/ – Mr T Feb 25 '20 at 16:30
  • My browser console is empty, there is no error or warning – max keller Feb 25 '20 at 16:31
  • No, it is my only css file imported and I don't have any !important or anything else.. :/ – max keller Feb 25 '20 at 16:31
  • When I try to write my style directly in my html it works (like this:) but it's not very nice :( – max keller Feb 25 '20 at 16:33
  • What is the broader architecture of your application. Is it a framework like MVC, or some other framework, or a total 'ground-up' application? – Alan Feb 25 '20 at 16:49
  • It is a total ground-up application x) and I use bootstrap – max keller Feb 25 '20 at 16:52
  • I'm not familiar with applicationx. Is style.css your private add-on file to change the basic operation of bootstrap, or were you working inside the bootstrap files? – Alan Feb 25 '20 at 17:02
  • It is my private add-on file to make my own front-end. – max keller Feb 25 '20 at 17:10
  • you will never find css problem in the console. You need to explore your HTML code with the analyzer of your browser in order to check which style is applied. I assume there are some other css rules that are overriding the desired ones. Every css rule have points; according to those points, the browser know which one get priority. The rules you write in the 'style' attribute get higher priority – silviagreen Feb 25 '20 at 17:14
  • You may have already done this, but have you checked that style.css is absolutely the last .css file loaded? If bootstrap.css or any other .css loads after style.css, then your changes will be overwritten. In MVC the files are imported during application registration and the order can be set there. I don't know how applicationx loads css files, but sometimes a framework can do things in the background we might not be aware of. – Alan Feb 25 '20 at 17:14
  • okay thx, as you said Bootstrap is after my private css file, i will try to switch their place tomorrow. – max keller Feb 25 '20 at 17:40
  • Let me know if that fixes your problem, and I'll post the answer. – Alan Feb 25 '20 at 18:00
  • What does balise mean? – j08691 Feb 25 '20 at 19:21
  • @j08691 Not sure, but I think balise is French for 'basic', as in basic html tags. – Alan Feb 25 '20 at 21:11
  • Hello, I tried to switch the place between bootstrap and my css file but it didn't change anything. I looked the css applied on my tag and my css is correct but I always have the blue underlining. Yes I am sorry, balise means Tag. – max keller Feb 26 '20 at 07:51
  • I think I finally saw the issue. Overlooked it at first. See my posted answer below. – Alan Feb 26 '20 at 10:53
  • Did you decide to go with the css override? – Alan Feb 27 '20 at 22:43

3 Answers3

0

So, the behavior you experience is that defining CSS rules separately, based on the tag name or class name are not applied, yet, if you specify your CSS as an attribute value, then it's applied. Let's think together:

Rule by tagname

a{
    text-decoration: none;
}

You reasonably expect this rule to be applied on the anchor, but it's not the case. This evidently means that some other CSS rule (or Javascript) overrides it. Browser Dev Tools can aid you, just right-click anywhere on your page and click on Inspect (or a similar choice). Inside the Dev Tools panel you should see an Elements tab, which shows the HTML and clicking on elements you should see CSS rules on the right-hand side, like on the picture below:

enter image description here

So, I advise you to click on the anchor where you expect your rule to be applied and see what CSS applies there. The rule that you intend to specify here will appear striked through, because something with higher priority overrides it (another case is that a rule with similar prio level is evaluated later and overrides this one). You should be able to see which text-decoration rule is applied and you can gently hover on that rule and click on its checkbox to disable it for now. This will enable the rule applying on this attribute with the second priority level in the hierarchy and so on. This process is not yet a solution, it's exploring the problem. After this exploration you will know what the problem is.

Rule by class

.test{ text-decoration: none; }

The situation is either similar with the one described in the previous section (rule override due to higher priority or similar priority but later in the code), or, it's possible that for some reason the test class is removed from the tag. So, in the Elements tab of the browser console you will see whether that element still has the class. If not, then experiment by editing the tag and writing that class into it and see whether your rule applies or not. If the tag has the class, but the rule does not apply, then we have a similar issue as the one described in the previous section.

Solution

The best solution is to find out what the problem is, why are there other rules applied on this element and act accordingly. For now, you can apply a rule like

a.test#test {
    text-decoration: none;
}

and of course add test as an id to your tag, as below:

<a href="#" class="test" id="test">

and if this still doesn't work, then there is a high chance that the other rule which causes you trouble has !important. If that's the case, then try removing the other rule. If that's not an option, then look at what the selector of the other rule is and make sure that the selector of your tag contradicts it.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Hello, many thanks for your answer, I watched my css file on my html page and my css is good, I have my "text-decoration: none;' but it doesn't apply on my html page... I understand less and less.. I tried with the class and ID "test" too but like I thought it doesn't work either. – max keller Feb 26 '20 at 07:48
  • @maxkeller can you create a JSFiddle to show us how this is not working? We do not have access to your code, hence only you can find the exact problem there. – Lajos Arpad Feb 26 '20 at 09:45
  • sorry, for letting your comment without answer, I solved my problem by using "a:link" in my css file. So my problem is solved ! :) thanks ! – max keller Feb 28 '20 at 12:57
0

It wasn't immediately clear from your initial post exactly what display problem was occurring. But in your comments you indicated an undesired text decoration is showing up, presumably in one of the html elements. Your initial post appears to show your initial efforts to correct the undesired decoration by re-defining the a element's css in your style.css sheet, which is intended to override the bootstrap css.

But your problem really appears to be related to which css is most specific to the element being displayed. The closer a style is to an element, the more precedence it has.

Each of the html elements within your a element have classes applied to them "card h-150","card-body","info-card","sub-info-card". That's a lot of classes to sort through.

<a href="#" class="test">
            <div id="blue-card" class="card h-150">
              <div class="card-body">
                <p class="info-card"><strong><?php echo $_SESSION["_nbruser"] ?></strong></p>
                <p class="sub-info-card">Utilisateurs actifs</p>
              </div>
            </div>
        </a>

How those classes interact will take precedence over your a definition because they are more specific, in other words, closer to the element.

Trying to correct the problem by redefining the a element with an override like text-decoration: none!important will certainly work, but it is not good practice (see first answer here). You should look closely at what the invoked classes in your html elements do. If those classes aren't what you need, use a different class, or this could be a good opportunity for you to write your own custom class in the style.css. However, writing your own class if you're just beginning to get familiar with css may prove challenging. Probably better to find the class you really want from within bootstrap. That's the value of bootstrap.

To answer your original question which is basically why doesn't your css apply to your html elements, it's because a class is applied on the element and that takes precedence. CSS is tricky with specificity and it's hard to learn at first. See some of the answers in this post, and also this link mentioned in that same post.

Alan
  • 1,587
  • 3
  • 23
  • 43
-2

Try accessing the 'link' attribute of the anchor tag as below and setting the value as none, also add !important to it, this worked for me.

a:link {
text-decoration: none!important;
}
  • Hello ! I tried and it works with and without the !important, I would like to know what is the use of "a:link"? why not just use the tag ?. Many thanks. – max keller Feb 26 '20 at 07:53
  • After looking closely at all your comments, I believe your answer is in my post below. Although using an override will certainly work, overrides should be reserved for very special circumstances because they are disruptive to css cascading. – Alan Feb 26 '20 at 10:52