Я пытаюсь добиться этого общего макета страницы рабочего стола с помощью сетки CSS…
![page layout sample](https://i.stack.imgur.com/H0NZX.png)
Короче, некоторые страницы (например, мой блог), текст будет отформатирован в центре страницы для удобства чтения, но иногда мне понадобится изображение героя или другое большое изображение, которое проходит от левого края экрана к правому краю текстовой области (чтобы действовать какчто-то вроде противовеса мачте фиксированной ширины, которую я размещаю справа на странице). Я пытаюсь добиться этого с помощью CSS Grid, но я в тупике, поскольку желтый и красный ведут себя не так, как я ожидаю.
У меня есть ручка того места, где я до сих пор . В качестве краткого изложения я использую сетку с 5 столбцами, чтобы попытаться достичь этого, с элементом Main, охватывающим первые три столбца, и определяю вложенную сетку внутри нее, чтобы помочь расположить текст и полноразмерное изображение / содержимое объекта. Вложенная сетка - то, где я получаю зависания, она не будет устанавливать текстовое содержимое в столбец 712px, как я хочу, вместо того, чтобы начинать его с начала вложенной сетки. Код ниже…
Из ручки…
/* reset */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Define main grid */
body {
background-color: #efefef;
display: grid;
grid-template-columns: 180px 1fr 712px 1fr 180px; /* Last column must be 180px for masthead, and both columns to the left of 712 px should match both to the right, so mirroring the two sides */
grid-template-rows: 1fr 70px; /* Not sure I need this? */
}
/* Masthead: Set at right of page, narrow column, full page height */
header {
grid-column: 5 / 6;
grid-row: 1 / 3;
background: green;
}
/* Main content area:
Should span from left edge to just before second to last .body column.
Defining a nested grid here to position content. */
main {
grid-column: 1 / 4;
grid-row: 1 / 2;
display: grid;
grid-template: 180px 1fr 712px; /* Mimics body grid's first three columns */
}
/* Centered/margined content: Should be centered to page at 712 px */
main h2, main h3, main p {
grid-column: 3 / 4; /* PROBLEM AREA: Should be centered to the 712 px column, for line length readability, but is starting at left edge instead. Cannot figure this out. */
background: red;
}
/* Large content: Should fill all of main */
.full {
grid-column: 1 / 4;
background: yellow;
}
/* Footer: Should be centered like text safe content. This is behaving correctly. */
footer {
grid-column: 3 / 4;
grid-row: 2 / 2;
background: blue;
}
<header>
<h1>Masthead</h1>
</header>
<main>
<h2>Content text</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum rhoncus lectus nec nibh maximus, vel tincidunt lacus luctus. Aliquam sit amet sagittis dolor. Pellentesque feugiat nibh nec massa sagittis venenatis eu a purus. Nullam sed dignissim dolor. In fringilla egestas elit non sagittis.</p>
<img class="full" src="#" alt="Content full size object"></img>
<h3>Subhead</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum rhoncus lectus nec nibh maximus, vel tincidunt lacus luctus. Aliquam sit amet sagittis dolor. Pellentesque feugiat nibh nec massa sagittis venenatis eu a purus. Nullam sed dignissim dolor. In fringilla egestas elit non sagittis.</p>
<img class="full" src="#" alt="Content full size object"></img>
<h3>Subhead</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum rhoncus lectus nec nibh maximus, vel tincidunt lacus luctus. Aliquam sit amet sagittis dolor. Pellentesque feugiat nibh nec massa sagittis venenatis eu a purus. Nullam sed dignissim dolor. In fringilla egestas elit non sagittis.</p>
</main>
<footer>
<h1>Footer</h1>
</footer>