margin-top样式效果出错

本文探讨了CSS中子元素的margin-top属性如何影响其父级元素,特别是当父元素未设置padding-top值时,子元素的外边距会发生层叠现象。文中提供了两种解决方法:一是给父元素增加padding-top值;二是设置父元素的overflow为hidden。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   初始测试代码如下: 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        .con1 {
            background-color: seagreen;
            width: 600px;
            height: 600px;
        }
        .con2 {
            background-color: burlywood;
            width: 400px;
            height: 400px;
        }

        .con3 {
            background-color: #20c997;
            height: 200px;
            width: 200px;
        }
    </style>
</head>
<body>
<div class="con1">
    <div class="con2">
        <div class="con3"></div>
    </div>
</div>
</body>
</html>

初始效果如下:

当我们给con3设计一个margin-top30px;时,效果却不是我们想要的,呈现的是con1相对的效果,为什么呢?

       这是因为,当父容器没有设计padding-top的值或padding-top的值为0时,子容器的margin-top的效果就会层层上叠到父容器上,如con3---con2---con1,所以就如上图所示。

 

两种修改方法如下:

(1)给父容器增加padding-top的值:

            padding-top:1px;

 (2)给父容器增加overflow样式:

           overflow:hidden;

这样margin-top的效果就恢复正常了:

