【问题标题】:Can't style Fancybox modal无法设置 Fancybox 模态的样式
【发布时间】:2020-02-16 07:09:27
【问题描述】:

我在设置模态框的样式时遇到问题,我设置为用花哨的框打开。 modal/div 的内容在单击后显示没有问题,但似乎我应用于该模态 div 的所有样式都拒绝显示。

.hidden{
display:none;
}

.red{
background: red;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>modal</title>
  
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css">
</head>
  
  
<body>
  <div>
     <a data-fancybox data-src="#open" href="javascript:;">Open modal  box</a>
  </div>
  
  <div class="hidden">
     <div class="red" id="open">
          <h2>Hello</h2>
          <p>You are awesome.</p>
       </div>
   </div>
  
</body>
   <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>
</html>

这是我在 Codepen 上的setup

【问题讨论】:

  • .red{ 背景:红色 !important;}
  • 你的风格是正确的,但要优先考虑fancybox包含的风格
  • !important 也有效,但我无法将它用于我需要应用于 div 的所有样式。查看 Ritesh Khandekar 的回答

标签: jquery fancybox


【解决方案1】:

如果您的常用选择器无法应用样式,请尝试更具体地选择元素。

ID 具有更高的特定性,因为它们是唯一的。

类名也是特定的,但它们也用于其他样式表

标签名称不是最具体的,因为它们可以在页面的任何位置找到

* 选择器在 css 中的特异性最低

div.red{background: red;} // Selects divs that have 'red' classname

.hidden{
display:none;
}

div.red{
background: red;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>modal</title>
  
   <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css">
</head>
  
  
<body>
  <div>
     <a data-fancybox data-src="#open" href="javascript:;">Open modal  box</a>
  </div>
  
  <div class="hidden">
     <div class="red" id="open">
          <h2>Hello</h2>
          <p>You are awesome.</p>
       </div>
   </div>
  
</body>
   <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>
</html>

【讨论】:

    【解决方案2】:

    Fancybox 将fancybox-content 类名添加到内容中,以添加一些必要的 CSS 属性和一些默认样式,例如白色背景颜色、填充。

    因此,您只需覆盖这些规则,例如:

    .fancybox-content.red {
      background: red;
      padding: 10px 20px;
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多