使 div 比其父级更宽,没有水平滚动条且没有溢出:隐藏?

是否有可能在不进行水平滚动的情况下让子元素超过父元素的宽度达到浏览器的整个范围?

我已经尝试过这里发布的解决方案有没有办法使用 CSS 使子 DIV 的宽度比父 DIV 更宽?

但是对于这个特定的 HTML(见下文),只有一个可以工作并创建水平滚动条(代码片段和下面的图像中看到的额外宽度导致滚动条出现)

https://img1.sycdn.imooc.com/657672f60001b4af05380147.jpg

*我总是可以添加一个overflow-x: hidden;,<body>但我不想这样做


编辑:有一些 css 属性,即使在示例中没有意义,它们也应该保留在解决方案中(如果有)


    <div class="main">

      <div class="container">

        <div class="fit">

          something 1

        </div>

        <div class="parent">

          <header class="full full5">

            child full5

          </header>

        </div>

        <div class="fit">

          something 2

        </div>

      </div>

    </div>

    body: {

      margin: 0px;

    }


    .main {

      display: flex;

      flex-direction: column;

      align-items: center;

      background: #e8e8e8;

      border: 1px solid #000;

      margin: 0px;

      box-sizing: border-box;

    }


    .container {

      display: flex;

      flex-direction: column;

      max-width: 300px;

      width: 100%;

    }


    .parent {

      display: flex;

      /* flex-direction: column; */

      width: 100%;

      background-color: #e9cccc;

      box-sizing: border-box;

      position: sticky;

      top: 0;

    }


    .fit {

      height: 50px;

    }


    .full {

      background-color: #d1ccd8a6;

      border: 1px solid #666;

      border-radius: 3px;

      height: 50px;

      padding: 0px;

      box-sizing: border-box;

    }


    .full5 {

      width: 100vw;

      position: relative;

      left: 50%;

      right: 50%;

      margin-left: -50vw;

      margin-right: -50vw;

    }

如果<body>margin:0发布的解决方案有效,除非有垂直滚动条(什么时候没有?)



慕森卡
浏览 148回答 3
3回答

慕码人2483693

