MySQL导出表结构

-- 导出表结构
SELECT A.TABLE_SCHEMA,
       A.TABLE_NAME,
       A.TABLE_COMMENT
FROM INFORMATION_SCHEMA.TABLES A
WHERE A.TABLE_SCHEMA = '$table_name';
Posted in 默认分类 | Tagged | Comments Off on MySQL导出表结构

在Apache服务器上安装SSL证书


前提条件

  • 您的Apache服务器上已经开启了443端口(HTTPS服务的默认端口)。
  • 您的Apache服务器上已安装了mod_ssl.so模块(启用SSL功能)。
  • 本文档证书名称以domain name为示例,例如:证书文件名称为domain name_public.crt,证书链文件名称为domain name_chain.crt,证书密钥文件名称为domain name.key。
  • 申请证书时如果未选择系统自动创建CSR,证书下载压缩包中将不包含.key文件。

操作步骤

1、解压已下载保存到本地的Apache证书文件。

解压后的文件夹中有3个文件:

  • 证书文件:以.crt为后缀或文件类型。
  • 证书链文件:以.crt为后缀或文件类型。
  • 密钥文件:以.key为后缀或文件类型。

2、证书文件上传

在Apache安装目录中新建cert目录,并将解压的Apache证书、证书链文件和密钥文件拷贝到cert目录中。如果需要安装多个证书,需在Apache目录中新建对应数量的cert目录,用于存放不同的证书 。

说明 如果申请证书时选择了手动创建CSR文件,请将手动生成创建的密钥文件拷贝到cert目录中并命名为domain name.key。

3、修改httpd.conf配置文件。

在Apache安装目录下,打开Apache/conf/httpd.conf文件,并找到以下参数,按照下文中注释内容进行配置。

#LoadModule ssl_module modules/mod_ssl.so  #删除行首的配置语句注释符号“#”加载mod_ssl.so模块启用SSL服务,Apache默认是不启用该模块的。
#Include conf/extra/httpd-ssl.conf  #删除行首的配置语句注释符号“#”。   

说明 如果您在httpd.conf文件中没有找到以上配置语句,请确认您的Apache服务器中是否已经安装mod_ssl.so模块。可执行yum install -y mod_ssl命令安装mod_ssl模块。

保存httpd.conf文件并退出。

4、修改httpd-ssl.conf配置文件。

打开Apache/conf/extra/httpd-ssl.conf文件并找到以下参数,按照下文中注释内容进行配置。

注意:根据操作系统的不同,http-ssl.conf文件也可能存放在conf.d/ssl.conf目录中。

<VirtualHost *:443>     
  ServerName   #修改为申请证书时绑定的域名www.YourDomainName1.com。                    
  DocumentRoot /data/www/hbappserver/public          
  SSLEngine on  
  SSLProtocol all -SSLv2 -SSLv3 # 添加SSL协议支持协议,去掉不安全的协议。
  SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM   # 修改加密套件。
  SSLHonorCipherOrder on
  SSLCertificateFile cert/domain name1_public.crt   # 将domain name1_public.crt替换成您证书文件名。
  SSLCertificateKeyFile cert/domain name1.key   # 将domain name1.key替换成您证书的密钥文件名。
  SSLCertificateChainFile cert/domain name1_chain.crt  # 将domain name1_chain.crt替换成您证书的密钥文件名;证书链开头如果有#字符,请删除。
</VirtualHost>

#如果证书包含多个域名,复制以上参数,并将ServerName替换成第二个域名。
<VirtualHost *:443>    
  ServerName   #修改为申请证书时绑定的第二个域名www.YourDomainName2.com。                    
  DocumentRoot /data/www/hbappserver/public          
  SSLEngine on  
  SSLProtocol all -SSLv2 -SSLv3 # 添加SSL协议支持协议,去掉不安全的协议。
  SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM   # 修改加密套件。
  SSLHonorCipherOrder on
  SSLCertificateFile cert/domain name2_public.crt   # 将domain name2替换成您申请证书时的第二个域名。
  SSLCertificateKeyFile cert/domain name2.key   # 将domain name2替换成您申请证书时的第二个域名。
  SSLCertificateChainFile cert/domain name2_chain.crt  # 将domain name2替换成您申请证书时的第二个域名;证书链开头如果有#字符,请删除。
</VirtualHost>

说明 需注意您的浏览器版本是否支持SNI功能。如果不支持,多域名证书配置将无法生效。

保存httpd-ssl.conf文件并退出。

5、重启Apache服务器使SSL配置生效。

在Apache的bin目录下执行以下命令:

1、停止Apache服务。

systemctl stop  httpd

2、开启Apache服务。

systemctl start  httpd

6、可选:修改httpd.conf文件,设置HTTP请求自动跳转HTTPS。

在httpd.conf文件中的中间,添加以下重定向代码。

RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{SERVER_NAME}$1 [L,R]

后续操作

证书安装完成后,您可通过登录证书的绑定域名验证该证书是否安装成功。

https://domain name   #domain name替换成证书绑定的域名。

如果网页地址栏出现小锁标志,表示证书安装成功。

Posted in 默认分类 | Comments Off on 在Apache服务器上安装SSL证书

解密Navicat导出链接中的密码

<?php
namespace FatSmallTools;
class NavicatPassword
{
    protected $version = 0;
    protected $aesKey = 'libcckeylibcckey';
    protected $aesIv = 'libcciv libcciv ';
    protected $blowString = '3DC5CA39';
    protected $blowKey = null;
    protected $blowIv = null;
    public function __construct($version = 12)
    {
        $this->version = $version;
        $this->blowKey = sha1('3DC5CA39', true);
        $this->blowIv = hex2bin('d9c7c3c8870d64bd');
    }
    public function encrypt($string)
    {
        $result = FALSE;
        switch ($this->version) {
            case 11:
                $result = $this->encryptEleven($string);
                break;
            case 12:
                $result = $this->encryptTwelve($string);
                break;
            default:
                break;
        }
        return $result;
    }
    protected function encryptEleven($string)
    {
        $round = intval(floor(strlen($string) / 8));
        $leftLength = strlen($string) % 8;
        $result = '';
        $currentVector = $this->blowIv;
        for ($i = 0; $i < $round; $i  ) {
            $temp = $this->encryptBlock($this->xorBytes(substr($string, 8 * $i, 8), $currentVector));
            $currentVector = $this->xorBytes($currentVector, $temp);
            $result .= $temp;
        }
        if ($leftLength) {
            $currentVector = $this->encryptBlock($currentVector);
            $result .= $this->xorBytes(substr($string, 8 * $i, $leftLength), $currentVector);
        }
        return strtoupper(bin2hex($result));
    }
    protected function encryptBlock($block)
    {
        return openssl_encrypt($block, 'BF-ECB', $this->blowKey, OPENSSL_RAW_DATA|OPENSSL_NO_PADDING); 
    }
    protected function decryptBlock($block)
    {
        return openssl_decrypt($block, 'BF-ECB', $this->blowKey, OPENSSL_RAW_DATA|OPENSSL_NO_PADDING); 
    }
    protected function xorBytes($str1, $str2)
    {
        $result = '';
        for ($i = 0; $i < strlen($str1); $i  ) {
            $result .= chr(ord($str1[$i]) ^ ord($str2[$i]));
        }
        return $result;
    }
    protected function encryptTwelve($string)
    {
        $result = openssl_encrypt($string, 'AES-128-CBC', $this->aesKey, OPENSSL_RAW_DATA, $this->aesIv);
        return strtoupper(bin2hex($result));
    }
    public function decrypt($string)
    {
        $result = FALSE;
        switch ($this->version) {
            case 11:
                $result = $this->decryptEleven($string);
                break;
            case 12:
                $result = $this->decryptTwelve($string);
                break;
            default:
                break;
        }
        return $result;
    }
    protected function decryptEleven($upperString)
    {
        $string = hex2bin(strtolower($upperString));
        $round = intval(floor(strlen($string) / 8));
        $leftLength = strlen($string) % 8;
        $result = '';
        $currentVector = $this->blowIv;
        for ($i = 0; $i < $round; $i  ) {
            $encryptedBlock = substr($string, 8 * $i, 8);
            $temp = $this->xorBytes($this->decryptBlock($encryptedBlock), $currentVector);
            $currentVector = $this->xorBytes($currentVector, $encryptedBlock);
            $result .= $temp;
        }
        if ($leftLength) {
            $currentVector = $this->encryptBlock($currentVector);
            $result .= $this->xorBytes(substr($string, 8 * $i, $leftLength), $currentVector);
        }
        return $result;
    }
    protected function decryptTwelve($upperString)
    {
        $string = hex2bin(strtolower($upperString));
        return openssl_decrypt($string, 'AES-128-CBC', $this->aesKey, OPENSSL_RAW_DATA, $this->aesIv);
    }
}
use FatSmallToolsNavicatPassword;
//需要指定版本,11或12
$navicatPassword = new NavicatPassword(12);
//$navicatPassword = new NavicatPassword(11);
//解密
//$decode = $navicatPassword->decrypt('15057D7BA390');
$decode = $navicatPassword->decrypt('BD24FE2CDBD24FE2CDAA36A18B4AA36A18B4');
echo $decode."n";

