javascript - 如何使用 css 对齐传单 map 中的响应图像

标签 javascript html css leaflet maps

我正在做一个网络 map (带有传单)。此 map 包含一张 png 图像,我想在手机中以 max-width:50% 样式显示。 图像以 max-width:50% 显示,但我无法在 map 右侧对齐图像。

这是我添加图片的js代码:

var logo1 = L.control();
logo1.onAdd = function(map){
    var div = L.DomUtil.create('div');
    div.innerHTML= "<div href='https://www.uma.es/'><img class='resp1' src='logo1.png'/></div>";

    return div;
}  logo1.addTo(map);

这是我的 CSS 样式:

@media screen and (max-width: 500px) {
img.resp1 {
    max-width: 50%;
    text-align: right;
} } @media screen and (min-width: 760px) { img.resp1 {
max-width: 100%;}}

问题的结果和图解说明:

enter image description here

最佳答案

如果容器具有固定高度,则始终可以垂直或水平居中任何元素,使用父元素的相对位置和子元素的绝对位置,例如中心 x 和 y:

.container-elment {
  position: relative;
  width: 100%;
  height: 450px;
}

.child-element {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 50%;
  height: 50%;
  background: green;
}
<div class="container-elment">
  <div class="child-element">

  </div>
</div>

对于中心 x:

.container-elment {
  position: relative;
  width: 100%;
  height: 450px;
}

.child-element {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 50%;
  height: 50%;
  background: green;
}
<div class="container-elment">
  <div class="child-element">

  </div>
</div>

对于中心 y :

.container-elment {
  position: relative;
  width: 100%;
  height: 450px;
}

.child-element {
  position: absolute;
  top: 50%;
  right: 0;
  transform: translateY(-50%);
  width: 50%;
  height: 50%;
  background: green;
}
<div class="container-elment">
  <div class="child-element">

  </div>
</div>

关于javascript - 如何使用 css 对齐传单 map 中的响应图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53238095/

相关文章:

javascript - javascript代码的jquery语法

javascript - Chart.js 多色填充部分

html - 最小宽度的 child 增长到 parent 的大小

php - php变量后无法在html链接中使用类关键字

javascript - 调用dojo.ready内部的函数

javascript - writeFile 不等待变量被实例化

php - 将 Div 作为 Google 文档中的页面并粘贴拆分内容?

CSS3 背景雕刻或半径问题

html - 访问表中的第一行

css - 如何在 snap.svg 中对变换进行动画处理?