728x90

문단의 들여쓰기를 조절하는 속성입니다. 속성 값은 px나 em 등의 수치로 넣을 수 있으며, 값이 양수(+)일 경우 들여쓰기가 되고, 음수(-)일 경우 내어 쓰기가 됩니다.

이 text-indent를 이용해서 글자를 감추고 이미지로 대체하기도 합니다. 이러한 사용은 나중에 따로 살펴보겠습니다.

적용 예시

이 문단은 text-indent에 양수 값을 적용하여 문단 시작에 들여쓰기를 적용했습니다. 이렇게 들여쓰기를 함으로써, 새로운 문단의 시작을 표현할 수 있습니다. 이 문단은 text-indent에 양수 값을 적용하여 문단 시작에 들여쓰기를 적용했습니다. 이렇게 들여쓰기를 함으로써, 새로운 문단의 시작을 표현할 수 있습니다. 이 문단은 text-indent에 양수 값을 적용하여 문단 시작에 들여쓰기를 적용했습니다. 이렇게 들여쓰기를 함으로써, 새로운 문단의 시작을 표현할 수 있습니다.

이 문단은 text-indent에 음수 값을 적용하여 내어 쓰기를 적용했습니다. 안에 여백을 주지 않고 음수 값을 주게 되면 영역을 벗어날 수 있으니, 여백을 같이 주는 것이 좋습니다. 이 문단은 text-indent에 음수 값을 적용하여 내어 쓰기를 적용했습니다. 안에 여백을 주지 않고 음수 값을 주게 되면 영역을 벗어날 수 있으니, 여백을 같이 주는 것이 좋습니다. 이 문단은 text-indent에 음수값을 적용하여 내어쓰기를 적용했습니다. 안에 여백을 주지않고 음수값을 주게 되면 영역을 벗어날 수 있으니, 여백을 같이 주는 것이 좋습니다.


728x90


Consider the following code:

HTML:

<div>
    <label for='name'>Name:</label>
    <input type='text' id='name' />
</div>

CSS:

div {
    height: 50px;
    border: 1px solid blue;
}

What would be the easiest method to put the label and the input in the middle of the div(vertically) ?

shareimprove this question
1 
To my knowledge, the bottom line is "you can't", and the laziest way around it is to use a simple <table>and apply valign='middle' to its <td> s. – BeemerGuy Dec 17 '10 at 0:07 
div {
    display: table-cell;
    vertical-align: middle;
    height: 50px;
    border: 1px solid red;
}

The advantages of this method is that you can change the height of the div, change the height of the text field and change the font size and everything will always stay in the middle.

http://jsfiddle.net/53ALd/6/

shareimprove this answer
1 
looks elegant :) does anyone know how browser compatible this method is? – Zaven Nahapetyan Dec 17 '10 at 0:25
1 
For IE, I think it only works on IE8 or better. For the other web browsers it's fine. – Marc-François Dec 17 '10 at 0:34
1 
I didn't know about the display: table-cell. Is that supported on certain browsers? – BeemerGuy Dec 17 '10 at 0:41
5 
@Misha you should have specified the fact that your input / label were absolutely positioned. That totally changes the circumstances of the question. I am giving Marc a +1. Great answer. – jessegavin Dec 17 '10 at 1:16
2 
I'm fairly certain the point jenson was trying to make (and should have articulated) is that using display: table-cell makes divs behave in ways they aren't expected to behave in, such as subsequent divs rendering on the same line/etc. – taswyn Nov 1 '13 at 20:36

a more modern approach would be to use css flex-box.

div {
  height: 50px;
  background: grey;
  display: flex;
  align-items: center
}
<div>
  <label for='name'>Name:</label>
  <input type='text' id='name' />
</div>

a more complex example... if you have multible elements in the flex flow, you can use align-self to align single elements differently to the specified align...

div {
  display: flex;
  align-items: center
}

* {
  margin: 10px
}

label {
  align-self: flex-start
}
<div>
  <img src="https://de.gravatar.com/userimage/95932142/195b7f5651ad2d4662c3c0e0dccd003b.png?size=50" />
  <label>Text</label>
  <input placholder="Text" type="text" />
</div>

its also super easy to center horizontally and vertically:

div {
  position:absolute;
  top:0;left:0;right:0;bottom:0;
  background: grey;
  display: flex;
  align-items: center;
  justify-content:center
}
<div>
  <label for='name'>Name:</label>
  <input type='text' id='name' />
</div>

shareimprove this answer
   
I wish this would work when there is an image and text wrapped in the label. I have a small icon followed by text, but the text doesn't budge. – Kevin Scharnhorst Mar 9 '17 at 16:20
   
it works if label and text are seperate elements, or if your make your lable a flexbox too... – Holger Will Mar 11 '17 at 16:05 
   
This worked for me! Great examples too! – Tony Dec 5 '17 at 16:44

This works cross-browser, provides more accessibility and comes with less markup. ditch the div. Wrap the label

label{
     display: block; 
     height: 35px; 
     line-height: 35px; 
     border: 1px solid #000; 
}

