php - 如何对齐页面右侧的链接/图片列表,并阻止它们重叠?

标签 php html css api

我想为我的网站生成一个指向其他页面的链接列表(使用 PHP/HTML/CSS 的组合)并将它们在页面右侧对齐而不重叠。我能够生成链接/图片,但我遇到的问题是当我尝试使用绝对位置时它们相互重叠。

<style>
body{
    background: lightblue;
    margin: 25px;
}
.title{
    font-size: 20px;
}
.recipe{
    width: 60%;
}
.related{
    position: absolute;
    float: right;
    right: 10px;
    width: 25%;
    list-style-position: inside;
}
.a{
    float: right;

    right: 5px;
}
.relatedImages{

}img{
    width: 15%;
    height: 17%;
}

</style>


<title><?php $recipeInfo['title']; ?></title>
<body>
    <br>
    <?php  
        //Title and image of recipe
        echo    '<br><br><div class="title">' .$recipeInfo['title']. '</div><br>
                <div class="mainImage"><image src="' . $recipeInfo['image']. '"> </div>
                <br><h2> Ingredients </h2>';

        //Unfinished (Needs  to be styled correctly)
        //Generating related links with clickable images
        for($r = 0; $r < $relatedLinks[$r]; $r++){
            echo '<div class = "related">
                <a href = "recipeInfo.php?id='.$relatedLinks[$r]['id']. '">'.$relatedLinks[$r]['title'].'<br>
                <image src = "https://spoonacular.com/recipeImages/' . $relatedLinks[$r]['image'] . '"></a>
                <br>
                </div>';
        };
        ;

        //Loop that generates a list of the ingredients used
        for($i = 0; $i < $recipeInfo['extendedIngredients'][$i]; $i++){
            $amount = $recipeInfo['extendedIngredients'][$i]['amount'];
            $unit = $recipeInfo['extendedIngredients'][$i]['unit'];
            $ingrName = $recipeInfo['extendedIngredients'][$i]['name'];
            echo '<div class = "ingredients">' . $amount , " " ,  $unit , " " ,  $ingrName .' </div>';
        }



        //Instructions with error handling for no instructions found
        $instructions = $recipeInfo['instructions'];
        if($instructions == ""){
            $instructions = "Whoops, there are no available instructions for this recipe.";
        }                
        echo '<br><h2> Insructions </h2> 
        <div class="recipe">' . $instructions . '</div><br>';

        //Unfinished, but will hopefully print a better list of instructions than just a dense paragraph
        //for($j = 0; $j < sizeOf($recipeInstr); $j++){
        //    echo '<h3>' .$recipeInstr[$j]['name'].'</h3>';
        //    for($n = 0; $n < $recipeInstr[$j]['steps']; $n++){
        //        echo '<div class="instruction">'. $n , " " , $recipeInstr[$j]['steps'][$n]['step'] . '<div>';
        //    }
        //}

    ?>  
</body>
</html>

我想要达到的预期效果类似于 youtube 在页面右侧显示相关视频的方式。

最佳答案

好吧,你可以试试这段代码:

<body>
<br>

<div class="maincontent">
<div class="main">
<?php  
    //Title and image of recipe
    echo    '<br><br><div class="title">' .$recipeInfo['title']. '</div><br>
            <div class="mainImage"><image src="' . $recipeInfo['image']. '"> </div>
            <br><h2> Ingredients </h2>';



    //Loop that generates a list of the ingredients used
    for($i = 0; $i < $recipeInfo['extendedIngredients'][$i]; $i++){
        $amount = $recipeInfo['extendedIngredients'][$i]['amount'];
        $unit = $recipeInfo['extendedIngredients'][$i]['unit'];
        $ingrName = $recipeInfo['extendedIngredients'][$i]['name'];
        echo '<div class = "ingredients">' . $amount , " " ,  $unit , " " ,  $ingrName .' </div>';
    }



    //Instructions with error handling for no instructions found
    $instructions = $recipeInfo['instructions'];
    if($instructions == ""){
        $instructions = "Whoops, there are no available instructions for this recipe.";
    }                
    echo '<br><h2> Insructions </h2> 
    <div class="recipe">' . $instructions . '</div><br>';

    //Unfinished, but will hopefully print a better list of instructions than just a dense paragraph
    //for($j = 0; $j < sizeOf($recipeInstr); $j++){
    //    echo '<h3>' .$recipeInstr[$j]['name'].'</h3>';
    //    for($n = 0; $n < $recipeInstr[$j]['steps']; $n++){
    //        echo '<div class="instruction">'. $n , " " , $recipeInstr[$j]['steps'][$n]['step'] . '<div>';
    //    }
    //}

?>  
</div>
<div class="sidelinks">
<?php 
//Unfinished (Needs  to be styled correctly)
    //Generating related links with clickable images
    for($r = 0; $r < $relatedLinks[$r]; $r++){
        echo '<div class = "related">
            <a href = "recipeInfo.php?id='.$relatedLinks[$r]['id']. '">'.$relatedLinks[$r]['title'].'<br>
            <image src = "https://spoonacular.com/recipeImages/' . $relatedLinks[$r]['image'] . '"></a>
            <br>
            </div>';
    };
    ;
</div>
</div>
</body>
 </html>

和CSS代码:

<style>
.maincontent {

 display: grid;
 grid-template-columns: 70% 30%;
 }
body{
background: lightblue;
margin: 25px;
 }
.title{
font-size: 20px;
}
.recipe{
width: 60%;
 }
.related{



list-style-position: inside;
}
.a{
float: right;

right: 5px;
  }
.relatedImages{

}img{
width: 15%;
height: 17%;
}

</style>

这应该可以,我无法测试

关于php - 如何对齐页面右侧的链接/图片列表,并阻止它们重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55385911/

相关文章:

php - 使用 php 更新表中的值

html - css代码中的语法错误

html - scss mixin 在 haml 文件中不起作用

python - Scrapy - 如何使用 python 抓取网络链接中的网络链接?

javascript - 使用 appenChild 添加 div 并使用 removeChild 减少 div

php - 如何包含其他目录中的文件

php - 通过自定义包的 Symfony 路由仅适用于控制台模式

javascript - 值未通过 Javascript 函数输入到 mysql 数据库中

html - 为什么隐藏了部分图像

jquery - 动画粘性导航?