gpt4 book ai didi

html - 可以将
  • 背景设置为全宽 + 居中垂直高度吗?
  • 转载 作者:可可西里 更新时间:2023-11-01 13:47:57 25 4
    gpt4 key购买 nike

    切入正题:

    我有一个垂直 菜单栏,我想要一个<li>具有完整背景颜色+垂直对齐的元素。我在这里看到有人建议添加 vertical-align: middle;不够,我需要添加 display: table-cell;但这并不能真正帮助垂直菜单。

    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;
    }

    所以我的问题是如何获得 <li>与覆盖 <li> 的背景颜色垂直对齐不改变 <li> 的填充/边距

    最佳答案

    • HTML:将图标和所有内容包裹在 A 元素中
    • 不要为 LI 设置样式
    • 着重于 A anchor 元素的样式
    • 对于垂直对齐,您可以使用 line-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>

    这是一个示例,没有使用line-heightheight,而是使用A 的填充:

    @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>

    关于html - 可以将 <li> 背景设置为全宽 + 居中垂直高度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39314970/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com