input{margin-top:15px; height:20px}

<label for="name">Name: <input type="text" id="name" /></label>
shareimprove this answer
3 
This obviously won't work for a two line label. And if you are sure that label is always single line, than there are millions of other alternatives. – Lashae Apr 18 '13 at 7:33
11 
the question was in regards to a single line label, and i answered accordingly. not sure about your point(s) or intentions here, but if x is so obvious you should go make a million examples of it. at the very least so you can gloat under my answer and feel the power of your million victories. bravo. – albert Apr 18 '13 at 8:23
1 
This is the best answer for single line labels. The specification says: "If the for attribute is not specified, but the label element has a labelable form-associated element descendant, then the first such descendant in tree order is the label element's labeled control." Therefore this is the only approach where you can omit the for and id attributes for maximum simplicity. – parliament Apr 9 '16 at 1:28 

I'm aware this question was asked over two years ago, but for any recent viewers, here's an alternative solution, which has a few advantages over Marc-François's solution:

div {
    height: 50px;
    border: 1px solid blue;
    line-height: 50px;
}

Here we simply only add a line-height equal to that of the height of the div. The advantage being you can now change the display property of the div as you see fit, to inline-block for instance, and it's contents will remain vertically centered. The accepted solution requires you treat the div as a table cell. This should work perfectly, cross-browser.

The only other advantage being it's just one more CSS rule instead of two :)

Cheers!

shareimprove this answer
1 
But won't work if the label text is greater than one line – GlennG Jun 10 '14 at 13:31

Use padding on the div (top and bottom) and vertical-align:middle on the label and input.

example at http://jsfiddle.net/VLFeV/1/

shareimprove this answer
   
That doesn't work on the jsfiddle. I changed the height of the div and the content inside didn't move at all. Am I missing something? – BeemerGuy Dec 17 '10 at 0:39
   
In my opinion, layouts shouldn't have fixed pixel heights… Layouts should flow fluidly around the content. However, I come from a day and time when browsers scaled text sizes when zooming in and out. Recent browsers actually scale the whole page, so setting pixel heights works a little better. However, there are whole new reasons to avoid setting pixel heights… for example, responsive layout techniques. That's why this would be my preferred method. – thirdender Jun 26 '12 at 17:55

Wrap the label and input in another div with a defined height. This may not work in IE versions lower than 8.

position:absolute; 
top:0; bottom:0; left:0; right:0;
margin:auto;
shareimprove this answer

You can use display: table-cell property as in the following code:

div {
     height: 100%;
     display: table-cell; 
     vertical-align: middle;
    }
shareimprove this answer


'WEB > CSS' 카테고리의 다른 글

[CSS]크로스 브라우징  (0) 2018.01.24
CSS background Property ❮ PreviousComplete CSS Reference Next ❯  (0) 2018.01.24
[CSS]text-indent  (0) 2018.01.18
[CSS] Background Image Size handling  (0) 2018.01.17
CSS Layout - Horizontal & Vertical Align  (0) 2018.01.16
728x90

큰 이미지를 작게해서 반복하기

1233x1233 픽셀의 굉장히 큰 파이어폭스 로고를 150x150 픽셀로 작게 하고 반복한 배경을 300x300 픽셀의 엘리먼트에 지정해 봅시다. 엘리먼트의 가로, 세로 크기는 300픽셀이고 로고의 가로, 세로 크기는 150픽셀이므로 로고는 4번 반복될 것입니다.

screenshot1.png

다음과 같은 CSS를 사용하면 됩니다.

.square {
  width: 300px;
  height: 300px;
  background-image: url(fxlogo.png);
  border: solid 2px;
  text-shadow: white 0px 0px 2px;
  font-size: 16px;
  background-size: 150px;
}

이제 background-size 앞에 접두어(역자: -webkit-, -moz- 등)는 더 이상 필요하지 않습니다. 만약 굉장히 오래된 특정 브라우저에 적용하려면 접두어가 필요합니다. 

이미지 늘이기

가로, 세로 크기를 각각 지정할 수도 있습니다. 

background-size: 300px 150px;

다음과 같이 보입니다.

screenshot3.png

작은 이미지를 크게하기

큰 이미지를 줄이는 것처럼 작은 이미지를 크게 할 수도 있습니다. 160x160픽셀 이미지를 300x300픽셀로 늘여봅시다.

screenshot2.png

.square2 {
  width: 300px;
  height: 300px;
  background-image: url(favicon.png);
  background-size: 300px;
  border: solid 2px;
  text-shadow: white 0px 0px 2px;
  font-size: 16px;
}

특별한 값: "contain"과 "cover"

길이를 나타내는 <length> 값 대신에 contain 과 cover라는 두개의 특별한 값을 지정할 수도 있습니다.

contain

contain으로 지정하면 배경 이미지의 가로, 세로 길이 모두 엘리먼트 안에 들어온다라는 조건하에 가능한한 배경 이미지를 크게 조정합니다. 가로/세로 비율은 유지됩니다. 따라서 배경 이미지의 크기는 엘리먼트의 크기보다 항상 작거나 같습니다. 이 창의 크기를 바꾸어서 아래 엘리먼트의 배경이 어떻게 변하는지 확인하여 보세요.

