쉽게 을 잡을 수 요?CSS를 사용하고 있습니까?
<div>최소 너비로 설정합니다.장장 간간 ?? ??? ????나는 그 것을 원한다.<div>다른 페이지와 인라인으로 연결할 수 있습니다.예를 들어 보겠습니다.
page text page text page text page text
page text page text page text page text
               -------
               | div |
               -------
page text page text page text page text
page text page text page text page text
폭 div가 고정되지 않은 경우(즉, div가 차지하는 공간을 알 수 없습니다).
#wrapper {
  background-color: green; /* for visualization purposes */
  text-align: center;
}
#yourdiv {
  background-color: red; /* for visualization purposes */
  display: inline-block;
}<div id="wrapper">    
    <div id="yourdiv">Your text</div>
</div>의의  폭에 해 주세요.#yourdiv동적 -> 텍스트에 맞게 확장 및 축소됩니다.
Caniuse에서 브라우저 호환성을 확인할 수 있습니다.
대부분의 브라우저에서는 다음과 같이 동작합니다.
div.centre {
  width: 200px;
  display: block;
  background-color: #eee;
  margin-left: auto;
  margin-right: auto;
}<div class="centre">Some Text</div>에서는 다른  IE6을 추가해야 .div:
div.layout {
  text-align: center;
}
div.centre {
  text-align: left;
  width: 200px;
  background-color: #eee;
  display: block;
  margin-left: auto;
  margin-right: auto;
}<div class="layout">
  <div class="centre">Some Text</div>
</div>margin: 0 auto;
ck가 말했듯이 min-width는 모든 브라우저에서 지원되지 않습니다.
에 두 가지 하겠습니다.Flexbox.
, 아, 아, 맞다.Flexbox 및  파괴될  IE8은 IE9로, IE9로, IE8은 IE9로, IE8은 IE8로, IE9로, IE/E/I/I/I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I-I
Flexbox에 대한 현재 브라우저 호환성 테이블 확인
단일 요소
.container {
  display: flex;
  justify-content: center;
}<div class="container">
  <img src="http://placehold.it/100x100">
</div>여러 요소가 있지만 하나의 요소만 중앙에 배치
 동작은 「Default Behavior」입니다.flex-direction: row을 사용하다「 」로 flex-direction: column선이 쌓이는 데 도움이 됩니다.
.container {
  display: flex;
  flex-direction: column;
}
.centered {
  align-self: center;
}<div class="container">
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
   </p>
  <div class="centered"><img src="http://placehold.it/100x100"></div>
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
</div>오래된 브라우저가 문제가 되지 않으면 HTML5 / CSS3을 사용합니다.사용 가능한 경우 폴리필을 도포하고 HTML5 / CSS3를 사용합니다.당신의 div는 여백이나 패딩이 없는 것 같습니다만, 비교적 알기 쉬운 것입니다.코드는 다음과 같다.
.centered {
    position: relative;
    left: 50%;
    transform: translateX(-50%);
}
이 기능은 다음과 같이 동작합니다.
- 「 」의 위치를 합니다.div컨테이너에 대한 상대성
- 「 」의 위치를 합니다.div컨테이너 폭의 50% 수평에서의 왼쪽 경계
- 님의 폭의 50%를 가로로 변환합니다.
은 쉽게 할 수 .div결국 수평으로 중심이 맞춰지게 됩니다.추가 비용 없이 세로 중앙에 배치할 수 있습니다.
.centered-vertically {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}
이 접근법의 장점은 div를 일종의 텍스트로 간주하거나 추가 용기에 포장하거나(종종 의미상 쓸모없는) 고정된 너비를 제공하는 것과 같이 직관에 반하는 일을 할 필요가 없다는 것입니다.
「」의 벤더 transform필요한 경우.
.center {
   margin-left: auto;
   margin-right: auto;
}
최소 폭은 글로벌하게 지원되지 않지만 다음을 사용하여 구현할 수 있습니다.
.divclass {
   min-width: 200px;
}
그러면 당신의 div를 다음과 같이 설정할 수 있습니다.
<div class="center divclass">stuff in here</div>
CSS, HTML:
div.mydiv {width: 200px; margin: 0 auto}<div class="mydiv">
    
    I am in the middle
    
