css 宽度取决于子元素的宽度和内容高度

m4pnthwp  于 2023-03-05  发布在  其他
关注(0)|答案(3)|浏览(176)
    • bounty将于明天到期**。回答此问题可获得+100的声望奖励。Temani Afif希望引起更多人关注此问题:我很确定只使用CSS是不可能的,但是你有机会证明我是错的。PS:我会否决任何使用JavaScript的解决方案(没有阅读它),所以不,甚至不要尝试。

我想创建一个有Angular 的文本流,其中文本行的宽度是一个已知值--w。到目前为止,我所拥有的代码如下:

* { margin: 0 }

html, body { display: grid }

body {
  --w: min(35em, 65vw);
  --f: 1/ 8;
  background: linear-gradient(90deg, transparent var(--w), pink 0);
  font: 1em ubuntu, sans-serif
}

.outer-wrapper {
  width: min-content;
  box-shadow: 0 0 0 2px red;
  background: lemonchiffon
}

span::before, span::after {
  --i: 0;
  --j: calc(1 - var(--i));
  --g: 
    linear-gradient(to right bottom, 
        hsla(50, 50%, 50%, var(--j)) 50%, 
        hsla(50, 50%, 50%, var(--i)) 0);
  float: left;
  height: 100%;
  aspect-ratio: var(--f);
  background: var(--g);
  shape-outside: var(--g);
  content: ''
}
span::after {
  --i: 1;
  float: right
}

.inner-wrapper {
  width: var(--w);
  box-shadow: inset 0 0 0 2px blue
}
<div class="outer-wrapper">
  <span aria-hidden="true"></span>
  <div class="inner-wrapper">
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
  </div>
</div>

问题是,我希望将两侧的浮动伪值添加到--w宽度中,并使.outer-wrapper宽1倍,否则,文本将溢出红色外框。

我不知道如何用CSS来做这些。我知道用JS是可以做到的,我有一个工作版本,我读取px值的高度,将其存储在一个变量中,在调整大小时更新它,然后从那里计算所有内容。但我真的很想只用CSS来做这些。
在这种情况下,倾斜长方体不是有用的解决方案。

2admgd59

2admgd591#

它并非没有缺点,可能不适合您的特定用例,但您可以使用一个更窄容器中内容的隐藏副本来确定所需的高度,然后使用height: 100%.outer-wrapper提供所需的高度。

* { margin: 0 }

html, body { display: grid }

body {
  --w: min(35em, 65vw);
  --f: 1/ 8;
  background: linear-gradient(90deg, transparent var(--w), pink 0);
  font: 1em ubuntu, sans-serif
}

.outer-outer-wrapper {
  height: auto;
  display: flex;
}

.height-setter {
  background: lightgreen;
  width: calc(var(--w) * (1 - var(--f)));
  visibility: hidden;
}

.outer-wrapper {
  width: min-content;
  box-shadow: 0 0 0 2px red;
  background: lemonchiffon;
  height: 100%;
}

span::before, span::after {
  --i: 0;
  --j: calc(1 - var(--i));
  --g: 
    linear-gradient(to right bottom, 
        hsla(50, 50%, 50%, var(--j)) 50%, 
        hsla(50, 50%, 50%, var(--i)) 0);
  float: left;
  height: 100%;
  aspect-ratio: var(--f);
  background: var(--g);
  shape-outside: var(--g);
  content: ''
}
span::after {
  --i: 1;
  float: right
}

.inner-wrapper {
  width: var(--w);
  box-shadow: inset 0 0 0 2px blue;
}
<div class="outer-outer-wrapper">
<div class="outer-wrapper">
  <span aria-hidden="true"></span>
  <div class="inner-wrapper">
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
  </div>
</div>
  
  <div class="height-setter">
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
  </div>
</div>

