简体   繁体   English

CSS项目无法正确呈现

[英]CSS items not rendering properly

I want to create a sortable list in which sorting features will be in left column and right column will contain the list items (the items are blocks with some information) 我想创建一个可排序的列表,其中的排序功能将在左列中,而右列将包含列表项(这些项是具有某些信息的块)

I have written following code : 我写了以下代码:

HTML : HTML:

<div id="container" style="width:auto;">
        <div id="item-menu">
        Sort items by :  </div>
        <div id="content-items">
            <div id="item"> content</div>
            <div id="item"> content</div>
        </div>
</div>

CSS: CSS:

#item-menu {
    width:15%;
    float:left;
    border-right:1px dotted #333333;
}

#content-itemss {
    float:left;
    padding-left:20px;
    width:85%;
}

#item {
    display:block;
    background:#333;
    border:1px dotted #000000;
    color:#FFF;
    width:inherit;
    margin:10px 10px;
    padding:10px 10px;
}

However, the list items somehow end up messing with the layout. 但是,列表项最终以某种方式弄乱了布局。 Here's the screenshot.. 这是屏幕截图。 物品没有放在正确的位置

How to correct this? 如何纠正呢? Also, is there any easy way like some WYSIWYG editor for such CSS designs? 另外,是否有任何简单的方法,例如用于此类CSS设计的所见即所得(WYSIWYG)编辑器?

To achieve the layout you wanted, I changed the box-model to border-box which means that padding given to an element becomes part of it's width. 为了获得所需的布局,我将box-model更改为border-box ,这意味着赋予元素的填充成为其宽度的一部分。 ( http://css-tricks.com/the-css-box-model/ ) http://css-tricks.com/the-css-box-model/

I added a clearfix class to parent elements that hold floated children, because this causes the parent to collapse. 我向持有浮动子代的父代元素添加了一个clearfix类,因为这会导致父代崩溃。 ( http://css-tricks.com/all-about-floats/ ) http://css-tricks.com/all-about-floats/

fiddle: http://jsfiddle.net/dHc8W/ 小提琴: http//jsfiddle.net/dHc8W/

Code: 码:

* { box-sizing: border-box; }

.clear:after {
    content: "";
    display: table;
    clear: both;
}
#container {
    width: 100%;
}

#item-menu {
    width:15%;
    float:left;
    border-right:1px dotted #333333;
}

#content-items {
    float:left;
    padding-left:20px;
    width:85%;
}

.item {
    background:#333;
    border:1px dotted #000000;
    color:#FFF;
    width:inherit;
    margin:10px 10px;
    padding:10px 10px;
}

These may or may not work depending on how you want the 'content' to display. 根据您希望“内容”的显示方式,这些选项可能会起作用,也可能不会起作用。
But as far as I can tell with the given code they give the desired result. 但据我所提供的代码所知,它们给出了预期的结果。
You'll probably want to adjust the padding: and margin: if you want it flush with the top. 您可能需要调整padding:margin:如果您希望它与顶部齐平。

Solution #1 解决方案1

This method will behave strangely if the actual content is greater than the max-width: . 如果实际内容大于max-width: :,则此方法的行为会很奇怪。
Works with smaller content. 使用较小的内容。

CSS 的CSS

#content-items {
float:left;
padding-left:20px;
max-width:85%;   /*changed to 'max-width'*/
}

http://jsfiddle.net/sP6t2/ http://jsfiddle.net/sP6t2/

Solution #2 (suggested method) 解决方案2 (建议的方法)

if you want the content to always be longer, decreasing to width:84% will bump up the content. 如果您希望内容总是更长,则减小到width:84%会使内容增加。

http://jsfiddle.net/sP6t2/1/ http://jsfiddle.net/sP6t2/1/

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

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