</div>다이어그램에는 인라인 요소가 아닌 블록 수준 요소(일반적으로 div)도 나와 있습니다.
 꼭대기에서는min-width는, FF2로 되고 있습니다.해킹 CSS 또는 간단한 JS를 사용하여 IE6에 대해 수행할 수 있습니다.
 하면 됩니다.position: relative ★★★★★★★★★★★★★★★★★」text-align: center display: inline-block중앙에 배치할 하위 요소에서 선택합니다.이것은 모든 주요 브라우저에서 동작하는 단순한 CSS 설계 패턴입니다.다음은 예를 제시하겠습니다.또는 CodePen의 예를 확인해 주세요.
p {
  text-align: left;
}
.container {
  position: relative;
  display: block;
  text-align: center;
}
/* Style your object */
.object {
  padding: 10px;
  color: #ffffff;
  background-color: #556270;
}
.centerthis {
  display: inline-block;
}<div class="container">
  <p>Aeroplanigera Mi Psychopathologia Subdistinctio Chirographum Intuor Sons Superbiloquentia Os Sors Sesquiseptimus Municipatio Archipresbyteratus O Conclusio Compedagogius An Maius Septentrionarius Plas Inproportionabilit Constantinopolis Particularisticus.</p>
  <span class="object centerthis">Something Centered</span>
  <p>Aeroplanigera Mi Psychopathologia Subdistinctio Chirographum Intuor Sons Superbiloquentia Os Sors Sesquiseptimus Municipatio Archipresbyteratus O Conclusio Compedagogius.</p>
</div>아래 코드를 사용해 주시면 div가 중앙에 배치됩니다.
.class-name {
    display:block;
    margin:0 auto;
}
 하면 .margin: 0 auto"css"가 아닌 margin-left: auto; margin-right: auto;
div의 폭을 알고 있고 고정된 경우 다음 css를 사용할 수 있습니다.
margin-left: calc(50% - 'half-of-your-div-width');
여기서 'half-of-your-div-width'는 div 폭의 절반이어야 합니다.
9년 후 나는 새로운 버전을 가져올 때라고 생각했다.여기 제가 좋아하는 두 개가 있습니다.
마진
 ★★margin로로 합니다.auto는 '먹다' 입니다.margin: *top* *right* *bottom* *left*; ★★★★★★★★★★★★★★★★★」margin: *top&bottom* *left&right*
aside{
    display: block;
    width: 50px;
    height: 100px;
    background-color: green;
    float: left;
}
article{
    height: 100px;
    margin: 0 0 0 50px; /* 50px aside width */
    background-color: grey;
}
div{
  margin: 0 auto;
  display:block;
  width: 60px;
  height: 60px;
  background-color: blue;
  color: white;
}<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <aside>
        </aside>
        <article>           
                <div>The div</div>
        </article>
    </body>
</html>센터: 탈피, 사용하지 마세요!
<center></center>를 <div></div>.
예:
aside{
    display:block;
    background-color:green;
    width: 50px;
    height: 100px;
    float: left;
}
center{
    display:block;
    background-color:grey;
    height: 100px;
    margin-left: 50px; /* Width of the aside */
}
div{
    display:block; 
    width: 60px; 
    height: 60px; 
    background-color:blue;
    color: white;
}<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <aside>
        </aside>
        <article>
            <center>
                <div>The div</div>
            </center>
        </article>
    </body>
</html>이 클래스를 css 파일에 추가하면 다음 절차를 완벽하게 수행할 수 있습니다.
1) 먼저 작성
<div class="center-role-form">
  <!--your div (contrent) place here-->
</div>
2) 이것을 css에 추가합니다.
.center-role-form {
    width: fit-content;
    text-align: center;
    margin: 1em auto;
    display: table;
}
의 '' ''가<div> 있다position: absolute이 합니다.width: 100%;
#parent {
    width: 100%;
    text-align: center;
}
    #child {
        display: inline-block;
    }
.center {
  height: 20px;
  background-color: blue;
}
.center>div {
  margin: auto;
  background-color: green;
  width: 200px;
}<div class="center">
  <div>You text</div>
</div>여기에 적절한 답변을 덧붙입니다.
이 스니펫 코드를 사용하여 커스터마이즈할 수 있습니다.여기서는 2개의 어린이 블록을 사용합니다.페이지 중앙에 표시됩니다.하나 또는 여러 블록을 사용할 수 있습니다.
<html>
<head>
<style>
#parent {
    width: 100%;
    border: solid 1px #aaa;
    text-align: center;
    font-size: 20px;
    letter-spacing: 35px;
    white-space: nowrap;
    line-height: 12px;
    overflow: hidden;
}
.child {
    width: 100px;
    height: 100px;
    border: solid 1px #ccc;
    display: inline-block;
    vertical-align: middle;
}
</style>
</head>
<body>
<div class="mydiv" id="parent">
<div class="child">
Block 1
</div>
<div class="child">
Block 2
</div>
</div>
</body>
</html>
 가장 은 "예"를 사용하는 입니다.margin-auto, 는 ' 있어야 .widthdivpx ★★★★★★★★★★★★★★★★★」%.
