简体   繁体   English

将边距扩展到未知宽度

[英]Extend margins to unknown width

I have some text elements displayed as inline-block and each has a fixed amount of padding , say 10 pixels. 我有一些显示为行inline-block文本元素,每个文本元素都有固定的padding量,例如10个像素。 All of these text elements are centered by text-align inside of a container of unknown width. 所有这些文本元素均以text-align居中放置在未知宽度的容器内。 Here is a JSFiddle as an example: http://jsfiddle.net/fhtkLwak/5/ 这里以JSFiddle为例: http : //jsfiddle.net/fhtkLwak/5/

HTML : HTML

<div class="outer">
    <div class="inner">
        <span style="background:#0f0;">A</span>
        <span style="background:#00f;">B</span>
        <span style="background:#ff0;">C</span>
        <span style="background:#0ff;">D</span>
        <span style="background:#f0f;">E</span>
    </div>
</div>

CSS : CSS

.outer {width:500px;}
.inner {
    background:#f00;
    font-size:0; /* To avoid white space width */
    text-align:center;
}
.inner span {
    display:inline-block;
    font-size:20px;
    padding:10px;
}

What I would like is that the first element's background / padding extends as far to the left as possible and the last element's background / padding extends as far to the right as possible. 我想要的是第一个元素的background / padding尽可能向左延伸,而最后一个元素的background / padding尽可能向右延伸。 If it can be done without padding that is fine, just as long as the background has the described behavior. 如果可以不用padding就可以做到,只要background具有所描述的行为即可。

If you don't mind sacrificing some browser support, CSS flexbox is the easiest way to go: http://jsfiddle.net/teddyrised/fhtkLwak/9/ 如果您不介意牺牲一些浏览器支持,那么CSS flexbox是最简单的方法: http : //jsfiddle.net/teddyrised/fhtkLwak/9/

The main advantage of this method is that it does not require explicitly setting the width of .outer , so it is flexible (hence the name flex display): 此方法的主要优点是它不需要显式设置.outer的宽度 ,因此它很灵活(因此名称为flex display):

.outer {width:500px;}
.inner {
    background:#f00;
    display: flex;
    justify-content: center;
}
.inner span {
    display: block;
    font-size:20px;
    padding:10px;
}
.inner span:first-child,
.inner span:last-child {
    flex-grow: 1;
}
.inner span:first-child {
    text-align: right;
}

You will need to add appropriate vendor prefixes to the flex properties to enable cross-browser support. 您将需要在flex属性中添加适当的供应商前缀,以启用跨浏览器支持。

What we are doing here is basically give the parent element, .inner , a flex display property. 我们在这里所做的基本上是给父元素.inner一个flex显示属性。 This allows us to fine tune the dimensions of its <span> children. 这使我们可以微调其<span>子级的尺寸。 Since you only want the first and last child to grow to fill all remaining space, we allow them to grow to fill all available space flex-grow: 1 (default is flex-grow: 0 ), and then set the text alignment on the first child to the right to achieve the desired text alignment. 由于您只希望第一个和最后一个孩子成长为填充所有剩余空间,因此我们允许他们成长为填充所有可用空间flex-grow: 1 (默认值为flex-grow: 0 ),然后在向右第一个孩子以实现所需的文本对齐方式。

For a comprehensive guide to CSS flexbox, you can check out the guide published by Chris Coyier . 有关CSS flexbox的全面指南,请查看Chris Coyier 发行的指南


Update : If CSS flexbox support is lacking in the browser of your target audience, you can consider setting a background gradient instead. 更新 :如果目标受众的浏览器缺少CSS flexbox支持,则可以考虑设置背景渐变。 The trick is that you will set a two-tone background that is parted in the middle, where the left colour corresponds to the left-most <span> the right colour corresponds to the right-most counterpart. 诀窍是您将设置一个中间分隔的两色背景,其中左侧颜色对应于最左边的<span> ,右侧颜色对应于最右边的对应部分。

Again, the advantage of this method is that it does not require the width of .outer to be explicitly stated. 同样,此方法的优点是它不需要显式声明.outer的宽度。 The major drawback, however, is that the background colour of the first and last span has to be known, and static, otherwise you will have to rely on JS to sniff out the (dynamic) background colour and create new background gradients on the fly. 但是,主要缺点是,必须知道第一个和最后一个跨度的背景色,并且背景色是静态的,否则您将不得不依靠JS来嗅探(动态)背景色并动态创建新的背景渐变。 。

.outer {
    width:500px;
    background-image: linear-gradient(90deg, #0f0 0%, #0f0 50%, #f0f 50%, #f0f 100%);
}

This solution is less invasive in the sense that all your code has been preserved, and I have only added the background-image declaration :) 从保留所有代码的角度来说,此解决方案的侵入性较小,而我仅添加了background-image声明:)

http://jsfiddle.net/teddyrised/fhtkLwak/11/ http://jsfiddle.net/teddyrised/fhtkLwak/11/

Actually there are couple of ways to achieve that, I'm sure about 3 of them at least at the moment. 实际上,有两种方法可以实现这一点,我敢肯定至少目前有3种方法。

Flexbox is not supported on IE9 and older, and CSS tables also have their own cons, ... However without altering the current method (ie keep using inline-block s) you could fake the effect by adding a background-color and border-right to the container: IE9及更早版本不支持Flexbox,CSS表也有其缺点,...但是,如果不更改当前方法(即继续使用inline-block ),则可以通过添加background-colorborder-right集装箱的border-right

Example Here 这里的例子

.outer {
    width:250px;
    background-color: #0f0;         /* equal to background color of the first item */
    border-right: 250px solid #f0f; /* equal to background color of the last item */
}

.inner {
    margin-right: -250px;
    font-size:0; /* To avoid white space width */
    text-align:center;
}

Having vary width 宽度各异

If the container doesn't have an explicit width, you can still achieve the goal by using inline-block s. 如果容器没有明确的宽度,您仍然可以通过使用inline-block s实现目标。

And ff course that we can achieve that by CSS Gradients as @Terry mentioned, but for the sake of browser support I'd go with background colors. 当然,我们可以通过CSS Gradients实现,@Terry所述,但是为了支持浏览器,我会使用背景色。

In this case instead of adding border-right to the .outer we can give the .inner a background-color as well, and finally align the items to the center by relative positioning and giving them a right of -50% as follows: 在这种情况下,我们也可以给.inner一个background-color ,而不是给.outer加上border-right ,最后通过相对定位使它们与中心对齐,并赋予它们-50%right ,如下所示:

Updated Demo 更新的演示

.outer {
    /* width: 90%; */        /* Arbitrary */
    background-color: #f0f;  /* equal to background color of the last item */
}

.inner {
    background-color: #0f0; /* equal to background color of the first item */
    width: 50%;
    font-size:0; /* To avoid white space width */
    text-align:center;
}

.inner span {
    display:inline-block;
    font-size:20px;
    padding:10px;

    position: relative;
    right: -50%;
}

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

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