You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

卡片中Material Icons调整px后尺寸未变化的解决方法咨询

解决Material Icons调整font-size后尺寸不变的问题

你的图标尺寸无法通过修改font-size生效,核心原因是HTML结构存在错误,导致样式未正确加载,同时可以通过增强样式优先级确保设置生效。以下是具体解决方案:

一、修复HTML结构错误

你的代码存在标签嵌套错误:<style>、Material Icons的<link>标签被放在了<body>内部,甚至重复定义了<body>和<head>,浏览器解析时会忽略部分样式规则。正确的HTML结构应该是:

<!DOCTYPE html>
<html>
<head>
  <link rel="icon" type="png" href="/images/icon.png"/>
  <meta charset="utf-8"/>
  <title>Project-LuLo</title>
  <meta content="width=device-width, initial-scale=1" name="viewport"/>
  <link rel="stylesheet" href="css/index.css">
  <!-- 字体链接移至head标签内 -->
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:400,600" rel="stylesheet">
  
  <!-- 样式代码放在head内 -->
  <style>
    * {
      box-sizing: border-box;
    }

    body {
      margin: 0;
      font-family: Arial, Helvetica, sans-serif;
    }

    .shell {
      height:65vh;
      justify-content: center;
      display: flex;
      flex-direction: row;
      flex-basis: auto;
      margin: 5px;
      align-items: center;
    }

    .card {
      display: inline-block;
      width: 200px;
      height: 150px;
      border: 1px solid #EF9A9A;
      border-radius: 4px;
      margin: 5px;
      text-decoration: none;
    }

    .card-header {
      color: #D32F2F;
      text-align: center;
      font-size: 24px;
      font-weight: 600;
      border-bottom: 1px solid #EF9A9A;
      background-color: #FFEBEE;
      padding: 5px 10px;
    }

    .card-main {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      padding: 15px 0;
    }

    /* 修改这里的选择器,增强优先级 */
    .card-main .material-icons {
      font-size: 48px; /* 调整为你需要的尺寸 */
      color: #D32F2F;
      margin-bottom: 5px;
    }

    .main-description {
      color: #D32F2F;
      font-size: 24px;
      text-align: center;
    }

    .header {
      overflow: hidden;
      background-color: #FFEBEE;
      padding: 20px 10px;
      border-bottom: 1px solid #EF9A9A;
      border-radius: 4px;
    }

    .header a {
      float: left;
      color: #D32F2F;
      text-align: center;
      padding: 12px;
      text-decoration: none;
      font-size: 18px;
      line-height: 25px;
      border-radius: 4px;
    }

    .header a.logo {
      font-size: 25px;
      font-weight: bold;
    }

    .header a:hover {
      background-color: #dfd5d7;
      color: #942626;
    }

    .header a.active {
      background-color: #D32F2F;
      color: #FFEBEE;
    }

    .header-right {
      float: right;
    }

    @media screen and (max-width: 500px) {
      .header a {
        float: none;
        display: block;
        text-align: left;
      }
      .header-right {
        float: none;
      }
    }
  </style>
</head>
<body>
  <div class="header">
    <a href="#home" class="logo">Project-LuLo</a>
    <div class="header-right">
      <a class="active" href="#home">Home</a>
      <a href="#games">Games</a>
      <a href="#contact">Contact</a>
    </div>
  </div>

  <div class="shell"> 
    <a href="#test" class="card">
      <div class="card-header">Games</div>
      <div class="card-main">
        <i class="material-icons">videogame_asset</i>
        <div class="main-description">Web Based Games</div>
      </div>
    </a>

    <a href="#test" class="card">
      <div class="card-header">Other</div>
      <div class="card-main">
        <i class="material-icons">question_mark</i>
        <div class="main-description">Cool Random Stuff</div>
      </div>
    </a>
  </div>
</body>
</html>

二、关键调整说明

  1. 结构修复:将所有外部资源链接、样式代码移至<head>标签内,确保浏览器正确解析样式规则。
  2. 增强样式优先级:使用.card-main .material-icons选择器替代通用的.material-icons,避免样式被其他规则覆盖,确保font-size设置生效。

三、备选方案(若上述方法无效)

  • 清除浏览器缓存:按下Ctrl + F5(Windows)或Cmd + Shift + R(Mac)强制刷新页面,避免旧缓存的样式干扰。
  • 使用缩放属性:如果font-size仍不生效,可以临时用transform: scale()放大图标(注意可能导致轻微模糊):
.card-main .material-icons {
  transform: scale(2); /* 放大2倍,按需调整 */
  transform-origin: center; /* 保持中心缩放 */
}

内容的提问来源于stack exchange,提问作者user20151407

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 09:10:32