본문 바로가기

소프트웨어/JavaScript

[CSS] Grid(그리드) 소개와 활용

오늘은 Grid를 배웠다. 

Grid를 사용하면 목록과 본문이 나란히 위치하는 디자인을 만들 수 있다.

 

Grid를 사용하기 전, 두 개의 태그를 나란히 배치하고 싶으면 그것을 감싸는 부모 태그가 필요하다. 

디자인 목적을 위해 어떤 의미도 없는 태그인 <div> 또는 <span>을 사용한다.

<div> 태그는 block level element 이므로 줄바꿈, <span> 태그는 inline element 줄바꿈 되지는 않는다. 

 

 

<div id="grid">
      <ol>
           <li><a href="1.html">HTML</a></li>
           <li><a href="2.html">CSS</a></li>
           <li><a href="3.html">JavaScript</a></li>
      </ol>

      <div>
           <h2>CSS</h2>
              <p>
                   //
              </p>
      </div>
 </div>

 <ol> 부분 <h2>, <p> 부분을 나란히 배치하고 줄바꿈 싶어서 <div> 태그를 사용하여 묶었다. 

 

 

   <style>
  		#grid{
                display: grid;
                grid-template-columns: 150px 1fr;
            }
   </style>
	...
   <h1><a href="index.html">WEB</a></h1>
            <div id="grid">
                <ol>
                    <li><a href="1.html">HTML</a></li>
                    <li><a href="2.html">CSS</a></li>
                    <li><a href="3.html">JavaScript</a></li>
                </ol>
            </div>
 ...

첫번째 칼럼 <ol>태그와 두번째 칼럼 <div>태그를 나란히 배치하면 

 

그리드를 사용한 웹 사이트의 구체적인 디자인이 구현된다. 

 

 

간격 조절을 위해 이전에 배웠던 box model 속성을 이용하여 더욱 구체적으로 디자인을 구현

id 값이 grid인 태그 밑에 있는 <ol>태그라고 지정하면 값이 더욱 분명해지기 때문에 사용했다.

 

<!DOCTYPE HTML>
<html>
    <head>
        <title>WEB - CSS</title>
        <meta charset="utf-8">
        <style>
            body{
                margin:0;
            }
            a{
                color:black;
                text-decoration:none;
            }
            h1{
                font-size: 60px;
                text-align:center;
                border-bottom:1px solid gray;
                margin:0;
                padding:20px;
            }
            #grid ol{
                border-right: 1px solid gray;
                width: 100px;
                margin: 0;
                padding: 20px;
            }
            #grid{
                display: grid;
                grid-template-columns: 150px 1fr;
            }
            ol{
                padding: 33px;
            }
            #article{
                padding: 25px;
            }

        </style>
    </head>
    <body>
        <h1><a href="index.html">WEB</a></h1>
            <div id="grid">
                <ol>
                    <li><a href="1.html">HTML</a></li>
                    <li><a href="2.html">CSS</a></li>
                    <li><a href="3.html">JavaScript</a></li>
                </ol>

                <div id ="article">
                    <h2>CSS</h2>
                    <p>
                        Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language such as HTML or XML (including XML dialects such as SVG, MathML or XHTML).[1] CSS is a cornerstone technology of the World Wide Web, alongside HTML and JavaScript.[2]
            CSS is designed to enable the separation of content and presentation, including layout, colors, and fonts.[3] This separation can improve content accessibility; provide more flexibility and control in the specification of presentation characteristics; enable multiple web pages to share formatting by specifying the relevant CSS in a separate .css file, which reduces complexity and repetition in the structural content; and enable the .css file to be cached to improve the page load speed between the pages that share the file and its formatting.
                    </p>
                    <div id="disqus_thread"></div>
                    <script>
                        /**
                        *  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
                        *  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables    */
                        /*
                        var disqus_config = function () {
                        this.page.url = PAGE_URL;  // Replace PAGE_URL with your page's canonical URL variable
                        this.page.identifier = PAGE_IDENTIFIER; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
                        };
                        */
                        (function() { // DON'T EDIT BELOW THIS LINE
                        var d = document, s = d.createElement('script');
                        s.src = 'https://web1-0kdgliklwo.disqus.com/embed.js';
                        s.setAttribute('data-timestamp', +new Date());
                        (d.head || d.body).appendChild(s);
                        })();
                    </script>
                    <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
                        </p>
                </div>
            </div>    
    </body>
</html>

 

참고) 1fr는 나머지 공간을 다 쓴다는 의미이다.