• 카테고리

    질문 & 답변
  • 세부 분야

    웹 개발

  • 해결 여부

    미해결

gap을 대신하여 margin을 사용할 경우.

20.07.30 14:04 작성 조회수 520

1

gap을 대신하여 margin을 사용할 경우.

크롬에서는 margin 과 gap 이 중복으로 실행되어 배치가 틀려지는데 explorer 에만 margin 을 부여 할 수 있는 방법이 있을까요?

답변 2

·

답변을 작성해보세요.

3

아래 코드는 브라우저가 인터넷익스플로러인지 아닌지,
익스플로러라면 몇버전인지 체크해주는 코드입니다.
여러가지 방법이 있을 수 있는데 userAgent를 활용하는 간단하고 독립적인 방법이에요~

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>IE 체크</title>
</head>
<body>
<script>
	function checkIE() {
		const htmlElem = document.querySelector('html');
		const ieVersion = -1;

		if (navigator.appName == 'Microsoft Internet Explorer') {
			const ua = navigator.userAgent;
			const re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
			if (re.exec(ua) != null)
				ieVersion = parseFloat( RegExp.$1 );
		} else if (navigator.appName == 'Netscape') {
			const ua = navigator.userAgent;
			const re  = new RegExp("Trident/.*ieVersion:([0-9]{1,}[\.0-9]{0,})");
			if (re.exec(ua) != null)
				ieVersion = parseFloat(RegExp.$1);
		}

		// 조사 결과에 따라 <html>에 class를 붙임
		if (ieVersion > 0) {
			htmlElem.classList.add('ie');
			htmlElem.classList.add('ie-' + ieVersion);
		} else if (navigator.userAgent.indexOf('rv:11') >= 0) {
			htmlElem.classList.add('ie', 'ie-11');
		} else {
			htmlElem.classList.add('not-ie');
		}

		return ieVersion;
	};

	// 콘솔에 확인
	console.log(checkIE());
</script>
</body>
</html>

checkIE() 를 실행해 보시면 브라우저에 따라
<html class="ie ie-11">
<html class="not-ie">
이런 식으로 class가 붙습니다.
.ie .container {} 이런 식으로 CSS를 작성하시면 익스플로러의 .container에만 적용하는 식이 되는 거고요^^
이런 방법으로 익스플로러만의 CSS를 따로 처리해주시면 됩니다.
(꼭 이 방법만 있는 것은 아니고요, 기본적으로 "브라우저가 뭔지 체크 후, 분기처리를 한다-" 이렇게 생각하시면 될 것 같아요)

0

mkp0131님의 프로필

mkp0131

질문자

2020.08.02

직접 JS 코드 까지 적어주시다니 신경써주셔서 감사합니다.