Try resizing this window and see what happens.

위 예제에 사용된 CSS는 다음과 같습니다.

width: 100%;
height: 200px;
background-image: url(https://developer.mozilla.org/files/2917/fxlogo.png);
background-size: contain;
border: solid 2px;

cover

cover로 지정하면 배경 이미지의 가로, 세로 길이 모두 엘리먼트보다 크다라는 조건하에 가능한한 배경 이미지를 작게 조정합니다. 가로/세로 비율은 유지됩니다. 따라서 배경 이미지의 크기는 엘리먼트의 크기보다 항상 크거나 같습니다. 이 창의 크기를 바꾸어서 아래 엘리먼트의 배경이 어떻게 변하는지 확인하여 보세요.

Try resizing this window and see what happens.

위 예제에서 사용한 CSS는 다음과 같습니다.

width: 100%;
height: 200px;
background-image: url(https://developer.mozilla.org/files/2917/fxlogo.png);
background-size: cover;
border: solid 2px;

더 보기

문서 태그 및 공헌자

 태그:  
 이 페이지의 공헌자: mrstorkteolijaemin_jo
 최종 변경: mrstork


728x90

Center Align Elements

To horizontally center a block element (like <div>), use margin: auto;

Setting the width of the element will prevent it from stretching out to the edges of its container.

The element will then take up the specified width, and the remaining space will be split equally between the two margins:

This div element is centered.

Example

.center {
    margin: auto;
    width: 50%;
    border: 3px solid green;
    padding: 10px;
}
Try it Yourself »

Note: Center aligning has no effect if the width property is not set (or set to 100%).


Center Align Text

To just center the text inside an element, use text-align: center;

This text is centered.

Example

.center {
    text-align: center;
    border: 3px solid green;
}
Try it Yourself »

Tip: For more examples on how to align text, see the CSS Text chapter.



Center an Image

To center an image, set left and right margin to auto and make it into a block element:

Paris

Example

img {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 40%;
}
Try it Yourself »

Left and Right Align - Using position

One method for aligning elements is to use position: absolute;:

In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.

Example

.right {
    position: absolute;
    right: 0px;
    width: 300px;
    border: 3px solid #73AD21;
    padding: 10px;
}
Try it Yourself »

Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.

Tip: When aligning elements with position, always define margin and padding for the <body> element. This is to avoid visual differences in different browsers.

There is also a problem with IE8 and earlier, when using position. If a container element (in our case <div class="container">) has a specified width, and the !DOCTYPE declaration is missing, IE8 and earlier versions will add a 17px margin on the right side. This seems to be space reserved for a scrollbar. So, always set the !DOCTYPE declaration when using position:

Example

body {
    margin: 0;
    padding: 0;
}

.container {
    position: relative;
    width: 100%;
}

.right {
    position: absolute;
    right: 0px;
    width: 300px;
    background-color: #b0e0e6;
}
Try it Yourself »

Left and Right Align - Using float

Another method for aligning elements is to use the float property:

Example

.right {
    float: right;
    width: 300px;
    border: 3px solid #73AD21;
    padding: 10px;
}
Try it Yourself »

Tip: When aligning elements with float, always define margin and padding for the <body> element. This is to avoid visual differences in different browsers.

There is also a problem with IE8 and earlier, when using float. If the !DOCTYPE declaration is missing, IE8 and earlier versions will add a 17px margin on the right side. This seems to be space reserved for a scrollbar. So, always set the !DOCTYPE declaration when using float:

Example

body {
    margin: 0;
    padding: 0;
}

.right {
    float: right;
    width: 300px;
    background-color: #b0e0e6;
}
Try it Yourself »

Center Vertically - Using padding

There are many ways to center an element vertically in CSS. A simple solution is to use top and bottom padding:

I am vertically centered.

Example

.center {
    padding: 70px 0;
    border: 3px solid green;
}
Try it Yourself »

To center both vertically and horizontally, use padding and text-align: center:

I am vertically and horizontally centered.

Example

.center {
    padding: 70px 0;
    border: 3px solid green;
    text-align: center;
}
Try it Yourself »

Center Vertically - Using line-height

Another trick is to use the line-height property with a value that is equal to the height property.

I am vertically and horizontally centered.

Example

.center {
    line-height: 200px;
    height: 200px;
    border: 3px solid green;
    text-align: center;
}

/* If the text has multiple lines, add the following: */
.center p {
    line-height: 1.5;
    display: inline-block;
    vertical-align: middle;
}
Try it Yourself »

Center Vertically - Using position & transform

If padding and line-height is not an option, a third solution is to use positioning and the transform property:

I am vertically and horizontally centered.

Example

.center { 
    height: 200px;
    position: relative;
    border: 3px solid green; 
}

.center p {
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
Try it Yourself »

Tip: You will learn more about the transform property in our 2D Transforms Chapter.


Test Yourself with Exercises!


+ Recent posts