I have a bunch of divs that have a content div inside of them. In the content div are 3 elements, an h1
, a p
and a span
, all left-aligned. What I want to happen is the following:
- The content div should be vertically and horizontally centered
- The content div should be exactly as wide as the text in the
h1
or the text in thespan
(whichever is longer), if above themax-width
these should wrap - The
p
should be 75% as wide as the content div but not have an impact on the content div's size (effectively being 75% as wide as theh1
orspan
, whichever is longer)
However I'm running into the following problems:
- Problem 1: Having a long
p
element causes the content div to expand to itsmax-width
no matter the size of theh1
orspan
. I've tried using absolute positioning to fix this but it disrupts the vertical centering of the div - Problem 2: Having a long
h1
element leaves a gap where the word breaks over 2 lines making the content div not appear centered
See the code snippet below to clarify what I'm after and what's going wrong, the borders are just to help visualise what's happening.
Has anyone got an idea of how this is possible? I would like to stick to CSS as these need to be responsive, although if there is a simple JS/jQuery solution it would be considered.
EDIT: To clarify the visual effect I am after here's a run-down of why the examples are good or bad. I've also added the ability to remove borders to demonstrate what I mean by something being visually centred:
1) Good: Content div fits to width of h1
, looks centered without the borders as equal space to left and right of h1
2) Good: Content div fits to width of span
as it's longer than the h1
, looks centered without the borders as equal space to left and right of span
Problem 1:
3) Bad: p
is expanding the width of the content div, looks shifted to the left without borders as more space on right than left. If the p
did not expand the div and stayed at 75% of the width this would not happen
4) Improvement on 3 but still bad: Potential fix found in various SO questions showing absolute positioning stops the p
expanding the content div, but now that it is not part of the flow it messes up the vertical centering
Problem 2:
5) Bad: The problem here is the h1
element, because it is now longer than the max-width
it splits into 2 lines. But the extra space between the end of the first line and the max-width
of the div is kept so when removing borders it doesn't look centered because there is more space to the right than the left of the h1
6) Fixes 5 but not a solution: Manually breaking the line (using a <br>
) achieves the look I need, because the h1
isn't expanded to the max-width
so looks centered without the borders. This isn't feasible in the real application though because the divs can vary in width
function toggleBorders() {
$('h1, p, span').toggleClass('bordered');
$('.content').toggleClass('content-bordered');
}
.box-holder {
display: flex;
flex-flow: row wrap;
}
.box {
flex: 0 0 380px;
display: flex;
align-items: center;
justify-content: center;
height: 350px;
border: 1px solid blue;
}
.content {
max-width: 80%;
position: relative;
}
.content-bordered {
border: 1px solid red;
}
.bordered {
border: 1px solid green;
}
p {
width: 75%;
}
.abs {
position: absolute;
}
button {
position: fixed;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="toggleBorders()">Toggle Borders</button>
<div class="box-holder">
<div class="box">
<div class="content">
<h1>1. Example Title</h1>
<p>Good Example</p>
<span>Example link to the article</span>
</div>
</div>
<div class="box">
<div class="content">
<h1>2. Title</h1>
<p>Min Width Good Example</p>
<span>Example link to the article</span>
</div>
</div>
<div class="box">
<div class="content">
<h1>3. Example Title</h1>
<p>But when the description gets too long then it expands the content div</p>
<span>Example link to the article</span>
</div>
</div>
<div class="box">
<div class="content">
<h1>4. Example Title</h1>
<p class="abs">Setting absolute position avoids expansion but messes up the vertical layout</p>
<span class="abs">Example link to the article</span>
</div>
</div>
<div class="box">
<div class="content">
<h1>5. Also Long Titles Leave White Space</h1>
<p>This doesn't look centered, see 6</p>
<span>Example link to the article</span>
</div>
</div>
<div class="box">
<div class="content">
<h1>6. Also Long Titles<br>Leave White Space</h1>
<p>Manually breaking lines fixes this</p>
<span>Example link to the article</span>
</div>
</div>
</div>