简体   繁体   English

如何拉伸 h1 容器以匹配父 div 的 100% 宽度

[英]how to stretch h1 container to match 100% width of parent div

这是 H1,我希望空白覆盖 100%

Here is sample of the code with parent and child as well as the current styling这是带有父母和孩子以及当前样式的代码示例

 main #main-content-wrapper div { padding: 0; margin: 0; } img { border-radius: 8px; }.overlay1 { position: absolute; top: 0; right: .1px; bottom: 399px; height: 100%; width: 100%; opacity: 0; transition: all.5s ease-in-out; background-color: #088508; }.overlay2 { position: absolute; top: 0; right: .1px; bottom: 399px; height: 100%; width: 100%; opacity: 0; transition: all.5s ease-in-out; background-color: #088508; }.overlay3 { position: absolute; top: 0; right: .1px; bottom: 399px; height: 100%; width: 100%; opacity: 0; transition: all.5s ease-in-out; background-color: #088508; }.jadePackage1:hover.overlay1 { opacity: 1; background: rgba(8, 133, 8, 0.53); }.rubyPackage1:hover.overlay2 { opacity: 1; background: rgba(100, 43, 46, 0.53); }.sapphirePackage1:hover.overlay3 { opacity: 1; background: rgba(0, 0, 255, 0.43); }.text { color: white; font-size: 20px; position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); text-align: center; }.jadePackage1 { background-image: url("https://placekitten.com/400/300"); background-size: cover; height: 400px; width: 300px; } ul { width: 370px; } ul li { font-size: 17px; text-align: left; /*border: 1px dotted black;*/ } ul.colorOptions { list-style-type: decimal; }
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/> <main id="main-content-wrapper"> <div class="container-fluid" id="travelPackages"> <div class="row"> <div class="col-12 col-md-6 col-lg-3 jadePackage1" style="margin-right: 30px; margin-left: 200px; border: 1px solid black;"> <h1 style="font-size: 20px; background-color: white;display: flex; justify-content: center; margin: auto; padding: 0 100px 0 100px; ">The Jade Package <span>$19.99</span></h1> </div> </div> </div> </main>

I am attempting to have the header that reads "The Jade" cover 100% of the top of the photo that way it will look like a header on top of a photo and not one placed in the middle of photo.我试图让 header 读取“The Jade”覆盖照片顶部的 100%,这样它看起来就像在照片顶部的 header,而不是放在照片中间。

I have played around with different display sets and widths yet this is the largest I have been able to get it, close but not quite there.我玩过不同的显示装置和宽度,但这是我能得到的最大的,接近但不完全在那里。

You have a bit of a mess in inline and css styles, so I suggest you to move all styles to css and reorganize it, then you'll have less troubles when you try to add any new style. You have a bit of a mess in inline and css styles, so I suggest you to move all styles to css and reorganize it, then you'll have less troubles when you try to add any new style.

As for your question, h1 already takes 100% of available width, but it stops because of wrapper's paddings.至于您的问题, h1已经占用了可用宽度的 100%,但由于包装器的填充而停止。 You need to remove padding in the wrapper container ( .jadePackage1 ) and it will work — I added inline padding: 0 to it.您需要删除包装容器( .jadePackage1 )中的填充,它会起作用 - 我添加了 inline padding: 0 See attached snippet.请参阅附加的片段。

 main #main-content-wrapper div { padding: 0; margin: 0; } img { border-radius: 8px; }.overlay1 { position: absolute; top: 0; right: .1px; bottom: 399px; height: 100%; width: 100%; opacity: 0; transition: all.5s ease-in-out; background-color: #088508; }.overlay2 { position: absolute; top: 0; right: .1px; bottom: 399px; height: 100%; width: 100%; opacity: 0; transition: all.5s ease-in-out; background-color: #088508; }.overlay3 { position: absolute; top: 0; right: .1px; bottom: 399px; height: 100%; width: 100%; opacity: 0; transition: all.5s ease-in-out; background-color: #088508; }.jadePackage1:hover.overlay1 { opacity: 1; background: rgba(8, 133, 8, 0.53); }.rubyPackage1:hover.overlay2 { opacity: 1; background: rgba(100, 43, 46, 0.53); }.sapphirePackage1:hover.overlay3 { opacity: 1; background: rgba(0, 0, 255, 0.43); }.text { color: white; font-size: 20px; position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); text-align: center; }.jadePackage1 { background-image: url("https://placekitten.com/400/300"); background-size: cover; height: 400px; width: 300px; } ul { width: 370px; } ul li { font-size: 17px; text-align: left; /*border: 1px dotted black;*/ } ul.colorOptions { list-style-type: decimal; }
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/> <main id="main-content-wrapper"> <div class="container-fluid" id="travelPackages"> <div class="row"> <div class="col-12 col-md-6 col-lg-3 jadePackage1" style="margin-right: 30px; margin-left: 200px; border: 1px solid black; padding: 0"> <h1 style="font-size: 20px; background-color: white;display: flex; justify-content: center; padding: 0 100px 0 100px; ">The Jade Package <span>$19.99</span></h1> </div> </div> </div> </main>

