python 基准测试(cProfile kcachegrind line_profiler memory_profiler)

learn from 《python高性能(第2版)》

类似工具pycharm profile对函数调用效率进行测试

1. 例子

一个圆周运动的动画

代码语言:javascript代码运行次数:0运行复制

from matplotlib import pyplot as pltfrom matplotlib import animationfrom random import uniformimport timeitclass Particle:    __slots__ = ('x', 'y', 'ang_speed')    # 声明成员只允许这么多,不能动态添加,当生成大量实例时,可以减少内存占用    def __init__(self, x, y, ang_speed):        self.x = x        self.y = y        self.ang_speed = ang_speedclass ParticleSimulator:    def __init__(self, particles):        self.particles = particles    def evolve(self, dt):        timestep = 0.00001        nsteps = int(dt / timestep)        for i in range(nsteps):            for p in self.particles:                norm = (p.x ** 2 + p.y ** 2) ** 0.5                v_x = (-p.y) / norm                v_y = p.x / norm                d_x = timestep * p.ang_speed * v_x                d_y = timestep * p.ang_speed * v_y                p.x += d_x                p.y += d_ydef visualize(simulator):    X = [p.x for p in simulator.particles]    Y = [p.y for p in simulator.particles]    fig = plt.figure()    ax = plt.subplot(111, aspect='equal')    line, = ax.plot(X, Y, 'ro')    # Axis limits    plt.xlim(-1, 1)    plt.ylim(-1, 1)    # It will be run when the animation starts    def init():        line.set_data([], [])        return line,    def animate(i):        # We let the particle evolve for 0.1 time units        simulator.evolve(0.01)        X = [p.x for p in simulator.particles]        Y = [p.y for p in simulator.particles]        line.set_data(X, Y)        return line,    # Call the animate function each 10 ms    anim = animation.FuncAnimation(fig,                                   animate,                                   init_func=init,                                   blit=True,                                   interval=10)    plt.show()def test_visualize():    particles = [Particle(0.3, 0.5, +1),                 Particle(0.0, -0.5, -1),                 Particle(-0.1, -0.4, +3),                 Particle(-0.2, -0.8, +3),]    simulator = ParticleSimulator(particles)    visualize(simulator)if __name__ == '__main__':    test_visualize()
python 基准测试(cProfile  kcachegrind  line_profiler  memory_profiler)

2. 运行耗时测试linux time 命令代码语言:javascript代码运行次数:0运行复制

def benchmark():    particles = [Particle(uniform(-1.0, 1.0),                          uniform(-1.0, 1.0),                          uniform(-1.0, 1.0))                  for i in range(100)]    simulator = ParticleSimulator(particles)    # visualize(simulator)    simulator.evolve(0.1)if __name__ == '__main__':    benchmark()

生成100个实例,模拟 0.1 秒

在 linux 中进行测试耗时:

立即学习“Python免费学习笔记(深入)”;

代码语言:javascript代码运行次数:0运行复制

time python my.pyreal    0m10.435s  # 进程实际花费时间user    0m2.078s  # 计算期间 所有CPU花费总时间sys     0m1.412s  #  执行系统相关任务(内存分配)期间,所有CPU花费总时间

python timeit包指定 循环次数、重复次数代码语言:javascript代码运行次数:0运行复制

def timing():    result = timeit.timeit('benchmark()',                           setup='from __main__ import benchmark',                           number=10)    # Result is the time it takes to run the whole loop    print(result)    result = timeit.repeat('benchmark()',                           setup='from __main__ import benchmark',                           number=10,                           repeat=3)    # Result is a list of times    print(result)

输出:

代码语言:javascript代码运行次数:0运行复制

6.9873279229996115[6.382431660999828, 6.248147055000118, 6.325469069000064]

pytest、pytest-benchmark代码语言:javascript代码运行次数:0运行复制

pip install pytestpip install pytest-benchmark

代码语言:javascript代码运行次数:0运行复制

$ pytest test_simul.py::test_evolve=================== test session starts ====================platform linux -- Python 3.8.10, pytest-7.1.2, pluggy-1.0.0benchmark: 3.4.1 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)rootdir: /mnt/d/gitcode/Python_learning/Python-High-Performance-Second-Edition-master/Chapter01plugins: benchmark-3.4.1collected 1 itemtest_simul.py .                                      [100%]---------------------------------------------- benchmark: 1 tests ---------------------------------------------Name (time in ms)         Min      Max     Mean  StdDev   Median     IQR  Outliers      OPS  Rounds  Iterations---------------------------------------------------------------------------------------------------------------test_evolve           15.9304  42.7975  20.1502  5.6825  18.2795  3.7249       5;5  49.6274      58           1---------------------------------------------------------------------------------------------------------------Legend:  Outliers: 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.  OPS: Operations Per Second, computed as 1 / Mean

上面显示,测了58次,用时的最小、最大、均值、方差、中位数等

3. cProfile 找出瓶颈profile包是 python写的开销比较大,cProfile 是C语言编写的,开销小代码语言:javascript代码运行次数:0运行复制

python -m cProfile simul.py

代码语言:javascript代码运行次数:0运行复制

$ python -m cProfile simul.py         2272804 function calls (2258641 primitive calls) in 8.209 seconds   Ordered by: standard name   ncalls  tottime  percall  cumtime  percall filename:lineno(function)       30    0.000    0.000    0.001    0.000 :177(any)      160    0.000    0.000    0.002    0.000 :177(column_stack)      161    0.000    0.000    0.004    0.000 :177(concatenate)       34    0.000    0.000    0.000    0.000 :177(copyto)       30    0.000    0.000    0.002    0.000 :177(linspace)       30    0.000    0.000    0.000    0.000 :177(ndim)       30    0.000    0.000    0.000    0.000 :177(result_type)        5    0.000    0.000    0.116    0.023 :1002(_gcd_import)   485/33    0.001    0.000    6.807    0.206 :1017(_handle_fromlist)   。。。

输出结果非常长

tottime 排序 -s tottime,看前几个就是耗时最多的几个

代码语言:javascript代码运行次数:0运行复制

$ python -m cProfile -s tottime simul.py         2272784 function calls (2258621 primitive calls) in 7.866 seconds   Ordered by: internal time   ncalls  tottime  percall  cumtime  percall filename:lineno(function)     1258    2.498    0.002    2.498    0.002 {built-in method posix.stat}      273    1.057    0.004    1.057    0.004 {built-in method io.open_code}       27    0.874    0.032    0.879    0.033 {built-in method _imp.create_dynamic}        1    0.691    0.691    0.691    0.691 simul.py:21(evolve)      273    0.464    0.002    0.464    0.002 {method 'read' of '_io.BufferedReader' objects}      273    0.432    0.002    1.953    0.007 :1034(get_data)    32045    0.245    0.000    0.411    0.000 inspect.py:625(cleandoc)       30    0.171    0.006    0.171    0.006 {built-in method posix.listdir}       33    0.151    0.005    0.151    0.005 {built-in method io.open}

或者使用代码

代码语言:javascript代码运行次数:0运行复制

>>> from simul import benchmark>>> import cProfile>>> cProfile.run('benchmark()')                  707 function calls in 0.733 seconds   Ordered by: standard name   ncalls  tottime  percall  cumtime  percall filename:lineno(function)        1    0.000    0.000    0.733    0.733 :1()      300    0.000    0.000    0.000    0.000 random.py:415(uniform)      100    0.000    0.000    0.000    0.000 simul.py:10(__init__)        1    0.000    0.000    0.733    0.733 simul.py:117(benchmark)        1    0.000    0.000    0.000    0.000 simul.py:118()        1    0.000    0.000    0.000    0.000 simul.py:18(__init__)        1    0.733    0.733    0.733    0.733 simul.py:21(evolve)        1    0.000    0.000    0.733    0.733 {built-in method builtins.exec}        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}      300    0.000    0.000    0.000    0.000 {method 'random' of '_random.Random' objects}

profile 对象开启和关闭之间可以包含任意代码

代码语言:javascript代码运行次数:0运行复制

>>> from simul import benchmark>>> import cProfile>>>>>> pr = cProfile.Profile()>>> pr.enable()>>> benchmark()>>> pr.disable()>>> pr.print_stats()         706 function calls in 0.599 seconds   Ordered by: standard name   ncalls  tottime  percall  cumtime  percall filename:lineno(function)        1    0.000    0.000    0.000    0.000 :1()      300    0.000    0.000    0.000    0.000 random.py:415(uniform)      100    0.000    0.000    0.000    0.000 simul.py:10(__init__)        1    0.000    0.000    0.599    0.599 simul.py:117(benchmark)        1    0.000    0.000    0.000    0.000 simul.py:118()        1    0.000    0.000    0.000    0.000 simul.py:18(__init__)        1    0.599    0.599    0.599    0.599 simul.py:21(evolve)        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}      300    0.000    0.000    0.000    0.000 {method 'random' of '_random.Random' objects}

tottime 不含调用其他函数的时间,cumtime 执行函数(包含调用其他函数的时间)的总时间KCachegrind 图形化分析

KCachegrindpyprof2calltreecProfile

代码语言:javascript代码运行次数:0运行复制

sudo apt install kcachegrindpip install pyprof2calltree

代码语言:javascript代码运行次数:0运行复制

python -m cProfile -o prof.out taylor.pypyprof2calltree -i prof.out -o prof.calltree

代码语言:javascript代码运行次数:0运行复制

kcachegrind prof.calltree

安装 kcachegrind 失败,没有运行截图

还有其他工具 Gprof2Dot 可以生成调用图

4. line_profiler

它是一个 py 包,安装后,对要监视的函数应用 装饰器 @profile

代码语言:javascript代码运行次数:0运行复制

pip install line_profiler

https://github.com/rkern/line_profiler

代码语言:javascript代码运行次数:0运行复制

kernprof -l -v simul.py

代码语言:javascript代码运行次数:0运行复制

$ kernprof -l -v simul.pyWrote profile results to simul.py.lprofTimer unit: 1e-06 sTotal time: 4.39747 sFile: simul.pyFunction: evolve at line 21Line #      Hits         Time  Per Hit   % Time  Line Contents==============================================================    21                                               @profile    22                                               def evolve(self, dt):    23         1          5.0      5.0      0.0          timestep = 0.00001    24         1          5.0      5.0      0.0          nsteps = int(dt/timestep)    25    26     10001       5419.0      0.5      0.1          for i in range(nsteps):    27   1010000     454924.0      0.5     10.3              for p in self.particles:    28    29   1000000     791441.0      0.8     18.0                  norm = (p.x**2 + p.y**2)**0.5    30   1000000     537019.0      0.5     12.2                  v_x = (-p.y)/norm    31   1000000     492304.0      0.5     11.2                  v_y = p.x/norm    32    33   1000000     525471.0      0.5     11.9                  d_x = timestep * p.ang_speed * v_x    34   1000000     521829.0      0.5     11.9                  d_y = timestep * p.ang_speed * v_y    35    36   1000000     537637.0      0.5     12.2                  p.x += d_x    37   1000000     531418.0      0.5     12.1                  p.y += d_y

代码语言:javascript代码运行次数:0运行复制

python -m line_profiler simul.py.lprof

代码语言:javascript代码运行次数:0运行复制

$ python -m line_profiler simul.py.lprofTimer unit: 1e-06 sTotal time: 5.34553 sFile: simul.pyFunction: evolve at line 21Line #      Hits         Time  Per Hit   % Time  Line Contents==============================================================    21                                               @profile    22                                               def evolve(self, dt):    23         1          3.0      3.0      0.0          timestep = 0.00001    24         1          3.0      3.0      0.0          nsteps = int(dt/timestep)    25    26     10001       6837.0      0.7      0.1          for i in range(nsteps):    27   1010000     567894.0      0.6     10.6              for p in self.particles:    28    29   1000000     953363.0      1.0     17.8                  norm = (p.x**2 + p.y**2)**0.5    30   1000000     656821.0      0.7     12.3                  v_x = (-p.y)/norm    31   1000000     601929.0      0.6     11.3                  v_y = p.x/norm    32    33   1000000     635255.0      0.6     11.9                  d_x = timestep * p.ang_speed * v_x    34   1000000     636091.0      0.6     11.9                  d_y = timestep * p.ang_speed * v_y    35    36   1000000     651873.0      0.7     12.2                  p.x += d_x    37   1000000     635462.0      0.6     11.9                  p.y += d_y

5. 性能优化用更简洁的计算公式预计算不变量减少赋值语句,消除中间变量

注意:细微的优化,速度有所提高,但可能并不显著,还需要保证算法正确

6. dis 模块

该包可以了解代码是如何转换为字节码的, dis 表示 disassemble 反汇编

代码语言:javascript代码运行次数:0运行复制

import disdis.dis(函数名)

代码语言:javascript代码运行次数:0运行复制

dis.dis(ParticleSimulator.evolve) 22           0 LOAD_CONST               1 (1e-05)              2 STORE_FAST               2 (timestep) 23           4 LOAD_GLOBAL              0 (int)              6 LOAD_FAST                1 (dt)              8 LOAD_FAST                2 (timestep)             10 BINARY_TRUE_DIVIDE             12 CALL_FUNCTION            1             14 STORE_FAST               3 (nsteps) 25          16 LOAD_GLOBAL              1 (range)             18 LOAD_FAST                3 (nsteps)             20 CALL_FUNCTION            1             22 GET_ITER        >>   24 FOR_ITER               118 (to 144)             26 STORE_FAST               4 (i) 26          28 LOAD_FAST                0 (self)             30 LOAD_ATTR                2 (particles)             32 GET_ITER        >>   34 FOR_ITER               106 (to 142)             36 STORE_FAST               5 (p) 28          38 LOAD_FAST                5 (p)             40 LOAD_ATTR                3 (x)             42 LOAD_CONST               2 (2)             44 BINARY_POWER             46 LOAD_FAST                5 (p)             48 LOAD_ATTR                4 (y)             50 LOAD_CONST               2 (2)             52 BINARY_POWER             54 BINARY_ADD             56 LOAD_CONST               3 (0.5)             58 BINARY_POWER             60 STORE_FAST               6 (norm) 29          62 LOAD_FAST                5 (p)             64 LOAD_ATTR                4 (y)             66 UNARY_NEGATIVE             68 LOAD_FAST                6 (norm)             70 BINARY_TRUE_DIVIDE             72 STORE_FAST               7 (v_x) 30          74 LOAD_FAST                5 (p)             76 LOAD_ATTR                3 (x)             78 LOAD_FAST                6 (norm)             80 BINARY_TRUE_DIVIDE             82 STORE_FAST               8 (v_y) 32          84 LOAD_FAST                2 (timestep)             86 LOAD_FAST                5 (p)             88 LOAD_ATTR                5 (ang_speed)             90 BINARY_MULTIPLY             92 LOAD_FAST                7 (v_x)             94 BINARY_MULTIPLY             96 STORE_FAST               9 (d_x) 33          98 LOAD_FAST                2 (timestep)            100 LOAD_FAST                5 (p)            102 LOAD_ATTR                5 (ang_speed)            104 BINARY_MULTIPLY            106 LOAD_FAST                8 (v_y)            108 BINARY_MULTIPLY            110 STORE_FAST              10 (d_y) 35         112 LOAD_FAST                5 (p)            114 DUP_TOP            116 LOAD_ATTR                3 (x)            118 LOAD_FAST                9 (d_x)            120 INPLACE_ADD            122 ROT_TWO            124 STORE_ATTR               3 (x) 36         126 LOAD_FAST                5 (p)            128 DUP_TOP            130 LOAD_ATTR                4 (y)            132 LOAD_FAST               10 (d_y)            134 INPLACE_ADD            136 ROT_TWO            138 STORE_ATTR               4 (y)            140 JUMP_ABSOLUTE           34        >>  142 JUMP_ABSOLUTE           24        >>  144 LOAD_CONST               0 (None)            146 RETURN_VALUE

可以是用该工具了解指令的多少和代码是如何转换的

7. memory_profiler

https://pypi.org/project/memory-profiler/

代码语言:javascript代码运行次数:0运行复制

pip install memory_profilerpip install psutil

psutil说明

也需要对监视的函数 加装饰器 @profile

代码语言:javascript代码运行次数:0运行复制

python -m memory_profiler simul.py

代码语言:javascript代码运行次数:0运行复制

$ python -m memory_profiler simul.pyFilename: simul.pyLine #    Mem usage    Increment  Occurrences   Line Contents=============================================================   141   67.465 MiB   67.465 MiB           1   @profile   142                                         def benchmark_memory():   143   84.023 MiB   16.559 MiB      300004       particles = [Particle(uniform(-1.0, 1.0),   144   84.023 MiB    0.000 MiB      100000                             uniform(-1.0, 1.0),   145   84.023 MiB    0.000 MiB      100000                             uniform(-1.0, 1.0))   146   84.023 MiB    0.000 MiB      100001                     for i in range(100000)]   147   148   84.023 MiB    0.000 MiB           1       simulator = ParticleSimulator(particles)   149   84.023 MiB    0.000 MiB           1       simulator.evolve(0.001)

内存使用随时间的变化

代码语言:javascript代码运行次数:0运行复制

$ mprof run simul.pymprof: Sampling memory every 0.1srunning new processrunning as a Python program...

绘制曲线

代码语言:javascript代码运行次数:0运行复制

$ mprof plot
python 基准测试(cProfile  kcachegrind  line_profiler  memory_profiler)

以上就是python 基准测试(cProfile kcachegrind line_profiler memory_profiler)的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/24753.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年11月2日 08:04:15
下一篇 2025年11月2日 08:29:46

