简体   繁体   English

css表单格布局中的圆角?

[英]Rounded corners in a css table-cell layout?

I need some help with my design. 我的设计需要一些帮助。 I want to display three equal-height boxes next to each other, like this ASCI art: 我想要显示三个相等高度的盒子,就像这个ASCI艺术:

+------+ +------+ +------+
|      | |      | |      |
|      | |      | |      |
|      | |      | |      |
+------+ +------+ +------+

I also have an example online (with all the CSS) . 我也有一个在线示例(使用所有CSS)

The contents of the boxes varies in height. 盒子的内容高度不同。 The tricky thing is that these boxes also need to have rounded corners. 棘手的是这些盒子也需要圆角。 For that I am using the "sliding doors" technique. 为此,我使用“推拉门”技术。 Basically, the markup of a box is something like this: 基本上,盒子的标记是这样的:

<div class="box">
  <div class="box-header">
    <h4>header</h4>
  </div>
  <div class="box-body">
    <p>Contents</p>
  </div>
</div>

Four block elements so I can make rounded corners and borders with background images. 四个块元素,所以我可以使用背景图像制作圆角和边框。 The top-right corner goes on the h4. 右上角是h4。 The top left corner goes on the box-header. 左上角位于框标题上。 The bottom-right corner goes on the outer box div and the bottom-left corner goes on the box-body. 右下角在外框div上,左下角在盒体上。

I am using CSS display:table-cell to make all three boxes of equal height, but here my problem starts. 我正在使用CSS显示:table-cell使所有三个盒子的高度相等,但这里我的问题开始了。 All the box elements are now of equal height, but the box-body elements are not of equal height because the contents are not of equal height. 现在所有的盒子元素都具有相同的高度,但是盒体元素的高度不同,因为内容的高度不同。 Result: The bottom-right corners are not in the correct position. 结果:右下角未处于正确位置。 See also the link I posted. 另见我发布的链接。

How can I fix that? 我该如何解决这个问题? All the box divs are equal in height now. 现在所有盒子div的高度相等。 I would like box-body to expand to use all available height, even when the content is short. 我希望盒体扩展到使用所有可用的高度,即使内容很短。 I tried height:100% but that doesn't work. 我试过身高:100%,但这不起作用。 How can I make that work? 我怎样才能做到这一点?

Or is there an alternative way to achieve what I want? 还是有另一种方法来实现我想要的东西? I cannot use something like faux-columns because I define the width of the boxes in ems. 我不能使用像faux这样的东西,因为我在ems中定义了框的宽度。 That means I cannot give the box div a single background image that provides both bottom corners. 这意味着我不能给盒子div提供一个提供两个底角的背景图像。

Google is being absolutely useless here. 谷歌在这里绝对没用。 Any query involving "corners" and "table" just gives me a gazillion links to 1990's tutorials that use a 3x3 table for rounded corners. 任何涉及“角落”和“桌子”的查询都只是给了我一个与1990年教程相关的大量链接,这些教程使用3x3桌子作圆角。

As for browser compatibility, it would be nice if IE7/8 can deal with it too but it's not required (I can replace with unequal-height floats in that case). 至于浏览器兼容性,如果IE7 / 8也可以处理它会很好但是不需要(在这种情况下我可以替换为不等高度的浮点数)。 For the website I am developing I estimate IE market share to be 20% or less. 对于我正在开发的网站,我估计IE市场份额为20%或更低。 I don't care about IE6 at all. 我根本不关心IE6。

Any tips are greatly appreciated! 任何提示都非常感谢!

As per my comment above, I have decided to use CSS3 border-radius to solve my problem. 根据我上面的评论,我决定使用CSS3 border-radius来解决我的问题。 Using the statements below it will show rounded borders on every browser except Internet Explorer. 使用下面的语句,它将在除Internet Explorer之外的每个浏览器上显示圆形边框。 I don't care much about IE so IE users can simply look at straight corners. 我不太关心IE,所以IE用户可以简单地看看直角。

.box {
    display: table-cell;
    width: 16em;
    padding: 1em 2em;
    background: #f6c75d url(gradient.png) repeat-x scroll top left;
    border: 3px solid #de9542;
    border-radius: 25px; /* Standard */
    -o-border-radius: 25px; /* Opera 10.x */
    -moz-border-radius: 25px; /* Mozilla/Firefox */
    -icab-border-radius: 25px; /* iCab */
    -khtml-border-radius: 25px; /* KHTML/Konqueror */
    -webkit-border-radius: 25px; /* Webkit/Safari/Chrome/etcetera */
}

In the above, gradient.png is a small tileable image that provides the gradient at the top of a box. 在上面,gradient.png是一个小的tileable图像,它在框顶部提供渐变。

It works perfectly. 它完美地运作。 It also simplifies the markup and the CSS, and it reduces the amount and the size of the background images that I need. 它还简化了标记和CSS,并减少了我需要的背景图像的数量和大小。

There is solution, which works in Safari , Firefox and Chrome . 有解决方案,适用于SafariFirefoxChrome It does not work in IE, nor Opera (as far as I have tested it ― not even in 10.0b). 它不适用于IE,也不适用于Opera (据我测试过它 - 甚至不是10.0b)。 It uses CSS3 property border-image . 它使用CSS3属性border-image As it is still feature included in working draft, not recommendation, browsers implement it only with their specific prefixes: 由于它仍然包含在工作草案中,而不是推荐,因此浏览器仅使用其特定前缀实现它:

