简体   繁体   English

如何使页脚元素与CSS具有相同的宽度

[英]How to make footer elements be same width with CSS

I have a footer for my project which has two images... each one of them is stuck to a particular corner. 我的项目有一个页脚,其中包含两个图像...每个图像都固定在一个特定的角落。 The lower left corner is the Division logo (system_footer_left); 左下角是Division徽标(system_footer_left); while the lower right corner is the Brand Logo (system_footer_right). 而右下角是品牌徽标(system_footer_right)。 What I would like to do is to have the size of the section to be the same as the width, even if the widths of the images are not the same. 我想做的是使该部分的大小与宽度相同,即使图像的宽度不同。

How can I make this happen? 我怎样才能做到这一点?

In my current project design, the left logo is about half the width of the right logo. 在我当前的项目设计中,左徽标大约是右徽标宽度的一半。

I am rusty with CSS, I have not been able to find any answers online. 我对CSS感到生疏,无法在线找到任何答案。 I did not want to set the width as a fixed percentage as I would like to have it be dynamic, so that if we change brand assets (logos) it will just "plug and play". 我不想将宽度设置为固定百分比,因为我希望它是动态的,因此,如果我们更改品牌资产(徽标),它将只是“即插即用”。

 #system_footer { padding: 5 5 0 5; position: fixed; left: 0; bottom: 0; width: 100%; background-color: rgba(5, 51, 107, .75); text-align: center; margin: auto; } #system_footer_center { padding: 10px; bottom: 0; background-color: transparent; color: #FFD700; text-align: center; font-size: 12px; font-weight: 500; margin: auto; word-wrap: break-word; text-shadow: 2px 2px 2px #FF8C00; } #system_footer_right { background: rgb(170, 213, 255); background: -webkit-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: -o-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#aad5ff", endColorstr="#05336b", GradientType=1); padding: 14px; float: right; max-width: 30%; text-align: right; border: 3px outset rgba(208, 207, 204, .5); -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; border-radius: 10px 10px 10px 10px; -webkit-box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); -moz-box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); } #system_footer_right:hover { background: rgb(170, 213, 255); background: -webkit-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: -o-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#aad5ff", endColorstr="#05336b", GradientType=1); padding: 14px; float: right; max-width: 30%; text-align: right; border: 4px outset rgba(231, 231, 229, 1); -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; border-radius: 10px 10px 10px 10px; -webkit-box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); -moz-box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); } #system_footer_left { background: rgb(170, 213, 255); background: -webkit-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: -o-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#aad5ff", endColorstr="#05336b", GradientType=1); padding: 14px; float: left; max-width: 30%; text-align: left; -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; border-radius: 10px 10px 10px 10px; border: 3px outset rgba(208, 207, 204, .5); -webkit-box-shadow: 1px -0px 1px 1px rgba(255, 255, 255, 0.75); -moz-box-shadow: 1px -0px 1px 1px rgba(255, 255, 255, 0.75); box-shadow: 1px -0px 1px 1px rgba(255, 255, 255, 0.75); } #system_footer_left:hover { background: rgb(170, 213, 255); background: -webkit-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: -o-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#aad5ff", endColorstr="#05336b", GradientType=1); padding: 14px; float: left; max-width: 30%; text-align: left; -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; border-radius: 10px 10px 10px 10px; border: 4px outset rgba(231, 231, 229, 1); -webkit-box-shadow: 1px -0px 1px 1px rgba(255, 255, 255, 0.75); -moz-box-shadow: 1px -0px 1px 1px rgba(255, 255, 255, 0.75); box-shadow: 1px -0px 1px 1px rgba(255, 255, 255, 0.75); } .cny-logo-sm { width: auto; max-height: 35px; object-fit: scale-down; } @media screen and (min-width: 300px) { #system_footer_center { font-size: calc(12px + (16 - 12) * ((100vw - 300px) / (1000 - 300))); } } /* WITHOUT THE BLOCK BELOW, THE FONT WOULD CONTINUE TO GROW */ @media screen and (min-width: 1000px) { #system_footer_center { font-size: 16px; } } 
 <div id='system_footer'> <div id='system_footer_left'> <img src='../resources/logo/b_white_trans.png' alt='(Decorative) ' class='cny-logo-sm'> </div> <div id='system_footer_right'> <img src='.../resources/logo/cny.png' alt='CNY (Logo)' class='cny-logo-sm'> </div> <div id='system_footer_center'> <p>For technical support, please contact XXXX at <a href='mailto:DONTEMAIL@MY.BRAND.COM?subject=MCR System'>XXX@XXXX.XXX</a></p> </div> </div> 

