CSS-调整浏览器窗口大小时的固定边框宽度更改

时间:2018-10-25 15:58:53

标签: html css3 browser flexbox border

我制作了2个简单的链接按钮,它们位于2个ActionResult标签内。

这是我尝试过的divcss代码:

html
.lang-box {
  position: absolute;
  bottom: 60px;
  left: 2%;
  z-index: 3;
}

.flex-container {
  display: flex;
}

.flex-direction-column {
  flex-direction: column;
}

.border-gold {
  border: 2px solid #ac8b45;
}

.text-gold {
  color: #ac8b45;
}

.background-gold {
  background-color: #ac8b45;
}

.text-black {
  color: #000;
}

.no-background {
  background-color: transparent;
}

.lang-a-button {
  text-decoration: none;
  outline: 0;
  display: block;
  font-size: 20px;
  padding-bottom: 6px;
  padding-top: 10px;
  padding-left: 6px;
  padding-right: 6px;
  height: auto;
  text-align: center;
}

.lang-a-button:hover {
  text-decoration: none;
  opacity: .7;
}

这是最终结果:

this is the final result

问题是<div class="lang-box"> <div class="flex-container flex-direction-column border-gold"> <a href="/en" target="_self" class="lang-a-button no-background text-gold" title="Change to English">EN</a> <a href="/" target="_self" class="active_lang lang-a-button text-black background-gold" title="Cambia in Italiano">IT</a> </div> </div>的右边框在我调整浏览器屏幕大小时的厚度发生了变化:

Problem with border 1 Problem with border 2

我不明白为什么会这样。我尝试使用this,但没有任何变化。

我使用Google Chrome v69进行测试。

我需要添加一些东西吗?

1 个答案:

答案 0 :(得分:1)

尝试添加链接flex-grow:0; flex-shrink:0;和固定宽度,如下所示:

* {
  box-sizing: border-box;
}

.lang-box {
    position: absolute; 
    top: 60px;
    left: 2%; 
    z-index: 3;
  
  width: 42px;
}

.flex-container{
  display: flex;
  width: 100%;
}

.flex-direction-column{
    flex-direction: column;
}

.border-gold{
    border: 2px solid #ac8b45;
}

.text-gold{
  color: #ac8b45;
}

.background-gold{
    background-color: #ac8b45;
}

.text-black{
    color: #000;
}

.no-background{
  background-color: transparent;
}

.lang-a-button{
    text-decoration: none;
    outline: 0;
    display: block;
    font-size: 20px;
    padding-bottom: 6px;
    padding-top: 10px;
    /* padding-left: 6px;
    padding-right: 6px; */
    height: auto;
    text-align: center;
  
  width: 100%;
  flex-grow:0;
  flex-shrink:0;
  width: 40px;
}

.lang-a-button:hover{
    text-decoration: none;
    opacity: .7;
}
<div class="lang-box">
    <div class="flex-container flex-direction-column border-gold">
         <a href="/en" target="_self" class="lang-a-button no-background text-gold" title="Change to English">EN</a>
          <a href="/" target="_self" class="active_lang lang-a-button text-black background-gold" title="Cambia in Italiano">IT</a>
    </div>
</div>

我没有确切的答案,但是我认为这是因为flexflex-direction: column的特殊性。即使我们在flex块中设置元素的固定宽度,也不一定总是考虑到它。因此,对于具有固定宽度的块,您需要另外指定flex-grow: 0; flex-shrink: 0;

所以……对于flex-direction: column ...为包装和内部链接添加固定宽度。