CSS margin-trim

文章介绍了CSS实验属性margin-trim,它能帮助修剪子元素和父容器相邻的外边距,但目前仅Safari版本16.4及以上支持,内联方向上无效。

margin-trim

🧪这是一个实验性的属性, 目前仅有 Safari 支持

看这个属性的名字就知道, 外边距修剪.

平常都会遇到一些排版上的问题, 比如垂直排列的元素之间增加下外边距

<div>
  <li>123</li>
  <li>456</li>
  <li>789</li>
  <li>000</li>
</div>
div {
  padding: 10px;
}
div > li {
  margin-bottom: 10px;
}
</style>

在这里插入图片描述

但是通常我们不希望最后一个元素增加下外边距, 因为列表容器已经有了下内边距, 因此需要对最后一个元素做特殊处理

li:last-child {
  margin-bottom: 0;
}

在这里插入图片描述

主角登场

说到这里 margin-trim 可以做什么呢? 哎, 把 margin-trim 加在列表元素的父容器上, 父容器就会修剪子元素和父容器相邻的子元素的外边距.

 div {
  padding: 10px;
  margin-trim: block; // 主角在这
}
div > li {
  margin-bottom: 10px;
}
/* li:last-child {
  margin-bottom: 0;
} */

我们从下图看到, 虽然每个子元素的下边距都是 10px, 但是和父容器相邻的子元素的外边距却被裁减了.

在这里插入图片描述

语法

在上面的例子我们通过 block 属性指定只能修剪逻辑块方向的外边距, 其实 margin-trim 的值可以是下面的任何一个

  • none
  • block
  • block-start
  • block-end
  • inline
  • inline-start
  • inline-end

主角的局限性

然而, 我们的主角还没有得到 Safari 的全部支持, 因为在内联方向上的 margin 裁减没!有!效!果!

在这里插入图片描述

在这里插入图片描述

为什么会这样呢? 因为在 inline 方向上的裁剪效果还没有定

在这里插入图片描述

兼容性

Safari >= 16.4 才支持

