简体   繁体   English

div布局的CSS样式

[英]CSS Styling for div layout

Please see my HTML structure below - I am trying to have the .prod divs to be to the right of the logo, and the logo to be the full height of the .row div 请在下面查看我的HTML结构-我正在尝试将.prod divs设置在徽标的右侧,并将徽标设置为.row div的全高。

I know this can be done using tables and floats but I want to try and avoid using those. 我知道可以使用tablesfloats完成此操作,但我想尝试避免使用它们。

Here's my structure: 这是我的结构:

 .row { width: 100%; } .row > div { display: inline-block; } .row .image { height: 100%; width: 24%; } .row .prod { width: 75%; height: auto; } .prod > div { display: inline-block; width: Calc(50% - 4px); } div { border: solid 1px black; } 
 <div class="row"> <div class="image"> <img alt="Full Height Logo" src="" /> </div> <div class="prod"> <div class="prod_image"> <img alt="Product Image" src="" /> </div> <div class="prod_info"> Prod Info </div> </div> <div class="prod"> <div class="prod_image"> <img alt="Product Image" src="" /> </div> <div class="prod_info"> Prod Info </div> </div> </div> 

Wrap the additional info inside another div and give it the remaining width. 将其他信息包装在另一个div中,并为其提供剩余的宽度。

I have given it 74% because of the extra space from inline-block elements. 我给它74%是因为内联块元素有额外的空间。 Adjust it to your requirement. 根据您的要求进行调整。 I would prefer flexbox if you are implementing it for modern browsers. 如果您要为现代浏览器实现flexbox则我更希望使用flexbox

 .row { width: 100%; } .row > div { display: inline-block; } .row .image { height: 100%; width: 24%; vertical-align: top; /* Default to baseline, align to the top */ } .row .product_info { width: 74%; /* Remaining width */ } .row .product_info .prod { /* width: 75% */ Remove height: auto; } .prod > div { display: inline-block; width: Calc(50% - 4px); } div { border: solid 1px black; } 
 <div class="row"> <div class="image"> <img alt="Full Height Logo" src="" /> </div> <div class="product_info"> <div class="prod"> <div class="prod_image"> <img alt="Product Image" src="" /> </div> <div class="prod_info"> Prod Info </div> </div> <div class="prod"> <div class="prod_image"> <img alt="Product Image" src="" /> </div> <div class="prod_info"> Prod Info </div> </div> </div> </div> 

flexbox can do that. flexbox可以做到这一点。

 .row { display: flex; } .image, .prod { flex: 1; background: lightblue; text-align: center; height: 75px; } .image { flex: 0 0 auto; background: orange; display: flex; flex-direction: column; } .image img { height: 100%; width: auto; } 
 <div class="row"> <div class="image"> <img alt="Full Height Logo" src="http://lorempixel.com/output/food-qc-50-50-1.jpg" /> </div> <div class="prod"> <div class="prod_image"> <img alt="Product Image" src="" /> </div> <div class="prod_info"> Prod Info </div> </div> <div class="prod"> <div class="prod_image"> <img alt="Product Image" src="" /> </div> <div class="prod_info"> Prod Info </div> </div> </div> 

Flexbox? Flexbox的?

I'm still pretty new to flexbox myself, but you could use the align-items: stretch property on your container to cause your items to stretch, but give the .prod div's a max-height so that they only stretch so much. 我本人对flexbox还是很陌生,但是您可以在容器上使用align-items: stretch属性来使您的项目拉伸,但是给.prod div一个max-height,以便它们只能拉伸那么多。 You can also set various widths properties using the flex property on your flex items so that you get the width your after. 您还可以在flex项目上使用flex属性来设置各种widths属性,以便获得后宽度。

For more information: https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 有关更多信息: https : //css-tricks.com/snippets/css/a-guide-to-flexbox/

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

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