Vertical border / divider centered in title

Is it possible and fairly easy to create a vertical border centered in my title? I want it to separate my logo / h1 text and my navigation bar.

I know css is messy, I just found out html and css last week! I could remove some of the css.

        header {
            height: 60px;
            width: 85%;
            /background-color: white;
            margin: 0 auto;
            /box-shadow: 0px 2px 31px -2px rgba(0, 0, 0, 0.86);
            /border: 2px solid #333;
            left: 0;
            right: 0;
            /border-sizing: border-box;
            /-moz-box-sizing: border-box;
            /-webkit-box-sizing: border-box;
            z-index: 999999;
        }


        header #kage > *, header li {
            display: inline-block;
        }

        header li {
            padding: 0 8px 0 8px;
            float:left;
        }

        #kage {
            width: 99%;
            margin: 0 auto;
            height: 100%;
            line-height: 59px;
            text-align: center;
        }

        .button1:hover {
            background-color: #f2f2f2;
        }
        .button2:hover {
            background-color: #f2f2f2;
        }
        .button3:hover {
            background-color: #f2f2f2;
        }

        .active {
            text-decoration: underline;
        }

        header a {
            text-decoration: none;
            color: #333;
            position: relative;
        }

        header h1 {
            margin: 0;
            float: left;
            height: 100%;
            text-shadow: 1px 2px lightgrey;
        }

        header h1:hover {
            color: #f2f2f2;
        }

        #topnav {
            height: 100%;
            /*float: right;*/
            font-weight: 700;
            font-size: 1.3em;
            width: 310.95px;
        }

        header ul {
            list-style-type: none;
            margin: 0;
        }
    <header>
      <div id="kage">
      <a href="index.html"><h1>H1 TEXT HEREEEEE</h1></a>
        <nav id="topnav">
            <ul class="menu">
                <a href="index.html"><li class="button1 active">Home</li></a>
                <a href="profil.html"><li class="button2">About</li></a>
                <a href="mdu.html"><li class="button3">MDU</li></a>
            </ul>
        </nav>
      </div>
    </header>
Run codeHide result

It looks like you need to run the fragment in full screen, so the title will not be split into two lines.

+4
source share
2 answers

There are many ways to do this. I think the easiest one is to put a border on the left on your #topnav.

#topnav {
            height: 100%;
            /*float: right;*/
            font-weight: 700;
            font-size: 1.3em;
            width: 310.95px;
            border-left: 2px solid rgb(0,0,0);
            margin: 0 0 0 40px;
        }

2 topnav, . . margin: 0 0 0 40px; ~ (: )

        header {
            height: 60px;
            width: 85%;
            /background-color: white;
            margin: 0 auto;
            /box-shadow: 0px 2px 31px -2px rgba(0, 0, 0, 0.86);
            /border: 2px solid #333;
            left: 0;
            right: 0;
            /border-sizing: border-box;
            /-moz-box-sizing: border-box;
            /-webkit-box-sizing: border-box;
            z-index: 999999;
        }


        header #kage > *, header li {
            display: inline-block;
        }

        header li {
            padding: 0 8px 0 8px;
            float:left;
        }

        #kage {
            width: 99%;
            margin: 0 auto;
            height: 100%;
            line-height: 59px;
            text-align: center;
        }

        .button1:hover {
            background-color: #f2f2f2;
        }
        .button2:hover {
            background-color: #f2f2f2;
        }
        .button3:hover {
            background-color: #f2f2f2;
        }

        .active {
            text-decoration: underline;
        }

        header a {
            text-decoration: none;
            color: #333;
            position: relative;
        }

        header h1 {
            margin: 0;
            float: left;
            height: 100%;
            text-shadow: 1px 2px lightgrey;
        }

        header h1:hover {
            color: #f2f2f2;
        }

        #topnav {
            height: 100%;
            /*float: right;*/
            font-weight: 700;
            font-size: 1.3em;
            width: 310.95px;
            border-left: 2px solid rgb(0,0,0);
            margin: 0 0 0 40px;
        }

        header ul {
            list-style-type: none;
            margin: 0;
        }
    <header>
      <div id="kage">
      <a href="index.html"><h1>H1 TEXT HEREEEEE</h1></a>
        <nav id="topnav">
            <ul class="menu">
                <a href="index.html"><li class="button1 active">Home</li></a>
                <a href="profil.html"><li class="button2">About</li></a>
                <a href="mdu.html"><li class="button3">MDU</li></a>
            </ul>
        </nav>
      </div>
    </header>
Hide result
+1

HTML CSS - , .

header .

header , h1 nav, .

, header, text-align: center .

header , white-space: nowrap, ( ).

, .

, nav vertical-align: top, .

, height header, line-height, ( , ).

header {
  height: 60px;
  line-height: 60px;
  width: 85%;
  margin: 0 auto;
  border: 1px dotted blue; /* for demo only */
  text-align: center; /* optional? */
  white-space: nowrap; /* to prevent line from wrapping, optional */
}
header h1 {
  display: inline-block;
  border-right: 1px solid black;
  margin: 0;
  padding-left: 10px; /* adjust to taste */
  padding-right: 10px; /* add whitespace before vertical border */
  height: 100%;
}
header h1 a {
  text-shadow: 1px 2px lightgrey;
}
header h1:hover a {
  color: #f2f2f2;
}

header a {
  text-decoration: none;
  color: #333;
}
.button:hover {
  background-color: #f2f2f2;
}
.active {
  text-decoration: underline;
}
nav {
  display: inline-block;
  vertical-align: top;
  padding-left: 10px; /* whitespace after vertical border */
  font-weight: bold;
  font-size: 1.3em;
  border: 1px dotted gray; /* for demo only */
}
nav a {
  margin: 0 20px;
}
<header>
  <h1><a href="index.html">H1 TEXT HEREEEEE</a></h1>
  <nav>
    <a href="index.html" class="button active">Home</a>
    <a href="profil.html" class="button">About</a>
    <a href="mdu.html" class="button">MDU</a>
  </nav>
</header>
Hide result
+1

Source: https://habr.com/ru/post/1656072/


All Articles