简体   繁体   English

还有另一个“为什么我的div不会浮动”(在jquery-mobile中)?

[英]Yet another “why won't my divs float” (in jquery-mobile)?

I'm trying to make some html code mobile-first, so I've stuck an @media to test for "desktop" widths (arbitrarily set to 45em in my sass at the moment). 我正在尝试使一些html代码成为移动优先,因此我使用@media来测试“桌面”宽度(此刻在我的Sass中任意设置为45em)。 The idea for the two divs ( #picture-block and #question-block ) in question is: 所讨论的两个div( #picture-block#question-block )的想法是:

$desktopwidth: 45em
#question-block, #picture-block
  width: 100%
@media (min-width: $desktopwidth)
  #question-block, #picture-block
    width: 50%
    float: left

Despite clicking around in my code inspector, I can only get my two divs in question to float left side-by-side when I kill both width properties (50% and 100%). 尽管在代码检查器中单击,但在杀死两个width属性(50%和100%)时,我只能使有问题的两个div并排浮动。 I'm confused by this, because I thought display: block would have been the default, and I don't understand why the width property would affect the float . 我对此感到困惑,因为我认为display: block将是默认值,并且我不明白为什么width属性会影响float I'm using the jquery-mobile framework, but I can't see how it would be interfering in the code inspector. 我正在使用jquery-mobile框架,但是我看不到它将如何干扰代码检查器。

I've created a codepen link to try to replicate the problem. 我创建了一个Codepen链接来尝试复制该问题。 My question: what do I need to do to the css to get the picture-block and question-block divs to float next to each other for desktop widths? 我的问题:对于桌面宽度,我需要对CSS进行什么操作才能使picture-blockquestion-block div彼此浮动?

In the codepen, you also have a 1px border around the blocks. 在Codepen中,块周围也有1px的边框。 This causes the total width of each to be 50% plus 2 pixels. 这将导致每个像素的总宽度为50%加上2个像素。 So two of them won't fit side by side in a 100% container! 因此,其中两个不能并排放置在100%的容器中!

Possible solutions: 可能的解决方案:

  • either give them box-sizing:border-box to include the border width in the width 要么给他们box-sizing:border-box在宽度中包括边框宽度
  • or, set the width as calc(50% - 2px) 或者,将宽度设置为calc(50% - 2px)

(but not both.) (但不能两者都选)。

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

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