相关推荐

  • Uniapp 中如何不拉伸不裁剪地展示图片?

    灵活展示图片:如何不拉伸不裁剪 在界面设计中,常常需要以原尺寸展示用户上传的图片。本文将介绍一种在 uniapp 框架中实现该功能的简单方法。 对于不同尺寸的图片,可以采用以下处理方式: 极端宽高比:撑满屏幕宽度或高度,再等比缩放居中。非极端宽高比:居中显示,若能撑满则撑满。 然而,如果需要不拉伸不…

    2025年12月24日
    400
  • 如何让小说网站控制台显示乱码,同时网页内容正常显示?

    如何在不影响用户界面的情况下实现控制台乱码? 当在小说网站上下载小说时,大家可能会遇到一个问题:网站上的文本在网页内正常显示,但是在控制台中却是乱码。如何实现此类操作,从而在不影响用户界面(UI)的情况下保持控制台乱码呢? 答案在于使用自定义字体。网站可以通过在服务器端配置自定义字体,并通过在客户端…

    2025年12月24日
    800
  • 如何在地图上轻松创建气泡信息框?

    地图上气泡信息框的巧妙生成 地图上气泡信息框是一种常用的交互功能,它简便易用,能够为用户提供额外信息。本文将探讨如何借助地图库的功能轻松创建这一功能。 利用地图库的原生功能 大多数地图库,如高德地图,都提供了现成的信息窗体和右键菜单功能。这些功能可以通过以下途径实现: 高德地图 JS API 参考文…

    2025年12月24日
    400
  • 如何使用 scroll-behavior 属性实现元素scrollLeft变化时的平滑动画?

    如何实现元素scrollleft变化时的平滑动画效果? 在许多网页应用中,滚动容器的水平滚动条(scrollleft)需要频繁使用。为了让滚动动作更加自然,你希望给scrollleft的变化添加动画效果。 解决方案:scroll-behavior 属性 要实现scrollleft变化时的平滑动画效果…

    2025年12月24日
    000
  • 如何为滚动元素添加平滑过渡,使滚动条滑动时更自然流畅?

    给滚动元素平滑过渡 如何在滚动条属性(scrollleft)发生改变时为元素添加平滑的过渡效果? 解决方案:scroll-behavior 属性 为滚动容器设置 scroll-behavior 属性可以实现平滑滚动。 html 代码: click the button to slide right!…

    2025年12月24日
    500
  • 如何选择元素个数不固定的指定类名子元素?

    灵活选择元素个数不固定的指定类名子元素 在网页布局中,有时需要选择特定类名的子元素,但这些元素的数量并不固定。例如,下面这段 html 代码中,activebar 和 item 元素的数量均不固定: *n *n 如果需要选择第一个 item元素,可以使用 css 选择器 :nth-child()。该…

    2025年12月24日
    200
  • 使用 SVG 如何实现自定义宽度、间距和半径的虚线边框?

    使用 svg 实现自定义虚线边框 如何实现一个具有自定义宽度、间距和半径的虚线边框是一个常见的前端开发问题。传统的解决方案通常涉及使用 border-image 引入切片图片,但是这种方法存在引入外部资源、性能低下的缺点。 为了避免上述问题,可以使用 svg(可缩放矢量图形)来创建纯代码实现。一种方…

    2025年12月24日
    100
  • 如何解决本地图片在使用 mask JS 库时出现的跨域错误?

    如何跨越localhost使用本地图片? 问题: 在本地使用mask js库时,引入本地图片会报跨域错误。 解决方案: 要解决此问题,需要使用本地服务器启动文件,以http或https协议访问图片,而不是使用file://协议。例如: python -m http.server 8000 然后,可以…

    2025年12月24日
    200
  • 如何让“元素跟随文本高度,而不是撑高父容器?

    如何让 元素跟随文本高度,而不是撑高父容器 在页面布局中,经常遇到父容器高度被子元素撑开的问题。在图例所示的案例中,父容器被较高的图片撑开,而文本的高度没有被考虑。本问答将提供纯css解决方案,让图片跟随文本高度,确保父容器的高度不会被图片影响。 解决方法 为了解决这个问题,需要将图片从文档流中脱离…

    2025年12月24日
    000
  • 为什么 CSS mask 属性未请求指定图片?

    解决 css mask 属性未请求图片的问题 在使用 css mask 属性时,指定了图片地址,但网络面板显示未请求获取该图片,这可能是由于浏览器兼容性问题造成的。 问题 如下代码所示: 立即学习“前端免费学习笔记(深入)”; icon [data-icon=”cloud”] { –icon-cl…

    2025年12月24日
    200
  • 如何利用 CSS 选中激活标签并影响相邻元素的样式?

    如何利用 css 选中激活标签并影响相邻元素? 为了实现激活标签影响相邻元素的样式需求,可以通过 :has 选择器来实现。以下是如何具体操作: 对于激活标签相邻后的元素,可以在 css 中使用以下代码进行设置: li:has(+li.active) { border-radius: 0 0 10px…

    2025年12月24日
    100
  • 如何模拟Windows 10 设置界面中的鼠标悬浮放大效果?

    win10设置界面的鼠标移动显示周边的样式(探照灯效果)的实现方式 在windows设置界面的鼠标悬浮效果中,光标周围会显示一个放大区域。在前端开发中,可以通过多种方式实现类似的效果。 使用css 使用css的transform和box-shadow属性。通过将transform: scale(1.…

    2025年12月24日
    200
  • 为什么我的 Safari 自定义样式表在百度页面上失效了?

    为什么在 Safari 中自定义样式表未能正常工作? 在 Safari 的偏好设置中设置自定义样式表后,您对其进行测试却发现效果不同。在您自己的网页中,样式有效,而在百度页面中却失效。 造成这种情况的原因是,第一个访问的项目使用了文件协议,可以访问本地目录中的图片文件。而第二个访问的百度使用了 ht…

    2025年12月24日
    000
  • 如何用前端实现 Windows 10 设置界面的鼠标移动探照灯效果?

    如何在前端实现 Windows 10 设置界面中的鼠标移动探照灯效果 想要在前端开发中实现 Windows 10 设置界面中类似的鼠标移动探照灯效果,可以通过以下途径: CSS 解决方案 DEMO 1: Windows 10 网格悬停效果:https://codepen.io/tr4553r7/pe…

    2025年12月24日
    000
  • 使用CSS mask属性指定图片URL时,为什么浏览器无法加载图片?

    css mask属性未能加载图片的解决方法 使用css mask属性指定图片url时,如示例中所示: mask: url(“https://api.iconify.design/mdi:apple-icloud.svg”) center / contain no-repeat; 但是,在网络面板中却…

    2025年12月24日
    000
  • 如何用CSS Paint API为网页元素添加时尚的斑马线边框?

    为元素添加时尚的斑马线边框 在网页设计中,有时我们需要添加时尚的边框来提升元素的视觉效果。其中,斑马线边框是一种既醒目又别致的设计元素。 实现斜向斑马线边框 要实现斜向斑马线间隔圆环,我们可以使用css paint api。该api提供了强大的功能,可以让我们在元素上绘制复杂的图形。 立即学习“前端…

    2025年12月24日
    000
  • 图片如何不撑高父容器?

    如何让图片不撑高父容器? 当父容器包含不同高度的子元素时,父容器的高度通常会被最高元素撑开。如果你希望父容器的高度由文本内容撑开,避免图片对其产生影响,可以通过以下 css 解决方法: 绝对定位元素: .child-image { position: absolute; top: 0; left: …

    2025年12月24日
    000
  • 使用 Mask 导入本地图片时,如何解决跨域问题?

    跨域疑难:如何解决 mask 引入本地图片产生的跨域问题? 在使用 mask 导入本地图片时,你可能会遇到令人沮丧的跨域错误。为什么会出现跨域问题呢?让我们深入了解一下: mask 框架假设你以 http(s) 协议加载你的 html 文件,而当使用 file:// 协议打开本地文件时,就会产生跨域…

    2025年12月24日
    200
  • CSS 帮助

    我正在尝试将文本附加到棕色框的左侧。我不能。我不知道代码有什么问题。请帮助我。 css .hero { position: relative; bottom: 80px; display: flex; justify-content: left; align-items: start; color:…

    2025年12月24日 好文分享
    200
  • HTML、CSS 和 JavaScript 中的简单侧边栏菜单

    构建一个简单的侧边栏菜单是一个很好的主意,它可以为您的网站添加有价值的功能和令人惊叹的外观。 侧边栏菜单对于客户找到不同项目的方式很有用,而不会让他们觉得自己有太多选择,从而创造了简单性和秩序。 今天,我将分享一个简单的 HTML、CSS 和 JavaScript 源代码来创建一个简单的侧边栏菜单。…

    2025年12月24日
    200

发表回复

登录后才能评论
关注微信