简体   繁体   English

CSS内联块宽度问题

[英]CSS inline-block width issue

I have a page with a container div, this container div has two columns, both inline-blocks. 我有一个包含容器div的页面,该容器div有两列,均为内联块。 One column (left Hand side (LHS)) is the ticbox selection for a shopping catalogue, the right column is the output of the chosen selection. 一栏(左侧(LHS))是购物目录的ticbox选择,右栏是所选选择的输出。

The issue I have is that each is assigned a width based on percentage of the parent width, the left - fixed column is 20% width, the right, output column is 79% width (I tend to allow 1% for variability) . 我的问题是,根据父级宽度的百分比分配了每个宽度,左固定列为20%宽度,右输出列为79%宽度(我倾向于允许1%的可变性)。 BUT: the left column needs a minimum width - defined in px as 155px; 但是:左列需要最小宽度-以px为155px定义; The right hand side (RHS) column is filled with inline-blocks for each product displayed by the catalogue search. 右侧(RHS)列填充有目录搜索所显示的每种产品的内联块。 These blocks are fixed width (140px) 这些块是固定宽度(140像素)

MY ISSUE: When the page loads on my screen it's fine, but when: 我的问题:页面加载到我的屏幕上就可以了,但是在以下情况下:

LHS: min-width:155px < width:20% LHS:最小宽度:155像素<宽度:20%

(the browser window is resized) (已调整浏览器窗口的大小)

The whole of the right hand side drops below the content of the left hand side (as the width for it is less than the required 79%. 整个右侧下降到左侧含量以下(因为其宽度小于所需的79%)。

Some simple example: Please note there is no borders or paddings to be considered when measuring widths. 一些简单的示例:请注意,在测量宽度时,不考虑边框或填充。

HTML: HTML:

<div id="container">
    <div class="leftsideMenu">Menu column.</div>
    <div class="rightsideShop">Shop Contents</div>
</div> 

CSS: CSS:

#container{
    width:80% /* of screen */
    min-width:555px; /*should leave 400px for shop contents */
}

.leftsideMenu {
    display:inline-block;
    width:20%;
    min-width:155px;
    vertical-align:top;
}

.rightsideShop{
    display:inline-block;
    width:79%;
    max-width:calc(100% - 156px) !important; /* fix attempt - doesn't appear to work */
    vertical-align:top;
}

[Some] ATTEMPTED FIXES (not in order of attempt): [某些]尝试修复(不按尝试顺序):

1) Calc to make the max-width always less than 100%-155, doesn't appear to work 1)计算以使最大宽度始终小于100%-155,似乎不起作用

2) Floats and margins : this does work but presents the problem of layout that the client doesn't want shoes underneath the LHS column and float height = 100% parent is another issue, 2)浮动和边距:这确实有效,但存在布局问题,即客户不希望在LHS列下方穿鞋,而浮动高度= 100%父级是另一个问题,

3) I have also tried to use https://stackoverflow.com/a/6350781/3536236 answer to a similar question - with the approach of having the RHS relative and using a forced LHS margin but this doesn't work as the solution linked here didn't work for me in this situation. 3)我也尝试过使用https://stackoverflow.com/a/6350781/3536236来回答类似的问题-具有相对RHS和使用强制LHS余量的方法,但这不能作为解决方案在这种情况下,链接到此处对我不起作用。

4) I think flex-box style working is probably a best way forward but I do not know enough about it, and to be brutally honest, I was hoping to avoid a massive reworking of the page CSS. 4)我认为flex-box风格的工作可能是最好的方法,但是我对此还不够了解,老实说,我希望避免对页面CSS的大量修改。 (I had originally expected this issue to be 30minutes!). (我原本希望这个问题是30分钟!)。

5) Setting no width (just max-width) for RHS - to auto defined width, this auto defines to 100% and goes underneath the left hand side column. 5)为RHS设置没有宽度(只是最大宽度)-为自动定义的宽度,该宽度自动定义为100%,位于左侧栏的下方。

I think the answer is pretty simple but I can't see it :(. 我认为答案很简单,但我看不到:(。

To explain parts for the points above, the LHS was originally a float and that worked fine but the client wanted no products appearing underneath the menu in the LHS column, so I thought - ah simple, make it an inline Block.... 为了解释以上几点,LHS最初是一个浮动控件,可以正常工作,但是客户不希望在LHS列的菜单下方显示任何产品,所以我想-啊,很简单,将其设置为内联块。

Any help as to keeping the right hand side giving the left hand side the space it needs, even upon screen resizing? 关于保持右手边给左手边所需的空间,即使在调整屏幕大小时,是否有任何帮助?

............ ............

OH FOR FFFFFFFF SAKE -- I have just written this all out, and while I've been writing this have been trying different ideas as they've occured and it's finally worked: OH FFFFFFFF SAKE-我刚刚将所有内容全部写下来,而在我撰写本文时,他们一直在尝试不同的想法,因为它们已经出现,并且最终成功了:

Now after all this effort for writing this out I want to post it anyway, but the Solution is below!!! 经过所有这些努力,我还是想将其发布,但是解决方案在下面!!!

OH FOR FFFFFFFF SAKE -- I have just written this all out, and while I've been writing this have been trying different ideas as they've occured and it's finally worked: OH FFFFFFFF SAKE-我刚刚将所有内容全部写下来,而在我撰写本文时,他们一直在尝试不同的想法,因为它们已经出现,并且最终成功了:

For some reason my calc works with: 由于某些原因,我的calc适用于:

 max-width:calc(100% - 160px); Giving a spare space of 5px . 

Any ideas why this is so, as I say, within the container div standard widths are percentages and there is some padding in the product container (inline-blocks) inside the RHS, but this really shouldn't have influenced having to add more "padding" space in the calc method. 正如我说的,为什么这样,在容器div中,标准宽度都是百分比,并且RHS内的产品容器(内联块)有一些填充,但这真的不应该影响添加更多的“ calc方法中的“ padding”空格。

Anyway, it works now. 无论如何,现在就可以使用。 I'm happy. 我很高兴。 Maybe this will help someone? 也许这会帮助某人?

With inline-block you will have some whitespace taking up some space in your layout. 使用内联块,您将有一些空格占用布局中的一些空间。

Two inline-block divs with a width of 400px in a container div of 800px won't always render next to each other. 在800像素的容器div中,宽度为400像素的两个内联块div不会始终彼此相邻渲染。

You can fix this by making sure the closing tag of an element is directly next to the opening tag of the next element in the HTML (eg </div><div> , no newlines or spaces). 您可以通过确保元素的结束标记紧邻HTML中下一个元素的开始标记(例如</div><div> ,不包含换行符或空格)来解决此问题。

A better option is to apply font-size: 0 to the containing element and then reset the font-size to eg 1rem for the inline-block elements. 更好的选择是将font-size: 0应用于包含的元素,然后将inline-block元素的font-size重置为例如1rem

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

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