简体   繁体   English

如何忽略CSS中的滚动条宽度?

[英]How to ignore a scrollbar width in CSS?

Explanation 说明

在此输入图像描述

I want to line up three boxes in one line horizontally but the last one falls down. 我想在一条线上水平排列三个盒子,但最后一个盒子掉下来。

在此输入图像描述

When I remove the scrollbar, it lines up fine. 当我删除滚动条时,它排成一行。

So, the problem is caused by the scrollbar width. 因此,问题是由滚动条宽度引起的。

How can I ignore the scrollbar width in css? 如何忽略css中的滚动条宽度?

Do I need to write JavaScript code to calculate the scrollbar width and adjust the width of the wrapper dom element? 我是否需要编写JavaScript代码来计算滚动条宽度并调整包装器dom元素的宽度?

DEMO & CODE 演示和代码

I posted html and css code in codePen.io. 我在codePen.io中发布了html和css代码。

http://codepen.io/anon/pen/kXAPap http://codepen.io/anon/pen/kXAPap

HTML HTML

<div id="main">

    <ul id="window-list">

        <li class="window">
            <div class="window-thumbnail">
            </div>
        </li><!--

        --><li class="window">
            <div class="window-thumbnail">
            </div>
        </li><!--

        --><li class="window">
            <div class="window-thumbnail">
            </div>
        </li><!--

        --><li class="window">
            <div class="window-thumbnail">
            </div>
        </li><!--

        --><li class="window">
            <div class="window-thumbnail">
            </div>
        </li>


    </ul>

</div>

CSS CSS

body {
    border-top: solid 1px #a3a1a3;
    margin: 0;
    padding: 20px;
    background-color: #e7e7e7;
}

ul, li, p {

    margin: 0;
    padding: 0;

}

/*
1px(border-left) + 30px(padding-left) + 158px(width) + 18px(margin-right) + 158px(width) + 18px(margin-right) + 158px(width) + 30px(padding-right) + 1px(border-right) = 572px
*/

#main {
    width: 572px;
}


#window-list {

    background-color: #ffffff;
    border: solid 1px #b8b8b8;
    width: 510px;
    height: 350px;
    margin: 0 0 8px 0;
    overflow: scroll; /*This line causes the problem*/
    padding: 10px 30px;
    list-style-type: none;


}

.window {
    display: inline-block;
    margin: 0 18px 18px 0;


}

.window:nth-child(3n) {

    margin-right: 0;

}

.window-thumbnail {

    margin: 0 0 8px 0;
    height: 158px;
    width: 158px;
    border-radius: 8px;
    cursor: pointer;
    background-color: #e0e0e0;
}

One way to do it in pure CSS is to make the scroll bar invisible completely. 在纯CSS中执行此操作的一种方法是使滚动条完全不可见。 This will still allow scrolling. 这仍然允许滚动。

::-webkit-scrollbar { 
display: none; 
}

You can also use jQuery to achieve exactly what you wanted: 您还可以使用jQuery来实现您想要的内容:

$(document).ready(function(){
    var body = $('body');
    var normalwidth = 0;
    var scrollwidth = 0;
    if(body.prop('scrollHeight')>body.height()){
        normalwidth = window.innerWidth;
        scrollwidth = normalwidth - body.width();
        $('#main').css({marginRight:'-'+scrollwidth+'px'});
    }
});

And you can hide the horizontal scroll using: 您可以使用以下方法隐藏水平滚动:

body {
    overflow-x:hidden;
}

Here's a jsfiddle made by Lwym . 这是Lwym制作的jsfiddle
I suggest you check out his original answer too. 我建议你查看他原来的答案

Unless it is a business requirement to give these boxes a fixed, I would strongly recommend you to give them a variable width using percentage units which will make your document more responsive to different viewport widths and screen sizes. 除非这些盒子是固定的商业要求,否则我强烈建议您使用百分比单位给它们一个可变宽度,这将使您的文档对不同的视口宽度和屏幕尺寸更具响应性。

 .container { display: block; width: 100%; padding: 0; margin: 0; } .container .box { display: inline-block; background-color: #ddd; width: 28%; height: 80px; margin: 20px 0 20px 4%; border: 1px solid #ddd; border-radius: 3px; overflow: hidden; } 
 <div class="container"> <div class="box"></div><!-- --><div class="box"></div><!-- --><div class="box"></div><!-- --><div class="box"></div><!-- --><div class="box"></div> </div> 

Things to note 注意事项

1- This solution is 99% CSS2-compatible...some features will degrade gracefully such as the border-radius which is currently supported by all major browsers out there 1-此解决方案与99%CSS2兼容......某些功能会优雅地降级,例如当前所有主流浏览器都支持的border-radius

2- you said it doesn't need to be responsive because it's a chrome extension. 2-你说它不需要响应,因为它是一个chrome扩展。 Well, it does because desktops come in a lot of screen sizes. 嗯,这是因为桌面有很多屏幕尺寸。 So, it is responsive 所以,它是响应

3- I've given the boxes a fixed height to make them look uniform but if you want a variable height you can wrap the boxes in containers with a group of 3 boxes each 3-我给了盒子一个固定的高度,使它们看起来很均匀但如果你想要一个可变的高度,你可以将盒子装在一个容器中,每个盒子有3个盒子

If these have to be fixed, I would highly recommend using flex for this. 如果必须修复这些,我强烈建议使用flex。 Here is your CSS using Flex: 这是使用Flex的CSS:

body {
    border-top: solid 1px #a3a1a3;
    margin: 0;
    padding: 20px;
    background-color: #e7e7e7;
}

ul, li, p {

    margin: 0;
    padding: 0;

}

/*
1px(border-left) + 30px(padding-left) + 158px(width) + 18px(margin-right) + 158px(width) + 18px(margin-right) + 158px(width) + 30px(padding-right) + 1px(border-right) = 572px
*/

#main {
    width: 572px;
}


#window-list {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    background-color: #ffffff;
    border: solid 1px #b8b8b8;
    width: 510px;
    height: 350px;
    margin: 0 0 8px 0;
    overflow: scroll; /*This line causes the problem*/
    padding: 10px 30px;
    list-style-type: none;


}

.window {
    margin: 0 18px 18px 0;


}

.window:nth-child(3n) {

    margin-right: 0;

}

.window-thumbnail {

    margin: 0 -6px 8px 0;
    height: 158px;
    width: 158px;
    border-radius: 8px;
    cursor: pointer;
    background-color: #e0e0e0;
}

Here is a working example: http://codepen.io/jsanatar/pen/xOyLWK 这是一个工作示例: http//codepen.io/jsanatar/pen/xOyLWK

Here, http://codepen.io/bhshawon/pen/OXBjOg 在这里, http://codepen.io/bhshawon/pen/OXBjOg

Using negative value for margin-right of every 3rd child 对每个第3个孩子的保证金权利使用负值

.window:nth-child(3n) {

    margin-right: -16px;

}

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

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