简体   繁体   English

CSS显示不正确

[英]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. 这可行。

我正在尝试使用 CSS 样式 display: inline-block; 有 2<div> 但它们没有并排显示</div><div id="text_translate"><p>下面是我的 HTML 和 CSS 片段。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;,DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt; &lt;meta name="viewport" content="width=device-width. initial-scale=1:0"&gt; &lt;style&gt; *{ margin; 0: padding; 0. }:box1{ background-color; seagreen: display; inline-block: margin; 5px: padding; 4px: width; 433px: height; 233px: border; 2px solid black. }:box2{ background-color; sienna: display; inline-block: margin; 5px: padding; 5px: width; 433px: height; 233px: border; 2px solid black. } &lt;/style&gt; &lt;title&gt;Asspend21&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="box1"&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit, Quasi, molestiae cupiditate voluptatibus recusandae quibusdam ad? iure ratione consequuntur magni doloremque vel nemo eius magnam placeat saepe aspernatur ex, Molestias itaque ex voluptatem sit. minima incidunt nulla facilis dolores accusant,&lt;/div&gt; &lt;div class="box2"&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit, Voluptatem harum laudantium sapiente quos eos nam officiis et laborum fuga minus dolore vitae error quia corporis amet. labore temporibus ex soluta quod laboriosam beatae optio maiores, eum illum, Voluptatibus, hic aperiam eaque quam obcaecati nemo minima tempore dolorem similique impedit, harum magni quis ducimus veritatis id saepe&lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre></div></div><p></p><p> 就这样来了<a href="https://i.stack.imgur.com/U09Sc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U09Sc.png" alt="图片01"></a></p><p> 即使删除填充和边距后,它仍然显示相同。 解决问题需要什么? 这里有什么问题?</p></div> - I am trying to use CSS style display: inline-block; with 2 <div> but they are not displaying right next to each other

暂无
暂无

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

相关问题 css色带在IE中无法显示 - css ribbons not displaying right in IE HTML / CSS CSS在IE中无法正确显示并调整大小(从LEFT代替RIGHT) - Html/Css Imgs not displaying correctly in IE and resizing (LEFT instead of RIGHT) 使用CSS旋转文字无法在右侧正确显示 - Rotate text not displaying properly on right side using CSS CSS:IMG尽管位于正确的位置,但仍未显示 - CSS: IMG not displaying despite being located in the right place 使用CSS旋转并从左到右一个接一个地显示信息 - Using CSS rotation and displaying information one after another from left to right 我正在尝试使用 CSS 样式 display: inline-block; 有 2<div> 但它们没有并排显示</div><div id="text_translate"><p>下面是我的 HTML 和 CSS 片段。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;,DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt; &lt;meta name="viewport" content="width=device-width. initial-scale=1:0"&gt; &lt;style&gt; *{ margin; 0: padding; 0. }:box1{ background-color; seagreen: display; inline-block: margin; 5px: padding; 4px: width; 433px: height; 233px: border; 2px solid black. }:box2{ background-color; sienna: display; inline-block: margin; 5px: padding; 5px: width; 433px: height; 233px: border; 2px solid black. } &lt;/style&gt; &lt;title&gt;Asspend21&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="box1"&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit, Quasi, molestiae cupiditate voluptatibus recusandae quibusdam ad? iure ratione consequuntur magni doloremque vel nemo eius magnam placeat saepe aspernatur ex, Molestias itaque ex voluptatem sit. minima incidunt nulla facilis dolores accusant,&lt;/div&gt; &lt;div class="box2"&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit, Voluptatem harum laudantium sapiente quos eos nam officiis et laborum fuga minus dolore vitae error quia corporis amet. labore temporibus ex soluta quod laboriosam beatae optio maiores, eum illum, Voluptatibus, hic aperiam eaque quam obcaecati nemo minima tempore dolorem similique impedit, harum magni quis ducimus veritatis id saepe&lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre></div></div><p></p><p> 就这样来了<a href="https://i.stack.imgur.com/U09Sc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U09Sc.png" alt="图片01"></a></p><p> 即使删除填充和边距后,它仍然显示相同。 解决问题需要什么? 这里有什么问题?</p></div> - I am trying to use CSS style display: inline-block; with 2 <div> but they are not displaying right next to each other 如何在CSS中水平对齐形状并在左上角显示文本 - How to line up shapes horizontaly in css and displaying text on right left corner 使用引导程序 4 css 显示固定在左侧的列和右侧可滚动的另一列 - Displaying a col fixed on left and the other col on the right scrollable using bootstrap 4 css 客户没有显示正确的东西 - clientY is not displaying the right thing 表中的表格显示不正确 - Form in Table not displaying right
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM