【问题标题】:how to create a responsive line in css?如何在 CSS 中创建响应行?
【发布时间】:2016-04-10 03:50:46
【问题描述】:

我想画一条线,并确保该线调整大小以适应屏幕大小,并且线点应始终与段落一致。像这样的:

有什么方法可以添加该行,使其始终指向div?我做了一些小研究,发现使用border-right: 1px solid #000000; 用于列表,我该如何为div 做同样的事情?

这是我的代码:

.jumbotron {
  /*  background-color: #353535;*/
  /* background-image: url("https://unsplash.it/1440/736/?random");*/
  background-size: cover;
  color: #fff;
  /*   padding: 155px 25px;*/
  padding-bottom: 200px;
  font-family: Montserrat, sans-serif;
}
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<div class="container">
  <div class="jumbotron text-center">
    <h1>company name </h1>
    <div calss="line" style="    border-bottom: 5px solid #000000;">
      <div class="col-sm-6">
        <h2 class="usertype">For Businesses</h2>
        <p> Authentication .</p>
        <a href="#" class="btn btn-default">Try It Now</a>
      </div>
      <div class="col-sm-6">
        <h2 class="usertype">For Individuals</h2>
        <p>For Google, Facebook, Coinbase .</p>
        <a href="#" class="btn btn-default">Download App</a>
      </div>
    </div>
  </div>
</div>

【问题讨论】:

  • 您可以显示 div 或任何其他 html 控件的边框。你在哪里有问题?
  • 我想画线,我的线现在只是直线@elrado

标签: html twitter-bootstrap css


【解决方案1】:

这里是例子。请看一下

* {
  box-sizing: border-box;
}

body {
  background: #fff;
}

.intro {
  position: relative;
}

.main-heading:before {
  width: 5px;
  position: absolute;
  left: 50%;
  bottom: 0;
  height: 20px;
  background: #000;
  content: '';
  transform: translate(-50%, 0);
}

.main-heading {
  text-align: center;
  font-size: 30px;
  line-height: 30px;
  position: relative;
  padding-bottom: 25px;
  border-bottom: 5px solid #000;
}

.two-col:after {
  display: block;
  clear: both;
  content: '';
}

.two-col .col {
  float: left;
  width: 50%;
  position: relative;
  padding: 0 20px;
}

.sub-heading {
  position: relative;
  display: inline-block;
  padding: 20px 0;
}

.sub-heading:before {
  width: 10px;
  position: absolute;
  left: 50%;
  top: 0;
  height: 20px;
  background: #000;
  content: '';
  transform: translate(-50%, 0);
}

.sub-heading:after {
  position: absolute;
  background: #fff;
  top: -5px;
  left: -9999px;
  content: '';
  right: 50%;
  height: 25px;
}

.two-col .col:last-child {
  text-align: right;
}

.two-col .col:last-child .sub-heading:after {
  right: -9999px;
  left: 50%;
}
<div class="intro">
  <div class="main-heading">Company Name</div>
  <div class="two-col">
    <div class="col">
      <div class="sub-heading">For Business</div>
    </div>
    <div class="col">
      <div class="sub-heading">For Individuals</div>
    </div>
  </div>
</div>

这里也是小提琴链接https://jsfiddle.net/gafnk4rg/

【讨论】:

  • 从图片上看,我认为 OP 想要的是让行转到名称的中间而不是它们的开头/结尾
  • 请您使用伪元素。比如:after/:before
  • :after:before 与我的评论无关。我想说的是,您的行覆盖了div 的整个宽度,而不是从第一列的中心到第二列的中心,如问题图片所示
  • 我有更新代码。请检查一次。但请记住父块背景和子标题:背景必须相同
【解决方案2】:

您可以使用水平线:“hr”。它是响应式的。

请参考

How to draw lines through Bootstrap vertical dividers?

【讨论】:

    【解决方案3】:

    一种可能的解决方案:

    • 将该行下移一级。
    • 使其仅占宽度的 50% 并将其居中(这样它将从两列的中心到中心)。
    • 添加:before:after 来制作垂直线。
    • xs 中隐藏该行,因为您的菜单的样式是从sm 开始的。

    这是一个演示(点击“整页”按钮查看效果):

    .jumbotron {
      background-size: cover;
      color: #fff;
      padding-bottom: 200px;
      font-family: Montserrat, sans-serif;
    }
    .line {
      border-bottom:4px solid #000000;
      width:50%;
      margin:auto auto;
      position:relative;
    }
    .line:before {
      content:"";
      position:absolute;
      top:-15px;
      left:50%;
      height:15px;
      border-left:4px solid #000000;
    }
    .line:after {
      content:"";
      position:absolute;
      top:0;
      left:0;
      border-left:4px solid #000000;
      border-right:4px solid #000000;
      width:100%;
      height:20px;
    }
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    
    <div class="container">
      <div class="jumbotron text-center">
        <h1>company name </h1>
        <div>
          <div class="line hidden-xs"></div>
          <div class="col-sm-6">
            <h2 class="usertype">For Businesses</h2>
            <p> Authentication .</p>
            <a href="#" class="btn btn-default">Try It Now</a>
          </div>
          <div class="col-sm-6">
            <h2 class="usertype">For Individuals</h2>
            <p>For Google, Facebook, Coinbase .</p>
            <a href="#" class="btn btn-default">Download App</a>
          </div>
        </div>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多