为什么你应该停止使用"传统"的margin和padding来设置CSS样式

随着使用网络的人比以往任何时候都多,确保网络体验对所有用户都可访问的需求变得更加迫切。这包括从从不同方向/角度((从右到左,从上到下等)。

当添加传统的 marginpadding 时,你明确地定义了来自各个方向(上、下、左和右)的边距和填充。这可能会在你的区域设置中创建一个看起来不错的布局,但当使用不同区域设置的用户以与你设计的方式不同的 方向/角度 和查看内容时,可能会感到困惑。

每个现代开发者都应该使用margin-inline/margin-blockpadding-inline/padding-block

我们来分析这些CSS属性是如何工作的,以及为什么我们要使用它们。

margin-inlinepadding-inline控制左右 margin/padding。我们不是明确定义与这些特定方向对应的边距,而是将它们定义为startend边距。虽然一个区域可能从左开始到右结束,但其他区域可能不是,所以以这种方式定义它们将确保无论区域设置如何,margin 和 padding 都会按预期显示。

同样,margin-blockpadding-block控制上下 margin/padding,使用相同的startend模式,而不是明确定义topbottom

要同时为多个方向定义 margin 或 padding,你可以使用:

  • margin-inline: [start值] [end值]; 用于左右边距

  • margin-block: [start值] [end值]; 用于上下边距

  • padding-inline: [start值] [end值]; 用于左右填充

  • padding-block: [start值] [end值]; 用于上下填充

要仅为一个方向定义边距或填充,你可以使用:

  • margin-inline-start(而不是margin-left

  • margin-inline-end(而不是margin-right

  • margin-block-start(而不是margin-top

  • margin-block-end(而不是margin-bottom

  • padding-inline-start(而不是padding-left

  • padding-inline-end(而不是padding-right

  • padding-block-start(而不是padding-top

  • padding-block-end(而不是padding-bottom

我们看一些例子:

// 传统
margin-left: 1.2em;

// 等效
margin-inline-start: 1.2em;

// 传统
padding-top: 5px;

// 等效
padding-block-start: 5px;

或者当为多个方向定义填充时:

// 传统
padding: 5px 10px 20px 15px;

// 等效
padding-block: 5px 20px;
padding-inline: 15px 10px;

当与使用flexbox进行内容对齐而不是float结合使用时,这些现代margin/padding模式更加强大。Flexbox是一个与方向无关的布局模型。它不是明确地将元素对齐到leftrighttopbottom,而是允许将内容对齐到其父元素的垂直和水平startend

例如:

// 在从左到右查看时将子元素对齐到右侧,或在从右到左查看时对齐到左侧。
parent-element {
   display: flex;
   align-items: flex-end;
}

即使您的应用程序或网站目前不支持根据地域以不同的方向/方位显示文本/内容,开发人员最终也应通过采用上述模式,努力使所有用户都能获得无障碍的网络体验。这是一个很容易实现的过渡,每个开发人员都应采用,以实现未来的无障碍访问。

最后:

CSS技巧与案例详解

vue2与vue3技巧合集

VueUse源码解读

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的带轮播图的购物网页示例: ```html <!DOCTYPE html> <html> <head> <title>购物网页示例</title> <style> .container { max-width: 800px; margin: 0 auto; padding: 20px; display: flex; flex-wrap: wrap; justify-content: space-between; } .product { width: 300px; margin-bottom: 40px; box-shadow: 0 0 5px rgba(0, 0, 0, 0.2); overflow: hidden; } .product img { width: 100%; height: 200px; object-fit: cover; } .product-info { padding: 20px; } .product-name { font-size: 24px; margin-bottom: 10px; } .product-price { font-size: 18px; color: red; } .slider { position: relative; overflow: hidden; height: 400px; margin-bottom: 40px; } .slider img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; z-index: -1; opacity: 0; transition: opacity 1s ease-in-out; } .slider img.active { opacity: 1; } .controls { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: row; } .controls button { background-color: transparent; border: none; color: white; font-size: 24px; margin: 0 10px; cursor: pointer; } .controls button:hover { color: gray; } </style> <script> // 获取轮播图容器和所有图片 const slider = document.querySelector('.slider'); const images = slider.querySelectorAll('img'); // 定义当前显示图片的索引和定时器 let currentIndex = 0; let timer; // 定义切换图片的函数 function switchImage(index) { // 隐藏当前显示的图片 images[currentIndex].classList.remove('active'); // 显示指定的图片 images[index].classList.add('active'); // 更新当前显示图片的索引 currentIndex = index; } // 定义自动切换图片的函数 function autoSwitch() { // 计算下一张要显示的图片的索引 const nextIndex = (currentIndex + 1) % images.length; // 切换图片 switchImage(nextIndex); } // 启动自动切换图片 timer = setInterval(autoSwitch, 3000); // 获取左右切换按钮并添加点击事件 const prevButton = document.querySelector('.prev-button'); prevButton.addEventListener('click', () => { // 计算上一张要显示的图片的索引 const prevIndex = (currentIndex - 1 + images.length) % images.length; // 切换图片 switchImage(prevIndex); // 停止自动切换 clearInterval(timer); // 重新启动自动切换 timer = setInterval(autoSwitch, 3000); }); const nextButton = document.querySelector('.next-button'); nextButton.addEventListener('click', () => { // 计算下一张要显示的图片的索引 const nextIndex = (currentIndex + 1) % images.length; // 切换图片 switchImage(nextIndex); // 停止自动切换 clearInterval(timer); // 重新启动自动切换 timer = setInterval(autoSwitch, 3000); }); </script> </head> <body> <div class="container"> <div class="slider"> <img src="https://picsum.photos/id/1018/800/400" class="active"> <img src="https://picsum.photos/id/1019/800/400"> <img src="https://picsum.photos/id/1020/800/400"> <img src="https://picsum.photos/id/1021/800/400"> <img src="https://picsum.photos/id/1022/800/400"> </div> <div class="product"> <img src="https://picsum.photos/id/1003/300/200"> <div class="product-info"> <div class="product-name">商品 1</div> <div class="product-price">¥ 100</div> </div> </div> <div class="product"> <img src="https://picsum.photos/id/1004/300/200"> <div class="product-info"> <div class="product-name">商品 2</div> <div class="product-price">¥ 200</div> </div> </div> <div class="product"> <img src="https://picsum.photos/id/1005/300/200"> <div class="product-info"> <div class="product-name">商品 3</div> <div class="product-price">¥ 300</div> </div> </div> <div class="product"> <img src="https://picsum.photos/id/1006/300/200"> <div class="product-info"> <div class="product-name">商品 4</div> <div class="product-price">¥ 400</div> </div> </div> <div class="product"> <img src="https://picsum.photos/id/1007/300/200"> <div class="product-info"> <div class="product-name">商品 5</div> <div class="product-price">¥ 500</div> </div> </div> <div class="controls"> <button class="prev-button">❮</button> <button class="next-button">❯</button> </div> </div> </body> </html> ``` 在这个示例中,我们使用了 HTML、CSS 和 JavaScript 实现了一个简单的购物网页,其中包含了一个带轮播图的商品列表。 在 HTML 中,我们使用了一个 `.container` 容器将所有商品包裹起来,并使用了 Flexbox 布局将它们按照一定的间距和布局进行排列。每个商品都是一个 `.product` 容器,包含了一个图片和商品信息。轮播图使用了和之前相同的代码。 在 CSS 中,我们定义了商品的样式,包括宽度、边框阴影、图片大小和商品信息的样式。轮播图的样式也和之前的一样。 在 JavaScript 中,我们使用了同样的轮播图代码,但是没有添加任何新的逻辑。 这个示例只是一个简单的示例,实际的购物网页中还需要添加更多的商品信息和交互功能,例如加入购物车、结算等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@大迁世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值