#boxes {
    display: table;
    border-spacing: 1em;
}
.box-row { display: table-row; }
.box {
    width: 16em;
    display: table-cell;
    padding-right: 2em;
    border-image: url(box.png) 6 8 6 8 stretch; // this line actually does not influence rendering in any engine
    -o-border-image: url(box.png) 6 8 6 8 stretch;
    -khtml-border-image: url(box.png) 6 8 6 8 stretch;
    -icab-border-image: url(box.png) 6 8 6 8 stretch;
    -webkit-border-image: url(box.png) 6 8 6 8 stretch;
}

Using it would actually need you to recreate background image, but it's a detail. 使用它实际上需要你重新创建背景图像,但它是一个细节。

If you don't mind adding a little extra markup, you can achieve this effect. 如果您不介意添加一些额外的标记,您可以实现此效果。 Of course I don't really advocate code-bloat, but if you must get it done, you can double-wrap the box and have the bottom corners be in one of each of the wrappers. 当然我并不是真的提倡代码膨胀,但是如果你必须完成它,你可以双重包装盒子并让每个包装器的底角都有。 Set the height of your inner wrapper with display: table-cell and you should be golden. 使用display: table-cell设置内包装器的高度,你应该是金色的。

你能在盒体元素上使用min-height属性吗?

try setting the box-body to display: table-cell ? 尝试设置框体显示:table-cell that might force it to render the three box-bodies to the same height and force the rounded borders to the bottom too. 这可能会强制它将三个盒体渲染到相同的高度,并将圆形边框强制到底部。

I hope this gets to you (I am new to these forum posting things) As a Graphic Designer, the "squarness" of HTML is horrible. 我希望这能得到你(我是这些论坛发布的新手)作为平面设计师,HTML的“方形”是可怕的。 I know this is not really a fix for HTML or CSS, as it is a simple graphic solution. 我知道这不是HTML或CSS的真正修复,因为它是一个简单的图形解决方案。 But it works great, and because it is a simple gif image it does not fail across browsers. 但它效果很好,因为它是一个简单的GIF图像,它不会在浏览器中失败。 The down side is that it takes time because of the trial and error and if you change the amount of content in your cell you may have to change the image used as a rounded rectangle background. 缺点是由于试验和错误需要时间,如果您更改单元格中的内容量,您可能需要更改用作圆角矩形背景的图像。

So, with that caveat... 所以,有了这个警告......

Work out the size of the table cell you need by adding the text and measuring the cell, or looking at the image size if only an image, in pixels. 通过添加文本和测量单元格来计算所需的表格单元格的大小,或者仅查看图像的图像大小(以像素为单位)。

In a graphics program create a rectangle of any colour (say black if you have white text on the website etc.) This rectangle is the bottom layer. 在图形程序中创建一个任何颜色的矩形(如果你在网站上有白色文字等,请说黑色)。这个矩形是底层。

Create a rounded rectangle, for example with a 1 pixel line thickness and NO fill colour (line colour can be any colour you choose as can thickness..) which is ABOVE the basic filled rectangle. 创建一个圆角矩形,例如,1像素线条粗细和NO填充颜色(线条颜色可以是您选择的任何颜色,可以是厚度..),它是基本填充矩形的上方。 What you will have then is a solid colour square with a thin outline with rounded corners. 那么你将拥有一个纯色方形,带有圆角的薄轮廓。

I make the rounded UNfilled rectangle 10 pixels smaller (xy) then I can position it equi-distance over the square. 我将圆形的未填充矩形缩小了10个像素(xy),然后我可以将它定位在正方形上等距离。

Export this image, say as a GIF or JPEG. 导出此图像,例如GIF或JPEG。

In your website, on the page click the table cell you want the rounded rectangle to be in and set it as the BACKGROUND image of just that cell. 在您的网站中,在页面上单击您希望圆角矩形所在的表格单元格,并将其设置为该单元格的BACKGROUND图像。 Make sure the cell is the same size as the image or it will not fit or tile... Then, because it is a BACKGROUND element to a cell, any content, text or images you put in the cell will appear above the background image making it look like you have a ROUNDED table cell. 确保单元格与图像大小相同或不适合或平铺...然后,因为它是单元格的BACKGROUND元素,所以放在单元格中的任何内容,文本或图像都将显示在背景图像上方使它看起来像你有一个ROUNDED表格单元格。

You can if you want make the background image transparent by exporting the native image as a gif with transparency (you can use PNG with Alhpa transparency which has more colours, but I am not so sure of full browser support for the PNG image in webpages yet) Just select the basic solid background colour, say Black, and add it as a transparent colour. 您可以通过将原生图像导出为具有透明度的GIF来使背景图像透明(您可以使用具有更多颜色的Alhpa透明度的PNG,但我不确定网页中的PNG图像的完整浏览器支持)只需选择基本的纯色背景颜色,比如黑色,然后将其添加为透明色。

You may want the transparency if you have a background image using CSS for the whole page, but, be warned, depending on the image in use, it may make the reading of the text difficult. 如果您有整个页面使用CSS的背景图像,您可能需要透明度,但是,请注意,根据使用的图像,它可能会使文本的阅读变得困难。

I hope this is of help. 我希望这有帮助。

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

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