在这里插入图片描述
谢谢你看到这里😊

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>用户登录</title> <style> body { background-color: black; background-image: url(../../../static/imags/2.jpg); background-repeat: no-repeat; background-size: 1330px; } h1 { color: #fafafa; } .container { max-width: 250px; margin-left: 900px; padding: 20px; background-color: #eb5c0e; border-radius: 20px; box-shadow: 0 0 10px rgba(216, 84, 17, 0.1); margin-top: 250px; } .login { width: 400px; height: 200px; margin: 1000px; margin-top: 300px; border-radius: 5px; overflow: hidden; } .input { width: 200px; height: 30px; padding-left: 20px; margin-top: 15px; margin-left: 0px; border-radius: 10px; } .button { width: 100px; height: 40px; margin-top: 40px; margin-left: 60px; border-radius: 5px; border: none; outline: none; background-color: #ff8000; cursor: pointer; color: #ffffff; } </style> </head> <body> <div class="container"> <h1>用户登录</h1> <form onsubmit="event.preventDefault(); validateLogin();"> 用户名:<input type="text" id="username" class="input" /> <br /> 密码:<input type="password" id="password" class="input" /> <br /> <button id="btn" class="button" type="submit">登录</button> </form> </form> <script src="login.js"></script> </body> </html> function validateLogin() { var username = document.getElementById("username").value; var password = document.getElementById("password").value; if (username === "111" && password === "111") { let btn = document.getElementById("btn"); btn.onclick = () => { location.href = '主界面.html' } } else { alert("用户名或密码错误,请重新输入!"); } } 上述两段代码中添加:成功登录后在主页面显示昵称,保持登录状态,没有登录不让跳转到主界面(登录拦截)
08-16
<template> <div style="margin: 16px; position: relative;"> <!-- 顶部大图信息 --> <p><img id="img" class="imgLogo" src="../assets/img/log1.jpg" /></p> <div align="right" style="position: absolute; right: 0; top: 0;"> <span> <el-input style="width: 40%; font-size: 15px" v-model.trim="searchStr" placeholder="请输入搜索内容" v-on:keypress.enter="search()" clearable /> </span> <span> <el-button type="text" style="padding-right: 50px" v-bind:icon="Delete" @click="search()"> 搜索</el-button> </span> <el-button type="text" @click="gotoLoginPage()">登录</el-button> <el-button type="text" style="padding-right: 10px" @click="gotoUserRegisterPage()">注册</el-button> </div> <!-- 轮播图放在中间 --> <div class="center-banner"> <Banner04 :images="bannerImages" /> </div> </div> <p></p> <!-- 页面区域 --> <div style="" class="cont" align="center"> <p class="color"><span class="big">新</span>书速递</p> <p class="more"><a @click="goToSpacePage()">更多>></a></p> <div class="book-list"> <div class="book-item" v-for="(book, index) in books" :key="index"> <div class="book-info"> <div class="book-title">{{ book.书名 }}</div> <div class="book-author">{{ book.作者 }}</div> <div class="book-publisher">{{ book.出版社 }}</div> <div class="book-price">{{ book.单价 }}</div> </div> </div> </div> </div> <!-- 页侧区域 --> <div class="service"> <div id="local_service" class="chat_btn" @click="$message('请联系系统管理员!')"> <img src="../assets/img/log2.png" /> <el-button class="service-info" type="text" size="small">本地服务</el-button> </div> <div class="chat_btn"> <a href="https://wpa.qq.com/msgrd?v=3&uin=2193778066&site=qq&menu=yes" target="_blank"> <img src="../assets/img/log3.png" /> </a> <div class="service-info"> <el-button size="small" type="text">读者服务</el-button> </div> </div> <div class="chat_btn" @click="toTop" style="text-decoration: none;"> <img src="@/assets/img/log4.png" /> <div class="service-info"> <el-button size="small" type="text" @click="toTop()" aria-label="返回页面顶部">返回顶部</el-button> </div> </div> </div> </template> <script> import axios from 'axios'; import { ElInput, ElButton } from 'element-plus'; import { Delete } from '@element-plus/icons-vue'; import Banner04 from "@/components/Banner04.vue"; export default { name: "Index01", components: { ElInput, ElButton, Banner04, }, data() { return { searchStr: "", Delete, books: [], bannerImages: [ ] }; }, created() { axios .get("http://127.0.0.1/book/all") .then((res) => { console.log(res.data); this.books = res.data; }) .catch((err) => { console.log("获取数据失败" + err); }) }, methods: { search() { console.log("搜索内容:", this.searchStr); axios .get('http://127.0.0.1/book/searchBookName', { params: { bookNameInput: this.searchStr, }, }) .then((res) => { console.log(res.data); this.books = res.data; }) .catch((err) => { console.log("操作失败!" + err); }); }, gotoLoginPage() { const jumPath = this.$parent.jumPath; this.$router.push(jumPath || "/Login01"); }, gotoUserRegisterPage() { const jumPath = this.$parent.jumPath; this.$router.push(jumPath || "/UserRegister01"); }, toTop() { document.documentElement.scrollTop = 0; }, goToSpacePage() { this.$router.push("/Space01"); } } }; </script> <style> @import "../assets/css/index01.css"; </style> <style> .imgLogo { width: 60%; max-width: 100px; height: auto; display: block; } .center-banner { margin: 20px auto; width: 80%; max-width: 800px; text-align: center; } .cont { margin-top: 20px; text-align: center; width: 80%; margin: 0 auto; } .book-list { display: flex; flex-wrap: wrap; justify-content: flex-start; gap: 20px; padding: 20px; } .book-item { display: flex; width: 45%; min-width: 300px; padding: 15px; border: 1px solid #eee; margin-bottom: 20px; } .book-image { width: 100px; height: 140px; object-fit: cover; margin-right: 15px; } .book-info { flex: 1; text-align: left; } .book-title { font-weight: bold; margin-bottom: 8px; font-size: 16px; } .book-author { color: #666; margin-bottom: 5px; } .book-publisher { color: #666; margin-bottom: 5px; } .book-price { color: #f56c6c; font-weight: bold; margin-top: 8px; } .color { font-size: 24px; margin-bottom: 20px; text-align: left; padding-left: 20px; } .big { font-size: 32px; color: #f56c6c; } .more { text-align: right; margin-right: 20px; margin-bottom: 20px; } .more a { color: #409eff; cursor: pointer; text-decoration: none; } </style> 现在这个代码得到的布局不合理,有些功能之间相互遮挡,请帮我修改各个功能的布局以适应全屏,并生成修改后的代码
06-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值