简体   繁体   English

为什么 display:inline-block 不适用于我的 HTML?

[英]Why doesn't display:inline-block work for my HTML?

The display: inline-block technique to make div elements appear next to each other does not work with my dynamically-generated content cards.使 div 元素彼此相邻display: inline-block技术不适用于我动态生成的内容卡。

My content cards are a modified version of a tutorial found on the w3schools website, which can be found here:我的内容卡片是 w3schools 网站上教程的修改版,可在此处找到:

https://www.w3schools.com/howto/howto_css_cards.asp https://www.w3schools.com/howto/howto_css_cards.asp

Goal目标

I'm in the process of creating a relatively simple search engine for my website based on a query that checks a MySQL database for any potential matches.我正在根据一个查询为我的网站创建一个相对简单的搜索引擎,该查询检查 MySQL 数据库是否有任何潜在匹配项。 The results are returned in the form of a content card.结果以内容卡的形式返回。 If the system finds 3 matches, 3 content cards will be generated in the results.如果系统找到 3 个匹配项,结果中将生成 3 个内容卡片。 The code is being controlled by a for-loop (PHP) that generates a content card for each match found.该代码由 for 循环 (PHP) 控制,该循环为找到的每个匹配项生成内容卡。

Problem问题

The corresponding content cards are generated for each match, however, they appear on separate lines below each other (vertically).为每场比赛生成相应的内容卡,但是,它们出现在彼此下方(垂直)的单独行上。 I attempted to use the display: inline-block technique to force them next to each other with no results.我尝试使用display: inline-block技术强制它们彼此相邻而没有结果。 I suspect the reason why is because the code for each content card must already be there for the effect to take place.我怀疑原因是因为每个内容卡的代码必须已经存在才能产生效果。 If not, CSS & HTML assume that there was only ever one content card and doesn't align them properly.如果没有,CSS 和 HTML 会假设只有一张内容卡并且没有正确对齐它们。

HTML/CSS/PHP Code for Content Cards内容卡的 HTML/CSS/PHP 代码

 .card { box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); max-width: 300px; margin: auto; text-align: center; font-family: arial; width: 30%; } .card button { border: none; outline: 0; padding: 12px; color: white; background-color: #000; text-align: center; cursor: pointer; width: 20%; font-size: 18px; } .card button:hover { opacity: 0.7; } .shrink { -webkit-transform: scale(0.8); -moz-transform: scale(0.8); -ms-transform: scale(0.8); transform: scale(0.8); }
 <!-- Container --> <div class="container" style="background-color: white; width:89%; padding-top: 400px;"> <!-- Generates 1 Content Card for each Match --> <?php for($x = 0; $x < count($title); $x++) { ?> <!-- Content Card Design & Data --> <div class="shrink"> <div class="card" style="background-color: white; border-radius: 2%; display: inline-block;"> <a href="#" data-toggle="modal" data-target="#ModalCarousel<?php echo " $x ";?>" style="text-decoration: none; color: black;"> <img src="listingimages/<?php echo "$firstListingImage[$x]";?>" style="width:100%; border-top-left-radius: 2%; border-top-right-radius: 2%;"> <h4><?php echo "$title[$x]";?></h4> <hr> <p><span class = "glyphicon glyphicon-cutlery"></span> <?php echo "$foodType[$x]";?></p> <hr> <p><span class = "glyphicon glyphicon-map-marker"></span> <?php echo "$city[$x]";?>, <?php echo "$state[$x]";?></p> <hr> <p style="font-size: 30px;"><b>$<?php echo "$price[$x]";?></b><span style="font-size: 15px;"> USD</span></p> </a> </div> </div> <?php } ?> </div>

这很简单,你只需要在类中添加 .card {float:left} 然后它就会如你所愿

  1. With inline-block for it to work you must also set a fixed width on .shrink , which is the repeated holder, and maybe vertical-align使用inline-block使其工作,您还必须在.shrink上设置固定宽度,这是重复的持有人,也许是vertical-align
  2. The preferred way nowadays is by setting display:flex; flex-wrap:wrap现在首选的方式是设置display:flex; flex-wrap:wrap display:flex; flex-wrap:wrap on the container which is made just for this kind of box display. display:flex; flex-wrap:wrap在专为这种盒子展示而制作的容器上。 Also set width on .shrink with this solution.还使用此解决方案在.shrink上设置宽度。

Your .card s are nicely displayed as inline-blocks, but they're each wrapped inside a .shrink which are full blocks.您的.card很好地显示为内联块,但它们每个都包裹在.shrink ,这是完整的块。 That's why they're not lining up as you'd expect.这就是为什么他们没有像你期望的那样排队。

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

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