在页面调整大小时,在flexbox中将标题与图像对齐

huangapple go评论91阅读模式
英文:

align title over images in flexbox when page resized

问题

我想在一个flexbox内的图像列表上有4个链接,但当我调整页面大小时,链接穿过图像,我认为这是因为图像和链接不是“相关的”,因此它们之间的填充和边距不起作用,但我不知道如何修复它。我猜我必须为它们都添加特定的position命令来使它们相关,但我不知道如何。

  1. <!-- 开始代码片段:js 隐藏:false 控制台:true babel:false -->
  2. <!-- 语言:lang-js -->
  3. const chapter1 = [];
  4. for (let i = 1; i <= 49; i++) {
  5. chapter1.push({ src: `../images/maps/chapter1/${i}.jpg`, alt: `Map ${i}` });
  6. }
  7. const chapter2 = [{ src: '../images/maps/76.jpg' }];
  8. const chapter3 = [{ src: '../images/maps/64.jpg' }];
  9. const chapter4 = [{ src: '../images/maps/98.jpg' }];
  10. // 用于显示所选章节的图像的函数
  11. function showImages(chapter) {
  12. const img_list = document.getElementById('imageList');
  13. img_list.replaceChildren();
  14. // 循环遍历所选章节中的图像
  15. chapter.forEach(image => {
  16. const li = document.createElement('li');
  17. const img = new Image();
  18. img.src = image.src;
  19. img.alt = image.alt;
  20. li.append(img);
  21. const span = document.createElement('span');
  22. span.textContent = "Image";
  23. li.append(span);
  24. img_list.appendChild(li);
  25. });
  26. }
  27. // 为第1章链接添加点击事件
  28. document.querySelector('#chapter1').addEventListener('click', function (e) {
  29. e.preventDefault();
  30. showImages(chapter1);
  31. });
  32. // 为第2章链接添加点击事件
  33. document.querySelector('#chapter2').addEventListener('click', function (e) {
  34. e.preventDefault();
  35. showImages(chapter2);
  36. });
  37. document.querySelector('#chapter3').addEventListener('click', function (e) {
  38. e.preventDefault();
  39. showImages(chapter3);
  40. });
  41. document.querySelector('#chapter4').addEventListener('click', function (e) {
  42. e.preventDefault();
  43. showImages(chapter4);
  44. });
  45. window.onload = function () {
  46. document.body.style.opacity = 1
  47. showImages(chapter1);
  48. }
  1. <!-- 语言:lang-css -->
  2. @charset "utf-8;
  3. /* CSS文档 */
  4. body {
  5. background-image: url("../images/background-maps.jpg");
  6. background-repeat: no-repeat;
  7. background-size: cover;
  8. padding: 0;
  9. margin: 0;
  10. opacity: 0;
  11. transition: opacity 1s;
  12. }
  13. .menu {
  14. background-color: rgba(255, 255, 255, 0.5);
  15. justify-content: center;
  16. display: flex;
  17. margin: 0 auto;
  18. width: 55%;
  19. height: 100%;
  20. padding: 10px;
  21. position: relative;
  22. }
  23. .menu ul {
  24. list-style-type: none;
  25. padding: 0;
  26. display: flex;
  27. flex-wrap: wrap;
  28. justify-content: space-between;
  29. }
  30. .menu li {
  31. width: 20%;
  32. min-width: 200px;
  33. margin: 15px;
  34. text-align: center;
  35. }
  36. .menu img {
  37. width: 100%;
  38. height: 100%;
  39. object-fit: cover;
  40. }
  41. .menu li span {
  42. padding: 5px;
  43. }
  44. a {
  45. font-size: 20px;
  46. font-weight: 200;
  47. padding: 5px;
  48. }
  49. h1 {
  50. font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
  51. font-weight: 600;
  52. font-size: 75px;
  53. text-align: center;
  54. text-shadow: 1px 1px 2px red, 0 0 0.5em blue, 0 0 0.1em blue;
  55. margin: auto;
  56. padding: 5px;
  57. }
  58. .mappages {
  59. padding: 5px;
  60. position: absolute;
  61. top: 8px;
  62. left: 16px;
  63. }
  1. <!-- 语言:lang-html -->
  2. <header>
  3. <h1>地图画廊</h1>
  4. </header>
  5. <div class="menu">
  6. <ul id="imageList"></ul>
  7. <div class="mappages">
  8. <a href="#" id="chapter1">第1章</a>
  9. <a href="#" id="chapter2">第2章</a>
  10. <a href="#" id="chapter3">第3章</a>
  11. <a href="#" id="chapter4">第4章</a>
  12. </div>
  13. </div>
英文:

i want to have 4 links over a list of images inside a flexbox but when i resize the page the links go through the images, i believe its because the images and the links arent "related" and therefore padding and margin doesnt work between them, but i dont know how to fix it. im guessing i have to add specific position: commands to them both to make them related but i dont know.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const chapter1 = [];
  2. for (let i = 1; i &lt;= 49; i++){
  3. chapter1.push({src: `../images/maps/chapter1/${i}.jpg`, alt: `Map ${i}`});
  4. }
  5. const chapter2 = [{src: &#39;../images/maps/76.jpg&#39;}];
  6. const chapter3 = [{src: &#39;../images/maps/64.jpg&#39;}];
  7. const chapter4 = [{src: &#39;../images/maps/98.jpg&#39;}];
  8. // Function to display images for the chosen chapter
  9. function showImages(chapter) {
  10. const img_list = document.getElementById(&#39;imageList&#39;);
  11. img_list.replaceChildren();
  12. // Loop through the images in the selected chapter
  13. chapter.forEach(image =&gt; {
  14. const li = document.createElement(&#39;li&#39;);
  15. const img = new Image();
  16. img.src= image.src;
  17. img.alt=image.alt;
  18. li.append(img);
  19. const span = document.createElement(&#39;span&#39;);
  20. span.textContent = &quot;Image&quot;;
  21. li.append(span);
  22. img_list.appendChild(li);
  23. });
  24. }
  25. // Add click event to the Chapter 1 link
  26. document.querySelector(&#39;#chapter1&#39;).addEventListener(&#39;click&#39;, function(e) {
  27. e.preventDefault();
  28. showImages(chapter1);
  29. });
  30. // Add click event to the Chapter 2 link
  31. document.querySelector(&#39;#chapter2&#39;).addEventListener(&#39;click&#39;, function(e) {
  32. e.preventDefault();
  33. showImages(chapter2);
  34. });
  35. document.querySelector(&#39;#chapter3&#39;).addEventListener(&#39;click&#39;, function(e) {
  36. e.preventDefault();
  37. showImages(chapter3);
  38. });
  39. document.querySelector(&#39;#chapter4&#39;).addEventListener(&#39;click&#39;, function(e) {
  40. e.preventDefault();
  41. showImages(chapter4);
  42. });
  43. window.onload = function(){
  44. document.body.style.opacity = 1
  45. showImages(chapter1);
  46. }

<!-- language: lang-css -->

  1. @charset &quot;utf-8&quot;;
  2. /* CSS Document */
  3. body {
  4. background-image: url(&quot;../images/background-maps.jpg&quot;);
  5. background-repeat: no-repeat;
  6. background-size: cover;
  7. padding:0;
  8. margin:0;
  9. opacity: 0;
  10. transition: opacity 1s;
  11. /*animation: fadeInAnimation ease 3s;
  12. animation-iteration-count: 1;
  13. animation-fill-mode: forwards;*/
  14. }
  15. /*@keyframes fadeInAnimation {
  16. 0% {
  17. opacity: 0;
  18. }
  19. 100% {
  20. opacity: 1;
  21. }
  22. }*/
  23. .menu{
  24. background-color: rgba(255,255,255,0.5);
  25. justify-content: center;
  26. display: flex;
  27. margin: 0 auto;
  28. width:55%;
  29. height: 100%;
  30. padding:10px;
  31. position: relative;
  32. }
  33. .menu ul{
  34. list-style-type: none;
  35. padding: 0;
  36. display: flex;
  37. flex-wrap: wrap;
  38. justify-content: space-between;
  39. }
  40. .menu li{
  41. width: 20%;
  42. min-width: 200px;
  43. margin: 15px;
  44. text-align: center;
  45. }
  46. .menu img{
  47. width: 100%;
  48. height: 100%;
  49. object-fit: cover;
  50. }
  51. .menu li span{
  52. padding: 5px;
  53. }
  54. a{
  55. font-size: 20px;
  56. font-weight: 200;
  57. padding:5px;
  58. }
  59. h1{
  60. font-family: &#39;Gill Sans&#39;, &#39;Gill Sans MT&#39;, Calibri, &#39;Trebuchet MS&#39;, sans-serif;
  61. font-weight: 600;
  62. font-size: 75px;
  63. text-align: center;
  64. text-shadow: 1px 1px 2px red, 0 0 0.5em blue, 0 0 0.1em blue;
  65. margin: auto;
  66. padding: 5px;
  67. }
  68. .mappages{
  69. padding: 5px;
  70. position: absolute;
  71. top: 8px;
  72. left: 16px;
  73. }

<!-- language: lang-html -->

  1. &lt;header&gt;
  2. &lt;h1&gt;Map Gallery&lt;/h1&gt;
  3. &lt;/header&gt;
  4. &lt;div class=&quot;menu&quot;&gt;
  5. &lt;ul id=&quot;imageList&quot;&gt;&lt;/ul&gt;
  6. &lt;div class=&quot;mappages&quot;&gt;
  7. &lt;a href=&quot;#&quot; id=&quot;chapter1&quot;&gt;Chapter 1&lt;/a&gt;
  8. &lt;a href=&quot;#&quot; id=&quot;chapter2&quot;&gt;Chapter 2&lt;/a&gt;
  9. &lt;a href=&quot;#&quot; id=&quot;chapter3&quot;&gt;Chapter 3&lt;/a&gt;
  10. &lt;a href=&quot;#&quot; id=&quot;chapter4&quot;&gt;Chapter 4&lt;/a&gt;
  11. &lt;/div&gt;
  12. &lt;/div&gt;

<!-- end snippet -->

答案1

得分: 0

  1. 把 "mappages" 放在 "imageList" 上方,并移除 "mappages" 的绝对定位。同时,需要给 "menu" 添加 "flex-wrap: wrap"。

  2. 如果你想保持元素的当前顺序,那么仍然可以使用绝对定位,但你需要给父元素 "menu" 添加至少 50-60px 的上内边距来解决重叠问题。

英文:
  1. Put "mappages" above the "imageList" and remove the absolute position from "mappages". Also you have to add "flex-wrap: wrap" to "menu".

<!-- begin snippet: js hide: true console: false babel: false -->

<!-- language: lang-html -->

  1. &lt;div class=&quot;menu&quot;&gt;
  2. &lt;div class=&quot;mappages&quot;&gt;&lt;/div&gt;
  3. &lt;ul id=&quot;imageList&quot;&gt;&lt;/ul&gt;
  4. &lt;/div&gt;

<!-- end snippet -->

or

  1. If you want to keep the order of the elements as it's now, then you can still use absolute position but you have to add padding-top at least 50-60px to the parent's element "menu" to fix the overlapping

huangapple
  • 本文由 发表于 2023年2月8日 19:04:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定