Github仓库中readme.md的自定义css文件

sbdsn5lh  于 2023-11-19  发布在  Git
关注(0)|答案(4)|浏览(204)

我在验证.css文件的名称时遇到了麻烦,该文件将在Github repo的根目录下修改readme.md文件。
我认为是:

  1. .github/github.css

字符串
但这似乎对Markdown没有任何影响。有人知道这是否是错误的吗?

ej83mcc0

ej83mcc01#

您可以在svg文件中的<foreignObject>标记中添加一些HTML(实际上是XHTML)和CSS,然后将其嵌入GitHub README中的<img>标记中。
这是一个简单的CSS动画,它改变了h1文本的颜色:

  1. h1 {
  2. color: red;
  3. animation: myanimation 2s infinite;
  4. }
  5. @keyframes myanimation {
  6. from {
  7. color: red;
  8. }
  9. to {
  10. color: yellow;
  11. }
  12. }

个字符
您可以将样式和HTML嵌入到svg内部的<foreignObject>标记中,如下所示:

example.svg

  1. <svg fill="none" viewBox="0 0 400 400" width="400" height="400" xmlns="http://www.w3.org/2000/svg">
  2. <foreignObject width="100%" height="100%">
  3. <div xmlns="http://www.w3.org/1999/xhtml">
  4. <style>
  5. h1 {
  6. color: red;
  7. animation: mymove 2s infinite;
  8. }
  9. @keyframes mymove {
  10. from {
  11. color: red;
  12. }
  13. to {
  14. color: yellow;
  15. }
  16. }
  17. </style>
  18. <h1>HELLO WORLD!</h1>
  19. </div>
  20. </foreignObject>
  21. </svg>


然后,最后你可以使用<img>标签将svg嵌入到你的README中,它应该使用应用的CSS样式呈现你的HTML:

README.md

  1. # My GitHub README
  2. Welcome to my README!
  3. <div align="center">
  4. <img src="example.svg" width="400" height="400" alt="css-in-readme">
  5. </div>


another example & source

展开查看全部
jgzswidk

jgzswidk2#

出于安全原因,GitHub不允许CSS通过CSS影响README.md文件(就好像你可以将CSS注入ReadMe,你可以很容易地发起钓鱼攻击)。这包括通过<link rel>引用的样式表和<style>使用的内联样式。
自述文件采用markdown语法,因此可以进行一些样式化,例如通过占位符图像添加颜色,就像StackOverflow上的这里一样。例如,您可以使用以下内容添加红色方块

  1. - ![#f03c15](https://placehold.it/15/f03c15/000000?text=+) `#f03c15`

字符串
您还可以使用diffjsonhtmljscss来影响文本着色。

n3ipq98p

n3ipq98p3#

对于Readme.md文件的简单甚至复杂的样式。您也可以以这种最简单的方式使用SVG,以及其他方式。
这是我的Readme.md与一个例子的风格。Readme.md:

  1. # Using CSS in Readme.md file
  2. Readme.md files do not allow direct CSS classes injection.
  3. Though, there are several ways you can inject a CSS file or link into your README.md file, I prefer using the SVG files for simple styling or even complex charts and tables. [see my simple `Readme.md` example with SVG styling][1]
  4. ## ![plot](./title.svg)
  5. This is a title that can be manipulated.
  6. ## ![plot](./subTitle.svg)
  7. This is a sub-title with cyan color but can be manipulated

字符串
title.svg:

  1. <svg xmlns="http://www.w3.org/2000/svg" height="80">
  2. <style src="./style.css" >
  3. .title {
  4. transition: fill .3s ease;
  5. <!-- cursor: works in github not in preview mode -->
  6. cursor: pointer;
  7. font-family: Helvetica, sans-serif;
  8. border-radius: 5px 5px 5px 5px;
  9. fill:#2c1300;
  10. text-anchor: middle;
  11. dominant-baseline: middle;
  12. font-size: 24px;
  13. }
  14. </style>
  15. <g class="title">
  16. <rect rx="5" width="100%" height="80"></rect>
  17. <text x="50%" y="50%" width="" font-weight="bold" fill="yellow" >TypeScript Course</text>
  18. </g>
  19. </svg>


subTitle.svg:

  1. <svg xmlns="http://www.w3.org/2000/svg" height="40">
  2. <style src="./style.css" >
  3. .subTitle {
  4. fill: #00FFFF;
  5. transition: fill .3s ease;
  6. }
  7. </style>
  8. <g class="subTitle">
  9. <text x="30%" y="10" dy=".35em">
  10. TypeScript Jobs
  11. </text>
  12. </g>
  13. </svg>

展开查看全部
weylhg0b

weylhg0b4#

将图像 Package 在标签中,如下所示

  1. <div>
  2. <img src="/images/count.png" width="250" > &nbsp;
  3. <img src="/images/home.png" width="250"> &nbsp;
  4. <img src="/images/profile.png" width="250"> &nbsp;
  5. </div>

字符串
注意:images文件夹和自述文件在项目的根目录下

相关问题