다음과 같이 div 안에 이미지를 넣고 그 위에 투명한 div가 올라오도록 하는 이미지를 만들어보았다

컴퓨터에 마땅한 이미지가 없어 예전에 그려두었던 호시노겐 그림을 넣었다

 

그냥 이미지 HTML

    <div class="item-box">
      <div class="image">
        <img src="../assets/image.png" alt="">
      </div>
    </div>

CSS

  .item-box{
    width: 100px;
    height: 100px;
    padding: 5px;
    margin: 5px;
    box-shadow: inset 1px 2px 4px rgba(0, 0, 0, 0.25);
    background: #fff;
    border-radius: 10px;
    display: table;
  }
  .image{
    display: table-cell;
    vertical-align: middle;

  }
  img{
    width: 100%;
    border-radius: 10px;
  }

이미지를 가운데 정렬 시키기 위해서 부모 div의 display 를 table 로 만들고 자식 div에 display : table-cell을 준 후

vertical-align : middle을 주었다!

 

div 겹친 HTML

<div class="item-box">
  <div class="image">
    <img src="../assets/image.png" alt="">
  </div>
  <div class="sold-out">
    <p>sold out</p>
  </div>
 </div>

다음과 같이 내가 원하는 div의 직속자식으로 sold-out 클래스를 가진 div를 만들어주었다

CSS

  .item-box{
    position: relative;
  }
  
  .sold-out{
    position: absolute;
    left: 0px;
    top: 0px;
    width: 100%;
    height: 100%;
    display: table;
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    border-radius: 10px;
  }

  .sold-out > p{
    display: table-cell;
    vertical-align: middle;
  }

item-box 클래스에는 position : relative 값을 추가해주고

sold-out 클래스는 position : absolute 값을 주었다

처음 다른 블로그를 참고하여 부모에 relative 자식에 absolute 값만 줬을 때는 이런식으로 자식 상자가 옆으로 가벼렸는데

left : 0px; right : 0px을 줘서 위치값을 초기화 시켜주면 원하는 위치에 이동한다!

반응형

1. 규칙의 충돌

css요소를 추가하다보면 css요소들이 겹쳐져 충돌하는 일이 일어난다. css는 우선순위에 따라 스타일을 적용시킨다

 

2. style 우선순위

1순위 !important
2순위 id선택자
3순위 class선택자
4순위 요소선택자
5순위 소스순서

5순위 순서의 경우

    .red{
      color: red;
    }
    .blue{
      color: blue;
    }
  <div class="red blue">red</div>
  <div class="blue red">blue</div>

다음과 같이 div class의 순서와 상관 없이 style sheet에서 나중에 적힌 스타일이 적용된다

 

3. 상속

CSS는 상속을 통해 부모 요소의 모든 속성을 자식에게 상속하지는 않는다

 

상속 되는 것 

> Text관련요소 visibility, opacity, font, color, line-heignt, text-align, white-space

 

상속 되지 않는 것

> box model 관련 요소 position 관련 요소 width/height, amrgin, padding, border, box-sizing, display, background,vertical-align, text-decoration, positon, top/right/bottom/left, z-index, overflow

 

	.red{
      color: red;
    }

    .border{
      border: 1px solid blue;
    }
<div class="red border">
      상위 태그입니다
    <div>
      하위 태그입니다
    </div>
  </div>

css의 border가 하위 태그에는 적용이 되지 않은 것을 확인할 수 있다

반응형

1. float 요소

이미지의 좌, 우측에 텍스트를 둘러싸는 레이아웃을 위해 도입된 레이아웃으로
이미지가 아닌 다른 요소에 적용해 엡 사이트의 전체 레이아웃을 만드는데 까지 발전 했다.

css를 처음 배우던 4년 전만해도 float로 모든 화면을 처리했던 것 같은데 float을 통해 요즘은 화면구성을 잘 하지 않는다는 말을 들었다.
4년전에는 신 기술이었는데 4년이라는 짧은 시간동안 이렇게 빠르게 발전한건지,,, 아니면 그냥 그 때 내가 몰랐던건지
배우던 당시에는 table을 통해서 화면 구성을 할 수 있고 table 을 이용한 화면 구성이 오래된 방식, float최신방식이라 배웠었는데...
flexbox를 처음 접해서 얼떨떨하다

 

Float 속성

  • float: left;      요소를 왼쪽으로 정렬시킨다
  • float: right;    요소를 오른쪽으로 정렬시킨다
  • float: none;   기본 값

 

2. float 으로 화면 구성하기

    .box{
      width: 100px;
      height: 100px;
      margin: 10px;
      background-color: burlywood;
    }
    .float-left{
      float: left;
    }
    .float-right{
      float: right;
    }