注意代码需要修改一下使用。

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.util.Arrays;
 
/**
 * 以下程序均为ChatGPT生成
 * @Author: 木芒果
 */
public class NavicatPassword {
    public static void main(String[] args) throws Exception {
        NavicatPassword navicatPassword = new NavicatPassword();
 
        // 解密11版本及以前的密码
        //String decode = navicatPassword.decrypt("15057D7BA390", 11);
 
        // 解密12版本及以后的密码
        //String decode = navicatPassword.decrypt("15057D7BA390", 12);
        String decode = navicatPassword.decrypt("解密密码", 12);
        System.out.println(decode);
    }
 
    private static final String AES_KEY = "libcckeylibcckey";
    private static final String AES_IV = "libcciv libcciv ";
    private static final String BLOW_KEY = "3DC5CA39";
    private static final String BLOW_IV = "d9c7c3c8870d64bd";
 
    public static String encrypt(String plaintext, int version) throws Exception {
        switch (version) {
            case 11:
                return encryptEleven(plaintext);
            case 12:
                return encryptTwelve(plaintext);
            default:
                throw new IllegalArgumentException("Unsupported version");
        }
    }
 
    public static String decrypt(String ciphertext, int version) throws Exception {
        switch (version) {
            case 11:
                return decryptEleven(ciphertext);
            case 12:
                return decryptTwelve(ciphertext);
            default:
                throw new IllegalArgumentException("Unsupported version");
        }
    }
 
    private static String encryptEleven(String plaintext) throws Exception {
        byte[] iv = hexStringToByteArray(BLOW_IV);
        byte[] key = hashToBytes(BLOW_KEY);
 
        int round = plaintext.length() / 8;
        int leftLength = plaintext.length() % 8;
        StringBuilder result = new StringBuilder();
        byte[] currentVector = iv.clone();
 
        Cipher cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "Blowfish");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
 
        for (int i = 0; i < round; i++) {
            byte[] block = xorBytes(plaintext.substring(i * 8, (i + 1) * 8).getBytes(), currentVector);
            byte[] temp = cipher.doFinal(block);
            currentVector = xorBytes(currentVector, temp);
            result.append(bytesToHex(temp));
        }
 
        if (leftLength > 0) {
            currentVector = cipher.doFinal(currentVector);
            byte[] block = xorBytes(plaintext.substring(round * 8).getBytes(), currentVector);
            result.append(bytesToHex(block));
        }
 
        return result.toString().toUpperCase();
    }
 
    private static String encryptTwelve(String plaintext) throws Exception {
        byte[] iv = AES_IV.getBytes();
        byte[] key = AES_KEY.getBytes();
 
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
 
        byte[] result = cipher.doFinal(plaintext.getBytes());
        return bytesToHex(result).toUpperCase();
    }
 
    private static String decryptEleven(String ciphertext) throws Exception {
        byte[] iv = hexStringToByteArray(BLOW_IV);
        byte[] key = hashToBytes(BLOW_KEY);
        byte[] encrypted = hexStringToByteArray(ciphertext.toLowerCase());
 
        int round = encrypted.length / 8;
        int leftLength = encrypted.length % 8;
        StringBuilder result = new StringBuilder();
        byte[] currentVector = iv.clone();
 
        Cipher cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "Blowfish");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
 
        for (int i = 0; i < round; i++) {
            byte[] block = Arrays.copyOfRange(encrypted, i * 8, (i + 1) * 8);
            byte[] temp = xorBytes(cipher.doFinal(block), currentVector);
            currentVector = xorBytes(currentVector, block);
            result.append(new String(temp));
        }
 
        if (leftLength > 0) {
            currentVector = cipher.doFinal(currentVector);
            byte[] block = Arrays.copyOfRange(encrypted, round * 8, round * 8 + leftLength);
            result.append(new String(xorBytes(block, currentVector)));
        }
 
        return result.toString();
    }
 
    private static String decryptTwelve(String ciphertext) throws Exception {
        byte[] iv = AES_IV.getBytes();
        byte[] key = AES_KEY.getBytes();
        byte[] encrypted = hexStringToByteArray(ciphertext.toLowerCase());
 
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
 
        byte[] result = cipher.doFinal(encrypted);
        return new String(result);
    }
 
    private static byte[] xorBytes(byte[] bytes1, byte[] bytes2) {
        byte[] result = new byte[bytes1.length];
        for (int i = 0; i < bytes1.length; i++) {
            result[i] = (byte) (bytes1[i] ^ bytes2[i]);
        }
        return result;
    }
 
    private static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }
 
    private static byte[] hashToBytes(String s) throws Exception {
        return MessageDigest.getInstance("SHA-1").digest(s.getBytes());
    }
 
    private static String bytesToHex(byte[] bytes) {
        StringBuilder result = new StringBuilder();
        for (byte b : bytes) {
            result.append(String.format("%02X", b));
        }
        return result.toString();
    }
}
Posted in 默认分类 | Comments Off on 解密Navicat导出链接中的密码

hosts文件恢复方法


意外将hosts文件删除,将恢复方法记录

版本信息: Windows 10 专业版 1909

C:\Windows\System32\drivers\etc\目录下的lmhosts.sam文件复制一份并重命名为hosts即可。

Posted in 默认分类 | Comments Off on hosts文件恢复方法

markdown-preview缺少tslib模块解决方案


环境

  • windows 10 1909
  • vim-vi improved 8.2 ms-windows 64-bit gui version

缘起

安装markdown-preview插件后,使用时无论是使用快捷键还是命令,都没有任何反馈

排查

在vim命令行输入messages输出以下出错信息( 部分):

......
[vim-node-rpc] rpc error: internal/modules/cjs/loader.js:883
[vim-node-rpc] rpc error: throw err;
[vim-node-rpc] rpc error: ^
[vim-node-rpc] rpc error:
[vim-node-rpc] rpc error: error: cannot find module 'tslib'
[vim-node-rpc] rpc error: require stack:
[vim-node-rpc] rpc error: - /users/dxm/.vim/bundle/markdown-preview.nvim/app/lib/app/index.js
[vim-node-rpc] rpc error: - /users/dxm/.vim/bundle/markdown-preview.nvim/app/index.js
......

可以看到是因为缺少tslib模块引起的错误,经查资料得知此模块用途超出我的知识范围qaq,

tslib是一个开源的程序,能够为触摸屏驱动获得的采样提供诸如滤波、去抖、校准等功能。-百度百科

所以去插件的Github仓库去碰碰运气,终于在一条issue下找到解决方法:

解决

在插件目录下执行:

npm install

后记

其他操作系统或许可以尝试诸如yarn install等操作,当然,还是要根据出错信息 自己排查解决。

参考资料

  1. https://github.com/iamcco/markdown-preview.nvim/issues/305
  2. https://baike.baidu.com/item/tslib/722869?fr=aladdin
Posted in 默认分类 | Comments Off on markdown-preview缺少tslib模块解决方案

MySQL密码重置


修改 MySQL 配置文件以跳过密码登录

# vim /etc/my.cnf 

添加skip-grant-tables;

重启 MySQL 服务

# systemctl restart mysqld.service

登录修改

[root@cvm-3ibcdh728a225 html]# mysql -u root 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> USE mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set authentication_string='your-new-passwd' where user='root';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> flush privileges ;
Query OK, 0 rows affected (0.02 sec)

mysql> quit
Bye

将设置文件改回

# vim /etc/my.cnf 

重启 MySQL

# systemctl restart mysqld.service

修改完成。

— end —

Posted in 默认分类 | Tagged | Comments Off on MySQL密码重置

一段自适应图片排版CSS

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width" />
    <title>T</title>
    <style>
        .images-box {
            padding: 0 0vw 2vw 0vw;
            font-size: 0;
            word-spacing: 0;
            letter-spacing: 1vw;
            line-height: 1vw;
        }

        .images-box a {
            display: inline-block;
            height: 30%;
            width: 30%;
            overflow: hidden;
            text-decoration: none;
            -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
        }

        .images-box a:only-child {
            height: initial;
            width: initial;
            max-width: 80%;
        }

        .images-box a:nth-child(1):nth-last-child(2) {
            width: 45%;
            height: 45%;
        }

        .images-box a:nth-child(2):nth-last-child(1) {
            width: 45%;
            height: 45%;
        }

        .images-box a:nth-child(1):nth-last-child(4) {
            height: 45%;
            width: 45%;
        }

        .images-box a:nth-child(2):nth-last-child(3) {
            height: 45%;
            width: 45%;
        }

        .images-box a:nth-child(3):nth-last-child(2) {
            height: 45%;
            width: 45%;
        }

        .images-box a:nth-child(4):nth-last-child(1) {
            height: 45%;
            width: 45%;
        }

        .images-box a img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
    </style>
