[英]CSS displaying is not right
Below is a preview (made in Photoshop) of how my list must look like: 以下是我的列表外观的预览(在Photoshop中制作):
Below is the CSS code that I have: 以下是我拥有的CSS代码:
.newsFeed {
float: left;
width: 728px;
margin: 0;
padding: 0;
list-style: none;
}
.newsFeed li {
width: 233px;
height: 244px;
padding: 5px;
border-right: 10px solid #dddddd;
margin: 0;
float: left;
}
.newsFeed li img.image {
width: 226px;
height: 157px;
margin: 0 0 5px 0;
}
If you calculate the margin resulted from 3 list elements 10px*3 = 30px
you will know that instead of 3 items per line, I only get two items. 如果您计算3个列表元素
10px*3 = 30px
产生的边距,您将知道,每行不是3个项目,而是2个项目。 How can I remove the border of the last element in a line? 如何删除一行中最后一个元素的边框?
(instead of this)
+-----------+
| 1 | 2 | 3 |
|---+---+---|
| 4 | 5 | 6 |
+-----------|
(I get this)
+-----------+
| 1 | 2 | |
|---+---+ |
| 3 | 4 | |
|---+---+ |
| 5 | 6 | |
+-----------+
I have tried to apply a negative margin/padding to the .newsFeed
rule: 我试图对
.newsFeed
规则应用负边距/填充:
.newsFeed {
// ... code
// margin-right: -10px;
}
Obviously, this didn't work. 显然,这没有用。 Can anybody help me to solve this?
有人可以帮我解决这个问题吗?
Merry Christmas! 圣诞节快乐!
total pixel for the maincontainer .newsFeed is 728px
...........................................................
less:.newsFeed li width: 233px;
padding: 5px *2 =padding: 10px; right and left
border-right: 10px
................................................................
total pixel occupied by one div is 253px
total pixel occupied by three divs are 759px
so this reason it pushes the third div into the next row
Padding:5px also adds to the width (so 3*2*5 = 30px ) 填充:5px也增加了宽度(因此3 * 2 * 5 = 30px)
.newsFeed li {
width: 226px;
height: 244px;
padding: 5px;
border-right: 10px solid #dddddd;
margin: 0;
float: left;
}
.newsFeed li.last-item {
border-right: none;
}
Html HTML
<ul>
<li></li>
<li></li>
<li class="last-item"></li>
<li></li>
<li></li>
<li class="last-item"></li>
</ul>
My suggestions is that instead of using fixed sized li
elements, you should use a percentage based size in conjunction with box-sizing: border-box
我的建议是,不要使用固定大小的
li
元素,而应将基于百分比的大小与box-sizing: border-box
大小结合使用box-sizing: border-box
So a working code would be: 因此,一个有效的代码将是:
li {
box-sizing: border-box;
-moz-box-sizing: border-box;
width: 33%;
}
Explanation: 说明:
You are effectively giving a width of: 您有效地给出了以下宽度:
233 - The 'inner' width
+2*5 - Two times the padding
+10 - The left border
=253 - The effective width
253*3 = 759 pixels
But you are only giving a pixel size of 728. Of course you could set the inner width to 759 pixels but a relative size with box-sizing
would also allow a resizing of the list without having to change the individual values. 但是,您只能提供728的像素大小。当然,您可以将内部宽度设置为759像素,但是具有
box-sizing
的相对大小也可以在不更改单个值的情况下调整列表的大小。
Conclusion Use a relative width with box-sizing
or set the list width to 759. 结论使用具有
box-sizing
的相对宽度或将列表宽度设置为759。
PS: Please do not use another class as it just pollutes the css classes. PS:请不要使用其他类,因为它只会污染CSS类。
Edit 编辑
Oops, here also have a plunker showing a solution. 糟糕,这里也有一个显示解决方案的插件。
An idea came to me: 我想到一个主意:
I can use the nth-child()
property with the 3n
attribute, so that I will only have 3, 6, 9, 12... 我可以将
nth-child()
属性与3n
属性一起使用,这样我将只有3n
...
.newsFeed li:nth-child(3n) {
border-right: none;
}
And this works. 这可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.