潜在问题包括:

  • 由于换行符的位置不同,不能保证高度是正确的。如果.outer-wrapper稍大一些是可以接受的,您可以通过给.height-setter 1或2 ems的底部填充来添加一行或两行额外的行来覆盖自己。
  • 您的代码中有重复的内容(尽管我相信只要设置了visibility: hiddenscreen readers won't see it就是正确的)。
  • .outer-outer-wrapper比它内部显示的内容要宽,在我的屏幕上显示您的代码时,这很好,但实际上,您可能需要一些巧妙的弹性裁剪或设置overflow: hidden.outer-outer-outer-wrapper来解决这个问题。
pqwbnv8z

pqwbnv8z2#

我不相信用shape-outside和纯css能达到这个效果。
然而,如果我们有一点创造性,我们可以操纵transform属性来实现,(我相信是) 您想要的结果。
斜体字有一个不幸的副作用,但这可能是你可以接受的,或者可能纠正。

* {
  margin: 0
}

html,
body {
  display: grid
}

body {
  --w: min(35em, 65vw);
  background: pink;
  font: 1em ubuntu, sans-serif
}

.outer-wrapper {
  width: var(--w);
  transform: skewX(-8deg);
  transform-origin: 0% 100%;
  background: #bfaa40;
}

.inner-wrapper {
  background: lemonchiffon;
}

.inner-wrapper::before,
.inner-wrapper::after {
  content: '';
  background: #bfaa40;
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  right: 0;
  z-index: -1;
  transform: skewX(8deg);
  transform-origin: 0% 100%;
}

.inner-wrapper::after {
  top: 0;
  right: 0;
  transform-origin: 0% 0%;
}
<div class="outer-wrapper">
  <div class="inner-wrapper">
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy.
      Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop
      lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice
      icing.
    </p>
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy.
      Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop
      lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice
      icing.
    </p>
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy.
      Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop
      lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice
      icing.
    </p>
  </div>
</div>
xyhw6mcr

xyhw6mcr3#

只是要求的纯CSS

    • 注意:您必须在HTML代码中放置两次内容,如下所示**

如果您使用动态语言打印内容,这将非常容易。
它适用于任何屏幕尺寸。内容的大小无关紧要,它也可以有填充。你也可以改变--f--w的值,一切工作了!

* {
        margin: 0;
    }

    html, body { display: grid }

    body {
        --w: min(35em, 65vw);
        --f: 15%;
        background: linear-gradient(90deg, transparent var(--w), pink 0);
        font: 1em ubuntu, sans-serif
    }

    .outer-wrapper {
        width: var(--w);
        box-shadow: 0 0 0 2px red;
        background: lemonchiffon;
        height: 100%;
        position: relative;
    }

    span::before, span::after {
        --i: 0;
        --j: calc(1 - var(--i));
        --g:
                linear-gradient(to right bottom,
                hsla(50, 50%, 50%, var(--j)) 50%,
                hsla(50, 50%, 50%, var(--i)) 0);
        float: left;
        height: 100%;
        width: var(--f);
        background: var(--g);
        shape-outside: var(--g);
        content: ''
    }
    span::after {
        --i: 1;
        float: right
    }

    .inner-wrapper {
        box-shadow: inset 0 0 0 2px blue
    }

    .inner-wrapper-hidden {
        width: calc(100% - var(--f));
        visibility: hidden;
        pointer-events: none;
    }

    .inner-wrapper-absolute {
        position: absolute;
        height: 100%;
        width: 100%;
        top: 0;
        left: 0;
    }
<div class="outer-wrapper">
<div class="inner-wrapper-absolute">
    <span aria-hidden="true"></span>
    <div class="inner-wrapper">
        <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
        <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
        <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
    </div>
</div>
<div class="inner-wrapper-hidden">
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
    <p>Gingerbread macaroon cake topping wafer cake dessert. Lemon drops cheesecake lemon drops lollipop biscuit tart. Chupa chups bonbon dragée tart pie. Oat cake cupcake bear claw pie ice cream ice cream. Jelly beans ice cream icing halvah cotton candy. Tiramisu dragée macaroon chocolate bar chocolate cake. Sweet roll wafer candy gummies donut wafer liquorice sugar plum. Icing gummies chocolate cake gummies caramels sweet roll. Tootsie roll jelly beans jelly chocolate jelly beans pie. Halvah lollipop lemon drops shortbread bonbon. Jelly biscuit bear claw cotton candy cotton candy chocolate bar soufflé donut lollipop. Fruitcake lemon drops marshmallow sugar plum cheesecake gingerbread. Sugar plum chupa chups candy canes jelly-o powder liquorice icing.</p>
</div>
</div>

相关问题