css

<body>
  <div>
    <div class="box float-left">
      box1
    </div>
    <div class="box float-left">
      box2
    </div>
    <div class="box float-right">
      box3
    </div>
    Lorem ipsum dolor sit amet consectetur adipisicing elit. At repellendus impedit similique. Eos, illum aliquam veniam reiciendis quos iure dignissimos nobis quisquam voluptate quod accusamus nemo animi. Deserunt, nobis pariatur. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Unde deserunt tenetur eos velit maxime cumque quas quos? Facilis nobis quisquam minus, quasi quidem alias esse perferendis reiciendis delectus! Aliquam, architecto! Lorem ipsum, dolor sit amet consectetur adipisicing elit. Sequi nesciunt quo doloremque aperiam fugiat. Distinctio eveniet illum modi expedita atque nemo libero doloremque qui! Rerum esse magnam est asperiores molestiae! Lorem ipsum dolor sit amet consectetur adipisicing elit. Ea sunt, veniam sapiente amet fugit quidem dolore obcaecati recusandae totam tenetur aliquam nulla quisquam esse, tempora suscipit ipsa quam cum delectus.
  </div>

html

box1, 2에 float left / box 3에 float right 속성을 주면 다음과 같이 box가 이동하는 것을 볼 수 있다.

 

3. float 속성 지우기

float 속성을 주지 않는 non-float-box를 만들어 박스들의 하단에 위치시키면 아무 클래스의 영향을 받지 않은 non-float-box는 화면의 상단으로 이동한다(float 박스들은 화면에 띄워져 있는 상태)

그럼 이렇게 화면이 깨지는데 이걸 방지하기 위해 float 속성을 제거해줘야한다.

.clearfix>::after{
    content:"";
    display:block;
    clear:both;
}

 

.non-float{
      clear: both;
    }

non float box가 div 로 만들어져 block 속성을 가지고 있기 때문에 글까지도 아래로 이동하는 모습을 볼 수 있다

 

float을 통해 정렬을 한 후 정렬이 필요하지 않은 요소를 사용하는 시점에서 clear: both를 사용한다고 생각하면 될 것 같다

반응형

https://flexboxfroggy.com/#ko

 

Flexbox Froggy

A game for learning CSS flexbox

flexboxfroggy.com

Flexbox를 연습하며 개구리를 도와주는 게임으로 코드를 입력해보며 flexbox 속성들을 익힐 수 있다

왼쪽에 코드를 직접 입력해주면 된다

 

해답

 

1 단계

justify-content: flex-end;

2 단계

justify-content: center

3 단계

justify-content: space-around

4 단계

justify-content: space-between

5 단계

align-items: flex-end;

6 단계

justify-content: center;
    align-items: center;

7 단계

align-items: flex-end;
    justify-content: space-around;

8 단계

flex-direction: row-reverse;

9 단계

flex-direction:column;

10 단계

flex-direction: row-reverse;
    justify-content: flex-end

11 단계

flex-direction: column;
    justify-content: flex-end;

12 단계

flex-direction: column-reverse;
    justify-content: space-between

13 단계

flex-direction: row-reverse;
    justify-content: center;
    align-items: flex-end;

14 단계

order:1;

15 단계

order:-1;

16 단계

align-self: flex-end;

17 단계

order:1;
    align-self: flex-end

18 단계

flex-wrap: wrap;

19 단계

flex-direction: column;
    flex-wrap: wrap

20 단계

flex-flow: column wrap

21 단계

align-content: flex-start

22 단계

align-content: flex-end;

23 단계

flex-direction: column-reverse;
    align-content: center

24 단계

flex-flow: column-reverse wrap-reverse;
    justify-content: center;
    align-content: space-between;

 

반응형

기본적으로 div 박스 크기를 만질때 width를 설정 할 경우 content를 기준으로 width가 결정된다

div 상자를 만들고 width를 주는 경우 border은 콘텐츠의 크기 + 패딩값의 크기 값이 되는 것이다

 

CSS 코드

 

.box{
      width: 100px;
      margin: 20px;
      padding: 20px;
      border: 1px;
      background-color: brown;
    }

box1의 경우에는 콘텐츠 영역 100px + padding 20+20 그리고 border 1+1px을 더해 보더의 값은 총 142가 된다

    .box{
      width: 100px;
      margin: 20px;
      padding: 20px;
      border: 1px;
      background-color: brown;
    }
    .box-sizing{
      box-sizing: border-box;
    }

box-sizing : border-box 코드를 추가한 박스의 box2의 경우 border가 기준이 되고 있기 때문에 border값이 100px이 된다.

반응형

+ Recent posts