【问题标题】:Align div in the middle li on ul list在 ul 列表的中间 li 对齐 div
【发布时间】:2013-06-18 02:34:33
【问题描述】:

我在及时将我的 div 对齐到 <li> 中时遇到了一个小问题。我想垂直对齐我的 div(里面有一张图片),无论我的<li> 的高度如何,它总是在中间。不使用利润率最高百分比 (%)。已经使用了显示表,但不适用于我的情况。

这是我想留下的照片:

我的<li>的高度如何增加。

图像不在<li> 的中间。 ^

如果有人可以帮助我,这是我的文件小提琴。记住不要使用边距:)。就我而言,我暂时使用文件小提琴。

http://jsfiddle.net/Igaojsfiddle/T6KrE/37/

#frdImgProfile {
    width: 50px;
    height: 50px;
    border: 1px solid #aaa;
    background: #ffe;
    position:absolute;
    margin-top:3px;
    margin-left:4px;
    -webkit-border-radius: 6px;
    -moz-border-radius: 6px;
}

谢谢!

【问题讨论】:

  • 您的<div>s 具有固定的高度和宽度50px。这是故意的吗?
  • 这个 css #contactList > li { bla..bla..bla.. 120px; }
  • 如果你的<div class="frdImgProfile">s 有固定的宽度和高度50px,你可以试试这个:jsfiddle.net/T6KrE/38 注意我把id 改成了class,因为你不应该有多个具有相同 ID 的元素。
  • 是的,id 是固定的,因为我使用了 ctrl c, ctrl v .. margin-top:-25px 是什么意思?是你变了吗?或者还有什么别的吗?没有其他方法没有使用保证金:) - 感谢您的时间...
  • top:50%;margin-top:-25px。这是基于height:50px 是固定的事实。

标签: css list html position vertical-alignment


【解决方案1】:

可以添加..添加您完整的课程。 ++

#contactList > li {
    position:relative;
}
#frdImgProfile {
    width: 50px;
    height: 50px;
    position:absolute;
    margin-top:-25px;
    top:50%;
}