CSS 코드:
div{
    width:30%;
    margin-left:auto;
    margin-right:auto;
}
html 파일에 다음과 같이 씁니다.
<div class="banner">
  Center content
</div>
쓰는 css 파일:
.banner {
display: block;
margin: auto;
width: 100px;
height: 50px;
}
잘 먹히네요.
특정 상황에서는 margin-left:auto 및 margin-right:auto를 사용할 수 없습니다.여기 항상 효과가 있는 해결책이 있습니다.필요한 너비를 지정하고 나머지 너비의 절반으로 왼쪽 여백을 설정합니다.
    <div style="width:80%; margin-left:calc(10%);">
        your_html
    </div>
div 및 span 태그를 블록, 크로스 브라우저 인라인 블록, 텍스트 얼라인 센터 등의 css 속성과 함께 사용합니다.간단한 예를 참조하십시오.
<!DOCTYPE html>
<html>
    <head>
       <title>Page Title</title>
       <style>
           .block{display:block;}
           .text-center{text-align:center;}
           .border-dashed-black{border:1px dashed black;}
           .inline-block{
                 display: -moz-inline-stack;
                 display: inline-block;
                 zoom: 1;
                 *display: inline;
            }
           .border-solid-black{border:1px solid black;}
           .text-left{text-align:left;}
        </style>
    </head>
    <body>
          <div class="block text-center border-dashed-black">
              <span class="block text-center">
                  <span class="block"> 
        <!-- The Div we want to center set any width as long as it is not more than the container-->
                      <div class="inline-block text-left border-solid-black" style="width:450px !important;">
                             jjjjjk
                      </div> 
                  </span>
              </span>
          </div>
      </body>
   </html>
jQuery 사용:
$(document).ready(function() {
    $(".myElement").wrap( '<span class="myElement_container_new"></span>' ); // for IE6
    $(".myElement_container_new").css({// for IE6
        "display" : "block",
        "position" : "relative",
        "margin" : "0",
        "padding" : "0",
        "border" : "none",
        "background-color" : "transparent",
        "clear" : "both",
        "text-align" : "center"
    });
    $(".myElement").css({
        "display" : "block",
        "position" : "relative",
        "max-width" : "75%", // for example
        "margin-left" : "auto",
        "margin-right" : "auto",
        "clear" : "both",
        "text-align" : "left"
    });
});
또는 클래스 ".myElement"의 모든 요소를 중앙에 배치하는 경우:
$(document).ready(function() {
    $(".myElement").each(function() {
        $(this).wrap( '<span class="myElement_container_new"></span>' ); // for IE6
        $(".myElement_container_new").css({// for IE6
            "display" : "block",
            "position" : "relative",
            "margin" : "0",
            "padding" : "0",
            "border" : "none",
            "background-color" : "transparent",
            "clear" : "both",
            "text-align" : "center"
        });
        $(this).css({
            "display" : "block",
            "position" : "relative",
            "max-width" : "75%",
            "margin-left" : "auto",
            "margin-right" : "auto",
            "clear" : "both",
            "text-align" : "left"
        });
    });
});
위치:position:font;를 사용하여 왼쪽 및 위쪽 값을 설정할 수 있습니다.
.cenverDiv{
    position:relative;
    left:30%;
    top:0px;
}
언급URL : https://stackoverflow.com/questions/618097/how-do-you-easily-horizontally-center-a-div-using-css
'programing' 카테고리의 다른 글
| TabControl이 하위 항목을 다시 만들지 못하도록 합니다. (0) | 2023.04.16 | 
|---|---|
| UIScroll View의 중앙 콘텐츠(작은 경우) (0) | 2023.04.16 | 
| R에서 Excel로 데이터 내보내기 (0) | 2023.04.16 | 
| ComboBox에서 null 값을 선택할 수 없는 이유는 무엇입니까? (0) | 2023.04.16 | 
| Excel 시트에서 변수로 행 번호 전달 (0) | 2023.04.16 |