【AntDesign】解决嵌套section或layout中,h1字体比h2小问题

文章讲述了在AntDesignV5中,如何通过使用Typography组件和CSS覆盖浏览器默认样式来解决h1标签层级小于h2的问题,提供了两种解决方案:一是利用Typography组件管理层级,二是直接修改相关CSS属性。

问题:以下情况均会导致h1比h2小,具体原因是浏览器默认样式里面,对h1不同层级设置了特殊的样式,

<section class="css-dev-only-do-not-override-12q8zf4 ant-layout">
	<section class="css-dev-only-do-not-override-12q8zf4 ant-layout">
	</section>
</section>
import React from "react";
import "./index.css";
import { App, ConfigProvider, Layout } from "antd";
import "antd/dist/reset.css";
const { Content } = Layout;

export default () => (
  <ConfigProvider>
    <App>
      <Layout>
        <Content>
          <Layout>
            <Content>
              <h1>一级标题</h1>
              <h2>二级标题</h2>
            </Content>
          </Layout>
        </Content>
      </Layout>
    </App>
  </ConfigProvider>
);

解决:
方法1:嵌套Typography标签,antd v5 里不做完全覆盖了,全都推荐使用封装的组件来控制层级样式:

issue问题

在线演示

import React from "react";
import "./index.css";
import { Layout, Typography } from "antd";
import "antd/dist/reset.css";
const { Content } = Layout;

export default () => (
  <Layout>
    <Content>
      <Layout>
        <Content>
          <Typography>
            <h1>一级标题</h1>
            <h2>二级标题</h2>
            <h3>三级标题</h3>
            <h4>四级标题</h4>
          </Typography>

          <Typography.Title level={1}>一级标题</Typography.Title>
          <Typography.Title level={2}>二级标题</Typography.Title>
          <Typography.Title level={3}>三级标题</Typography.Title>
          <Typography.Title level={4}>四级标题</Typography.Title>
        </Content>
      </Layout>
    </Content>
  </Layout>
);

在这里插入图片描述
方法2: 对css参数覆盖

 :-webkit-any(article,aside,nav,section) h1 {
      display: block;
      font-size: 2em;
      margin-block-start: 0.67em;
      margin-block-end: 0.67em;
      margin-inline-start: 0px;
      margin-inline-end: 0px;
      font-weight: bold;
    }
/* General setup */ * { box-sizing: border-box; } body { margin: 0 auto; min-width: 1000px; max-width: 1400px; background-color: #e6f7ff; /* 添加浅蓝色背景 */ color: #003366; /* 深蓝文字,提升可读性 */ } /* Layout */ section { float: left; width: 50%; } aside { float: left; width: 30%; } nav { float: left; width: 20%; } footer { clear: both; } header, section, aside, nav, footer { padding: 20px; } /* header and footer */ header, footer { border-top: 5px solid #66ccff; /* 改为浅蓝边框 */ border-bottom: 5px solid #66ccff; background-color: #b3e0ff; /* 浅蓝背景 */ } /* WRITE YOUR CODE BELOW HERE */ @import url('https://fonts.googleapis.com/css?family=Labrada'); html { font-size: 10px; } body { font-family: serif; font-size: 1.6rem; line-height: 1.6; letter-spacing: 0.5px; word-spacing: 1px; color: #333; /* 原有设置被上面覆盖,现统一调整为深蓝灰 */ } h1, h2, h3, h4, h5, h6 { font-family: 'Labrada', serif; letter-spacing: 2px; text-align: center; color: #0055a5; /* 统一标题颜色为深蓝色 */ } h1 { font-size: 3rem; margin-top: 2rem; } h2 { font-size: 2.4rem; } h3 { font-size: 2rem; } h1 + p, h2 + p, h3 + p, h4 + p, h5 + p, h6 + p { text-indent: 20px; } a:link { color: #0099cc; /* 浅蓝主题链接色 */ text-decoration: underline; } a:visited { color: #6699cc; text-decoration: underline; } a:focus { outline: none; text-decoration: underline; color: #0056b3; } a:hover { text-decoration: none; color: #0055a5; } a:active { color: #c9302c; transform: scale(0.98); } a[href^="http"]:not([href*="yoursite.com"]):not([href^="#"])::after { content: ""; display: inline-block; width: 12px; height: 12px; background-image: url("external-link.svg"); background-size: contain; background-repeat: no-repeat; margin-left: 6px; vertical-align: text-bottom; } ul, ol { line-height: 1.6; padding-left: 1.6rem; } li { margin: 0.5rem 0; } ul li::before { content: "•"; color: #0099cc; /* 调整项目符号为浅蓝 */ font-weight: bold; display: inline-block; width: 1em; margin-left: -1em; } nav ul { display: flex; justify-content: center; list-style: none; padding: 1rem 0; margin: 0; border-top: 1px solid #0099cc; border-bottom: 1px solid #0099cc; background-color: #d4f0ff; /* 导航栏浅蓝背景 */ } nav a { text-decoration: none; color: #006699; /* 导航链接深蓝 */ font-size: 1.4rem; padding: 0.5rem 1rem; } nav a:hover, nav a:focus { text-decoration: none; color: #004c70; background-color: #cceeff; /* 悬停高亮 */ }
最新发布
12-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值