</head>

<body>

    <div class="images-box">
        <a><img src="./1.jpg"></img></a>
        <a><img src="./1.jpg"></img></a>
        <a><img src="./1.jpg"></img></a>
        ...
    </div>
</body>

</html>
Posted in 默认分类 | Tagged | Comments Off on 一段自适应图片排版CSS

时间复杂度分析之一

#include<stdio.h>
​
int main(){
    
    int x = 90;
    int y = 100;
    while(y > 0)
        if(x > 100){
            x = x - 10;
            y--;
        }
        else x++;
}
xy基础操作语句执行
90100未执行
91100未执行
92100未执行
93100未执行
94100未执行
95100未执行
96100未执行
97100未执行
98100未执行
99100未执行
100100未执行
101100执行第一次
9199未执行
9299未执行
9399未执行
9499未执行

x总是从90~101循环,y100减小到0y每减小1,最内层循环执行1次,共执行100次; 所以时间复杂度为O(1)

Posted in 默认分类 | Comments Off on 时间复杂度分析之一

Typora 自动显示序号

1、打开Typora软件的主题目录(文件-偏好设置-外观-打开主题文件夹)

类似C:\Users\i\AppData\Roaming\Typora\themes这种

2、在主题目录下新建文件,命名为base.user.css,注意拓展名为css,没有显示拓展名的系统可能为txt

3、将以下代码写入base.user.css中

/** initialize css counter */
#write, .sidebar-content,.md-toc-content {
   counter-reset: h1
}

#write h1, .outline-h1, .md-toc-item.md-toc-h1 {
   counter-reset: h2
}

#write h2, .outline-h2, .md-toc-item.md-toc-h2 {
   counter-reset: h3
}

#write h3, .outline-h3, .md-toc-item.md-toc-h3 {
   counter-reset: h4
}

#write h4, .outline-h4, .md-toc-item.md-toc-h4 {
   counter-reset: h5
}

#write h5, .outline-h5, .md-toc-item.md-toc-h5 {
   counter-reset: h6
}

/** put counter result into headings */
#write h1:before,
h1.md-focus.md-heading:before,
.outline-h1>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h1>.md-toc-inner:before{
   counter-increment: h1;
   content: counter(h1) " "
}

#write h2:before,
h2.md-focus.md-heading:before,
.outline-h2>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h2>.md-toc-inner:before{
   counter-increment: h2;
   content: counter(h1) "." counter(h2) " "
}

#write h3:before,
h3.md-focus.md-heading:before,
.outline-h3>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h3>.md-toc-inner:before {
   counter-increment: h3;
   content: counter(h1) "." counter(h2) "." counter(h3) " "
}

#write h4:before,
h4.md-focus.md-heading:before,
.outline-h4>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h4>.md-toc-inner:before {
   counter-increment: h4;
   content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) " "
}

#write h5:before,
h5.md-focus.md-heading:before,
.outline-h5>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h5>.md-toc-inner:before {
   counter-increment: h5;
   content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) " "
}

#write h6:before,
h6.md-focus.md-heading:before,
.outline-h6>.outline-item>.outline-label:before,
.md-toc-item.md-toc-h6>.md-toc-inner:before {
   counter-increment: h6;
   content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) "." counter(h6) " "
}

/** override the default style for focused headings */
#write>h3.md-focus:before,
#write>h4.md-focus:before,
#write>h5.md-focus:before,
#write>h6.md-focus:before,
h3.md-focus:before,
h4.md-focus:before,
h5.md-focus:before,
h6.md-focus:before {
   color: inherit;
   border: inherit;
   border-radius: inherit;
   position: inherit;
   left:initial;
   float: none;
   top:initial;
   font-size: inherit;
   padding-left: inherit;
   padding-right: inherit;
   vertical-align: inherit;
   font-weight: inherit;
   line-height: inherit;
}

4、重启软件,测试

—End—

Posted in 默认分类 | Comments Off on Typora 自动显示序号

关于CSS的定位属性 – Position


position 属性用于表名元素的定位类型,数据类型表示用于设置相对于框的位置的2D空间中的坐标,该属性共有5个值:staticrelativefixedabsolutesticky.

