简体   繁体   English

CSS3子元素宽度不尊重父容器宽度

[英]CSS3 Child Elements Width Not Respecting Parent Container Width

I am trying to keep my ul and li elements that are inside of a div to respect the div's width with margin-left: auto. 我试图保持我在div内部的ul和li元素以margin-left:auto来尊重div的宽度。 I am also not wanting ul/li to extend out of the div. 我也不想让ul / li延伸出div。 I want the ul and li elements to stay inside the div container. 我希望ul和li元素保留在div容器中。 From what I have read, this code is correct because I have the child elements contain a width which should be according to div's width. 从我读过的,这段代码是正确的,因为我有子元素包含宽度应该根据div的宽度。

#phonequeue {
    display: block;
    width: 400px;
    height: 800px;
    border-width: 5px;
    border-color: white;
    border-style: solid;
    float: right;
    margin-top: -650px;
}

li {
    display: block;
    padding: 15px;
    text-align: left;
    height: 1.2 em
    padding-left: 15px;
    padding-right: 15px;
    margin: 0px 0px 4px 0px;
    background-color #555
    -webkit-border-radius: 15px;
    border-radius: 40px;
    background: -webkit-gradient(linear, 0% 0%, 0% 30%, from(#444), to(#111));
    font-family: verdana, arial, helvetica, sans-serif;
    font-size: 22px;
    width: 100%;
    margin: auto;

}

ul {
    list-style-type: none;
    width: 100%;
    margin: auto;
}

index.html: index.html的:

    <div id="phonequeue"> 
    <ul>
        <li class="avail">
        <span class="pname">John Doe</span>
        <span class="ptime">03:00</span>
        </li>
    </ul>
    </div>

UPDATE: Here is a working example. 更新:这是一个工作示例。 The element width is 430px; 元素宽度为430px; when the parent div is 400px;. 当父div为400px时; I would the child elements to center themselves in div and have their width respecting the parents width. 我希望子元素以div为中心,宽度与父元素宽度相符。 http://jsfiddle.net/FBvQJ/4/ http://jsfiddle.net/FBvQJ/4/

I believe the problem was with the use of width: 100% 我认为问题在于使用宽度:100%

Check this jsFiddle and let me know if this answers your question. 检查这个jsFiddle ,让我知道这是否回答了你的问题。

This is the updated CSS to solve the overlapping width: 这是用于解决重叠宽度的更新CSS:

phonequeue {
    display: block;
    width: 200px;
    border: 1pt solid green;
}
ul{
    border: 1pt solid red;
    list-style-type: none;
    margin: auto;
    padding: 0;
}
li {
    display: block;
    padding: 15px;
    text-align: left;
    height: 1.2em;
    margin: 0px 0px 4px 0px;
    -webkit-border-radius: 15px;
    font-size: 22px;
    margin: auto;
    border: 1pt solid #ccc;
}

I don't think you need to define the full width for each element. 我认为您不需要为每个元素定义整个宽度。

If you'd like the text centered in the DIV, remove the width on the LI tag and make the margin: 如果您希望文本居中于DIV,请删除LI标签上的宽度并设置边距:

margin: 0px auto 4px auto;

That will center the text within the div. 这将使文本在div中居中。

If I'm understanding your question correctly (which is dubious) I believe you can achieve the centering you require by removing the 100% widths and using text-align:center: 如果我正确理解你的问题(这是可疑的),我相信你可以通过删除100%宽度并使用text-align:center来实现所需的居中:

li {
    display: block;
    padding: 15px;
    text-align: left;
    height: 1.2 em;
    padding-left: 15px;
    padding-right: 15px;
    margin: 0px auto 4px auto;
    background-color #555;
    -webkit-border-radius: 15px;
    border-radius: 40px;
    background: -webkit-gradient(linear, 0% 0%, 0% 30%, from(#444), to(#111));
    font-family: verdana, arial, helvetica, sans-serif;
    font-size: 22px;
    text-align:center; /* < this (or not, if you don't actually want it centred after all) */
}

ul {
    list-style-type: none;
    padding:0; /* < and this */
    margin: 0;
}

Demo fiddle 演示小提琴

And later, when you decide that you want the time on the right use 后来,当你决定要把时间用在正确的用途上时

.ptime {float:right;} 

example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM