有没有一种方法可以让迪夫容器在容器开始接触浏览器端时立即按比例缩小(如果浏览器被做得更小).而不是只在达到媒体查询宽度设置后才执行?

目前,我的容器会从左侧或右侧被切断,只有在Mediaquery将页边距设置为0时,它才会被"修复".有什么 idea 吗?

代码如下.值得在全尺寸上看起来更大,并将浏览器宽度调整为更小,看看事情是如何被切断的?

body {
  background-color: whitesmoke;
  margin: 20px;
  padding: 0;
  font-family: 'Times New Roman', Times, serif;
  font-size: 16px;
  line-height: 1.2;
}


/* Link styles */

a {
  color: blue;
  /* Set link color to blue */
  text-decoration: none;
  /* Remove default underline */
}


/* Hover and active link styles */

a:hover,
a:active,
a:focus {
  text-decoration: underline;
  /* Underline link on hover, click, or focus */
}

.container {
  background-color: ;
  margin-bottom: 80px;
  padding: 0px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  /* Change flex direction to column */
  position: relative;
  /* Add position relative */
  margin-top: 0px;
  /* Add margin-top for spacing */
}


/* Custom CSS for text boxes */

.textbox {
  background-color: white;
  max-width: 600px;
  padding: 10px;
  margin-top: 20px;
  /* Add margin-top for spacing */
  margin-left: auto;
  /* Align to the right */
  margin-bottom: 0px;
  /* Add margin-bottom for spacing */
  text-align: left;
  /* Align text to the left */
}


/* Media query for positioning and scaling the second container */

@media screen and (max-width: 700px) {
  .first-container,
  .second-container {
    margin-left: 0;
    /* Reset margin-left */
    margin-right: 0;
    /* Reset margin-left */
  }
}

@media screen and (min-width: 700px) {
  .first-container {
    margin-left: 8%;
    /* Position from the left */
  }
  .second-container {
    margin-right: 8%;
    /* Position from the right */
  }
}
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>

<div class="container first-container" style="max-width: 800px">
  <div style="background-color: orange; width: 100%">
    Content / text/ images
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>

  <div class="textbox">
    Lorem ipsum
  </div>
</div>

<div class="container second-container" style="max-width: 600px">
  <div style="background-color: lightcoral; width: 100%">
    Content / text/ images
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>

  <div class="textbox">
    Lorem ipsum
  </div>
</div>

推荐答案

您可以通过使用CSS calc()函数与用于查看区宽度的查看区单元vw相结合来实现这一点.

    body {
  background-color: whitesmoke;
  margin: 20px;
  padding: 0;
  font-family: 'Times New Roman', Times, serif;
  font-size: 16px;
  line-height: 1.2;
}

/* Link styles */
a {
  color: blue; /* Set link color to blue */
  text-decoration: none; /* Remove default underline */
}

/* Hover and active link styles */
a:hover,
a:active,
a:focus {
  text-decoration: underline; /* Underline link on hover, click, or focus */
}

.container {
  box-sizing: border-box; /* Include padding and border in element's total width and height */
  width: calc(100% - 40px); /* Set width to 100% minus the total horizontal padding */
  max-width: 800px; /* Set max-width to 800px */
  margin-bottom: 80px;
  padding: 0px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column; /* Change flex direction to column */
  position: relative; /* Add position relative */
  margin-top: 0px; /* Add margin-top for spacing */
}

/* Custom CSS for text boxes */
.textbox {
  background-color: white;
  max-width: 600px;
  padding: 10px;
  margin-top: 20px; /* Add margin-top for spacing */
  margin-left: auto; /* Align to the right */
  margin-bottom: 0px; /* Add margin-bottom for spacing */
  text-align: left; /* Align text to the left */
}

/* Media query for positioning and scaling the second container */
@media screen and (min-width: 700px) {
  .first-container {
    margin-left: calc(8vw - 20px); /* Position from the left */
  }
  .second-container {
    margin-right: calc(8vw - 20px); /* Position from the right */
  }
}

Css相关问答推荐

视频嵌入在Bootstrap 5轮播中没有响应扩展

如何在移动屏幕尺寸上包装元素?

NextJs - Tailwind css类样式未应用于响应屏幕

悬停时可展开的按钮与屏幕右侧对齐,如何保持未悬停的按钮正确对齐?

当面板以Angular 上升时如何定位下拉面板

如何为FLEX方向编写响应式的CSS

SVG文本在移动浏览器上增长并溢出

CSS子网格不显示行间距

如何将此 Select 器转换为TailWind类

如何使TWebPanel具有圆角?

使用 React 和 Flexbox 文本溢出框外

使用 styled-components,我如何定位组件的子类?

左右卡片的 OwlCarousel 导航:react js

如何通过单击
  • 激活 HTML 链接?
  • Bootstrap:在列之间添加边距/填充空间

    如何通过 JavaScript 重新触发 WebKit CSS 动画?

    在 Twitter Bootstrap 中创建圆角的正确方法

    :last-child 没有按预期工作?

    打印html时在页面上打印页码

    CSS显示:表格列应该如何工作?