简体   繁体   English

纯CSS3用于重叠 <div> 边界半径的元素

[英]Pure CSS3 for overlapping <div> elements with border radius

I have a varying number of <div> s inside a container <div> , that are each set to display:inline-block , have a -webkit-border-radius and some padding . 我在容器<div>有不同数量的<div> ,每个都设置为display:inline-block ,有一个-webkit-border-radius和一些padding I would like to position each of the <div> s in a way that the one to the right overlaps the one to the left enough, so that there is no break in the border on top and bottom. 我想以一种方式将每个<div>放置在一个方向上,右边的那个与左边的那个重叠,这样顶部和底部的边框就没有中断。 Also, Ideally the container <div> would only have a width that is exactly the size of the styled divs inside (hight of LAST_DIV and width equal to the distance from the leftmost to the rightmost div). 此外,理想情况下,容器<div>的宽度恰好与里面的样式div的大小相同(LAST_DIV的高度和宽度等于从最左边到最右边的div的距离)。

----------------
   --------------------/                  \
 /          /         |                    |
|    DIV_1 |    DIV_2 |    LAST_DIV        |
 \          \         |                    |
   ------------------- \                  /
                         ----------------

Since the number of <div> s that will display varies, I ruled out absolute positioning. 由于显示的<div>的数量不同,我排除了绝对定位。 I would like to refrain from javascript or adding additional elements to the html document, since I am creating multiple styles for the same website element, and some of those styles might not have rounded <div> s that have to overlap. 我想避免使用javascript或在html文档中添加其他元素,因为我正在为同一个网站元素创建多个样式,其中一些样式可能没有必须重叠的舍入<div>

Edit: 编辑:

I have tried setting only a border radius to the left edge of the inner divs and giving the container div a border on top and bottom and setting a negative value for left until the overlapping border of the container div disappears. 我已经尝试只将边框半径设置到内部div的左边缘,并为容器div设置顶部和底部的边框,并为left设置负值,直到容器div的重叠边框消失。 When all divs are the same hight, this gave me issues at the right end, since the container div now extended over the right end. 当所有div都是同一个hight时,这给了我右端的问题,因为容器div现在扩展到了右端。 This also gave me issues when the individual divs had different colors. 当个别div有不同的颜色时,这也给了我一些问题。

You could try it something like this: 你可以尝试这样的事情:

demo 演示

HTML : HTML

<div class='container'>
    <div>DIV_1</div><!--
    --><div>DIV_2</div><!--
    --><div>LAST_DIV</div>
</div>

Relevant CSS : 相关CSS

.container, .container div { display: inline-block; }
.container div {
    padding: .25em 1.25em;
    border-radius: .65em 0 0 .65em;
}
.container div:not(:first-child) {
    margin: 0 0 0 -.65em; /* negative left margin, same value as border-radius */
}
.container div:last-child {
    padding: 1.25em;
    border-radius: .65em;
}

Note 注意

Any kind of whitespace (space, tab, newline) between elements that have display: inline-block; 具有display: inline-block;元素的元素之间的任何类型的空格(空格,制表符,换行符) display: inline-block; set on them matters. 对他们来说很重要。 This mean that a newline in the HTML between the div s in the container will introduce a space between them. 这意味着容器中div之间的HTML中的换行符将在它们之间引入空格。 There are a few fixes for this . 对此有一些修复 The one I've chosen involves adding a comment between </div> (closing tag of a child div) and <div> (opening tag of the following div). 我选择的那个涉及在</div> (关闭子div的标签)和<div> (打开以下div的标签)之间添加注释。

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

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