CSS position属性用于指定一个元素在文档中的定位方式。toprightbottomleft 属性则决定了该元素的最终位置。

一个偏移量可以是一个相对值用以表示 % (百分比),或是一个绝对的 length (长度)值。正值是向右或向下的偏移,看适用于哪一个。负值则是在另外方向上的偏移。

语法

[ [ left | center | right | top | bottom | <percentage> | <length> ] |
                [ left | center | right | <percentage> | <length> ] [ top | center | bottom | <percentage> | <length> ] |
                [ center | [ left | right ] [ <percentage> | <length> ]? ] &&
                [ center | [ top | bottom ] [ <percentage> | <length> ]? ]
              ]

1、 静态定位 – static

静态定位是元素的默认属性,指定元素使用正常的布局行为,即元素在文档常规流中当前的布局位置。此时 top, right, bottom, leftz-index属性无效。

```css
.class {
  position: static;
}
```

2、 固定定位 – fixed

元素会被移出正常文档流,并不为元素预留空间,而是通过指定元素相对于屏幕视口(viewport)的位置来指定元素位置。元素的位置在屏幕滚动时不会改变。打印时,元素会出现在的每页的固定位置。fixed 属性会创建新的层叠上下文。当元素祖先的 transform, perspectivefilter 属性非 none 时,容器由视口改为该祖先。

元素的位置相对于浏览器窗口是固定位置,元素的位置与文档流无关,Fixed定位的元素和其他元素重叠。

.class {
   position:fixed;
   top:2px;
   right:7px;
}

3、相对定位 – relative

该关键字下,元素先放置在未添加定位时的位置,再在不改变页面布局的前提下调整元素位置(因此会在此元素未添加定位时所在位置留下空白)。position:relative 对 table-*-group, table-row, table-column, table-cell, table-caption 元素无效。

相对定位元素的定位是相对其正常位置,移动相对定位元素,但它原本所占的空间不会改变。

<div class="box" id="one">One</div>
<div class="box" id="two">Two</div>
<div class="box" id="three">Three</div>
<div class="box" id="four">Four</div>
.box {
 display: inline-block;
 width: 100px;
 height: 100px;
 background: red;
 color: white;
}

#two {
 position: relative;
 top: 20px;
 left: 20px;
 background: blue;
}

4、绝对定位 – absolute

元素会被移出正常文档流,并不为元素预留空间,通过指定元素相对于最近的非 static 定位祖先元素的偏移,来确定元素位置。绝对定位的元素可以设置外边距(margins),且不会与其他边距合并。

绝对定位的元素的位置相对于最近的已定位父元素,如果元素没有已定位的父元素,那么它的位置相对于<html>,absolute 定位使元素的位置与文档流无关,因此不占据空间,absolute 定位的元素和其他元素重叠。

.class {
   position:absolute;
   left:70px;
   top:80px;
}

5、粘性定位 – sticky

粘性定位的元素是依赖于用户的滚动,在 position:relativeposition:fixed 定位之间切换。

它的行为就像 position:relative; 而当页面滚动超出目标区域时,它的表现就像 position:fixed;,它会固定在目标位置。

元素定位表现为在跨越特定阈值前为相对定位,之后为固定定位。

这个特定阈值指的是 top, right, bottom 或 left 之一,换言之,指定 top, right, bottom 或 left 四个阈值其中之一,才可使粘性定位生效。否则其行为与相对定位相同。

元素根据正常文档流进行定位,然后相对它的最近滚动祖先(nearest scrolling ancestor)containing block (最近块级祖先 nearest block-level ancestor),包括table-related元素,基于top, right, bottom, 和 left的值进行偏移。偏移值不会影响任何其他元素的位置。

该值总是创建一个新的层叠上下文(stacking context)。注意,一个sticky元素会“固定”在离它最近的一个拥有“滚动机制”的祖先上(当该祖先的overflowhidden, scroll, auto, 或 overlay时),即便这个祖先不是最近的真实可滚动祖先。这有效地抑制了任何“sticky”行为(详情见Github issue on W3C CSSWG)。

.calss {
   position: sticky;
   top: 10px;
}

说明

1、大多数情况下,heightwidth 被设定为auto的绝对定位元素,按其内容大小调整尺寸。但是,被绝对定位的元素可以通过指定topbottom ,保留height未指定(即auto),来填充可用的垂直空间。它们同样可以通过指定leftright并将width 指定为auto来填充可用的水平空间。

Posted in 默认分类 | Tagged | Comments Off on 关于CSS的定位属性 – Position