gpt4 book ai didi

css - 如何将文本框从导航菜单的底部边框水平和垂直居中到横幅图像的末尾?

转载 作者:行者123 更新时间:2023-11-28 11:56:05 25 4
gpt4 key购买 nike

如何在响应式网站上将文本框从导航菜单的底部边框水平和垂直居中到横幅图像的末尾?
如果有任何建议,我将不胜感激。

我更新了更改。它现在居中。

CSS 媒体查询最小宽度 768:包含 enter image description here

CSS 封面: enter image description here

CSS 包含:我更喜欢这个背景,因为整个图像都显示出来了。但是,我无法使此方法起作用,因为我无法使字体框在导航的黄色底部边框和横幅图像底部之间居中。此外,如果我增加浏览器大小,背景图像将不会占用浏览器的宽度。 enter image description here

横幅的背景图片: banner's background image

原始 JSFiddle: https://jsfiddle.net/4enbjpyz/

更新 JSFiddle: https://jsfiddle.net/me37hnvs/

这是我的:

引导 HTML:

<div class="container-fluid">
<div class="bannerHeader">
<nav class="navbar navbar-expand-md navbar-dark" id="myNavmenu">
<a class="navbar-brand d-md-none ml-auto" href="#" id="menu">Menu</a>
<button class="navbar-toggler" type="button" id="TEST" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item pr-3"><a class="nav-link active" href="#">Nav 1</a></li>
<li class="nav-item"><a class="nav-link" href="payment.html">Nav 2</a></li>
</ul>
</div>
</nav>

<div class = "d-flex">
<div class = "container justify-content-center fontBox pl-4 pr-4 pt-3">
<h3>Heading</h3>
<p>Bootstrap is the most popular HTML, CSS, and JS framework for developing responsive, mobile-first projects on the web.</p>
</div>
</div>
</div>
</div>

<div class="container bg-white">
<p>This is some text.</p>
<p>This is another text.</p>
</div>

CSS:

/* Extra small devices (phones, less than 768px) */
/* No media query since this is the default in Bootstrap */

a {
text-decoration: none;
}

body {
background: #fff;
}

.container-fluid {
padding:0;
}

.bannerHeader {
/* background-color: I normally use a different color, however I used a red background to show there is no negative space (red) */
background-color: #fe0808;
background-image: url("../images/banner.jpg");
background-repeat: no-repeat;
background-size: cover;
position: relative;
height: 450px;
}

#myNavmenu {
background-color: rgba(0,0,0,.1);
border: 1px solid yellow;
}

/* Center Horizontal and Vertically */
.fontBox {
background-color: rgba(255,255,255,.3);
color: #fff;;
letter-spacing: 1px;
margin: 0;
position: absolute;
top: 58%;
left: 50%;
transform: translate(-50%, -58%);
width: 90%;
}

p {
font-size: 16px;
}

/* Medium devices (tablets, 768px and up)*/
@media (min-width: 768px) {
.bannerHeader {
background-position: center top;
background-size: contain;
background-repeat: no-repeat;
height: 0;
padding: 0;
/* aspect ratios: divide the height of the original image by it's own width, and multiply by 100 to get the percentage value */
padding-bottom: 52.33%;
position: relative;
}
}

最佳答案

<img>与其他标签相比,标签有一些特殊的行为。在你的情况下,我建议你使用 div (或者可能是 figure )标记并将横幅附加为背景图像,然后设置任何 CSS 属性来控制图像行为。然后你可以使用 css flex box 来让盒子居中。您也可以使用 CSS calc()方法如果导航和字体框的高度是固定的。 sudo 示例可能是这样的:

top: calc(100% - ({navigationHeight} + {fontBoxHeight}));

不要忘记将字体框放在 figure 中如果您将使用此方法,请标记。

希望对您有所帮助。

关于css - 如何将文本框从导航菜单的底部边框水平和垂直居中到横幅图像的末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52675825/

25 4 0