웹 페이지 상단에 세 개의 항목이 있습니다. 나는 그들이 왼쪽, 가운데, 오른쪽에 위치 할 것으로 기대한다. 그러나 중앙에있는 것은 깡충 뛰기입니다 (부분적으로 게임에서 늦게 만들어 졌기 때문에). 나는 운이없는 자동 마진 트릭을 시도했다. 상대 위치 지정을 시도했지만 완벽하게 중앙에 배치 할 수는 없습니다 (그리고 이웃을 그립니다). 아래의 전체 코드에서 "showInMiddle"를 중앙에 배치 할 수 있습니까? 해당 항목이 표시 되려면 로그인 버튼을 클릭해야합니다. 이상적으로는 페이지가 너무 좁지 만 정렬을 계속 유지하면 항목이 줄 바꿈됩니다 (왼쪽 또는 위로 모두 그리는 것이 아니라).스팬 요소를 가운데에 맞출 수 없습니다.
<!DOCTYPE html>
<html>
<head>
<title>This is dumb</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://knockoutjs.com/js/knockout-2.1.0.js"></script>
</head>
<body style="font-family: 'Segoe UI'; margin: 5px 20px;">
<header style="width: 100%">
<h4 id="showOnLeft" style="font-size: 1.1em; display: inline;">I'm the title</h4>
<span id="showInMiddle" data-bind="visible: LoggedIn">
I'm supposed to be in the middle with my two buttons.
<button>B1</button>
<button>B2</button>
</span>
<div id="showOnRight" style="display: inline; float: right">
<form id="someLoginForm" style="display: inline;" data-bind="visible: !LoggedIn(), submit: login" action="" method="post">
<input type="text" placeholder="Username" />
<input type="password" placeholder="Password" />
<input type="submit" value="Login" />
</form>
<form id="someLogoutForm" style="display: inline;" data-bind="visible: LoggedIn(), submit: logout" action="" method="post">
<span>Howdy</span>
<input type="submit" value="Logout" />
</form>
</div>
<nav><hr/></nav>
</header>
<script type="text/javascript">
function LoginViewModel() {
var self = this;
self.LoggedIn = ko.observable(false);
self.login = function (formElement) { self.LoggedIn(true); };
self.logout = function (formElement) { self.LoggedIn(false); };
}
ko.applyBindings(new LoginViewModel());
</script>
</body>
</html>
체크 아웃 http://jsfiddle.net –