質問

境界線の長さを制限する方法はありますか。私は持っています <div> 底の境界線がありますが、左側に境界線を追加したい <div> それは道のりの半分しか伸びていません。

ページに追加の要素を追加せずにそうする方法はありますか?

役に立ちましたか?

解決

お役に立てれば:

#mainDiv {
  height: 100px;
  width: 80px;
  position: relative;
  border-bottom: 2px solid #f51c40;
  background: #3beadc;
}

#borderLeft {
  border-left: 2px solid #f51c40;
  position: absolute;
  top: 50%;
  bottom: 0;
}
<div id="mainDiv">
  <div id="borderLeft"></div>
</div>

他のヒント

CSS生成コンテンツはあなたのためにこれを解決できます:

div {
  position: relative;
} 
/* Main div for border to extend to 50% from bottom left corner */
div:after {
  content:""; 
  background: black; 
  position: absolute; 
  bottom: 0; 
  left: 0; 
  height: 50%; 
  width: 1px;
}

(注 - content: ""; 擬似要素がレンダリングするために宣言が必要です)

:after 岩 :)

少しプレイする場合は、サイズ変更された境界要素を設定して中心に表示されるように設定したり、その隣に別の要素がある場合にのみ表示することもできます(メニューのように)。これがメニューの例です。

#menu > ul > li {
    position: relative;
    float: left;
    padding: 0 10px;
}

#menu > ul > li + li:after {
    content:"";
    background: #ccc;
    position: absolute;
    bottom: 25%;
    left: 0;
    height: 50%;
    width: 1px;
}

#menu > ul > li {
  position: relative;
  float: left;
  padding: 0 10px;
  list-style: none;
}
#menu > ul > li + li:after {
  content: "";
  background: #ccc;
  position: absolute;
  bottom: 25%;
  left: 0;
  height: 50%;
  width: 1px;
}
<div id="menu">
  <ul>
    <li>Foo</li>
    <li>Bar</li>
    <li>Baz</li>
  </ul>
</div>

CSSプロパティを使用すると、境界の厚さのみを制御できます。長さではありません。

ただし、境界効果を模倣して制御できます widthheight 他の方法で欲しいように。

CSSを使用(線形勾配):

使用できます linear-gradient() 背景画像を作成し、そのサイズと位置をCSSで制御して、境界のように見えるようにします。複数の背景画像を要素に適用できるため、この機能を使用して画像のような複数の境界線を作成し、要素の異なる側面に適用できます。また、残りの利用可能な領域を、いくらかの固体、勾配、または背景画像でカバーすることもできます。

必要なHTML:

必要なのは、1つの要素だけです(おそらくクラスがある可能性があります)。

<div class="box"></div>