I am trying to get the left and right footer element frames to be the same width dynamically. 我正在尝试使左右脚注元素框架动态地具有相同的宽度。

You can try something like this. 您可以尝试这样。 You can't by CSS set the same width for 2 elements based on their content, you can set the width in percent or in other relative units for both containers and align content inside. 您不能通过CSS为2个元素的内容设置相同的宽度,而可以为两个容器以百分比或其他相对单位设置宽度,并在其中对齐内容。 I have updated hover effects and sizes because it looks bad when content jumps. 我已经更新了悬停效果和大小,因为当内容跳转时它看起来很糟糕。 I remove duplicated CSS, you have same styles on the left and the right blocks. 我删除了重复的CSS,左右两个块的样式相同。 Images keep aspect ratio, updated styles. 图像保持宽高比,更新样式。 Sorry for my bad English. 对不起,我的英语不好。

 .cny-logo-sm { display: block; margin: auto; width: auto; max-width: 100%; max-height: 35px; object-fit: contain; } #system_footer { box-sizing: border-box; display: flex; justify-content: space-between; padding: 5px 5px 0; position: fixed; left: 0; bottom: 0; right: 0; width: 100%; background-color: rgba(5, 51, 107, .75); text-align: center; margin: auto; } #system_footer_center { flex: 1 0 auto; padding: 10px; bottom: 0; background-color: transparent; color: #FFD700; text-align: center; font-size: 12px; font-weight: 500; margin: auto; word-wrap: break-word; text-shadow: 2px 2px 2px #FF8C00; } #system_footer_right { order: 3; } #system_footer_left, #system_footer_right { display: inline-block; flex: 1 1 30%; /*max-width: 30%;*/ box-sizing: border-box; background: rgb(170, 213, 255); background: -webkit-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: -o-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#aad5ff", endColorstr="#05336b", GradientType=1); padding: 15px; text-align: right; border: 3px outset rgba(208, 207, 204, .5); -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; border-radius: 10px 10px 10px 10px; -webkit-box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); -moz-box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); } #system_footer_left:hover, #system_footer_right:hover { background: rgb(170, 213, 255); background: -webkit-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: -o-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#aad5ff", endColorstr="#05336b", GradientType=1); padding: 14px; max-width: 30%; border: 4px outset rgba(231, 231, 229, 1); -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; border-radius: 10px 10px 10px 10px; -webkit-box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); -moz-box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); } @media screen and (min-width: 300px) { #system_footer_center { font-size: calc(12px + (16 - 12) * ((100vw - 300px) / (1000 - 300))); } } /* WITHOUT THE BLOCK BELOW, THE FONT WOULD CONTINUE TO GROW */ @media screen and (min-width: 1000px) { #system_footer_center { font-size: 16px; } } 
 <div id='system_footer'> <div id='system_footer_left'> <img src='https://via.placeholder.com/150x30' alt='(Decorative) ' class='cny-logo-sm'> </div> <div id='system_footer_right'> <img src='https://via.placeholder.com/270x270' alt='CNY (Logo)' class='cny-logo-sm'> </div> <div id='system_footer_center'> <p>For technical support, please contact XXXX at <a href='mailto:DONTEMAIL@MY.BRAND.COM?subject=MCR System'>XXX@XXXX.XXX</a></p> </div> </div> 

I am still not sure I understand you request but I think that this is what you are needing 我仍然不确定我是否理解您的要求,但我认为这是您所需要的

