1

I have this lyrics of a song that i wrote like this:

<p>School bell rings, walk me home<br>
Sidewalk chalk covered in snow<br>
Lost my gloves, you give me one<br>
"Wanna hang out?"<br>
Yeah, sounds like fun<br>
Video games, you pass me a note<br>
Sleeping in tents</p>
    
<p>It's nice to have a friend<br>
(Ooh)<br>
It's nice to have a friend<br>
(Ooh)</p>
    
<p>Light pink sky up on the roof<br>
Sun sinks down, no curfew<br>
Twenty questions, we tell the truth<br>
You've been stressed out lately? Yeah, me too<br>
Something gave you the nerve<br>
To touch my hand</p>

The thing is i applied this code to the CSS of the whole page:

 *{
     margin:0;
     padding:0;
 }

Cause there were other things i needed to position in the page (i needed the divs to not have the default margins/paddings)

The problem is, with this last code, my "p" don't work. They don't leave a space-line in between the paragraphs. To make it work i can write "br" after every "/p", but i don't think tidy enough. How can i make my p work by also letting that last code there? or which is the best way to do it?

3
  • 2
    Try *:not(p) .
    – j08691
    Commented Apr 25, 2022 at 17:03
  • * applies to every element on the page. If you don't want every element to have that style then don't do that. You should be aware of CSS Specificity rules.
    – fnostro
    Commented Apr 25, 2022 at 17:13
  • 1
    It's not a bad idea to get rid of the browser settings like that and start again - helps you ensure you are in control of the formatting of everything, but you need to remember to set whatever you want for those properties for any element where you aren't doing more specific setting.
    – A Haworth
    Commented Apr 25, 2022 at 17:17

6 Answers 6

1

If you want to preserve the global selector to apply margin and padding, you can avoid applying it to the p tag by editing the selector:

* :not(p) {
    margin: 0;
    padding: 0;
}

This is also just bad practice in case you want to modify margin and padding for other elements down the road. You should be creating individual selectors for each section of the site you want to work with. Consider writing a separate style for your lyrics:

.lyrics p {
    margin: 1em 0 1em 0;    // top right bottom left
}

and then wrapping your lyrics your HTML document:

<div class="lyrics">
    <p>
        line1<br>
        line2<br>
        line3<br>
        line4<br>
    </p>
</div>
3
  • i love that solution (the ":not()" ) i didn't know that one. but is that what you say its a bad practice? i want to make this the most proffesional way possible. So should i use " .lyrics p { margin: 1rem 1rem;}" instead then?
    – danistor_m
    Commented Apr 25, 2022 at 18:17
  • The universal selector: * acts as a wildcard and will apply those properties to any element without a specified selector. In order to fully control your page's styling, only use the universal selector when declaring global values like font-size. You can read more about it here. So yes, you can use how I laid out my style or adapt it to your own requirements (name it whatever, use rem or px, etc.)
    – zayadur
    Commented Apr 25, 2022 at 18:28
  • oooh the * is a bad practice. got it. thank you very much!
    – danistor_m
    Commented Apr 25, 2022 at 18:34
1

You can re-add the styles you want back onto the p tags.

This will override the * styles due to a higher specificity.

p {
   margin-top: 1em;
   margin-bottom: 1em;
}
2
  • is 1rem the exact size the <p> would have if i hadn't used the " * { margin: 0; padding:0; }" ?
    – danistor_m
    Commented Apr 25, 2022 at 18:19
  • Yup, that's the default in most browsers, or 1.12em as in the specification: w3.org/TR/CSS21/sample.html Commented Apr 28, 2022 at 14:48
0

Maybe try:

*:not(p) {
   padding: 0;
   margin: 0;
} 
0

div.my-lyrics {
  margin: 0;
  padding: 0;
}
<div class="my-lyrics">
  <p>School bell rings, walk me home<br> Sidewalk chalk covered in snow<br> Lost my gloves, you give me one<br> "Wanna hang out?"<br> Yeah, sounds like fun<br> Video games, you pass me a note<br> Sleeping in tents</p>

  <p>It's nice to have a friend<br> (Ooh)
    <br> It's nice to have a friend<br> (Ooh)
  </p>

  <p>Light pink sky up on the roof<br> Sun sinks down, no curfew<br> Twenty questions, we tell the truth<br> You've been stressed out lately? Yeah, me too<br> Something gave you the nerve<br> To touch my hand</p>
</div>

0

You should use pre tag in this case.

<pre>
  School bell rings, walk me home
  Sidewalk chalk covered in snow
  Lost my gloves, you give me one
  "Wanna hang out?"
  Yeah, sounds like fun
  Video games, you pass me a note
  Sleeping in tents
</pre>

keep in mind that pre tag's font-family is monospaced font by default, but you can override it with closest parent's font with inherit

font-family: inherit;
0

You can try :

* :not(p) {
    margin: 0;
    padding: 0;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.