简体   繁体   English

CSS Flex延伸链接

[英]CSS Flex stretching out links

So I am learning a bit more about using CSS flex instead of using static positioning of content. 因此,我在学习更多有关使用CSS flex而不是使用内容的静态定位的知识。 However, I have defined my link styles as well as bold styles. 但是,我已经定义了链接样式以及粗体样式。 My guess is that it's adapting to the container that is in (which is using flex feature) and that is why it is stretching across the size of the container it is inside. 我的猜测是,它正在适应其中的容器(使用flex功能),这就是为什么它在其内部容器的整个范围内伸展。 My question now is, how do I fix this? 我现在的问题是,我该如何解决? I've seen that I can do "display:inline-block" on the link, but that has not fixed it. 我已经看到我可以在链接上执行“ display:inline-block”,但这并不能解决。

Here is my code: 这是我的代码:

.container{

display: -webkit-flex;
display: flex;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
width: 80%;
margin: auto;
background-color:#fff;
overflow: hidden;
font-size: 15px;
line-height: 20px;
padding:1em;

}

.container > * {
padding: 15px;
-webkit-flex: 1 100%;
flex: 1 100%;
}

a{
text-decoration:none;
border-bottom-style:double; 
border-bottom-width:2px;
color:#99d3df;
display:inline-block;
padding:0px;
overflow: hidden;

}

i{
display:inline-block;
color:#88bbd6;
text-decoration:italic; 
}

And what I have: 而我有:

This is a <a href="http://google.com">Google Link</a><BR>
Google is <i>extremely helpful</i>!

This is what it looks like for reference. 这是供参考的外观。 Problem image 问题图片

It is working fine when I tried your code in js fiddle 当我在js小提琴中尝试您的代码时,它工作正常

see in this image 在这张图片中看到 正如在js小提琴中尝试的那样

May be some other css is affecting your links to stretch it out. 可能是某些其他CSS正在影响您的链接以使其延伸。

It seems you missed the .container wrapper div in the markup you provided. 看来您错过了所提供标记中的.container包装器div。

Let's look at this code: 让我们看一下这段代码:

<!-- HTML -->
<div class="container">
  <span>This is a </span><a href="http://google.com">Google Link</a
</div>
<div class="container">
  <span>Google is </span><i>extremely helpful</i>!
</div>
<!-- /HTML -->

/* CSS */
.container > * {
  padding: 15px;
  -webkit-flex: 1 100%;
  flex: 1 100%;
}

Property flex with value of 1 100% means we tell the browser to style any elements (the asterisk *) nested in .container to have 100% width of their parent's width. 属性flex的值为1 100%意味着我们告诉浏览器设置嵌套在.container中的任何元素(星号*)的样式为其父级宽度的100%宽度。

I would suggest you to just remove that part to fix the problem. 我建议您只删除该部分以解决问题。

Here's my approach to your markup. 这是我的标记方法。

 .container { display: flex; width: 80%; /* flexible value */ flex-direction: row; /* this is to make sure that we'll have side-to-side layout within .container */ } a { text-decoration: none; border-bottom-style: double; border-bottom-width: 2px; color: #99d3df; display: inline-block; padding: 0px; } a, i{ margin-left: 5px; /* this is to give a 10px spacing */ } 
 <div class="container"><span>This is a </span><a href="#">random link</a><span></span></div> <div class="container"><span>Google </span><i>is extremely helpful! </i></div> 

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

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