如何使用 html 将我的图片置于幻灯片的中央? (w3 模板)

How do i center my images on a slideshow using html? (w3 template)

我正在使用 Html 和 Css 开发一个网站,我一直在尝试将幻灯片中的图像居中,我搜索了整个堆栈溢出和无数其他网站,但是我仍然无法找到使用与我编写的代码类似的代码的任何解决方案或答案。

<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<style>
.mySlides {display:none;}
</style>
<body>


<div class="w3-content w3-display-container">
  <img class="mySlides" src="pancakes.jpeg" style="width: 75%">
  <img class="mySlides" src="acai.jpg" style="width: 75%">
  <img class="mySlides" src="avocado.jpg" style="width: 75%">
  

  <button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1)">&#10094;</button>
  <button class="w3-button w3-black w3-display-right" onclick="plusDivs(1)">&#10095;</button>
</div>

<script>
var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  if (n > x.length) {slideIndex = 1}
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";  
  }
  x[slideIndex-1].style.display = "block";  
}
</script>

试试这个

<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<style>
.mySlides {display:none;margin:auto}
</style>
<body>


<div class="w3-content w3-display-container">
  <img class="mySlides" src="pancakes.jpeg">
  <img class="mySlides" src="acai.jpg">
  <img class="mySlides" src="avocado.jpg">
  

  <button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1)">&#10094;</button>
  <button class="w3-button w3-black w3-display-right" onclick="plusDivs(1)">&#10095;</button>
</div>

<script>
var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  if (n > x.length) {slideIndex = 1}
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";  
  }
  x[slideIndex-1].style.display = "block";  
}
</script>

我已将 margin:auto 添加到 .mySlides class 并且我已从 img 标签

中删除 width:75%

试试这个

<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<style>
.mySlides {display:none;}
</style>
<body>


<div class="w3-content w3-display-container">
  <img class="mySlides" src="pancakes.jpeg" style="max-width: inherit; max-height: inherit">
  <img class="mySlides" src="acai.jpg" style="max-width: inherit; max-height: inherit">
  <img class="mySlides" src="avocado.jpg" style="max-width: inherit; max-height: inherit">
  

  <button class="w3-button w3-black w3-display-left" onclick="plusDivs(-1)">&#10094;</button>
  <button class="w3-button w3-black w3-display-right" onclick="plusDivs(1)">&#10095;</button>
</div>

<script>
var slideIndex = 1;
showDivs(slideIndex);

function plusDivs(n) {
  showDivs(slideIndex += n);
}

function showDivs(n) {
  var i;
  var x = document.getElementsByClassName("mySlides");
  if (n > x.length) {slideIndex = 1}
  if (n < 1) {slideIndex = x.length}
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";  
  }
  x[slideIndex-1].style.display = "block";  
}
</script> 

不要对图像标签应用特殊的宽度或高度,而是让您的 img 继承它们的容器,这样每个 img 都将具有相同的大小并完全占据容器。