这是一个介绍CSS响应式布局的视频字幕,请将其翻译成中文 in recent years full bleed designs have become popular on…
- User
这是一个介绍CSS响应式布局的视频字幕,请将其翻译成中文 in recent years full bleed designs have become popular on the web web designers aim to enhance UI and readability by integrating full bleed sections into single column layouts and removing distractions like sidebars from around the main content in this tutorial we will explore various methods to create a responsive full bleed layout if you are passionate about front end development be sure to subscribe to our channel and hit the notification bell to stay updated on upcoming tutorials let's get started before we proceed let us set the stage in this mini project we have a main HTML element acting as the page container inside there's regular content including page headings and paragraphs we also have a banner section with a specific CSS class that we would like to span across the entire screen our CSS file is set up and ready to go with some foundational styles for the project additionally we have our full bleed CSS class ready to show its magic now let's assume our main section has a defined width which is a maximum of 80% in this case so far it's all basic CSS and this is how our web page looks at this stage as we know the entire page is contained within the main HTML element which has a restricted width and for this same reason the full bleed section can't extend beyond its boundaries you might wonder why not remove the max width from the main element and apply it to all its children while that could work it would be difficult to manage especially in a large scale web project there are a few ways to add a full width element within a constrained width column such as placing an edge to edge image within a narrower text column let's start exploring some of them the simplest way to achieve a full bleed section is by using relative positioning with a width of 100vw this means the left edge starts at the beginning of the container but the right edge extends off the screen then we set the left offset to 50 percent which moves the starting point of the element to the middle of the viewport specifying a left margin of minus 50vw will then pull it back to the left edge one consideration with this solution is the potential horizontal scroll due to a natural vertical scroll bar to prevent this we can add overflow X hidden to the parent container or body element additionally we can replace margin left with margin in line start which is the logical counterpart for the left margin alternatively we can use the transform property to pull the element back to the left edge of the screen now let's move on to another solution that works in a similar fashion again the main element has a defined maximum width of 80 percent this time we will use the calc function to dynamically set the left and right margins for our full width element what we are doing here is subtracting half the viewport width from half the width of the element this applies negative left and right margins to pull the element in the opposite direction when both negative margins pull the element stretches to achieve 100 percent of the width for example suppose our element is 800 pixels wide and the screen has a width of 1000 pixels the total difference is 200 pixels which is equally divided into left and right margins as before this method may introduce horizontal scroll therefore overflow x hidden will also be required furthermore the logical shorthand can be used to set left and right margins in a single line if you're interested in learning more about CSS logical properties feel free to check out our detailed video on the topic returning to our solutions the above two methods work well as long as the column is centered and you don't mind having overflow X on the column or the body element to prevent horizontal overflow moving forward let's see how we can use the CSS grid layout to achieve a full bleed section within a restricted width column first we convert our main element into a grid container then we use the Grid template columns property to establish a three column layout with the middle column occupying 80% width and the remaining space split evenly between the first and third columns next the default placement for all grid items should be in the middle column which we can achieve by adding the grid column property with a value of 2 before proceeding let's break down how the CSS grid layout functions the grid layout in CSS uses a two dimensional grid of rows and columns the dividing lines that make up the structure of the grid are called grid lines they can be either vertical known as column grid lines or horizontal known as row grid lines we can verify the line numbers by inspecting them with the dev tools each gridline is automatically assigned a positive number and interestingly they also have negative numbering from the alternate direction so the last grid line in our layout is numbered 4 as well as minus 1 the space between two adjacent rogue grid lines and column grid lines forms a grid cell in our example adding grid column 2 means every grid item should start at line No. 2 of the grid it doesn't refer to the second column instead it specifies the column line number then we can define the starting and ending positions of the full bleed CSS class in this case the full bleed element will start at line No. 1 and end at line No. 4 as we saw the last line number can also be referred to as minus 1 this statement can further be simplified by using span 3 which means the element should span across three columns let's revisit our grid column setup currently the middle column has a width of 80 percent which may affect readability on large screen devices it's generally hard to read lines of text that are too wide the ideal number of characters in a single line is considered about 65 characters anywhere between 45 and 85 characters is generally seen as acceptable so let's fine tune the middle column in this case we are using the min function to pick the smaller value on large screens it will take up to 65 characters in width while on small screens where there isn't enough horizontal space it is clamped to 100 percent of the available width now this appears good on large screens but on small screens we need to add some gutter space to prevent text from hugging viewport edges the grid column gap is a nifty property that lets us add gutters between columns in this case we are using 3REM for the spacing and to eliminate the horizontal scroll we deduct twice the spacing from the middle column now our middle column isn't a full 100 percent wide but rather 100 percent minus 6 rem for the spacing on both sides this illustrates how we can either use traditional positioning tricks to make a full width container within a restricted width parent or utilize a CSS grid layout to achieve the same desired result with the CSS grid we can easily add more than one column to non full bleed sections and also avoid the use of media queries by applying versatile units like math functions and the Fr unit if you found this tutorial helpful don't forget to like share and subscribe for more exciting tips and tricks thanks for tuning in and see you next time
Assistant
在过去的几年里 全屏设计在网络上变得流行 网页设计师旨在通过整合全屏部分到 单列布局中来增强UI和可读性 并从主内容周围移除干扰 如侧边栏 在本教程中 我们将探索创建响应式全屏布局的各种方法 如果你对前端开发充满热情 一定要订阅我们的频道 并点击通知铃铛以获取 即将到来的教程的最新信息 让我们开始吧 在我们开始之前,让我们设置舞台 在这个小项目中 我们有一个主要的 HTML元素作为页面容器 里面有常规内容 包括页面标题和段落 我们还有一个带有特定 CSS类的横幅部分 我们希望它能跨越整个屏幕 我们的CSS文件已经设置好 并准备就绪 为项目准备了一些基础样式 此外,我们还有我们的全屏 CSS类准备好展示它的魔力 现在让我们假设我们的主部分有一个定义的宽度 在这种情况下是最大80% 到目前为止,这都是基本的CSS 这是我们的网页在这个阶段的看起来的样子 我们知道 整个页面被包含在主 HTML元素中,它有一个限制的宽度 正因为这个原因 全屏部分不能超出其边界 你可能会想 为什么不从主元素中移除最大宽度 并将其应用于所有子元素 虽然这可能有效,但在管理上会比较困难 特别是在大规模的网页项目中 有几种方法可以在受限宽度的列中添加一个全宽 元素,例如在较窄的文本列中放置一个边缘到边缘的图像 让我们开始探索其中一些方法 实现全屏部分的最简单方法是通过相对定位 宽度为100vw 这意味着左 边缘从容器的开始处开始 但右边缘延伸到屏幕之外 然后我们设置左偏移量为50% 这将元素的起始点移动到视口中间 指定一个左外边距为负50vw 将然后把它拉回到左边缘 这种解决方案的一个考虑是潜在的水平滚动 由于自然的垂直滚动条 为了防止这种情况 我们可以在父容器或body元素上添加overflow X隐藏 此外,我们可以使用 transform属性将元素拉回到屏幕左边缘 现在让我们转向另一个 以类似方式工作的解决方案 这一次,我们的主元素有一个定义的最大宽度 80% 这次我们将使用calc函数 动态设置全宽元素的左右外边距 我们在这里做的是减去 视口宽度的一半 从元素宽度的一半 这适用于 负的左右外边距以拉动元素 相反的方向 当两个负外边距都拉动时 元素伸展以达到100%的宽度 例如 假设我们的元素宽800像素 屏幕宽1000像素 总差是200像素 这被平均分配到左和右外边距 如前所述,这种方法可能引入水平滚动 因此overflow x隐藏也将是必需的 此外,逻辑 速记可以用来在一行中设置左和右外边距 如果你对CSS逻辑属性感兴趣 请随时查看我们关于该主题的详细视频 回到我们的解决方案 上述两种方法在列居中时效果很好 而且你不介意在列或body元素上使用overflow X 为了防止水平溢出 让我们看看如何使用CSS 网格布局来在受限宽度列中实现全屏部分 首先,我们将我们的主元素转换为一个网格容器 然后我们使用网格模板列属性 建立一个三列布局 中间列占据80%宽度 剩余的空间 平均分配给第一和第三列 接下来,所有网格项的默认放置 应该在中间列 我们可以通过添加网格列属性和值2来实现 在继续之前 让我们分解CSS网格布局是如何工作的 CSS中的网格布局使用一个二维的行和列网格 构成网格结构的划分线称为网格线 它们可以是垂直的 称为列网格线,也可以是水平的 称为行网格线 我们可以通过检查它们来验证线条数字 使用开发者工具 每个 网格线自动被分配一个正数 有趣的是 它们也有从相反方向的负数编号 所以我们布局中的最后一条网格线编号为4 也可以是负1 网格线之间的空间 形成一个网格单元 在这种情况下,添加网格列2 意味着每个网格项应该从第2条线开始 它不指第二列 而是指定列线的起始位置 然后我们可以定义全屏 CSS类的起始和结束位置 在这种情况下,全屏元素 将从第1条线开始,到第4条线结束 正如我们所看到的,最后一条线编号 也可以是负1 这个声明可以进一步简化为使用span 3 这意味着元素应该跨越三个列 让我们回到我们的网格列设置 目前,中间列的宽度是80% 这可能会影响在大屏幕设备上的可读性 阅读太宽的文本行通常很困难 单行理想的字符数被认为是大约65个字符 任何地方在45 和85个字符之间通常被认为是可接受的 所以让我们微调中间列 在这种情况下,我们使用min函数 选择较小的值 在大屏幕上,它将占用最多65个字符的宽度 而在小屏幕上 如果没有足够的水平空间 它被限制在可用宽度的100% 现在这在大屏幕上看起来不错 但在小屏幕上 我们需要添加一些 间隙空间以防止文本紧贴视口边缘 网格列间隙 是一个方便的属性 它让我们在列之间添加间隙 在这种情况下,我们使用3REM作为间距 为了消除水平滚动 我们从中间列中减去两次的间距 现在我们的中间列不是一个完整的100%宽 而是100% 减去两侧的6rem间距 这说明了我们是如何 使用传统的定位技巧来制作全宽容器 在受限宽度的父容器内 或者使用CSS网格布局 来实现相同的结果 使用CSS网格 我们可以很容易地为非全屏部分添加多个列 也可以避免使用媒体查询 通过应用多功能单位,如数学函数和Fr单位 如果你发现这个教程有帮助 不要忘记点赞 分享并订阅更多令人兴奋的技巧和技巧 谢谢观看,下次见
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.