提问人:Ranish Kumar 提问时间:11/17/2023 最后编辑:Ranish Kumar 更新时间:11/17/2023 访问量:80
当我们在 HTML 中使用 PHP 时,为什么 CSS 不起作用?[关闭]
Why CSS not working when we use PHP inside the HTML? [closed]
问:
我创建了一个网页。在这个网页中,我给出了 max-width。这个网页看起来像一个照片库。每张图片都在锚标记内,因此如果有人单击它,它将重定向到其他页面。我在网格模板列为 1fr 1fr 1fr 的网格中进行了设计,以便每个设备的布局都保持不变。CSS在硬编码时可以正常工作,但是一旦我使用PHP来获取数据,布局/设计就无法正常工作。以下是我的代码-
@import url("https://fonts.googleapis.com/css2?family=Montserrat:wght@100;200;300;400;500;600;700;800;900&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Montserrat", sans-serif;
}
.blog-page-main-container {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
}
.blog-page-main-container .logo {
width: 100%;
display: flex;
align-items: center;
justify-content: center;
padding: 15px;
border-bottom: 1px solid #ddd;
gap: 8px;
}
.blog-page-main-container .logo img {
width: 100%;
max-width: 40px;
}
.blog-page-main-container .logo-title p {
font-weight: 500;
font-size: 0.95rem;
}
.blog-page-main-container .tagline {
padding: 15px 0;
}
.blog-page-main-container .tagline p {
color: #666;
font-size: 0.85rem;
}
.blogs-main-container {
width: 100%;
height: 100%;
display: flex;
align-items: flex-start;
justify-content: center;
}
.blogs-main-container .blogs-container {
width: 100%;
max-width: 916px;
gap: 5px;
display: flex;
flex-direction: column;
}
.blogs-main-container .blogs-container .blog-banner {
width: auto;
height: auto;
display: flex;
}
.blogs-main-container .blogs-container .blog-banner img {
width: 100%;
height: 100%;
object-fit: cover;
aspect-ratio: 16 / 9;
flex-grow: 1;
}
.blogs-main-container .blogs-container .blogs-grid {
width: 100%;
height: 100%;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-auto-rows: auto;
gap: 5px;
max-width: 916px;
margin-bottom: 5px;
justify-content: center;
}
.blogs-main-container .blogs-container .blogs-grid a {
display: flex;
width: auto;
height: auto;
}
.blogs-main-container .blogs-container .blogs-grid a img {
width: 100%;
height: 100%;
object-fit: cover;
aspect-ratio: 1 / 1;
flex-grow: 1;
width: 30%;
}
@media only screen and (max-width: 768px) {
.blog-page-main-container .logo {
justify-content: flex-start;
}
.blogs-main-container .blogs-container {
gap: 2px;
}
.blogs-main-container .blogs-container .blogs-grid {
gap: 2px;
}
}
<div class="blog-page-main-container">
<div class="logo">
<img src="img/company-logo.png" alt="">
<div class="logo-title">
<p>Lorem</p>
</div>
</div>
<div class="tagline">
<p>Click on media to learn more</p>
</div>
<div class="blogs-main-container">
<div class="blogs-container">
<div class="blog-banner">
<img src="img/banner.jpg" alt="">
</div>
<div class="blogs-grid">
<?php
$sql = "SELECT * FROM blogs ORDER BY blog_id DESC";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)){
$blog_image = $row['blog_image'];
$blog_img_alt_desc = $row['blog_img_alt_desc'];
$blog_image_link = $row['blog_image_credit_link'];
echo '<a href="'.$blog_image_link.'">
<img src="img/'.$blog_image.'" alt="'.$blog_img_alt_desc.'">
</a>';
}
?>
</div>
</div>
</div>
</div>
注意 - 当我们从图像标签中删除“$blog_image”时,CSS 工作正常。
答:
-2赞
lasha maraneli
11/17/2023
#1
我猜,如果你调用domain/img/'.$blog_image.,它将显示404,应该是路径错误,你只需要纠正它。
评论
0赞
Ranish Kumar
11/17/2023
好友 “$blog_image” 是一个变量。
4赞
ADyson
11/17/2023
这充其量应该是一个评论,而不是一个答案(即便如此,这显然是一个完全随机且无关紧要的猜测)
0赞
lasha maraneli
11/18/2023
我知道这是一个变量,我猜是找不到该变量名称下的图像。随机,如果它完全无关紧要......
评论
$blog_image
$blog_image
$blog_img_alt_desc
.blogs-main-container .blogs-container .blogs-grid a img
width: 100%;
width: 30%;