It's almost always a bad idea to muck around with Bootstrap's grid (containers, rows, and columns).使用 Bootstrap 的网格(容器、行和列)几乎总是一个坏主意。 Don't put custom margin and padding on them.不要在它们上面放置自定义边距和填充。 Definitely don't use specific widths.绝对不要使用特定的宽度。 That just breaks everything.那只会破坏一切。 Stop using inline styles, even for development.停止使用内联 styles,即使用于开发。 If you need to experiment, do it in the browser inspector and then put it on a custom class.如果您需要进行实验,请在浏览器检查器中进行,然后将其放在自定义 class 上。

I might create a custom class and pull that column's content out past the column padding.我可能会创建一个自定义 class 并将该列的内容拉出列填充。 Leave the grid alone.不理会网格。

 .jadePackage1 { background-image: url("https://placekitten.com/400/300"); background-size: cover; border: 1px solid black; min-height: 150px; } h1 { font-size: 20px; background-color: white; display: flex; justify-content: center; margin: auto; padding: 0 100px 0 100px; }.w-100-in-col { margin-left: -12px; margin-right: -12px; }
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" /> <main id="main-content-wrapper"> <div class="container-fluid" id="travelPackages"> <div class="row"> <div class="col-12 col-md-6 col-lg-3 jadePackage1"> <h1 class="w-100-in-col">The Jade Package <span>$19.99</span></h1> </div> </div> </div> </main>

My best advice you you right now is to strip out as much of your styling as you can related to Bootstrap elements.我现在最好的建议是尽可能多地去掉与 Bootstrap 元素相关的样式。 Work with the library, not against it, and only style your grid content unless there's no other way.使用库,而不是反对它,除非没有其他方法,否则只设置网格内容的样式。

You can add tis rule....您可以添加 tis 规则....

.row > .jadePackage1 {
  padding: 0;
}

...to reset the default padding applied by bootstrap to the parent container of that h1. ...重置引导程序应用到该 h1 的父容器的默认填充。

 main #main-content-wrapper div { padding: 0; margin: 0; } img { border-radius: 8px; }.overlay1 { position: absolute; top: 0; right: .1px; bottom: 399px; height: 100%; width: 100%; opacity: 0; transition: all.5s ease-in-out; background-color: #088508; }.overlay2 { position: absolute; top: 0; right: .1px; bottom: 399px; height: 100%; width: 100%; opacity: 0; transition: all.5s ease-in-out; background-color: #088508; }.overlay3 { position: absolute; top: 0; right: .1px; bottom: 399px; height: 100%; width: 100%; opacity: 0; transition: all.5s ease-in-out; background-color: #088508; }.jadePackage1:hover.overlay1 { opacity: 1; background: rgba(8, 133, 8, 0.53); }.rubyPackage1:hover.overlay2 { opacity: 1; background: rgba(100, 43, 46, 0.53); }.sapphirePackage1:hover.overlay3 { opacity: 1; background: rgba(0, 0, 255, 0.43); }.text { color: white; font-size: 20px; position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); text-align: center; }.jadePackage1 { background-image: url("https://placekitten.com/400/300"); background-size: cover; height: 400px; width: 300px; } ul { width: 370px; } ul li { font-size: 17px; text-align: left; /*border: 1px dotted black;*/ } ul.colorOptions { list-style-type: decimal; }.row >.jadePackage1 { padding: 0; }
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/> <main id="main-content-wrapper"> <div class="container-fluid" id="travelPackages"> <div class="row"> <div class="col-12 col-md-6 col-lg-3 jadePackage1" style="margin-right: 30px; margin-left: 200px; border: 1px solid black;"> <h1 style="font-size: 20px; background-color: white;display: flex; justify-content: center; margin: auto; padding: 0 100px 0 100px;">The Jade Package <span>$19.99</span></h1> </div> </div> </div> </main>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM