CSS布局 0x0 dispaly和margin

本文深入探讨CSS中的`display`属性,解释block、inline和none等基本概念,以及它们如何影响网页布局。同时,文章还介绍了如何使用`margin: auto;`实现元素的自动居中,帮助初学者更好地掌握CSS布局技巧。

"display"属性

    CSS display 属性

    display 是CSS中最重要的用于控制布局的属性。每个元素都有一个默认的 display 值,这与元素的类型有关。对于大多数元素它们的默认值通常是 block 或 inline 。一个 block 元素通常被叫做块级元素。一个 inline 元素通常被叫做行内元素。

    block

    div 是一个标准的块级元素。一个块级元素会新开始一行并且尽可能撑满容器。其他常用的块级元素包括 p 、 form 和HTML5中的新元素: header 、 footer 、 section 等等。

    inline

    span 是一个标准的行内元素。一个行内元素可以在段落中包裹一些文字而不会打乱段落的布局。 a 元素是最常用的行内元素,它可以被用作链接。

    none

    另一个常用的display值是 none 。一些特殊元素的默认 display 值是它,例如 script 。 display:none 通常被 JavaScript 用来在不删除元素的情况下隐藏或显示元素。

    它和 visibility 属性不一样。把 display 设置成 none 元素不会占据它本来应该显示的空间,但是设置成 visibility: hidden; 还会占据空间。

    其他 display 值

margin: auto;

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>margin</title>
        <style>
            #main{
                width: 600px;
                margin: 0 auto;
                background-color: #fa0;
            }
        </style>
    </head>
    <body>
        <div id="main">设置块级元素的 width 可以防止它从左到右撑满整个容器。然后你就可以设置左右外边距为 auto 来使其水平居中。元素会占据你所指定的宽度,然后剩余的宽度会一分为二成为左右外边距。

                <br>唯一的问题是,当浏览器窗口比元素的宽度还要窄时,浏览器会显示一个水平滚动条来容纳页面。</div>
    </body>
</html>

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>margin</title>
        <style>
            #main{
                max-width: 600px;
                margin: 0 auto;
                background-color: #fa0;
            }
        </style>
    </head>
    <body>
        <div id="main">设置块级元素的 width 可以防止它从左到右撑满整个容器。然后你就可以设置左右外边距为 auto 来使其水平居中。元素会占据你所指定的宽度,然后剩余的宽度会一分为二成为左右外边距。
                <br>唯一的问题是,当浏览器窗口比元素的宽度还要窄时,浏览器会显示一个水平滚动条来容纳页面。
                <br>在这种情况下使用 max-width 替代 width 可以使浏览器更好地处理小窗口的情况。
            </div>
    </body>
</html>

 

<style> #chatContainer{ width: 700px; margin: 20px auto; border: 1px solid #ccc; display: flex; flex-direction: column; background: #f9f9f9; } #chatBox { flex: 1; height: 800px; overflow-y: auto; padding: 10px; display: flex; flex-direction: column-reverse; } .msg{ max-width: 75%; margin: 8px; padding: 6px 10px; border-radius: 10px; position: relative; /* word-break: break-word; */ } .msg-info{ font-size: 11px; color: gray; margin-bottom: 2px; } .msg.inbox{ background: #f6f2f2; text-align: left; margin-right: auto; align-self: flex-start; } .msg.outbox{ background: #f6dcfc; text-align: left; margin-left: auto; /* color: white; */ align-self: flex-end; } .meta{ font-size: 14px; color : #666; margin-bottom: 4px; } #loadTip{ text-align: center; color: gray; font-size: 12px; } #editor{ border-top: 1px solid #ccc; padding: 8px; display: flex; flex-direction: column; background: #fff; } #inputContent{ width: 100%; height: 60px; resize: none; padding: 6px; box-sizing: border-box; margin-bottom: 6px; font-size: 14px; } #editorButtons{ display: flex; justify-content: flex-end; gap: 8px; } button{ padding: 6px 12px; border: none; border-radius: 5px; cursor: pointer; /* transition: background-color 0.2s ease; */ } .btn:hover{ background-color: #34a8b5; } #save{ background-color: #4acbd6; color: #f7f4f4; } #send{ background-color: #4acbd6; color: #f7f4f4; } </style> <script type="text/javascript"> (function() { var indexGlobal; var msgPageListGlobal; var smsMsgPageList; var webUIData; if ($.mainParam) { optPageGlobal = $.mainParam[0]; indexGlobal = $.mainParam[1]; msgPageListGlobal = $.mainParam[2]; } else { $.loadMain("lteSmsAllMsg.htm", null); } var lenSingleMsgArray = new Array(new Array(160, 146, 153, 153, 153), new Array(70, 64, 67, 67, 67)); var lenMsgArray = new Array(765, 335); var lenSingleMsgArrayIndex = 0; var lenSingleAddInGSM7Ext = new Array(0, 0, 0, 0, 0); var TotalLen = 0; function doEscapeCharEncode(string) { var showStr = ""; for (var i = 0; i < string.length; i++) { if (string.charAt(i) == '\n') { showStr += '\x12'; } else if (string.charAt(i) == '\r') { showStr += '\x11'; } else { showStr += string[i]; } } return showStr; } function doEscapeChar(string) { var showStr = ""; for (var i = 0; i < string.length; i++) { if (string.charCodeAt(i) == 18) { showStr += "\n"; } else if (string.charCodeAt(i) == 17) { showStr += "\r"; } else { showStr += string[i]; } } return showStr; } function outputMsgLen() { var i = 0; var lenTotalNow = $("#inputContent").val().length; var lenExceed = 0; var lenTotalAddTemp = 0; var curCharSpecial = 0; var flag; var curMsgIndex = 0; var curMsgIndexLenLeft = 0; var curMsgLenUpLimit4 = 0; var curMsgLenUpLimit3 = 0; var curMsgLenUpLimit2 = 0; var GSM_7BIT_NUM = 128; lenSingleAddInGSM7Ext[0] = 0; lenSingleAddInGSM7Ext[1] = 0; lenSingleAddInGSM7Ext[2] = 0; lenSingleAddInGSM7Ext[3] = 0; lenSingleAddInGSM7Ext[4] = 0; $("#inputLen").css("color", "#000000"); lenSingleMsgArrayIndex = 0; for (var i = 0; i < lenTotalNow; i++) { flag = 0; curCharSpecial = 0; if ( $("#inputContent").val().charCodeAt(i) == 94 || $("#inputContent").val().charCodeAt(i) == 123 || $("#inputContent").val().charCodeAt(i) == 125 || $("#inputContent").val().charCodeAt(i) == 92 || $("#inputContent").val().charCodeAt(i) == 91 || $("#inputContent").val().charCodeAt(i) == 126 || $("#inputContent").val().charCodeAt(i) == 93 || $("#inputContent").val().charCodeAt(i) == 124 || $("#inputContent").val().charCodeAt(i) == 8364 ) { flag = 1; } else { var arrayGSM_7DefaultTable = [ 0x0040, 0x00A3, 0x0024, 0x00A5, 0x00E8, 0x00E9, 0x00F9, 0x00EC, 0x00F2, 0x00C7, 0x000A, 0x00D8, 0x00F8, 0x000D, 0x00C5, 0x00E5, 0x0394, 0x005F, 0x03A6, 0x0393, 0x039B, 0x03A9, 0x03A0, 0x03A8, 0x03A3, 0x0398, 0x039E, 0x001B, 0x00C6, 0x00E6, 0x00DF, 0x00C9, 0x0020, 0x0021, 0x0022, 0x0023, 0x00A4, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F, 0x00A1, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A, 0x00C4, 0x00D6, 0x00D1, 0x00DC, 0x00A7, 0x00BF, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F, 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x00E4, 0x00F6, 0x00F1, 0x00FC, 0x00E0 ]; for(var j = 0; j < GSM_7BIT_NUM; j++) { if ($("#inputContent").val().charCodeAt(i) == arrayGSM_7DefaultTable[j]) { flag = 1; break; } } } if (flag == 0) { lenSingleMsgArrayIndex = 1; } lenTotalAddTemp = lenSingleAddInGSM7Ext[0] + lenSingleAddInGSM7Ext[1] + lenSingleAddInGSM7Ext[2] + lenSingleAddInGSM7Ext[3] + lenSingleAddInGSM7Ext[4]; curMsgLenUpLimit4 = lenSingleMsgArray[lenSingleMsgArrayIndex][0] + lenSingleMsgArray[lenSingleMsgArrayIndex][1] + lenSingleMsgArray[lenSingleMsgArrayIndex][2] + lenSingleMsgArray[lenSingleMsgArrayIndex][3]; curMsgLenUpLimit3 = lenSingleMsgArray[lenSingleMsgArrayIndex][0] + lenSingleMsgArray[lenSingleMsgArrayIndex][1] + lenSingleMsgArray[lenSingleMsgArrayIndex][2]; curMsgLenUpLimit2 = lenSingleMsgArray[lenSingleMsgArrayIndex][0] + lenSingleMsgArray[lenSingleMsgArrayIndex][1]; if (lenSingleMsgArrayIndex == 0) { if ( $("#inputContent").val().charCodeAt(i) == 94 || $("#inputContent").val().charCodeAt(i) == 123 || $("#inputContent").val().charCodeAt(i) == 125 || $("#inputContent").val().charCodeAt(i) == 92 || $("#inputContent").val().charCodeAt(i) == 91 || $("#inputContent").val().charCodeAt(i) == 126 || $("#inputContent").val().charCodeAt(i) == 93 || $("#inputContent").val().charCodeAt(i) == 124 || $("#inputContent").val().charCodeAt(i) == 8364 ) { curCharSpecial = 1; //special character , etc { } [ ] ; } } else { lenTotalAddTemp = 0; } if ( (i + 1 + lenTotalAddTemp + curCharSpecial) > lenMsgArray[lenSingleMsgArrayIndex]) { curMsgIndexLenLeft = 0; curMsgIndex = 5; if (curCharSpecial == 1) { lenSingleAddInGSM7Ext[4]++; } break; } else if ( (i + 1 + lenTotalAddTemp + curCharSpecial) > curMsgLenUpLimit4 ) { curMsgIndexLenLeft = lenMsgArray[lenSingleMsgArrayIndex] - (lenTotalNow + lenTotalAddTemp + curCharSpecial); curMsgIndex = 5; if (curCharSpecial == 1) { lenSingleAddInGSM7Ext[4]++; } } else if ( (i + 1 + lenTotalAddTemp + curCharSpecial) > curMsgLenUpLimit3 ) { curMsgIndexLenLeft = curMsgLenUpLimit4 - (lenTotalNow + lenTotalAddTemp + curCharSpecial); curMsgIndex = 4; if (curCharSpecial == 1) { lenSingleAddInGSM7Ext[3]++; } } else if ( (i + 1 + lenTotalAddTemp + curCharSpecial) > curMsgLenUpLimit2 ) { curMsgIndexLenLeft = curMsgLenUpLimit3 - (lenTotalNow + lenTotalAddTemp + curCharSpecial); curMsgIndex = 3; if (curCharSpecial == 1) { lenSingleAddInGSM7Ext[2]++; } } else if ( (i + 1 + lenTotalAddTemp + curCharSpecial) > lenSingleMsgArray[lenSingleMsgArrayIndex][0] ) { curMsgIndexLenLeft = curMsgLenUpLimit2 - (lenTotalNow + lenTotalAddTemp + curCharSpecial); curMsgIndex = 2; if (curCharSpecial == 1) { lenSingleAddInGSM7Ext[1]++; } } else if ( (i + 1 + lenTotalAddTemp + curCharSpecial) <= lenSingleMsgArray[lenSingleMsgArrayIndex][0] ) { curMsgIndexLenLeft = lenSingleMsgArray[lenSingleMsgArrayIndex][0] - (lenTotalNow + lenTotalAddTemp + curCharSpecial); curMsgIndex = 1; if (curCharSpecial == 1) { lenSingleAddInGSM7Ext[0]++; } } } lenTotalAddTemp = lenSingleAddInGSM7Ext[0] + lenSingleAddInGSM7Ext[1] + lenSingleAddInGSM7Ext[2] + lenSingleAddInGSM7Ext[3] + lenSingleAddInGSM7Ext[4]; $("#inputContent").prop("maxlength", lenMsgArray[lenSingleMsgArrayIndex] - lenTotalAddTemp); if ( (lenTotalNow+lenTotalAddTemp) > lenMsgArray[lenSingleMsgArrayIndex]) { lenExceed = lenTotalNow+lenTotalAddTemp - lenMsgArray[lenSingleMsgArrayIndex]; $("#inputLen").text(n_str.lteSmsNewMsg.messageTooLongAlert.replace('%s', lenMsgArray[lenSingleMsgArrayIndex]).replace('%d', lenExceed)); $("#inputLen").css("color", "#ff0000"); return; } if(curMsgIndex != 0) { $("#inputLen").text("(" + curMsgIndexLenLeft + "/" + curMsgIndex + ")"); } else { $("#inputLen").text(""); } } $("textarea#inputContent").keyup(function() { if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { /* I have found out that IE8 will crash on keyup, so do nothing here, instead we do it in keydown */ /* outputMsgLen(); */ } else { setTimeout(outputMsgLen, 10); } }); $("textarea#inputContent").keydown(function() { if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { outputMsgLen(); } else { } }); var pageSize = 6; let chatBox = document.getElementById("chatBox") var currentPage = 0; var numRows = msgPageListGlobal[indexGlobal].msgCount; var numPages = Math.ceil(numRows / pageSize); //讯息获取 function fetchMessages(callback){ currentPage += 1; $.dm.set({ oid: "DEV2_LTE_SMS_MSGBOX", data: {"ConvsatId": msgPageListGlobal[indexGlobal].index, "PageNumber": currentPage, "TotalNumber": numRows, }, callback: { success: function() { var showContent= ""; $.dm.getList({ oid: "DEV2_LTE_SMS_MSGENTRY", data: {}, callback: { success: function(data) { let newMsgs = data.map(item =>{ showContent = doEscapeChar(item.content); this.content = showContent; return { content: $.htmlEncode(showContent), address: msgPageListGlobal[indexGlobal].phoneNumber, date: this.time, type: this.direction == 0 ? "OUTBOX" : "INBOX" }; }); callback(newMsgs); } } }); } } }); } //渲染讯息 function renderMessages(msgs, insertTop = false){ msgs.forEach(msg => { var wrapper = document.createElement("div"); wrapper.className = "msg " + (msg.type === "INBOX" ? "inbox" : "outbox"); var info = document.createElement("div"); info.className = "msg-info"; info.textContent = msg.type === "INBOX" ? msg.address + " | " + msg.date : msg.date; var div = document.createElement("div"); div.textContent = msg.content; wrapper.appendChild(info); wrapper.appendChild(div); console.log(wrapper); if (insertTop){ chatBox.insertBefore(wrapper, chatBox.firstChild);//firstchilid } else{ chatBox.appendChild(wrapper); } }); } function loadInitial() { fetchMessages(function(newMsgs){ renderMessages(newMsgs); chatBox.scrollTop = chatBox.scrollHeight; }); } function init() { $.addLoading(); $('#inputLen').TPAria('role', 'alert').TPAria('aria-live', 'polite'); //初始化页面,加载对应内容 $("#t_et").text($.tpLang.lteSmsConvsat_nstr.t_et); loadInitial(); $.removeLoading(); } chatBox.addEventListener("scroll", function(){ if (chatBox.scrollTop === 0) { if (currentPage >= numPages) { $("#loadTip").text("No more messges"); return; } $.addLoading(); var oldHeight = chatBox.scrollHeight; fetchMessages(function(newMsgs){ renderMessages(newMsgs, true); var newHeight = chatBox.scrollHeight; chatBox.scrollTop = newHeight - oldHeight; $.removeLoading(); }); } }); $("#chatBox").on("scroll", function(){ if ($(this).scrollTop() == 0){ if (currentPage >= numPages) { $("#loadTip").text("No more messges"); return; } $.addLoading(); var oldHeight = chatBox.scrollHeight; fetchMessages(function(newMsgs){ renderMessages(newMsgs, true); var newHeight = chatBox.scrollHeight; chatBox.scrollTop = newHeight - oldHeight; $.removeLoading(); }); } }); $("#back").click(function() { $.confirm(c_str.isDoNotSaveStr, function(ret) { $.loadMain("lteSmsAllMsg.htm"); } ); }); function checkPhoneNum(phoneNumStr) { var subStrTmp; var charFstTmp; var errSign = 0; if(phoneNumStr.indexOf(';') != -1 || phoneNumStr.indexOf(',') != -1) { errSign = 1; $.alert(ERR_LTE_SMS_PHONE_NUM_ONE); } else if (phoneNumStr.length >= 21 || phoneNumStr.length <= 0) { errSign = 1; $.alert(ERR_LTE_SMS_PHONE_NUM_LEN); } else { charFstTmp = phoneNumStr.charAt(0); if (charFstTmp != "+" && !$.isnum(charFstTmp)) { errSign = 1; $.alert(ERR_LTE_SMS_PHONE_NUM_FORMAT); } else if (charFstTmp == "+") { subStrTmp = phoneNumStr.substring(1, phoneNumStr.length); if (!$.isnum(subStrTmp)) { errSign = 1; $.alert(ERR_LTE_SMS_PHONE_NUM_FORMAT); } } else { if (!$.isnum(phoneNumStr)) { errSign = 1; $.alert(ERR_LTE_SMS_PHONE_NUM_FORMAT); } } } if (errSign == 1) { var element = $("#toNumber"); if (element) { element.focus(); element.select(); } return false; } return true; } $("#save").click(function() { var toNumStr; var unencodecontStr; var contentStr; var lenTotalAddTemp = 0; toNumStr = msgPageListGlobal[indexGlobal].phoneNumber; unencodecontStr = $("#inputContent").val(); contentStr = doEscapeCharEncode(unencodecontStr); if (!checkPhoneNum(toNumStr)) { return false; } if (unencodecontStr=="") { $.alert(ERR_LTE_SMS_MSG_EMPTY); return false; } lenTotalAddTemp = lenSingleAddInGSM7Ext[0] + lenSingleAddInGSM7Ext[1] + lenSingleAddInGSM7Ext[2] + lenSingleAddInGSM7Ext[3] + lenSingleAddInGSM7Ext[4]; if (contentStr.length + lenTotalAddTemp > lenMsgArray[lenSingleMsgArrayIndex]) { $.alert(ERR_LTE_SMS_MSG_TOO_LONG); $("#inputContent").focus(); $("#inputContent").select(); return; } $.addLoading($(this)); if( !($.mainParam && optPageGlobal == 2 && msgPageListGlobal[indexGlobal].phoneNumber == toNumStr && msgPageListGlobal[indexGlobal].lastMsgContent == contentStr) ) { $.dm.set({ oid: "DEV2_LTE_SMS_SENDNEWMSG", data: { "index": 2,//2表示存信息 "to": toNumStr, "textContent": contentStr }, callback: { success: function() { $.removeLoading(); $.lteSmsTips(1, s_str.msgSaveSuc); $.reload(); }, fail: function() { $.removeLoading(); $.lteSmsTips(0, s_str.msgSaveFail); } } }); } else { $.removeLoading(); $.lteSmsTips(1, s_str.msgSaveSuc); $.reload(); } }); var retryTimer; function smsSendResult(){ $.dm.get({ oid: "DEV2_LTE_SMS_SENDNEWMSG", callback: { success: function (data) { if (1 == data.sendResult) { if (retryTimer != "undefined") clearTimeout(retryTimer); $.removeLoading(); $.lteSmsTips(1, s_str.msgSentSuc); $.reload(); } else if (2 == data.sendResult) { if (retryTimer != "undefined") clearTimeout(retryTimer); $.removeLoading(); $.lteSmsTips(0, s_str.msgSentBusy, 1800); } else if (3 == data.sendResult) { //msg on sending retryTimer = $.timeout(smsSendResult, 500); } else { //fail: data.sendResult = 0 $.removeLoading(); clearTimeout(retryTimer); $.lteSmsTips(0, s_str.msgSentFail); //$.reload(); } }, fail: function () { $.removeLoading(); clearTimeout(retryTimer); $.lteSmsTips(0, s_str.msgSentFail); $.reload(); } } }); } $("#send").click(function() { var toNumStr; var unencodecontStr; var contentStr; var lenTotalAddTemp = 0; toNumStr = msgPageListGlobal[indexGlobal].phoneNumber; unencodecontStr = $("#inputContent").val(); contentStr = doEscapeCharEncode(unencodecontStr); if (!checkPhoneNum(toNumStr)) { return false; } if (unencodecontStr=="") { $.alert(ERR_LTE_SMS_MSG_EMPTY); return false; } lenTotalAddTemp = lenSingleAddInGSM7Ext[0] + lenSingleAddInGSM7Ext[1] + lenSingleAddInGSM7Ext[2] + lenSingleAddInGSM7Ext[3] + lenSingleAddInGSM7Ext[4]; TotalLen = contentStr.length + lenTotalAddTemp; if (TotalLen > lenMsgArray[lenSingleMsgArrayIndex]) { $.alert(ERR_LTE_SMS_MSG_TOO_LONG); $("#inputContent").focus(); $("#inputContent").select(); return; } function doSend() { $.dm.set({ oid: "DEV2_LTE_SMS_SENDNEWMSG", data: { "index": 1, "to": toNumStr, "textContent": contentStr }, callback: { success: function() { smsSendResult(); } } }); } $.addLoading($(this)); doSend(); }); $.tpInit(init); })(); </script> <div style="display: flex; align-items: center; gap: 10px;"> <button type="submit" class="blue inline T_back" id="back" style="cursor:pointer;">Back</button> <h3 id="t_et">Conversation List</h3> </div> <div id="chatContainer"> <div id = "chatBox"> <div id = "loadTip">上滑加载更多...</div> </div> <div id="editor"> <label id="inputLen" class="label-title m" style="vertical-align: top;margin-top:8px;"></label> <textarea id="inputContent" placeholder="输入讯息内容..." maxlength="1000"></textarea> <div id="editorButtons"> <button id="save" class="btn">Save</button> <button id="send" class="btn">Send</button> </div> </div> </div> <script type="text/javascript"> (function() {})(); </script> 分析上述代码
最新发布
09-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值