div 너비보다 작은 테두리 길이?


115

다음 코드가 있습니다.

div {
   width:200px;
   border-bottom:1px solid magenta;
   height:50px;   
}

데모

div 너비는 200px이므로 border-bottom도 200px이지만 div 너비를 변경하지 않고 border-bottom 만 100px로하려면 어떻게해야합니까?

답변:


224

유사 요소를 사용할 수 있습니다. 예

div {
  width   : 200px;
  height  : 50px;   
  position: relative;
  z-index : 1;
  background: #eee;
}

div:before {
  content : "";
  position: absolute;
  left    : 0;
  bottom  : 0;
  height  : 1px;
  width   : 50%;  /* or 100px */
  border-bottom:1px solid magenta;
}
<div>Item 1</div>
<div>Item 2</div>

프리젠 테이션 목적으로 추가 마크 업을 사용할 필요가 없습니다. : after는 IE8에서도 지원됩니다.

편집하다:

당신이 오른쪽 정렬 경계를 필요로하는 경우, 단지 변경 left: 0으로right: 0

중앙 정렬 테두리가 필요한 경우 간단히 설정하십시오. left: 50px;


이것은 내 기술이기도했지만 div의 왼쪽 절반에 테두리를 제공합니다. 중앙에 배치하려면 div:before left: 50px.
Chowlett 2011

문제는 어떤 위치에 국경은 내가 고려하지 않은 다른 위치를 표시해야 지정하지 않습니다
파브리 지오 Calderan에게

5
아래쪽 테두리의 너비가 50 %이고 중앙에 배치하려는 경우 스타일 left: 25%은 25 % + 50 % + 25 % 가 되므로 스타일 이되어야합니다
스키 프트

5
예,이 예제에서 작동한다는 것을 알고 있습니다. 그러나 응답 성을 높이려는 유사한 작업을 수행하는 다른 사람들의 경우 대답이 필요한 경우처럼 %를 사용해야 할 수 있습니다.
skift

4
사용은 margin: auto, right: 0, left: 0:before라인을 중심합니다. 이것이 도움이 되었다면 찬성하십시오.
Ale_info

40

이를 수행하는 또 다른 방법 (최신 브라우저에서)은 네거티브 스프레드 상자 그림자를 사용하는 것입니다. 이 업데이트 된 바이올린을 확인하십시오 : http://jsfiddle.net/WuZat/290/

box-shadow: 0px 24px 3px -24px magenta;

그래도 가장 안전하고 호환 가능한 방법은 위의 대답입니다. 다른 기술을 공유 할 것이라고 생각했습니다.


9

선형 그래디언트를 사용할 수 있습니다.

div {
    width:100px;
    height:50px;
    display:block;
    background-image: linear-gradient(to right, #000 1px, rgba(255,255,255,0) 1px), linear-gradient(to left, #000 0.1rem, rgba(255,255,255,0) 1px);
	background-position: bottom;
	background-size: 100% 25px;
	background-repeat: no-repeat;
    border-bottom: 1px solid #000;
    border-top: 1px solid red;
}
<div></div>


8

이렇게 h3 태그 아래에 줄을 추가했습니다.

<h3 class="home_title">Your title here</h3> 
.home_title{
    display:block;
}
.home_title:after {
    display:block;
    clear:both;
    content : "";
    position: relative;
    left    : 0;
    bottom  : 0;
    max-width:250px;
    height  : 1px;
    width   : 50%;  /* or 100px */
    border-bottom:1px solid #e2000f;
    margin:0 auto;
    padding:4px 0px;
}

4

div 자체와 다른 크기의 테두리를 가질 수 없습니다.

해결책은 원하는 1 픽셀 테두리와 높이가 1 픽셀 인 neath, centered 또는 absolute 위치 아래에 다른 div를 추가하는 것입니다.

http://jsfiddle.net/WuZat/3/

너비를 볼 수 있도록 원래 테두리를 그대로 두었고 하나는 너비가 100이고 다른 하나는 너비가 중앙에 있습니다. 사용하지 않을 것을 삭제하십시오.


3

파티에 늦었지만 2 개의 테두리 (제 경우 하단과 오른쪽에)를 만들고 싶은 사람은 수락 된 답변에 기술을 사용하고 두 번째 줄에 : after psuedo-element를 추가 한 다음 속성을 다음과 같이 변경하십시오. 그래서 : http://jsfiddle.net/oeaL9fsm/

div
{
  width:500px;
  height:500px;   
  position: relative;
  z-index : 1;
}
div:before {
  content : "";
  position: absolute;
  left    : 25%;
  bottom  : 0;
  height  : 1px;
  width   : 50%;
  border-bottom:1px solid magenta;
}
div:after {
  content : "";
  position: absolute;
  right    : 0;
  bottom  : 25%;
  height  : 50%;
  width   : 1px;
  border-right:1px solid magenta;
}

2

div 컨테이너의 그림 사이에 아래쪽 테두리가 있고 가장 좋은 한 줄 코드는 -border-bottom-style : inset;


2
div{
    font-size: 25px;
    line-height: 27px;
    display:inline-block;
    width:200px;
    text-align:center;
}

div::after {
    background: #f1991b none repeat scroll 0 0;
    content: "";
    display: block;
    height: 3px;
    margin-top: 15px;
    width: 100px;
    margin:auto;
}

2

내 프로젝트에서 이와 같은 일을했습니다. 여기서 공유하고 싶습니다. 다른 div를 자식으로 추가하고 너비가 작은 테두리를 지정하고 일반적인 CSS를 사용하여 왼쪽, 가운데 또는 오른쪽에 배치 할 수 있습니다.

HTML 코드 :

    <div>
        content 
        <div class ="ac-brdr"></div>
    </div>

CSS는 아래와 같습니다.

   .active {
    color: magneta;
  }
   .active .ac-brdr {
        width: 20px;
        margin: 0 auto;
        border-bottom: 1px solid magneta;
  }

1

테두리에는 전체 html 요소가 제공됩니다. 절반 하단 테두리를 원하면 스팬과 같은 다른 식별 가능한 블록으로 포장 할 수 있습니다.

HTML 코드 :

<div> <span>content here </span></div>

CSS는 아래와 같습니다.

 div{
    width:200px;
    height:50px;   
    }
 span{
        width:100px;
        border-bottom:1px solid magenta;   
    } 


1

난 그냥 사용하여이의 반대를 수행 :after하고 ::after내 아래쪽 테두리 정확하게 할 필요가 있었기 때문에 1.3rem넓은 :

내 요소는 내가 사용하는 경우 슈퍼 변형되었다 :before:after동시에 요소는 수평으로 정렬되어 있기 때문에 display: flex, flex-direction: row하고 align-items: center.

이것을 사용하여 더 넓거나 좁게 만들거나 수학적 차원 모드를 만들 수 있습니다.

a.nav_link-active {
  color: $e1-red;
  margin-top: 3.7rem;
}
a.nav_link-active:visited {
  color: $e1-red;
}
a.nav_link-active:after {
  content: '';
  margin-top: 3.3rem;      // margin and height should
  height: 0.4rem;          // add up to active link margin
  background: $e1-red;
  margin-left: -$nav-spacer-margin;
  display: block;
}
a.nav_link-active::after {
  content: '';
  margin-top: 3.3rem;      // margin and height should
  height: 0.4rem;          // add up to active link margin
  background: $e1-red;
  margin-right: -$nav-spacer-margin;
  display: block;
}

죄송합니다. 이것은 SCSS숫자에 10을 곱하고 일부 정상 값으로 변수를 변경합니다.

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.