将你给出的文件上传方案汇总成一个完成的html页面,附带上基本的原生html,css,js,通过ajax请求一个服务接口,大佬级展现,同时在原有的代码上优化//发送ajax的请求 ParseError: KaTeX parse error: Expected '}', got 'EOF' at end of input: … var myXhr = .ajaxSettings.xhr(); if (myXhr.upload) { myXhr.upload.addEventListener(‘progress’, function(e) { if (e.lengthComputable) { /* e.loaded / e.total,已上传字节数/总的字节数 */ var percentComplete = (e.loaded / e.total) * 100; // 每秒更新进度数字 var te = setInterval(function() { //上传文件后使用文件名作为span标签的name值作为唯一标识 console.log(percentComplete.toFixed(2) + ‘%’); ParseError: KaTeX parse error: Expected '}', got 'EOF' at end of input: …模拟 (‘#box’).find(“ul”).append(‘<li>’ + ‘<span>’ + ‘<p class=“vedio_12301” style=“width: 115px;height: 125px;background-color: pink;float: left;”><img></p>’ + ‘<p style=“width: 225px;height: 75px;float: left;margin-top: 9px;font-size: 11px;border-top: 2px solid #ccc;margin-left: 5px;”>描述:该电影</p>’ + ‘<p style=“float: right;margin-top: 11px;margin-right: 11px;”><a href="Ajax-video.html?videoName=’+encodeURI(file1.name)+‘" target=“_blank”>播放</a></p>’ + ‘<p id=“anniu1” style=“float: right;margin-top: 11px;margin-right: 11px;”>下载</p>’ + ‘</span>’ + ‘</li>’) } }, 1); } }, false); } return myXhr;该前端代码上传的时候上传进度不会变只有0%和100%,请优化以下,最好加上依据网络波动停止下载,网络波动恢复后,添加一个开始下载图标,重新依据断点处开始下载,这有后端返回,你只需要优化前端这些功能即可,后端我自己尝试编写
03-09
我其他项目的登录界面代码如下:<template> <div class="login-container"> <div class="login-form"> <h2>用户登录</h2> <input v-model="username" placeholder="用户名"> <input v-model="password" type="password" placeholder="密码"> <button @click="handleLogin">登录</button> <!-- <router-link to="/register">注册账号</router-link> --> </div> </div> </template> <script setup> import { ref } from 'vue' import { useRouter } from 'vue-router' import axios from 'axios' import { ElMessage } from 'element-plus' const router = useRouter() const username = ref('') const password = ref('') const handleLogin = async () => { try { // 发送POST请求到登录接口 const response = await axios.post('http://172.26.26.43/dev-api/login', { username: username.value, password: password.value }) const { code, message, token } = response.data // 根据状态码判断登录是否成功 if (code === 200) { // 存储token到localStorage localStorage.setItem('token', token) ElMessage.success({ message:'登录成功!', customClass:'mobile-message', duration: 1000 }) setTimeout(() => { // 登录成功后跳转到详情页 router.push({ name: 'Detail' , params: { id: 123 } }) }, 1000) } else { alert(`登录失败: ${message}`) } } catch (error) { // 处理请求错误 console.error('登录请求出错:', error) alert('登录请求失败,请检查网络或联系管理员') } } </script> <style scoped> /* 添加移动端适配的样式 */ .login-container { display: flex; justify-content: center; align-items: center; min-height: 100vh; /* 使容器至少和视口一样高 */ background-color: #f5f5f5; /* 背景色 */ padding: 16px; /* 内边距 */ } .login-form { width: 100%; max-width: 400px; /* 最大宽度,避免在大屏幕上过宽 */ padding: 20px; background: #fff; border-radius: 8px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); } .login-form h2 { text-align: center; margin-bottom: 20px; color: #333; } .login-form input { width: 100%; padding: 12px; margin-bottom: 12px; border: 1px solid #ddd; border-radius: 4px; font-size: 16px; box-sizing: border-box; /* 确保宽度包含内边距和边框 */ } .login-form button { width: 100%; padding: 12px; background-color: #007bff; color: white; border: none; border-radius: 4px; font-size: 16px; cursor: pointer; } /* 按钮的点击区域已经足够大(高度44px左右) */ .login-form button:active { background-color: #0056b3; } .login-form a { display: block; text-align: center; margin-top: 12px; color: #007bff; text-decoration: none; } </style> 我想要复用改代码到我的uniapp登录页面里面
07-18
<template> <div class="login-container"> <div class="login-form"> <h2>用户登录</h2> <input v-model="username" placeholder="用户名"> <input v-model="password" type="password" placeholder="密码"> <button @click="handleLogin">登录</button> <!-- <router-link to="/register">注册账号</router-link> --> </div> </div> </template> <script setup> import { ref } from 'vue' import { useRouter } from 'vue-router' import axios from 'axios' import { ElMessage } from 'element-plus' const router = useRouter() const username = ref('') const password = ref('') const handleLogin = async () => { try { // 发送POST请求到登录接口 const response = await axios.post('http://172.26.26.43/dev-api/login', { username: username.value, password: password.value }) const { code, message, token } = response.data // 根据状态码判断登录是否成功 if (code === 200) { // 存储token到localStorage localStorage.setItem('token', token) ElMessage.success({ message:'登录成功!', customClass:'mobile-message', duration: 1000 }) setTimeout(() => { // 登录成功后跳转到详情页 router.push({ name: 'Detail' , params: { id: 123 } }) }, 1000) } else { alert(`登录失败: ${message}`) } } catch (error) { // 处理请求错误 console.error('登录请求出错:', error) alert('登录请求失败,请检查网络或联系管理员') } } </script> <style scoped> /* 添加移动端适配的样式 */ .login-container { display: flex; justify-content: center; align-items: center; min-height: 100vh; /* 使容器至少和视口一样高 */ background-color: #f5f5f5; /* 背景色 */ padding: 16px; /* 内边距 */ } .login-form { width: 100%; max-width: 400px; /* 最大宽度,避免在大屏幕上过宽 */ padding: 20px; background: #fff; border-radius: 8px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); } .login-form h2 { text-align: center; margin-bottom: 20px; color: #333; } .login-form input { width: 100%; padding: 12px; margin-bottom: 12px; border: 1px solid #ddd; border-radius: 4px; font-size: 16px; box-sizing: border-box; /* 确保宽度包含内边距和边框 */ } .login-form button { width: 100%; padding: 12px; background-color: #007bff; color: white; border: none; border-radius: 4px; font-size: 16px; cursor: pointer; } /* 按钮的点击区域已经足够大(高度44px左右) */ .login-form button:active { background-color: #0056b3; } .login-form a { display: block; text-align: center; margin-top: 12px; color: #007bff; text-decoration: none; } </style> 我什么我这个项目的登录界面可以正常登录,但是修改后:<template> <view class="login-container"> <view class="login-form"> <text class="login-title">用户登录</text> <input v-model="username" placeholder="用户名" class="login-input" placeholder-class="input-placeholder" /> <input v-model="password" type="password" placeholder="密码" class="login-input" placeholder-class="input-placeholder" /> <button class="login-button" :loading="loading" @click="handleLogin" >登录</button> </view> </view> </template> <script> import { ref } from 'vue' export default { setup() { const username = ref('') const password = ref('') const loading = ref(false) const handleLogin = async () => { // 验证输入 if (!username.value.trim()) { uni.showToast({ title: '请输入用户名', icon: 'none', duration: 2000 }) return } if (!password.value) { uni.showToast({ title: '请输入密码', icon: 'none', duration: 2000 }) return } loading.value = true try { // 正确使用 uni.request 并处理返回值 const [error, response] = await uni.request({ url: 'http://172.26.26.43/dev-api/login', method: 'POST', data: { username: username.value, password: password.value }, header: { 'Content-Type': 'application/json' }, timeout: 10000 // 10秒超时 }) // 处理请求错误 if (error) { throw new Error(`请求失败: ${error.errMsg}`) } // 检查响应状态 if (response.statusCode !== 200) { throw new Error(`服务器错误: ${response.statusCode}`) } // 解析响应数据 const { data } = response const { code, message, token } = data if (code === 200) { // 存储token uni.setStorageSync('token', token) uni.showToast({ title: '登录成功!', icon: 'success', duration: 1000 }) // 登录成功后跳转 setTimeout(() => { uni.redirectTo({ url: '/pages/index/index' }) }, 1000) } else { throw new Error(`登录失败: ${message || '未知错误'}`) } } catch (error) { console.error('登录请求出错:', error) uni.showToast({ title: error.message || '登录失败,请重试', icon: 'none', duration: 2000 }) } finally { loading.value = false } } return { username, password, loading, handleLogin } } } </script> <style scoped> /* 保持之前的样式不变 */ .login-container { display: flex; justify-content: center; align-items: center; min-height: 100vh; padding: 32rpx; background-color: #f5f5f5; } .login-form { width: 100%; max-width: 600rpx; padding: 60rpx; background: #fff; border-radius: 16rpx; box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1); } .login-title { display: block; text-align: center; margin-bottom: 60rpx; font-size: 40rpx; font-weight: bold; color: #333; } .login-input { width: 100%; height: 88rpx; padding: 0 24rpx; margin-bottom: 40rpx; border: 2rpx solid #ddd; border-radius: 12rpx; font-size: 32rpx; box-sizing: border-box; } .input-placeholder { color: #999; font-size: 32rpx; } .login-button { width: 100%; height: 88rpx; line-height: 88rpx; background-color: #007aff; color: white; font-size: 32rpx; border-radius: 12rpx; margin-top: 20rpx; } .login-button:active { background-color: #0062cc; opacity: 0.9; } </style> 用到uniapp里就登录不了
最新发布
07-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值