r/csshelp • u/dkonofalski • Jan 09 '20
Request [Non-Reddit] Flexbox layout without control of HTML
I have the following HTML and am wondering if a certain layout like this is possible using only Flexbox.
<div>
<img>
<header></header>
<div></div>
<footer></footer>
</div>I'm looking for this kind of layout:
|| header
img || div
|| footerI realize that I could wrap the header, content, and footer in a container element but I don't have the ability to change the HTML as it's dynamically generated and I can't just use float: left; on the image because then the text wraps underneath it. No matter what options I've tried, I can't seem to get it to float the image to the left with the content on the right.
Any thoughts would be helpful. If that's not possible, I'll need to try and work with a vendor to fix this and change the HTML being generated which will likely not happen.
Thanks in advance!
1
u/ForScale Jan 09 '20
I'd do something like:
<style>
.flex-container {
display: flex;
}
.flex-dir-col {
flex-direction: column;
justify-content: space-between;
}
</style>
<div class="flex-container">
<div>
<img src="https://vignette.wikia.nocookie.net/gardenpaws/images/3/35/Dog.png">
</div>
<div class="flex-container flex-dir-col">
<header>Header</header>
<div>div</div>
<footer>footer</footer>
</div>
</div>1
u/dkonofalski Jan 13 '20
As I mentioned in the original title and post, I cannot change the HTML source for this application so I can't do what you're suggesting. The issue is making it work within the existing HTML structure.
1
u/ForScale Jan 13 '20
Oh... whoops.
Have you used float before? Just do
float: left;on the img.1
u/dkonofalski Jan 13 '20
Already mentioned that. Float causes the text to wrap below the image.
1
u/ForScale Jan 13 '20
K... grid then:
<style> div * { border: 1px solid; } div { display: grid; grid-template-rows: repeat(10, 1fr); grid-template-columns: repeat(10, 1fr); width: 300px; height: 300px; } img { grid-row: 2 / 6; grid-column: 1 / 2; width: 100px; height: 100px; } header { grid-row: 1 / 2; grid-column: 2 / 3; height: 100px; } div div { grid-row: 2 / 3; grid-column: 2 / 3; height: 100px; } footer { grid-row: 3 / 4; grid-column: 2 / 3; height: 100px; } </style> <div> <img src=""> <header>header</header> <div>div</div> <footer>footer</footer> </div>1
u/dkonofalski Jan 14 '20
Does this work without specifying the heights? I won't know the heights for all the content blocks inside of the div. I'll try this out either way but I feel like I've tried something similar to this already when I experimented with grid.
Thanks for the attempt!
1
u/ryancperry Jan 10 '20
You can easily achieve that with grid. If you need help, link the source code or whatever. I’ll help you out.
1
u/dkonofalski Jan 13 '20
I posted the source in the original post. There's no more too it. It's a div with 4 child elements in it, one of which is an image.
2
u/Abuzz Jan 10 '20
This would be easier with CSS grid than flexbox