CSS学习笔记:id与class、padding和margin、min-height和height

本文探讨了CSS中id与class的选择原则、padding与margin的区别及应用场景、min-height与height的正确用法,帮助开发者掌握更高效的样式调整技巧。

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

id和class到底要用哪一个?

  首先要明白id和class的各自的优缺点。这样才能根据他们的各自的特点进行使用。


  id的优点(class的缺点):id写在css用"#"选择器,class写在css中用"."选择器。"#"选择器的优先级高于"."选择器大约10倍,所以当你需要提升优先级的时候,id标签,或者id容器内的标签将是很容易和有效的。而class标签,或者class容器内的标签将可能导致优先级的提升失败。


  id的缺点(class的优点):id应该是唯一的,所以它的可复用性是很差的,而class是可以复用的。所以如果一块东西是多个页面,甚至一个页面都会使用多次的,那么一定要使用class来作为样式选择器。id是唯一的,当一个控件的id的产生是不可控的,那么这个id选择器将失去意义,但是任何一个控件即使是动态产生的,他的cssClass仍然是可定制的,所以当你的这个标签需要用服务器端控件替代的时候,而服务器端控件的id是不确定的,那么请使用class选择器,这样只要将服务器端控件的cssClass设为你class选择器的名称即可。(当然,这个还需要大量的经验的积累,项目做的多了就会逐步的改进) 

  padding和margin到底要用哪一个? 

  padding和margin可以让一块区域的外观显示完全一样。所以可能让很多人认为padding和margin是可以互换的。其实它们的差别很大,而且选择哪个需要认真和慎重地考虑。我认为对容器使用padding还是对容器内的标签使用margin的原则:当隐藏这个容器或者容器内的标签时(现实项目中其实经常需要将某个部件隐藏、显示),对整体布局影响最小为益。


  对于padding再说一句:ie6,ie7(FF)对带有padding样式的标签的宽度的解析是不一样的。ie6的标签宽度不包含padding-left和padding-right的值,而ie7和ff则是包含的。例如一个div的width设置100px,padding设为10px,而在ie6中它要占据的宽度是120px(包含10个padding-left和10个padding-right),而在ie7和ff中则占据100px的宽度。因为ie7和ff会认为100已经包含了20px的padding。 

  min-height和height

  如果你只需要兼容ie6那么你完全不需要注意min-height这个样式,因为ie6根本就不支持这个样式。但是当你的页面需要照顾到ie7和ff的时候,这个样式一定要注意。因为很多在ie6下设置了height=固定值的样式,当容器被里面的东西撑的大于这个高度的时候,ie7和ff是不会自适应高度的。从而导致布局的混乱。要想在ie6,ie7和ff中都可以自适应高度,正确的做法是设置min-height和用cssHack设置height。例如:

