Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HTML
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Twoclasees</title>
    <style>
      .box {
        width: 700px;
        margin: 100px auto;
      }
      .top {
        border-top: 4px solid #3a5a40;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="border top">
        <p>Test</p>
      </div>
    </div>
  </body>
</html>


Why the border will not work if you put two classes like:

CSS
.border .top {border-top: 4px solid #3a5a40;}


It will only show if you put either one, but not both. Pardon my English. I'm trying to learn, but English is not my main language.

What I have tried:

I tried putting two classes, but the border will not show, it will only show if you put either .border or .top, I just want to know why putting both will not work.
Posted
Updated 28-Aug-23 8:07am
v4

To target an element that has both classes border and top, you should use a single selector without space between the classes. You had '.border .top', it should be '.border.top' -
HTML
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Twoclasees</title>
    <style>
      .box {
        width: 700px;
        margin: 100px auto;
      }
      .border.top {
        border-top: 4px solid #3a5a40;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="border top">
        <p>Test</p>
      </div>
    </div>
  </body>
</html>


See a working fiddle - both classes border and top[^]
 
Share this answer
 
Comments
Jomar Apas 14-Sep-23 1:04am    
Thank you so much, I get it now. If you don't mind can you explain to me why it won't work if you put space inbetween?
Sometimes your browser caching can make it appear that way. Easiest way to force a page refresh is to open you dev tools, stitching to the network tab, then turn on/check disable caching. Now when you refresh the page, a full reload will happen.

Have a read of this Stack Overflow answer: https://stackoverflow.com/a/7000899[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900