简体   繁体   English

无法弄清楚如何为网站正确覆盖带有图像的 div

[英]Can't figure out how to cover a div with an image correctly for website

Title poorly describes the issue, sorry.标题不好描述问题,抱歉。 I have div's ("cards") in my website that contain a lot of content.我的网站中有包含大量内容的 div(“卡片”)。 One of the important thing's I wanted was to have an image covering the "cards", however every time I've attempted to fit them properly, they either don't change from their current setup or they take on massive size relative to the page size and not the "card" size.我想要的重要事情之一是有一张覆盖“卡片”的图像,但是每次我尝试正确放置它们时,它们要么不会从当前设置改变,要么相对于页面尺寸过大大小而不是“卡片”大小。 I've read other threads regarding similar issues, but I haven't gotten anything to work yet.我已经阅读了关于类似问题的其他线程,但我还没有得到任何工作。

Image not fitting into Card correctly图片不适合卡片

 /* Entire div for card base + image inside it */.card { display: flex; flex-flow: row no-wrap; justify-content: space-between; width: 275px; height: 150px; font-size: 25px; padding: 20px; background-color: #f1f1f1; border-radius: 30px; box-shadow: 0px 0px 18px #888888; transition: all 0.5s ease; }.card:hover { background-color: #daeaf5; }.card-image { display: flex; justify-content: center; align-items: center; overflow: hidden }.card-img:hover { -webkit-filter: grayscale(0%); filter: greyscale(0%); }.card-img { width: 100%; height: 100%; flex-shrink: 0; min-width: 100%; min-height: 100%; border-radius: 30px; -webkit-filter: grayscale(100%); filter: greyscale(100%); -webkit-mask-image:-webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0))); mask-image: linear-gradient(to left, rgba(0,0,0,1), rgba(0,0,0,0)); transition: all 0.5s ease; } /* ignore this stuff, it's just styling for the rest of the card */.card-title { font-size: 10px; }.card-content { font-size: 18px; margin-bottom: 5px; white-space: nowrap; }.card-a { color: black; text-decoration: none; border-radius: 30px; margin: 15px; }.main { margin-left: 10px; }.flex-container { display: flex; flex-flow: row wrap; justify-content: flex-start; align-items: flex-start; align-content: flex-start; }
 /* Entire card div, yes I know my code looks like a goblin wrote it, I am a goblin. */ /*So far, this was the only way I have found to get it to work how I wanted to*/ <div class="main"> <ul type="none"> <div class="flex-container"> <a class="card-a" href="###"> <li> <div class="card"> <div class="left-side"> <div class="card-title">Make</div> <div class="card-content">Template</div> <div class="card-title">Model</div> <div class="card-content">Temp</div> <div class="card-title">Trim</div> <div class="card-content">Temp</div> <div class="card-title">Year</div> <div class="card-content">Temp</div> </div> <div class="right-side"> <img class="card-img" src="https://static.turbosquid.com/Preview/001308/648/FU/_D.jpg" alt="Temporary Picture provided by Turbosquid 1308648"/> </div> </div> </li> </a> </div> </ul> </div>

Ideally I want the image to fit snugly into the card, hence the corners on the image matching the corners on the card.理想情况下,我希望图像紧贴卡片,因此图像上的角与卡片上的角匹配。 I've tried to relocate the image into various places, re-sorting the div's and attempting to structure everything differently.我试图将图像重新定位到不同的地方,重新排序 div 并尝试以不同的方式构建所有内容。 Unfortunately, every time I've tried that, I get farther and farther away from the ideal card design.不幸的是,每次我尝试那样做,我都离理想的卡片设计越来越远。 Yes I know, the code looks like chicken scratch compared to anyone else's work, please excuse my poor man's amount of experience.是的,我知道,与其他人的工作相比,代码看起来像鸡爪,请原谅我可怜的人的经验。

 /* Entire div for card base + image inside it */.card { display: flex; flex-flow: row no-wrap; justify-content: space-between; width: 275px; height: 150px; font-size: 25px; padding: 20px; background-color: #f1f1f1; border-radius: 30px; box-shadow: 0px 0px 18px #888888; transition: all 0.5s ease; position: relative; overflow: hidden; }.card:hover { background-color: #daeaf5; }.card-image { display: flex; justify-content: center; align-items: center; overflow: hidden }.card-img:hover { -webkit-filter: grayscale(0%); filter: greyscale(0%); }.card-img { width: 100%; height: 100%; flex-shrink: 0; min-width: 100%; min-height: 100%; border-radius: 30px; -webkit-filter: grayscale(100%); filter: greyscale(100%); -webkit-mask-image:-webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0))); mask-image: linear-gradient(to left, rgba(0,0,0,1), rgba(0,0,0,0)); transition: all 0.5s ease; } /* ignore this stuff, it's just styling for the rest of the card */.card-title { font-size: 10px; }.card-content { font-size: 18px; margin-bottom: 5px; white-space: nowrap; }.card-a { color: black; text-decoration: none; border-radius: 30px; margin: 15px; }.main { margin-left: 10px; }.flex-container { display: flex; flex-flow: row wrap; justify-content: flex-start; align-items: flex-start; align-content: flex-start; }.right-side{ position: absolute; left: 0; top: 0; right: 0; bottom: 0; }
 /* Entire card div, yes I know my code looks like a goblin wrote it, I am a goblin. */ /*So far, this was the only way I have found to get it to work how I wanted to*/ <div class="main"> <ul type="none"> <div class="flex-container"> <a class="card-a" href="###"> <li> <div class="card"> <div class="left-side"> <div class="card-title">Make</div> <div class="card-content">Template</div> <div class="card-title">Model</div> <div class="card-content">Temp</div> <div class="card-title">Trim</div> <div class="card-content">Temp</div> <div class="card-title">Year</div> <div class="card-content">Temp</div> </div> <div class="right-side"> <img class="card-img" src="https://static.turbosquid.com/Preview/001308/648/FU/_D.jpg" alt="Temporary Picture provided by Turbosquid 1308648"/> </div> </div> </li> </a> </div> </ul> </div>

use the CSS background-image property then play with its properties till you get what you want IE:使用 CSS background-image 属性然后使用它的属性直到你得到你想要的 IE:

<div id="card_image">
  <h1>hello World</h1>
  <p>this Section Has a background image</p>
</div>

<style>
  #card_image {
   background-image:url("https://static.turbosquid.com/Preview/001308/648/FU/_D.jpg");
background-size:25%;
backgroun-position:top right;
}
</style>
play with this 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM