IT

CSS : 중간에 텍스트로 원을 그리는 방법은 무엇입니까?

lottoking 2020. 6. 26. 07:54
반응형

CSS : 중간에 텍스트로 원을 그리는 방법은 무엇입니까?


stackoverflow 에서이 예제를 찾았습니다.

CSS만으로 원 그리기

어느 것이 좋습니다. 그러나 원의 중간에 텍스트를 포함 할 수 있도록 예제를 수정하는 방법을 알고 싶습니다.

나는 또한 이것을 발견했다 : CSS에서 원의 수직 및 수평 중심 텍스트 (iphone 알림 배지와 같은)

그러나 어떤 이유로 든 나를 위해 작동하지 않습니다. 다음 테스트 코드를 만들 때 :

<div class="badge">1</div>

원 대신 타원형 모양을 얻습니다. 코드 작동 방법을 알아보기 위해 코드를 가지고 놀고 있습니다.


JSFiddle

.circle {
  width: 500px;
  height: 500px;
  border-radius: 50%;
  font-size: 50px;
  color: #fff;
  line-height: 500px;
  text-align: center;
  background: #000
}
<div class="circle">Hello I am A Circle</div>


콘텐츠가 줄 바꿈되어 높이를 알 수없는 경우 가장 좋은 방법입니다.

http://cssdeck.com/labs/aplvrmue

.badge {
  height: 100px;
  width: 100px;
  display: table-cell;
  text-align: center;
  vertical-align: middle;
  border-radius: 50%; /* may require vendor prefixes */
  background: yellow;
}

.badge {
  height: 100px;
  width: 100px;
  display: table-cell;
  text-align: center;
  vertical-align: middle;
  border-radius: 50%;
  background: yellow;
}
<div class="badge">1</div>


css3을 사용할 수 있습니다 flexbox.

HTML :

<div class="circle-with-text">
    Here is some text in circle
</div>

CSS :

.circle-with-text {
  justify-content: center;
  align-items: center;
  border-radius: 100%;
  text-align: center;
  display: flex;
}

이렇게하면 세로 및 가로 가운데 정렬 된 한 줄과 여러 줄 텍스트를 가질 수 있습니다.

body {
  margin: 0;
}
.circles {
  display: flex;
}
.circle-with-text {
  background: linear-gradient(orange, red);
  justify-content: center;
  align-items: center;
  border-radius: 100%;
  text-align: center;
  margin: 5px 20px;
  font-size: 15px;
  padding: 15px;
  display: flex;
  height: 180px;
  width: 180px;
  color: #fff;
}
.multi-line-text {
  font-size: 20px;
}
<div class="circles">
  <div class="circle-with-text">
    Here is some text in circle
  </div>
  <div class="circle-with-text multi-line-text">
    Here is some multi-line text in circle
  </div>
</div>


타원형이나 원 안에 텍스트를 쓰고 싶습니까? 왜 이러지 않습니까?

<span style="border-radius:50%; border:solid black 1px;padding:5px">Hello</span>


웹 디자인을 위해 최근에 나는 고정 된 원 문제에서 중심에 있고 알 수없는 양의 텍스트를 해결해야했으며 원 / 텍스트 콤보를 찾는 다른 사람들을 위해 여기에서 해결책을 공유 할 것이라고 생각했습니다.

내가 가진 주요 문제는 텍스트가 종종 원의 경계를 깰 수 있다는 것입니다. 이 문제를 해결하기 위해 4 div를 사용했습니다. 원의 최대 (고정) 경계를 지정하는 하나의 직사각형 컨테이너. 그 안에는 너비와 높이가 100 %로 설정된 원을 그리는 div가 있으므로 부모의 크기를 변경하면 실제 원의 크기가 변경됩니다. 그 안에 %를 사용하여 텍스트가 원을 떠나는 것을 방지하는 텍스트 경계 영역을 만들 수있는 다른 직사각형 div가 있습니다. 마지막으로 텍스트와 수직 중심에 대한 실제 div입니다.

코드로 더 의미가 있습니다.

/* Main Container -  this controls the size of the circle */
.circle_container
{
	width : 128px;
	height : 128px;
	margin : 0;
	padding : 0;
/*	border : 1px solid red; */
}

/* Circle Main draws the actual circle */
.circle_main
{
	width : 100%;
	height : 100%;
	border-radius : 50%;
	border : 2px solid black;	/* can alter thickness and colour of circle on this line */
	margin : 0;
	padding : 0;
}

/* Circle Text Container - constrains text area to within the circle */
.circle_text_container
{
	/* area constraints */
	width : 70%;
	height : 70%;
	max-width : 70%;
	max-height : 70%;
	margin : 0;
	padding : 0;

	/* some position nudging to center the text area */
	position : relative;
	left : 15%;
	top : 15%;
	
	/* preserve 3d prevents blurring sometimes caused by the text centering in the next class */
	transform-style : preserve-3d;
	
	/*border : 1px solid green;*/
}

/* Circle Text - the appearance of the text within the circle plus vertical centering */
.circle_text
{
	/* change font/size/etc here */
	font: 11px "Tahoma", Arial, Serif;	
	text-align : center;
	
	/* vertical centering technique */
	position : relative;
	top : 50%;
	transform : translateY(-50%);
}
<div class="circle_container">
	<div class="circle_main">
		<div class="circle_text_container">
			<div class = "circle_text">
				Here is an example of some text in my circle.
			</div>
		</div>
	</div>
</div>			

컨테이너 div의 테두리 색상을 주석 해제하여 어떻게 제한되는지 확인할 수 있습니다.

알아 두어야 할 사항 : 너무 많은 텍스트를 넣거나 너무 긴 단어 / 깨지지 않은 텍스트를 사용하면 여전히 원의 경계를 벗어날 수 있습니다. 여전히 알려지지 않은 텍스트 (예 : 사용자 입력)에는 적합하지 않지만 저장해야 할 최대 텍스트 양이 무엇인지 모호하게 알고 원 크기와 글꼴 크기를 적절하게 설정하면 가장 잘 작동합니다. 물론 텍스트 컨테이너 div를 설정하여 오버플로를 숨길 수는 있지만 "깨진"것처럼 보일 수 있으며 실제로 디자인에서 최대 크기를 올바르게 대체하는 것은 아닙니다.

이것이 누군가에게 유용하기를 바랍니다! HTML / CSS는 나의 주요 분야가 아니므로 개선 될 수 있다고 확신합니다!


한 줄의 텍스트 인 경우 요소 높이와 같은 값을 가진 line-height 속성을 사용할 수 있습니다.

height:100px;
line-height:100px;

텍스트에 여러 줄이 있거나 내용이 가변적 인 경우 패딩 상단을 사용할 수 있습니다.

padding-top:30px;
height:70px;

예 : http://jsfiddle.net/2GUFL/


물론 그렇게하려면 태그를 사용해야합니다. 하나는 원을 만들고 다른 하나는 텍스트를 만듭니다.

여기 일부 코드가 도움이 될 수 있습니다

#circle {
    background: #f00;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    color:black;

}
.innerTEXT{
    position:absolute;
    top:80px;
    left:60px;
}

<div id="circle">
    <span class="innerTEXT"> Here a text</span>
</div>

여기에 라이브 예제 http://jsbin.com/apumik/1/edit

최신 정보

몇 가지 변경으로 더 작아짐

http://jsbin.com/apumik/3/edit


에게이 솔루션 만 여러 줄 텍스트에서 작동했습니다.

.circle-multiline {
    display: table-cell;
    height: 200px;
    width: 200px;
    text-align: center;
    vertical-align: middle;
    border-radius: 50%;
    background: yellow;
}


Foundation 5 및 Compass 프레임 워크를 사용하는 경우이를 시도 할 수 있습니다.

.sass 입력

$circle-width: rem-calc(25) !default;
$circle-height: $circle-width !default;
$circle-bg: #fff !default;
$circle-radius: 50% !default;
$circle-line-height: $circle-width !default;
$circle-text-align: center !default;

@mixin circle($cw:$circle-width, $ch:$circle-height, $cb:$circle-bg, $clh:$circle-line-height, $cta:$circle-text-align, $cr:$circle-radius) {
    width: $cw;
    height: $ch;
    background: $cb;
    line-height: $clh;
    text-align: $cta;
    @include inline-block;
    @include border-radius($cr);
}

.circle-default {
    @include circle;
}

.css 출력

.circle-default {
  width: 1.78571rem;
  height: 1.78571rem;
  background: white;
  line-height: 1.78571rem;
  text-align: center;
  display: -moz-inline-stack;
  display: inline-block;
  vertical-align: middle;
  *vertical-align: auto;
  zoom: 1;
  *display: inline;
  -webkit-border-radius: 50%;
  -moz-border-radius: 50%;
  -ms-border-radius: 50%;
  -o-border-radius: 50%;
  border-radius: 50%;
}

HTML 태그와 CSS없이 텍스트가있는 원을 그립니다.

이를 위해 SVG 태그가있는 HTML. CSS를 사용하지 않으려는 경우이 표준 접근 방식을 따를 수 있습니다.

 <svg width="100" height="100">
   <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="white" />
     Sorry, your browser does not support inline SVG.
   <text fill="#000000" font-size="18" font-family="Verdana"
     x="15" y="60">ASHISH</text>
 </svg>

여기에 이미지 설명을 입력하십시오


정말 간단한 설정이 된 YouTube 페이지에서 가져 왔습니다. 절대적으로 유지 관리 가능하고 재사용 가능합니다.

.circle {
    position: absolute;
    top: 4px;
    color: white;
    background-color: red;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    line-height: 18px;
    font-size: 10px;
    text-align: center;
    cursor: pointer;
    z-index: 999;
}
<div class="circle">2</div>


여기에있는 솔루션 중 일부는 작은 서클에서 나에게 잘 작동하지 않았습니다. 그래서 나는 절대 위치를 사용 하여이 솔루션을 만들었습니다.

SASS를 사용하면 다음과 같습니다.

.circle-text {
    position: relative;
    display: block;
    text-align: center;
    border-radius: 50%;
    > .inner-text {
        display: block;
        @extend .center-align;
    }
}

.center-align {
    position: absolute;
    top: 50%;
    left: 50%;
    margin: auto;
    -webkit-transform: translateX(-50%) translateY(-50%);
    -ms-transform: translateX(-50%) translateY(-50%);
    transform: translateX(-50%) translateY(-50%);
}

@mixin circle-text($size) {
    width: $size;
    height: $size;
    @extend .circle-text;
}

그리고처럼 사용할 수 있습니다

#red-circle {
    background-color: red;
    border: 1px solid black;
    @include circle-text(50px);
}

#green-circle {
    background-color: green;
    border: 1px solid black;
    @include circle-text(150px);
}

https://codepen.io/matheusrufca/project/editor/DnYPMK 데모를 참조하십시오

출력 CSS를 보려면 스 니펫을 참조하십시오.

.circle-text {
  position: relative;
  display: block;
  border-radius: 50%;
  text-align: center;
  min-width: 50px;
  min-height: 50px;
}

.center-align {
  position: absolute;
  top: 50%;
  left: 50%;
  margin: auto;
  -webkit-transform: translateX(-50%) translateY(-50%);
  -ms-transform: translateX(-50%) translateY(-50%);
  transform: translateX(-50%) translateY(-50%);
}
<div id="red-circle" class="circle-text">
  <span class="inner-text center-align">Hey</span>
</div>

<div id="green-circle" class="circle-text">
  <span class="inner-text center-align">Big size circle</span>
  <div>
    <style>
      #red-circle {
        background-color: red;
        border: 1px solid black;
        width: 60px;
        height: 60px;
      }
      
      #green-circle {
        background-color: green;
        border: 1px solid black;
        width: 150px;
        height: 150px;
      }
    </style>


.circle {
  width: 500px;
  height: 500px;
  border-radius: 50%;
  font-size: 50px;
  color: #fff;
  line-height: 500px;
  text-align: center;
  background: #000
}
<div class="circle">Hello I am A CircleHello I am A CircleHello I am A CircleHello I am A CircleHello I am A CircleHello I am A CircleHello I am A CircleHello I am A CircleHello I am A Circle</div>


한 가지 방법은 텍스트를 가운데에 맞추기 위해 flexbox를 사용하는 것입니다. 내가 찾은 방법은 다음과 같습니다.

HTML :

<div class="circle-without-text">
  <div class="text-inside-circle">
    The text
  </div>
</div>

CSS :

.circle-without-text {
    border-radius: 50%;
    width: 70vh;
    height: 70vh;
    background-color: red;
    position: relative;
 }

.text-inside-circle {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
 }

여기 plnkr : https://plnkr.co/edit/EvWYLNfTb1B7igoc3TZx?p=preview


이 코드를 사용하면 반응이 좋습니다.

<div class="circle">ICON</div>

.circle {
  position: relative;
  display: inline-block;
  width: 100%;
  height: 0;
  padding: 50% 0;
  border-radius: 50%;
  /* Just making it pretty */
  -webkit-box-shadow: 0 4px 0 0 rgba(0, 0, 0, 0.1);
  box-shadow: 0 4px 0 0 rgba(0, 0, 0, 0.1);
  text-shadow: 0 4px 0 rgba(0, 0, 0, 0.1);
  background: #38a9e4;
  color: white;
  font-family: Helvetica, Arial Black, sans;
  font-size: 48px;
  text-align: center;
}

나는 다른 사람들과 함께 몇 가지 답변을 결합했다 float그리고 relative그것은 나에게 내가 필요한 결과를 주었다.

HTML에서는 div를 사용합니다. 나는 내에서 사용할 li탐색 모음을 위해.

.large-list-style {
    float: left;
    position: relative;
    top: -8px;

    border-radius: 50%;

    margin-right: 8px;

    background-color: rgb(34, 198, 200);

    font-size: 18px;
    color: white;
}
    .large-list-style:before,
    .large-list-style:after {
        content: '\200B';
        display:inline-block;
        line-height:0;

        padding-top: 50%;
        padding-bottom: 50%;
    }
    .large-list-style:before {
        padding-left: 16px;
    }
    .large-list-style:after {
        padding-right: 16px;
    }

참고 URL : https://stackoverflow.com/questions/16615403/css-how-to-draw-circle-with-text-in-middle

반응형