ステップ:

  1. 背景画像を作成します linear-gradient().
  2. 使用する background-size を調整します width / height 上記の画像のように作成された画像のようになります。
  3. 使用する background-position 位置を調整するには(次のように left, right, left bottom 上記の境界線など。

必要なCSS:

.box {
  background-image: linear-gradient(purple, purple),
                    // Above css will create background image that looks like a border.
                    linear-gradient(steelblue, steelblue);
                    // This will create background image for the container.

  background-repeat: no-repeat;

  /* First sizing pair (4px 50%) will define the size of the border i.e border
     will be of having 4px width and 50% height. */
  /* 2nd pair will define the size of stretched background image. */
  background-size: 4px 50%, calc(100% - 4px) 100%;

  /* Similar to size, first pair will define the position of the border
     and 2nd one for the container background */
  background-position: left bottom, 4px 0;
}

例:

linear-gradient() 勾配を持つだけでなく、固体の境界線を作成できます。以下は、この方法で作成された境界の例です。

境界線のみが片側にのみ適用されている例:

.container {
  display: flex;
}
.box {
  background-image: linear-gradient(purple, purple),
                    linear-gradient(steelblue, steelblue);
  background-repeat: no-repeat;
  background-size: 4px 50%, calc(100% - 4px) 100%;
  background-position: left bottom, 4px 0;

  height: 160px;
  width: 160px;
  margin: 20px;
}
.gradient-border {
  background-image: linear-gradient(red, purple),
                    linear-gradient(steelblue, steelblue);
}
<div class="container">
  <div class="box"></div>

  <div class="box gradient-border"></div>
</div>

両側に境界線が適用されている例:

.container {
  display: flex;
}
.box {
  background-image: linear-gradient(purple, purple),
                    linear-gradient(purple, purple),
                    linear-gradient(steelblue, steelblue);
  background-repeat: no-repeat;
  background-size: 4px 50%, 4px 50%, calc(100% - 8px) 100%;
  background-position: left bottom, right top, 4px 0;
  
  height: 160px;
  width: 160px;
  margin: 20px;
}

.gradient-border {
  background-image: linear-gradient(red, purple),
                    linear-gradient(purple, red),
                    linear-gradient(steelblue, steelblue);
}
<div class="container">
  <div class="box"></div>

  <div class="box gradient-border"></div>
</div>

すべての側面に境界線が適用されている例:

.container {
  display: flex;
}
.box {
  background-image: linear-gradient(purple, purple),
                    linear-gradient(purple, purple),
                    linear-gradient(purple, purple),
                    linear-gradient(purple, purple),
                    linear-gradient(steelblue, steelblue);
  background-repeat: no-repeat;
  background-size: 4px 50%, 50% 4px, 4px 50%, 50% 4px, calc(100% - 8px) calc(100% - 8px);
  background-position: left bottom, left bottom, right top, right top, 4px 4px;
  
  height: 160px;
  width: 160px;
  margin: 20px;
}

.gradient-border {
  background-image: linear-gradient(red, purple),
                    linear-gradient(to right, purple, red),
                    linear-gradient(to bottom, purple, red),
                    linear-gradient(to left, purple, red),
                    linear-gradient(steelblue, steelblue);
}
<div class="container">
  <div class="box"></div>

  <div class="box gradient-border"></div>
</div>

スクリーンショット:

Output Image showing half length borders

水平線の場合、HRタグを使用できます。

hr { width: 90%; }

しかし、境界の高さを制限することはできません。要素の高さのみ。

境界線は、側面の分数ではなく、側面ごとに定義されます。だから、いいえ、あなたはそれをすることはできません。

また、新しい要素も境界線ではなく、あなたが望む動作を模倣するだけですが、それでも要素です。

これを行う別の方法は、線形勾配と組み合わせてボーダーイメージを使用することです。

div {
  width: 100px;
  height: 75px;
  background-color: green;
  background-clip: content-box; /* so that the background color is not below the border */
  
  border-left: 5px solid black;
  border-image: linear-gradient(to top, #000 50%, rgba(0,0,0,0) 50%); /* to top - at 50% transparent */
  border-image-slice: 1;
}
<div></div>

jsfiddle: https://jsfiddle.net/u7zq0amc/1/


ブラウザサポート:IE:11+

Chrome:すべて

Firefox:15+

より良いサポートのために、ベンダーのプレフィックスも追加します。

カニュースボーダーイメージ

これはCSSのトリックであり、正式な解決策ではありません。要素を配置するのに役立つので、私はコードをブラックにして残します。その後、コンテンツ(色:白)と(マージントップ:-5px程度)を色付けして、期間がないかのようになります。

div.yourdivname:after {
content: ".";
  border-bottom:1px solid grey;
  width:60%;
  display:block;
  margin:0 auto;
}

別の解決策は、背景画像を使用して左の境界の外観を模倣できることです。

  1. グラフィックとして必要な国境左スタイルを作成します
  2. Divの左側に配置します(古いブラウザの約2つのテキストサイズの増加を処理するのに十分な長さにしてください)
  3. Divの上部から垂直位置を50%設定します。

(通常のように)IEで調整する必要があるかもしれませんが、それがあなたが目指しているデザインなら、それはショットの価値があります。

  • 私は一般的に、CSSが本質的に提供するものに画像を使用することに反対していますが、デザインがそれを必要とする場合、それを回る他の方法はありません。

側面に1つの境界線のみを定義できます。そのために追加の要素を追加する必要があります!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top