Is it possible to set the <li> background to full width and center it vertically?

Getting straight to the point:

I have a vertical menu bar and I am looking to have an <li> element with a full background color and vertical alignment. I came across a suggestion to add vertical-align: middle;, but that doesn't seem to work well with vertical menus as it was mentioned that adding display: table-cell; might be necessary.

HTML

<div class="sidebar">
  <img src="img/Logo.png">
<ul>
  <li><span class="glyphicon glyphicon-home" style="color: white;"></span><a href="#">Home</a></li>
</ul>
</div>

CSS:

.sidebar{
  width:150px;
  background-color: #0e1a35;
  height: 100%;
}
.sidebar > img:nth-child(1){
  width: 70%;
  display:block;
  margin:auto;
  padding: 5px  ;

}

.sidebar > ul {
  list-style: none;
  padding:0;
  margin:0;
  width: 100%;
}

.sidebar > ul > li{
  text-decoration: none;
  background-color: #42105d;
  height:50px;
  vertical-align: middle;
  float: none;
  width: 100%;


  text-align: left;
  border-left: #5584ff 4px solid;
}
.sidebar > ul > li > a{
  color: white;
  text-decoration: none;
  width: 100%;
  height: 100%;
  position: relative;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  display: inline-block;

}
.sidebar .glyphicon{
  margin-left: 0px;
  margin-right: 10px;
}

My query is how to achieve vertical alignment with full background color coverage for the <li> element without altering the padding/margin of the <li>.

Answer №1

  • HTML: Ensure the icon and its contents are wrapped inside the A element
  • Avoid styling the LI element
  • Apply focus styling to the A element instead
  • For vertical alignment, consider using line-height to match the actual height

@import url("//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css");

*{box-sizing: border-box;}
body{margin:0;}

.sidebar{
  width:150px;
}
.sidebar ul{
  margin:0;
  padding:0;
}
.sidebar a{
  text-decoration:none;
  display:block;
  background: #42105d;
  color:#fff;
  border-left: #5584ff 4px solid;
  height:50px;
  line-height:50px;
  padding: 0 8px;
}
<div class="sidebar">
  <ul>
    <li>
      <a href="#"> <!-- WRAP ALL INSIDE THE ANCHOR! -->
        <span class="glyphicon glyphicon-home" style="color: white;"></span> Home
      </a>
    </li>
  </ul>
</div>

Here's an example that does not use line-height and height, but instead adjusts the padding of the A element:

@import url("//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css");

*{box-sizing: border-box;}
body{margin:0;}

.sidebar ul{
  width:150px;
  margin:0;
  padding:0;
}
.sidebar a{
  text-decoration:none;
  display:block;
  background: #42105d;
  color:#fff;
  border-left: #5584ff 4px solid;
  padding: 16px 8px;
}
<div class="sidebar">
  <ul>
    <li>
      <a href="#">
        <span class="glyphicon glyphicon-home" style="color: white;"></span> Home
      </a>
    </li>
    <li>
      <a href="#">
        <span class="glyphicon glyphicon-user" style="color: white;"></span> About
      </a>
    </li>
    <li>
      <a href="#">
        <span class="glyphicon glyphicon-envelope" style="color: white;"></span> Contact
      </a>
    </li>
  </ul>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

MTG Life counter. Display fluctuations in count

I am currently working on a fun project creating an MTG (Magic The Gathering) life tracker, even though the code is quite messy. Despite its flaws, it still gets the job done. Click here to view the MTG life tracker https://i.stack.imgur.com/Su17J.png ...

Is the second parameter of the function being used as a condition?

Why is it necessary to validate the helpText argument within the function to be non-equative to null when its ID is linked with the span tag? The functions task is to set and clear help messages in the form field using built-in CSS classes. <input id ...

What's the best way to add two distinct colors as background for a header with css?

h2 {background-color:#C61236; color:#FFFFFF; font-size:medium; line-height:1.5; text-indent:20px;} Although the current setup looks good, I am looking to incorporate a new color block at the start of the text. I'm not sure what steps to take nex ...

Place an element in relation to its initial position, ensuring that the x-coordinate is not set to 0

Is there a method to place an element in relation to its initial position, only excluding x=0 - meaning that the element is placed at its original vertical coordinate but sticks to the window's left edge? ...

Limiting the displayed portion of a table and implementing scrolling instead

I am currently working with a static HTML template that contains the following code: <table> <thead></thead> <tbody></tbody> </table> Using jQuery and AJAX, I am dynamically adding and removing rows and columns ...

Angular 6: Issue TS2339 - The attribute 'value' is not recognized on the 'HTMLElement' type

I have a textarea on my website that allows users to submit comments. I want to automatically capture the date and time when the comment is submitted, and then save it in a JSON format along with the added comment: After a comment is submitted, I would li ...

Tips for arranging Angular Material cards in columns instead of rows

I am currently developing a web application using Angular, and I've encountered an issue while trying to display cards in a vertical layout rather than horizontally. My goal is to have the cards fill up the first column (5-6 cards) before moving on to ...

Looking to switch up the hide/show toggle animation?

Currently, I have a functioning element with the following code. It involves an object named #obj1 that remains hidden upon page load but becomes visible when #obj2 is clicked. #obj1{ position:fixed; width:100px; bottom:180px; right:10 ...

What is the best way to customize the color of the border and icon in an outlined MaterialUI input field

I'm a newcomer to materialUI and I have a question regarding customizing an outlined input field with an icon. Due to my dark background, I need the icon, border, and text color to be lighter, such as grey. Can someone please advise on the materialUI ...

Tips for establishing a custom thumbnail and title for HTML audio or video in the Chrome media control menu and Windows media control dialog

Is there a way to display the thumbnail and title of audio or video in HTML in the Chrome media control menu and Windows media control menu (where a dialog appears when the volume up or down key is pressed)? Chrome Menu https://i.sstatic.net/oLDgK.png W ...

The jQuery scrollTop function seems to be malfunctioning following an ajax request

My ajax call is functioning properly, but I am experiencing an issue with the scrollTo plugin from JQuery. It is not positioning the content where I want it to be located below. //This function reveals the email body when a list item is clicked. jQue ...

Next.js production mode prevents CSS from loading properly

Issue Upon building and launching a production build of our application, the CSS fails to load. Inspecting the devtools reveals a multitude of errors and warnings: https://i.sstatic.net/R07q3.png Possible Causes The problems do not occur when running th ...

Exploring the capabilities of HTML5 canvas and integrating it with Selenium using Python

I have been working on testing a web server that is built using HTML5 with a canvas. While examining the source code for the login page, I noticed very minimal content. However, upon inspecting the element in Chrome, I discovered the following snippet: &l ...

Having trouble getting the tailwindcss Google font link tag to apply properly

When I use the @import in the tailwind css file, it works fine. However, when I try to add the font using the <link> tag in _document.jsx, it fails to work properly. In Chrome dev tools, it shows that the classes are applied but the fallback font is ...

Bootstrap sticky footer solution

Having an issue with the sticky footer in Chrome while everything looks fine in FF and IE. When resizing the screen, the content wraps properly, but the footer does not adjust and overlaps the content. Any tips or suggestions would be greatly appreciated. ...

The left margin class in Bootstrap seems to be having trouble functioning properly within a React

Navbar <div className="collapse navbar-collapse" id="navbarSupportedContent"> <ul className="navbar-nav ml-auto"> <li className=&quo ...

What is the best way to achieve a horizontally compact layout in Bootstrap by centering the content?

I am looking to center my content on the page and maintain a close proximity between items within a div. However, with Bootstrap 5, when I resize the page wider, additional space is added which pushes the items apart. Is there a way to keep them compacted ...

Problem with displaying JSF Bootstrap Glyphicons

I recently encountered a problem in my web app where my Glyphicons in Bootstrap suddenly stopped working, appearing as empty squares for no apparent reason. Even after ensuring that the font files were intact and replacing them with fresh ones from versi ...

I am having trouble with the CSS and Bootstrap not being applied after printing

Once the submit button is clicked on the initial output page, the CSS styling disappears and only a simple default form page is displayed. This does not meet my requirements. Using inline CSS allows it to work, but external CSS does not. Can someone please ...

Exploring the concept of D3 data binding key accessor

Exploring D3 for the first time, I am working on a basic example to grasp the concept of data binding. My setup includes an array of colors, a function to add a color, and a function to remove a color based on index. However, I'm facing issues with t ...