在这里(全屏查看以进行可视化):body {&nbsp; margin: 0;}.main {&nbsp; display: flex;&nbsp; flex-direction: column;&nbsp; align-items: center;&nbsp; background: #e8e8e8;&nbsp; border: 1px solid #000;&nbsp; margin: 0px;&nbsp; box-sizing: border-box;}.container {&nbsp; display: flex;&nbsp; flex-direction: column;&nbsp; max-width: 300px;&nbsp; width: 100%;}.parent {&nbsp; display: flex;&nbsp; /* flex-direction: column; */&nbsp; width: 100%;&nbsp; background-color: #e9cccc;&nbsp; box-sizing: border-box;&nbsp; position: sticky;&nbsp; top: 0;}.fit {&nbsp; height: 50px;}.full {&nbsp; background-color: #d1ccd8a6;&nbsp; border: 1px solid #666;&nbsp; border-radius: 3px;&nbsp; height: 50px;&nbsp; padding: 0px;&nbsp; box-sizing: border-box;}.full1 {&nbsp; position: absolute;&nbsp; width: 100%;&nbsp; left: 0px;&nbsp; top: 0px;}.full2 {&nbsp; width: 100vw;&nbsp; position: relative;&nbsp; left: calc(-50vw + 50%);}.full3 {&nbsp; position: relative;&nbsp; width: 200%;&nbsp; left: -50%;}.full4 {&nbsp; margin-left: calc(-50vw + 50%);&nbsp; width: 100vw;}.full5 {&nbsp; width: 100vw;&nbsp; position: relative;&nbsp; left: 50%;&nbsp; right: 50%;&nbsp; margin-left: -50vw;&nbsp; margin-right: -50vw;}.full6 {&nbsp; margin-left: calc(-50vw + 50%);&nbsp; width: 100vw;}.hide {&nbsp; display: none;}<div class="main">&nbsp; <div class="container">&nbsp; &nbsp; <div class="fit">&nbsp; &nbsp; &nbsp; something 1&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="parent">&nbsp; &nbsp; &nbsp; <header class="full full1 hide">&nbsp; &nbsp; &nbsp; &nbsp; child full1&nbsp; &nbsp; &nbsp; </header>&nbsp; &nbsp; &nbsp; <header class="full full2 hide ">&nbsp; &nbsp; &nbsp; &nbsp; child full2&nbsp; &nbsp; &nbsp; </header>&nbsp; &nbsp; &nbsp; <header class="full full3 hide">&nbsp; &nbsp; &nbsp; &nbsp; child full3&nbsp; &nbsp; &nbsp; </header>&nbsp; &nbsp; &nbsp; <header class="full full4 hide">&nbsp; &nbsp; &nbsp; &nbsp; child full4&nbsp; &nbsp; &nbsp; </header>&nbsp; &nbsp; &nbsp; <header class="full full5">&nbsp; &nbsp; &nbsp; &nbsp; child full5&nbsp; &nbsp; &nbsp; </header>&nbsp; &nbsp; &nbsp; <header class="full full6 hide">&nbsp; &nbsp; &nbsp; &nbsp; child full6&nbsp; &nbsp; &nbsp; </header>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="fit">&nbsp; &nbsp; &nbsp; something 2&nbsp; &nbsp; </div>&nbsp; </div></div>

SMILET

这是你想要的吗?我刚刚更改了您的 CSS .main,.parent并且.full-5body: {&nbsp; margin: 0px;}.main {&nbsp; display: flex;&nbsp; flex-direction: column;&nbsp; align-items: center;&nbsp; background: #e8e8e8;&nbsp; border: 1px solid #000;&nbsp; margin: 0px;&nbsp; box-sizing: border-box;&nbsp; position: relative;}.container {&nbsp; display: flex;&nbsp; flex-direction: column;&nbsp; max-width: 300px;&nbsp; width: 100%;}.parent {&nbsp; display: flex;&nbsp; /* flex-direction: column; */&nbsp; width: 100%;&nbsp; background-color: #e9cccc;&nbsp; box-sizing: border-box;&nbsp; top: 0;&nbsp; height: 50px;}.fit {&nbsp; height: 50px;}.full {&nbsp; background-color: #d1ccd8a6;&nbsp; border: 1px solid #666;&nbsp; border-radius: 3px;&nbsp; height: 50px;&nbsp; padding: 0px;&nbsp; box-sizing: border-box;}.full1 {&nbsp; position: absolute;&nbsp; width: 100%;&nbsp; left: 0px;&nbsp; top: 0px;}.full2 {&nbsp; width: 100vw;&nbsp; position: relative;&nbsp; left: calc(-50vw + 50%);}.full3 {&nbsp; position: relative;&nbsp; width: 200%;&nbsp; left: -50%;}.full4 {&nbsp; margin-left: calc(-50vw + 50%);&nbsp; width: 100vw;}.full5 {&nbsp; &nbsp; position: absolute;&nbsp; &nbsp; left: 0;&nbsp; &nbsp; right: 0;}.full6 {&nbsp; margin-left: calc(-50vw + 50%);&nbsp; width: 100vw;}.hide {&nbsp; display: none;}<div class="main">&nbsp; <div class="container">&nbsp; &nbsp; <div class="fit">&nbsp; &nbsp; &nbsp; something 1&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="parent">&nbsp; &nbsp; &nbsp; <header class="full full1 hide">&nbsp; &nbsp; &nbsp; &nbsp; child full1&nbsp; &nbsp; &nbsp; </header>&nbsp; &nbsp; &nbsp; <header class="full full2 hide ">&nbsp; &nbsp; &nbsp; &nbsp; child full2&nbsp; &nbsp; &nbsp; </header>&nbsp; &nbsp; &nbsp; <header class="full full3 hide">&nbsp; &nbsp; &nbsp; &nbsp; child full3&nbsp; &nbsp; &nbsp; </header>&nbsp; &nbsp; &nbsp; <header class="full full4 hide">&nbsp; &nbsp; &nbsp; &nbsp; child full4&nbsp; &nbsp; &nbsp; </header>&nbsp; &nbsp; &nbsp; <header class="full full5">&nbsp; &nbsp; &nbsp; &nbsp; child full5&nbsp; &nbsp; &nbsp; </header>&nbsp; &nbsp; &nbsp; <header class="full full6 hide">&nbsp; &nbsp; &nbsp; &nbsp; child full6&nbsp; &nbsp; &nbsp; </header>&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="fit">&nbsp; &nbsp; &nbsp; something 2&nbsp; &nbsp; </div>&nbsp; </div></div>

拉风的咖菲猫

您可以在 css 代码中使用 z-index 来引入父级并填充颜色以推动背景
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5