.52css.com]
  min-height:600px;
  _height:600px;


  这样,在容器里面的东西很少的时候,它显示固定高度600px,但当里面的东西很多的时候,它也会自适应的增长高度。


  对于height的设置一定要特别注意,如果是布局用的容器的height则需要特别的注意,否则在ff中会导致无法浮起,从而使布局混乱。 

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>高三勇者大冒险:终极备考RPG指南</title> <style> /* 游戏风格基础设置 */ :root { --quest-blue: #4e73df; --epic-purple: #6f42c1; --rare-green: #1cc88a; --common-gray: #858796; --legendary-orange: #f6c23e; --health-red: #e74a3b; --mana-blue: #36b9cc; --stamina-yellow: #f6c23e; --dark-bg: #2a3042; --light-text: #f8f9fc; } @import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&family=Noto+Sans+SC:wght@400;700&display=swap'); body { background-color: var(--dark-bg); color: var(--light-text); font-family: 'Noto Sans SC', sans-serif; line-height: 1.6; background-image: url('https://img.freepik.com/free-vector/pixel-art-game-background-nature-landscape_107791-740.jpg'); background-size: cover; background-attachment: fixed; padding: 0; margin: 0; } .rpg-container { max-width: 1000px; margin: 0 auto; background-color: rgba(42, 48, 66, 0.9); border: 4px solid var(--epic-purple); border-radius: 10px; box-shadow: 0 0 30px rgba(110, 66, 193, 0.5); overflow: hidden; } /* 游戏标题样式 */ .rpg-header { background: linear-gradient(135deg, var(--quest-blue), var(--epic-purple)); padding: 30px; text-align: center; position: relative; border-bottom: 5px solid var(--legendary-orange); } .rpg-title { font-family: 'Press Start 2P', cursive; font-size: 2.2rem; color: white; text-shadow: 3px 3px 0 #000, -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000; margin-bottom: 15px; letter-spacing: 2px; } .rpg-subtitle { font-size: 1.2rem; color: rgba(255,255,255,0.8); font-weight: bold; } /* 角色状态栏 */ .character-status { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; padding: 15px; background-color: rgba(0,0,0,0.3); border-bottom: 2px solid var(--quest-blue); } .status-bar { display: flex; flex-direction: column; align-items: center; } .status-label { font-size: 0.8rem; color: var(--light-text); margin-bottom: 5px; } .status-value { font-weight: bold; font-size: 1.2rem; } .health { color: var(--health-red); } .mana { color: var(--mana-blue); } .stamina { color: var(--stamina-yellow); } .level { color: var(--rare-green); } /* 任务日志区域 */ .quest-log { padding: 20px; } .chapter-title { font-family: 'Press Start 2P', cursive; font-size: 1.2rem; color: var(--legendary-orange); margin: 30px 0 20px; padding-bottom: 10px; border-bottom: 2px dashed var(--epic-purple); text-shadow: 2px 2px 0 #000; } /* 任务卡片样式 */ .quest-card { background: linear-gradient(135deg, rgba(46, 52, 78, 0.8), rgba(32, 38, 58, 0.8)); border: 2px solid var(--quest-blue); border-radius: 8px; padding: 15px; margin-bottom: 20px; position: relative; box-shadow: 0 5px 15px rgba(0,0,0,0.3); transition: transform 0.3s; } .quest-card:hover { transform: translateY(-5px); border-color: var(--rare-green); } .quest-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; } .quest-name { font-weight: bold; font-size: 1.1rem; color: white; display: flex; align-items: center; } .quest-type { display: inline-block; padding: 3px 8px; border-radius: 4px; font-size: 0.7rem; font-weight: bold; margin-left: 10px; } .main-quest { background-color: var(--epic-purple); } .daily-quest { background-color: var(--quest-blue); } .skill-quest { background-color: var(--rare-green); } .event-quest { background-color: var(--legendary-orange); color: #000; } .quest-rewards { display: flex; } .reward { margin-left: 10px; font-size: 0.8rem; background-color: rgba(0,0,0,0.3); padding: 3px 8px; border-radius: 4px; border: 1px solid var(--common-gray); } .quest-description { margin: 10px 0; line-height: 1.5; } .quest-objectives { margin-top: 15px; } .objective { display: flex; align-items: center; margin-bottom: 8px; padding-left: 25px; position: relative; } .objective:before { content: "◻"; position: absolute; left: 5px; color: var(--common-gray); } .objective.completed:before { content: "✓"; color: var(--rare-green); } /* 技能树样式 */ .skill-tree { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 15px; margin-top: 20px; } .skill-card { background-color: rgba(32, 38, 58, 0.8); border: 2px solid var(--common-gray); border-radius: 8px; padding: 15px; transition: all 0.3s; } .skill-card:hover { border-color: var(--rare-green); box-shadow: 0 0 15px rgba(28, 200, 138, 0.3); } .skill-name { font-weight: bold; color: white; margin-bottom: 5px; display: flex; justify-content: space-between; } .skill-level { color: var(--legendary-orange); } .skill-description { font-size: 0.9rem; color: rgba(255,255,255,0.7); } .cooldown { margin-top: 10px; font-size: 0.8rem; color: var(--mana-blue); } /* 游戏底部区域 */ .rpg-footer { text-align: center; padding: 20px; background-color: rgba(0,0,0,0.5); border-top: 2px solid var(--epic-purple); } .save-button { background: linear-gradient(135deg, var(--rare-green), #17a673); color: white; border: none; padding: 12px 25px; font-size: 1.1rem; font-weight: bold; border-radius: 50px; cursor: pointer; box-shadow: 0 5px 15px rgba(28, 200, 138, 0.4); transition: all 0.3s; } .save-button:hover { transform: translateY(-3px); box-shadow: 0 8px 20px rgba(28, 200, 138, 0.6); } /* 响应式设计 */ @media (max-width: 768px) { .character-status { grid-template-columns: repeat(2, 1fr); } .rpg-title { font-size: 1.5rem; } .skill-tree { grid-template-columns: 1fr; } } </style> </head> <body> <div class="rpg-container"> <div class="rpg-header"> <h1 class="rpg-title">高三勇者大冒险</h1> <p class="rpg-subtitle">终极备考RPG指南 &middot; 2025届黄金版</p> </div> <div class="character-status"> <div class="status-bar"> <span class="status-label">生命值</span> <span class="status-value health">❤️ 85/100</span> </div> <div class="status-bar"> <span class="status-label">专注力</span> <span class="status-value mana">🔵 70/100</span> </div> <div class="status-bar"> <span class="status-label">耐力</span> <span class="status-value stamina">🟡 90/100</span> </div> <div class="status-bar"> <span class="status-label">等级</span> <span class="status-value level">⭐ Lv.5</span> </div> </div> <div class="quest-log"> <h2 class="chapter-title">第一章: 新手村准备</h2> <div class="quest-card"> <div class="quest-header"> <span class="quest-name">装备你的武器库 <span class="quest-type main-quest">主线任务</span> </span> <div class="quest-rewards"> <span class="reward">EXP +200</span> <span class="reward">金币 +500</span> </div> </div> <div class="quest-description"> 收集并整理你的学习装备:五科笔记本、错题本、历年真题集、彩色标记笔。记得定期维护你的装备! </div> <div class="quest-objectives"> <div class="objective completed">购买各科笔记本</div> <div class="objective completed">准备错题本系统</div> <div class="objective">收集近5年真题</div> <div class="objective">建立文具补给包</div> </div> </div> <h2 class="chapter-title">第二章: 每日修行</h2> <div class="quest-card"> <div class="quest-header"> <span class="quest-name">晨读的智慧 <span class="quest-type daily-quest">每日任务</span> </span> <div class="quest继续完成代码
05-31
<template> <div class="editor-layout"> <!-- 固定头部 --> <header class="app-header"> <button class="back-btn" @click="handleBack"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M19 12H5M12 19l-7-7 7-7"/> </svg> </button> <h1 class="app-title">AI笔记编辑器</h1> <div class="header-right"> <button class="history-btn" @click="toggleHistory"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <circle cx="12" cy="12" r="10"></circle> <polyline points="12 6 12 12 16 14"></polyline> </svg> <span>历史记录</span> </button> <div class="user-avatar" @click="goToProfile"> <div class="avatar-placeholder"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path> <circle cx="12" cy="7" r="4"></circle> </svg> </div> </div> </div> </header> <!-- 固定工具栏 --> <div class="fixed-toolbar"> <Toolbar :editor="editorRef" :defaultConfig="toolbarConfig" /> </div> <!-- 编辑器区域 --> <div class="editor-container"> <Editor v-model="valueHtml" :defaultConfig="editorConfig" @onChange="handleChange" @onCreated="handleCreated" @onDestroyed="handleDestroyed" @onFocus="handleFocus" @onBlur="handleBlur" @customAlert="customAlert" @customPaste="customPaste" /> </div> <!-- 历史记录侧边栏 --> <div class="history-sidebar" :class="{ active: showHistory }"> <div class="sidebar-header"> <h2>历史记录</h2> <button class="close-btn" @click="toggleHistory"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"> <line x1="18" y1="6" x2="6" y2="18"></line> <line x1="6" y1="6" x2="18" y2="18"></line> </svg> </button> </div> <div class="history-list"> <div v-for="(item, index) in historyItems" :key="index" class="history-item"> <div class="history-title">{{ item.title }}</div> <div class="history-date">{{ item.date }}</div> </div> </div> </div> <!-- 历史记录遮罩 --> <div v-if="showHistory" class="sidebar-mask" @click="toggleHistory"></div> </div> </template> <script setup> import { onBeforeUnmount, ref, shallowRef } from 'vue' import { Editor, Toolbar } from '@wangeditor/editor-for-vue' import '@wangeditor/editor/dist/css/style.css' // 编辑器实例 const editorRef = shallowRef() // 内容 HTML const valueHtml = ref('<h1>欢迎使用AI笔记编辑器</h1><p>这是一个功能强大的富文本编辑器,支持多种格式功能。</p><p>尝试使用工具栏上的功能来编辑内容!</p>') // 编辑器配置 const editorConfig = { placeholder: '请输入内容...', MENU_CONF: { insertImage: { checkImage(src) { if (src.indexOf("http") !== 0) { return "图片网址必须以 http/https 开头"; } return true; }, }, } } // 工具栏配置 const toolbarConfig = { toolbarKeys: [ 'headerSelect', 'bold', 'italic', 'underline', 'through', 'color', 'bgColor', 'fontSize', 'fontFamily', 'lineHeight', 'bulletedList', 'numberedList', 'todo', 'justifyLeft', 'justifyRight', 'justifyCenter', 'insertLink', 'insertImage', 'insertTable', 'codeBlock', 'blockquote', 'divider', 'emotion', 'undo', 'redo' ] } // 历史记录相关状态 const showHistory = ref(false) const historyItems = ref([ { title: 'AI生成的学习笔记', date: '2023-10-15 14:30' }, { title: '项目会议记录', date: '2023-10-14 09:45' }, { title: '技术方案设计', date: '2023-10-12 16:20' }, { title: '读书笔记 - 人工智能导论', date: '2023-10-10 11:15' }, { title: '周计划安排', date: '2023-10-08 08:30' } ]) // 编辑器回调函数 const handleCreated = (editor) => { editorRef.value = editor console.log("编辑器已创建", editor) } const handleChange = (editor) => { console.log("内容变化:", editor.children) } const handleDestroyed = (editor) => { console.log('编辑器已销毁', editor) } const handleFocus = (editor) => { console.log('编辑器获得焦点', editor) } const handleBlur = (editor) => { console.log('编辑器失去焦点', editor) } const customAlert = (info, type) => { alert(`【系统提示】${type} - ${info}`) } const customPaste = (editor, event, callback) => { console.log('粘贴事件', event) callback(true) // 继续默认的粘贴行为 } // 及时销毁编辑器 onBeforeUnmount(() => { const editor = editorRef.value if (editor == null) return editor.destroy() }) // 头部按钮功能 const handleBack = () => { alert('返回操作') } const toggleHistory = () => { showHistory.value = !showHistory.value } const goToProfile = () => { alert('跳转到个人用户管理界面') } </script> <style> /* 基础样式重置 */ * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } .editor-layout { position: relative; height: 100vh; overflow: hidden; background: linear-gradient(135deg, #f5f7fa 0%, #e4edf5 100%); } /* 固定头部样式 */ .app-header { position: fixed; top: 0; left: 0; right: 0; height: 60px; display: flex; align-items: center; padding: 0 20px; background: #ffffff; box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1); z-index: 1000; transition: all 0.3s ease; } .back-btn { width: 40px; height: 40px; border: none; background: none; cursor: pointer; border-radius: 50%; display: flex; align-items: center; justify-content: center; transition: all 0.2s ease; } .back-btn:hover { background: #f0f5ff; transform: translateX(-2px); } .back-btn svg { width: 20px; height: 20px; color: #4a6cf7; } .app-title { flex: 1; text-align: center; font-size: 1.4rem; font-weight: 600; color: #1a1a1a; letter-spacing: 0.5px; } .header-right { display: flex; align-items: center; gap: 15px; } .history-btn { display: flex; align-items: center; gap: 6px; padding: 8px 15px; background: #f0f5ff; border: none; border-radius: 20px; color: #4a6cf7; font-weight: 500; font-size: 0.9rem; cursor: pointer; transition: all 0.2s ease; } .history-btn:hover { background: #e1e9ff; transform: translateY(-1px); box-shadow: 0 2px 8px rgba(74, 108, 247, 0.2); } .history-btn svg { width: 18px; height: 18px; } .user-avatar { width: 40px; height: 40px; border-radius: 50%; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); display: flex; align-items: center; justify-content: center; cursor: pointer; transition: all 0.3s ease; box-shadow: 0 4px 10px rgba(118, 75, 162, 0.3); } .user-avatar:hover { transform: scale(1.05); box-shadow: 0 6px 15px rgba(118, 75, 162, 0.4); } .avatar-placeholder { width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: rgba(255, 255, 255, 0.2); } .avatar-placeholder svg { width: 18px; height: 18px; color: white; } /* 固定工具栏样式 */ .fixed-toolbar { position: fixed; top: 60px; /* 在头部下方 */ left: 0; right: 0; z-index: 999; background: white; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05); border-bottom: 1px solid #eaeef5; padding: 0 10px; } /* 编辑器容器样式 */ .editor-container { margin-top: 110px; /* 头部高度 + 工具栏高度 */ height: calc(100vh - 110px); overflow-y: auto; padding: 20px; background: white; border-radius: 12px; margin-left: 20px; margin-right: 20px; box-shadow: 0 5px 20px rgba(0, 0, 0, 0.05); } /* 历史记录侧边栏 */ .history-sidebar { position: fixed; top: 0; right: -400px; width: 380px; height: 100vh; background: white; z-index: 2000; box-shadow: -5px 0 25px rgba(0, 0, 0, 0.1); transition: right 0.4s cubic-bezier(0.23, 1, 0.32, 1); display: flex; flex-direction: column; } .history-sidebar.active { right: 0; } .sidebar-header { display: flex; justify-content: space-between; align-items: center; padding: 20px; border-bottom: 1px solid #eee; } .sidebar-header h2 { color: #333; font-weight: 600; } .close-btn { background: none; border: none; width: 36px; height: 36px; border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; transition: all 0.2s ease; } .close-btn:hover { background: #f5f7fa; } .close-btn svg { width: 20px; height: 20px; color: #666; } .history-list { flex: 1; overflow-y: auto; padding: 15px; } .history-item { padding: 15px; border-radius: 8px; margin-bottom: 10px; background: #f9fbfd; transition: all 0.2s ease; cursor: pointer; border-left: 3px solid #4a6cf7; } .history-item:hover { background: #edf3ff; transform: translateX(5px); } .history-title { font-weight: 500; color: #1a1a1a; margin-bottom: 5px; } .history-date { font-size: 0.85rem; color: #666; } /* 历史记录遮罩 */ .sidebar-mask { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.4); z-index: 1500; backdrop-filter: blur(2px); } /* 响应式设计 */ @media (max-width: 768px) { .app-header { padding: 0 10px; } .app-title { font-size: 1.1rem; } .history-btn span { display: none; } .editor-container { margin-left: 10px; margin-right: 10px; padding: 15px; } .history-sidebar { width: 85%; } } /* 滚动条美化 */ ::-webkit-scrollbar { width: 8px; } ::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 4px; } ::-webkit-scrollbar-thumb { background: #c1c1c1; border-radius: 4px; } ::-webkit-scrollbar-thumb:hover { background: #a8a8a8; } </style> 我是说这个编辑器调宽一点,然后滚轮是页面的
最新发布
07-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值