我想将图像与 div 的中心对齐

我尝试将文本对齐属性与显示块一起使用,但这也不起作用。我想要一个覆盖整个网页的 div,山图像中心在其底部对齐。我设法通过设置bottom:0;将山图像移动到底部。但后来我无法居中对齐图像。非常感谢。


 <!-- following is the html code: -->

    <body>

      <div class="topcontainer">

        <img class="topcloud" src="images/newcloud.png" alt="cloud-image">

        <h1>I'm Mohit</h1>

        <p class="occupation">A <span>pro</span>grammer</p>

        <img class="bottomcloud" src="images/newcloud.png" alt="cloud-image">

        <img class="mountain" src="http://seanhalpin.io/assets/img/content/home/masthead/land.svg" alt="">

      </div>

    </body>



    <!-- and this is the css: -->

    body {

      margin: 0;

      text-align: center;

      font-family: 'Merriweather', serif;

    }


    h1 {

      margin: 0;

      font-family: 'Sacramento', cursive;

      font-size: 70px;

      color: #30e3ca;

    }


    h2 {

      font-family: 'Montserrat', sans-serif;

    }


    h3 {

      font-family: 'Montserrat', sans-serif;

    }


    span {

      text-decoration: underline;

    }


    .mountain {

      display: inline-block;

      position: absolute;

      bottom: 0;

    }


    .topcontainer {

      display: inline-block;

      background-color: #e4f9f5;

      margin-bottom: 20px;

      height: 100vh;

      width: 100%;

      position: relative;

    }

    .bottomcloud {

      position: absolute;

      left:300px;

      height: 94.28px;

      width: 177.3333px;

    }


    .topcloud {

      position: relative;

      left: 290px;

      height: 94.28px;

      width: 177.3333px;

    }


长风秋雁
浏览 110回答 2
2回答

呼如林

我稍微编辑了代码以实现您想要的效果,只需将父 div 添加到图像标签并设置其样式即可。所以这是我的解决方案,希望对您有所帮助。HTML:<body>&nbsp; &nbsp; &nbsp; <div class="topcontainer">&nbsp; &nbsp; &nbsp; &nbsp; <img class="topcloud" src="images/newcloud.png" alt="cloud-image">&nbsp; &nbsp; &nbsp; &nbsp; <h1>I'm Mohit</h1>&nbsp; &nbsp; &nbsp; &nbsp; <p class="occupation">A <span>pro</span>grammer</p>&nbsp; &nbsp; &nbsp; &nbsp; <img class="bottomcloud" src="images/newcloud.png" alt="cloud-image">&nbsp; &nbsp; &nbsp; &nbsp; <div class="mountain">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img class="mountain" src="http://seanhalpin.io/assets/img/content/home/masthead/land.svg" alt="">&nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </body>CSS:&nbsp;body {&nbsp; &nbsp; &nbsp; margin: 0;&nbsp; &nbsp; &nbsp; text-align: center;&nbsp; &nbsp; &nbsp; font-family: 'Merriweather', serif;&nbsp; &nbsp; }&nbsp; &nbsp; h1 {&nbsp; &nbsp; &nbsp; margin: 0;&nbsp; &nbsp; &nbsp; font-family: 'Sacramento', cursive;&nbsp; &nbsp; &nbsp; font-size: 70px;&nbsp; &nbsp; &nbsp; color: #30e3ca;&nbsp; &nbsp; }&nbsp; &nbsp; h2 {&nbsp; &nbsp; &nbsp; font-family: 'Montserrat', sans-serif;&nbsp; &nbsp; }&nbsp; &nbsp; h3 {&nbsp; &nbsp; &nbsp; font-family: 'Montserrat', sans-serif;&nbsp; &nbsp; }&nbsp; &nbsp; span {&nbsp; &nbsp; &nbsp; text-decoration: underline;&nbsp; &nbsp; }&nbsp; &nbsp; .mountain {&nbsp; &nbsp; &nbsp; display: flex;&nbsp; &nbsp; &nbsp; justify-content: flex-end;&nbsp; &nbsp; &nbsp; align-items: center;&nbsp; &nbsp; &nbsp; position: absolute;&nbsp; &nbsp; &nbsp; bottom: 0;&nbsp; &nbsp; }&nbsp; &nbsp; .topcontainer {&nbsp; &nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; &nbsp; background-color: #e4f9f5;&nbsp; &nbsp; &nbsp; margin-bottom: 20px;&nbsp; &nbsp; &nbsp; height: 100vh;&nbsp; &nbsp; &nbsp; width: 100%;&nbsp; &nbsp; &nbsp; position: relative;&nbsp; &nbsp; }&nbsp; &nbsp; .bottomcloud {&nbsp; &nbsp; &nbsp; position: absolute;&nbsp; &nbsp; &nbsp; left:300px;&nbsp; &nbsp; &nbsp; height: 94.28px;&nbsp; &nbsp; &nbsp; width: 177.3333px;&nbsp; &nbsp; }&nbsp; &nbsp; .topcloud {&nbsp; &nbsp; &nbsp; position: relative;&nbsp; &nbsp; &nbsp; left: 290px;&nbsp; &nbsp; &nbsp; height: 94.28px;&nbsp; &nbsp; &nbsp; width: 177.3333px;&nbsp; &nbsp; }

拉丁的传说

这里最简单的解决方案是在 CSS 中制作 div 背景图像,而不是使用图像标签。像这样的东西:<div class="topcontainer">&nbsp; &nbsp;//other stuff in here&nbsp;</div>.topcontainer {height: 100vh;width: 100vw;background-image: url('http://seanhalpin.io/assets/img/content/home/masthead/land.svg');background-repeat: no-repeat;background-position: bottom;}希望这可以帮助!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5