【讨论】:

    【解决方案2】:

    嗯...我们会去零件:

    首先:您不必滥用 id 属性。

    第二:在您的 CSS 代码中,您有很多引用相同 id 的规则。这不是一个好习惯。它假设 id 是唯一的。

    第三:我看到你有一个名为:div#avatarUser 的 div。我猜你是为了设置特殊风格而创建的。那么你不需要这样做。使用 parent:first-child 或 parent:nth-child(1) 您可以为第一个元素设置特定样式:

    例如:

    <ul>
     <li></li> <!-- I want to set specific styles for this element. The first element -->
     <li></li>
     <li></li>
    </ul>
    

    所以,为了在我的 CSS 文件中执行此操作,我将输入:

    ul > li:nth-child(1) { /* Your CSS code */ }
    

    好吧,现在我们深入解决您的问题。

    我对你的 HTML 代码做了一点改动,因为我认为它的代码更有条理、更简洁:

    <div class="frdList">
        <ul class="contactList">
            <li>Friends :)</li>
            <li class="p-flexbox flex-hsc">
                <img src="http://w2.vanillicon.com/2c85954e3b080d9926c53b530ca40317_50.png" />
            </li>
            <li class="p-flexbox flex-hsc">
                <img src="http://w6.vanillicon.com/6cd18e7a56ebd6fb1f3f607823b7d5fe_50.png" />
            </li>
            <li class="p-flexbox flex-hsc">
                <img src="http://wc.vanillicon.com/cd7c7d1f9a0c56ff3b8296527a98564f_50.png" />
            </li>
            <li class="p-flexbox flex-hsc">
                <img src="http://vanillicon.com/0fff488a9952086c6785f260e2c127ad_50.png" />
            </li>
        </ul>
    </div>
    

    并且还更改了 CSS 文件:

    /* Reset CSS */
    
    body, div {
        margin: 0;
        padding: 0;
    }
    
    li { list-style: none; }
    
    /* @font-faces imports */
    
    @font-face {
        font-family:'Amatic SC';
        font-style: normal;
        font-weight: 400;
        src: local('Amatic SC Regular'), local('AmaticSC-Regular'), url(http://themes.googleusercontent.com/static/fonts/amaticsc/v3/DPPfSFKxRTXvae2bKDzp5D8E0i7KZn-EPnyo3HZu7kw.woff) format('woff');
    }
    
    /* Basic styles */
    
    .frdList {
        height:500px;
        width:500px;
    }
    .contactList > li:nth-child(1) {
        font-weight: bold;
        font-family: 'Amatic SC', cursive;
        font-size: 45px;
        text-align: center;
        color: #fff;
        border-top-left-radius: 5px;
        border-top-right-radius: 5px;
        box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4);
        background-image: -webkit-linear-gradient(#2da1ec, #0191d9);
        background-image: -moz-linear-gradient(#2da1ec, #0191d9);
        background-image: -ms-linear-gradient(#2da1ec, #0191d9);
        background-image: linear-gradient(#2da1ec, #0191d9);
        border: 1px solid #0082c3;
        border-bottom: 1px solid #077be0;
        position: relative;
        height:55px;
    }
    .contactList > li:nth-child(1):hover {
        background-image: -webkit-linear-gradient(#2eacff, #0191d9);
        background-image: -moz-linear-gradient(#2eacff, #0191d9);
        background-image: -ms-linear-gradient(#2eacff, #0191d9);
        background-image: linear-gradient(#2eacff, #0191d9);
    }
    .contactList > li:nth-child(1):after {
        content: url("http://images1.wikia.nocookie.net/knd/images/3/3a/PR2.gif");
        text-align: center;
        width: 68px;
        height: 65px;
        background: #8dfd07;
        display: inline-block;
        position: absolute;
        top: -10px;
        left: 15px;
        border-radius: 5px;
        border: solid 1px #aaa;
    }
    .contactList > li:nth-child(1):before {
        content: "";
        position: absolute;
        border-radius: 6px;
        width: 78px;
        height: 75px;
        background-color: white;
        line-height: 70px;
        /* Well see */
        text-align: center;
        border: solid 1px #aaa;
        top: -15px;
        left: 10px;
    }
    
    .contactList > li {
        font-weight: bold;
        color: #fff;
        box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), inset 0 -2px 2px -2px gray;
        background-image: -webkit-linear-gradient(#ededed, #eff0f2);
        background-image: -moz-linear-gradient(#ededed, #eff0f2);
        background-image: -ms-linear-gradient(#ededed, #eff0f2);
        background-image: linear-gradient(#ededed, #eff0f2);
        border-left: 10px solid green;
        border-right: 1px solid #999999;
        height:120px;
    }
    
    .p-flexbox {
    
       /* Flexbox: Init setup */
    
       display: -webkit-box;
       display: -moz-box;
       display: box;
    }
    
    .flex-hsc {
    
       /* Flexbox: Principal setup */
    
       -webkit-box-orient: horizontal;
          -moz-box-orient: horizontal;
               box-orient: horizontal;
    
       -webkit-box-pack: start;
          -moz-box-pack: start;
               box-pack: start;
    
       -webkit-box-align: center;
          -moz-box-align: center;
               box-align: center;
    }
    

    为了使图像居中,我使用了弹性盒模型或弹性盒。

    但我认为... 为什么要复杂化?如果您知道图像容器的高度,请使用 line-height

    在开发区存在一个叫做 KIS 的原则。意思是:

    保持简单。

    如果您有解决方案(和一个好的解决方案),请使用它!这样可以避免头痛。

    这是DEMO

    尝试更改演示中li 元素的高度,您将看到图像始终居中。

    干杯, 莱昂纳多

    【讨论】:

    • 奇怪的反应人!我的 css 使用 #id 因为这些样式只有一个!对于具有相同 id 的对象,被用作重复项(ctrl-c,ctrl-v),正如上面已经说过的那样。我最大的问题是 div '
    • ' 内的对齐方式相同。感谢您的宝贵时间。
  • 代码实际上有点乱,我会在一切结束时整理它。但是感谢您的提示!
  • 哦,拜托了!没什么。很高兴为您提供帮助。
  • Man 无意虐待... inside (.ContactList> li: nth-child (1): after {....}) 有一个图片... 需要插入一个css 我有为了圆这个图像的边缘,我尝试执行以下操作: ContactList> li: nth-child (1): after img {} ... 显然它不起作用。跟随小提琴更新:jsfiddle.net/Igaojsfiddle/T6KrE/45。你能告诉我我该怎么做吗?再次感谢
  • 没问题@Igao ASP.NET!但我不是在电脑里,我是在手机里……但我认为你提出了这些规则:contactList > li:nth-child(1):after {border-radius:value; }。如果您稍等片刻,我会打开我的电脑,很乐意为您提供帮助。
  • 【解决方案3】:

    如果您不使用边距,请尝试在 li.frdImgProfile 上使用 line-height

    1. #frdImgProfile 更改为.frdImgProfile 并在您的html 上将id=frdImgProfile 更改为class=frdImgProfile
    2. 删除margin-top 上的.frdImgProfile
    3. line-height: 120px; 添加到#contactList &gt; li
    4. display: inline-block; vertical-align: middle;line-height: normal; 添加到.frdImgProfile

    #contactList > li {
        font-weight: bold;
        color: #fff;
        box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), inset 0 -2px 2px -2px gray;
        background-image: -webkit-linear-gradient(#ededed, #eff0f2);
        background-image: -moz-linear-gradient(#ededed, #eff0f2);
        background-image: -ms-linear-gradient(#ededed, #eff0f2);
        background-image: linear-gradient(#ededed, #eff0f2);
        border-left:10px solid green;
        border-right:1px solid #999999;
        height:120px;
        line-height: 120px;
    }
    
    .frdImgProfile {
        width: 50px;
        height: 50px;
        border: 1px solid #aaa;
        background: #ffe;
        margin-left:4px;
        -webkit-border-radius: 6px;
        -moz-border-radius: 6px;
       display: inline-block;
       vertical-align: middle;
       line-height: normal;
    }
    

    demo on cssdeck

    希望有帮助

    【讨论】:

    • 答案也很好。我们达到了同一个目标!感谢您的时间!拥抱
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签