CSS snippet for headers not working

What I’m trying to do

Hi, I’m trying to make my headers have a nice horizontal bar going through the header line, but not through the header text itself (ie: to the left and right of the text). I’ve tried the CSS below, but cannot get it to work. This CSS works in a plain HTML file, but I think I cannot figure out what selector to use for Obsidian headers. I’m just grabbing h2 in this example.

Things I have tried

/* headers with lines */

.cm-header-2 {
     overflow: hidden;
     text-align: center;
 }
.cm-header-2  > span{
    position: relative;
    display: inline-block;
}
.cm-header-2  > span:before, .cm-header-2  > span:after{
    content: '';
    position: absolute;
    top: 50%;
    border-bottom: 2px solid;
    width: 100vw;
    margin: 0 20px;
}
.cm-header-2  > span:before{
    right: 100%;
}
.cm-header-2  > span:after{
    left: 100%;
}

Do you have a screenshot of what the final product you are hoping to achieve looks like?

horiz-bar-header
Something like this, but I’d prefer the headers left justified with just a short bar on the left about a tab’s width.

Hi @ControlAltPete, I actually made headers like your picture a while back so I messed around with it. If I understand your description “headers left justified”, “short bar on the left”, “about a tab’s width” you want something like this:

image

If so there is probably a much cleaner way of doing it but I’m a hack at css so here is what I came up with:

.cm-header.cm-header-1,.cm-header.cm-header-2 {
display: inline-flex;
align-items: center; /* makes the line in the middle */
}

.cm-header.cm-header-1:before,
.cm-header.cm-header-2:before {
content: '   '; /* How long the line is */
height: 1px; /* How thick the line is */
margin-right: .1em; /* distance from line to header text */
}

/* Simple one */
.cm-header.cm-header-1:before { 
  background: red;}

/* Fancy one */
.cm-header.cm-header-2:before {
  background-image: linear-gradient(270deg, white, red);}

Note this is for Live mode I don’t mess with any other mode.

If anyone knows a better way please post I’d like to see how since I am working on several different headers.

I see now you meant this as an addition to having the lines after it as well. Trying to figure that out now.

@ControlAltPete I think I have it now:

image


.cm-header.cm-header-1,.cm-header.cm-header-2 {
  display: inline-flex;
	align-items: center; /* makes the line in the middle */
  width: 95%;
  right: -30px;

}

.cm-header.cm-header-1:before,
.cm-header.cm-header-2:before {
  content: '   '; /* How long the line is */
  height: 1px; /* How thick the line is */
  margin-right: .1em; /* distance from before line to header text */
}

.cm-header.cm-header-1:before {
  background: red;}

.cm-header.cm-header-2:before {
  background-image: linear-gradient(270deg, white, red);}
  

.cm-header.cm-header-1:after,
.cm-header.cm-header-2:after {
  content: ''; /* Don't need content here flex-grow will handle it */
  height: 1px; /* How thick the line is */
  margin-left: .1em; /* distance from header text to after line */
  flex-grow: 100; /* Filles in rest of line */
  ;
}

.cm-header.cm-header-1:after {
  background: red;
}

.cm-header.cm-header-2:after {
  background-image: linear-gradient(270deg, red, white);}

Again if anyone knows a better way please post it.


Edit because I can’t reply more than 3 times…

If you use multiple font sizes like in my above example the lead in bar will have different lengths

image

if you want the beginning uniform with different font sizes etc like this:

image

Then, Instead of:

you can use this bit:


.cm-header.cm-header-1:before,
.cm-header.cm-header-2:before {
  content: '';
  flex-grow: 5; /* How long the line is */
  height: 1px; /* How thick the line is */
  margin-right: .1em; /* distance from before line to header text */
}

On my system this:

.cm-header.cm-header-1,.cm-header.cm-header-2 {
  display: inline-flex;
  align-items: center; /* makes the line in the middle */
  width: 95%;
  right: -30px;
}

.cm-header.cm-header-1:before,
.cm-header.cm-header-2:before {
  content: '';
  flex-grow: 5; /* How long the line is */
  height: 1px; /* How thick the line is */
  margin-right: .1em; /* distance from before line to header text */
}

.cm-header.cm-header-1:before {
  background: red;}

.cm-header.cm-header-2:before {
  background-image: linear-gradient(270deg, white, red);}

looks like this:

@ControlAltPete

Are you using any themes? I did mine in a new vault, no themes, no plugins, just the 1 CSS file for the headers. I could be doing something wrong as I’m not a CSS expert but as the dev team where I work often say…works on my machine. :slight_smile.

I am attaching the whole vault, see if it works for you. Should look like this (I have since added reading mode and some ugly color):

headers.zip (4.5 KB)

Looks good when I open your vault, still doesn’t work in my vault. I copied your latest css to my snippets, switched to default theme, dark mode, turned off all of my community plugins. I don’t have any other CSS snippets. I have no idea why it doesn’t work :frowning: Any idea? Any CSS+Obsidian experts out there?

The above few snippets alone will give you the look from your screenshot above, but if you copy JimK’s JMK-Headers-GradientWingBars.css full file into your vault (from their headers vault snippets folder), and enable it you should be good to go :boom:

You could add:

.theme-dark, .theme-light {

to the top, to cover both dark/light modes, but it works as is in dark mode.

@ControlAltPete if what @ariehen suggests doesn’t work I don;'t have any further ideas, sorry. Maybe you could jump on the Discord and ask there if no one comes along here.

I did that originally. I put a copy of JMK-Headers-GradientWingBars.css in my vault and it didn’t work. It actually has awful results, my headers are now all split onto two lines, a blank line where the hidden ## markers are and the header indented on the next line and no horizontal bars anywhere.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.