如何使用 CSS 将文本定位到图像的左上角


要将文本定位到左上角,请使用 left 和 top 属性。你可以尝试运行以下代码,将文本定位到图像的左上角位置 -

示例

演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         .box {
            position: relative;
         }
         img {
            width: 100%;
            height: auto;
            opacity: 0.6;
         }
         .direction {
            position: absolute;
            top: 10px;
            left: 19px;
            font-size: 13px;
         }
      </style>
   </head>
   <body>
      <h1>Heading One</h1>
      <p>Below image has text in the top left:</p>
      <div class = "box">
         <img src = "https://tutorialspoint.com/python/images/python_data_science.jpg" alt = "Tutorial" width = "800" height = "400">
         <div class = "direction">Top Left Corner</div>
      </div>
   </body>
</html>

更新于: 2020年7月4日

1 千次 + 查看

开启你的 职业

完成课程获得认证

立即开始
广告
© . All rights reserved.