#system_footer_left img,
#system_footer_right img{
    width:100%;
}

 #system_footer { padding: 5 5 0 5; position: fixed; left: 0; bottom: 0; width: 100%; background-color: rgba(5, 51, 107, .75); text-align: center; margin: auto; } #system_footer_center { padding: 10px; bottom: 0; background-color: transparent; color: #FFD700; text-align: center; font-size: 12px; font-weight: 500; margin: auto; word-wrap: break-word; text-shadow: 2px 2px 2px #FF8C00; } #system_footer_right { background: rgb(170, 213, 255); background: -webkit-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: -o-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#aad5ff", endColorstr="#05336b", GradientType=1); padding: 14px; float: right; max-width: 30%; text-align: right; border: 3px outset rgba(208, 207, 204, .5); -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; border-radius: 10px 10px 10px 10px; -webkit-box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); -moz-box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); } #system_footer_right:hover { background: rgb(170, 213, 255); background: -webkit-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: -o-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#aad5ff", endColorstr="#05336b", GradientType=1); padding: 14px; float: right; max-width: 30%; text-align: right; border: 4px outset rgba(231, 231, 229, 1); -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; border-radius: 10px 10px 10px 10px; -webkit-box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); -moz-box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); box-shadow: -1px -0px 1px 1px rgba(255, 255, 255, 0.75); } #system_footer_left { background: rgb(170, 213, 255); background: -webkit-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: -o-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#aad5ff", endColorstr="#05336b", GradientType=1); padding: 14px; float: left; max-width: 30%; text-align: left; -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; border-radius: 10px 10px 10px 10px; border: 3px outset rgba(208, 207, 204, .5); -webkit-box-shadow: 1px -0px 1px 1px rgba(255, 255, 255, 0.75); -moz-box-shadow: 1px -0px 1px 1px rgba(255, 255, 255, 0.75); box-shadow: 1px -0px 1px 1px rgba(255, 255, 255, 0.75); } #system_footer_left img, #system_footer_right img{ width:100%; } #system_footer_left:hover { background: rgb(170, 213, 255); background: -webkit-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: -o-linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); background: linear-gradient(rgba(170, 213, 255, 0.5) 8%, rgba(5, 51, 107, 1) 29%, rgba(5, 51, 107, 0.6362920168067228) 67%, rgba(170, 213, 255, 1) 100%, rgba(5, 51, 107, 1) 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#aad5ff", endColorstr="#05336b", GradientType=1); padding: 14px; float: left; max-width: 30%; text-align: left; -moz-border-radius: 10px 10px 10px 10px; -webkit-border-radius: 10px 10px 10px 10px; border-radius: 10px 10px 10px 10px; border: 4px outset rgba(231, 231, 229, 1); -webkit-box-shadow: 1px -0px 1px 1px rgba(255, 255, 255, 0.75); -moz-box-shadow: 1px -0px 1px 1px rgba(255, 255, 255, 0.75); box-shadow: 1px -0px 1px 1px rgba(255, 255, 255, 0.75); } .cny-logo-sm { width: auto; max-height: 35px; object-fit: scale-down; } @media screen and (min-width: 300px) { #system_footer_center { font-size: calc(12px + (16 - 12) * ((100vw - 300px) / (1000 - 300))); } } /* WITHOUT THE BLOCK BELOW, THE FONT WOULD CONTINUE TO GROW */ @media screen and (min-width: 1000px) { #system_footer_center { font-size: 16px; } } 
 <div id='system_footer'> <div id='system_footer_left'> <img src='https://via.placeholder.com/500x60?text=Decorative' alt='(Decorative) ' class='cny-logo-sm'> </div> <div id='system_footer_right'> <img src='https://via.placeholder.com/250?text=logo' alt='CNY (Logo)' class='cny-logo-sm'> </div> <div id='system_footer_center'> <p>For technical support, please contact XXXX at <a href='mailto:DONTEMAIL@MY.BRAND.COM?subject=MCR System'>XXX@XXXX.XXX</a></p> </div> </div> 

If I understand what you're asking, you could use grid: 如果我了解您的要求,则可以使用grid:

https://codepen.io/cschloegel/pen/wVzKZo https://codepen.io/cschloegel/pen/wVzKZo

<div class="grid-container">
  <div class="header"></div>
  <div class="content"></div>
  <div class="footer">
    <div class="footer-left"></div>
    <div class="footer-center"></div>
    <div class="footer-right"></div>
  </div>
</div>

CSS: CSS:

.grid-container {
  background-color:lightgrey;
  height:100vh;
  display: grid;
  grid-template-columns: 1fr 6fr 1fr;
  grid-template-rows: 100px 1fr 100px;
  grid-template-areas: "header header header" "content content content" "footer footer footer";
}

.header { 
  grid-area: header;
  background-color: grey;
}

.content { grid-area: content; }

.footer {
  display: grid;
  grid-template-columns: 1fr 6fr 1fr;
  grid-template-rows: 1fr;
  grid-template-areas: "footer-left footer-center footer-right";
  grid-area: footer;
  background-color:grey;
}

.footer-left { 
  grid-area: footer-left;
  border: 4px solid black;
  background-color:darkgrey;
}

.footer-center { grid-area: footer-center; }

.footer-right { grid-area: footer-right;
  border: 4px solid black;
  background-color:darkgrey;
}

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

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