I am learning flexbox and still not sure I fully understand how all the parts fit together. I would like to vertically align these columns so that the gray boxes line up with each other: http://codepen.io/anon/pen/EPZQZq (I updated the Codepen HTML/CSS to better reflect the challenge with my responsive layout.)
Some additional context: this is for a site that is responsive, so the width: 800px may be a bit misleading. And the gray bars can't be replaced by borders, they're meant to be stand-ins for actual content.
Code:
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
#container {
width: 800px;
font: 14px/22px "helvetica neue", sans-serif;
display: flex;
}
.item {
width: 33.33%;
display: flex;
flex-direction: column;
padding: 0 10px;
}
.item .blob {
width: 100%;
height: 20px;
background: #dedede;
}
<div id="container">
<div class="item">
<h1>Title TK</h1>
<div class="blob"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<div class="item">
<h1>A longer title TK TK TK</h1>
<div class="blob"></div>
<p>Lorem ipsum dolor sit amet.</p>
</div>
<div class="item">
<h1>A title that nobody could have possibly accounted for</h1>
<div class="blob"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
</div>
You want to control the height of .item h1. You can do it by either:
.item h1 {
min-height: 90px;
max-height: 90px;
}
or, the flexbox way:
.item h1 {
flex-basis: 90px;
flex-shrink: 0; /* if you don't want it to shrink */
flex-grow: 0; /* if you don't want it to grow */
}
Flexbox can't align or equalise items that do not share a common parent...so there is no native flexbox method here.
The header would need to be the same height in each column.
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
#container {
width: 90%;
font: 14px/22px"helvetica neue", sans-serif;
display: flex;
}
.item {
width: 33.33%;
display: flex;
flex-direction: column;
padding: 0 10px;
border: 1px solid grey;
}
.item h1 {
height: 120px;
}
.item .blob {
width: 100%;
height: 20px;
background: #dedede;
}
<div id="container">
<div class="item">
<h1>Title TK</h1>
<div class="blob"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
<div class="item">
<h1>A longer title TK TK TK</h1>
<div class="blob"></div>
<p>Lorem ipsum dolor sit amet.</p>
</div>
<div class="item">
<h1>A title that nobody could have possibly accounted for</h1>
<div class="blob"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
</div>
</div>
Related
Here's what I want to achieve both for desktop and mobile:
And here's how far I've gotten with it
.container {
background-image: url("https://houniqueconstruction.com/wp-content/uploads/2023/02/peng-chen-WFgSotZQECo-unsplash-copy-1024x803.jpg");
background-repeat: no-repeat;
background-size: cover;
min-height:700px;
}
.flexbox {
display:flex;
justify-content:center;
align-items: center;
min-height: 700px;
}
.text-box {
max-width:350px;
background:#f6f6f6;
padding: 30px 20px;
}
<div class="container">
<div class="flexbox">
<div class="text-box">
<h1>Complete Remodeling</h1>
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrude dolore magna.</p>
<a>CONTACT US</a>
</div>
</div>
</div>
I tried to achieve a layout with the CSS property background-image, however I lack the knowledge to achieve what I was expecting
For desktop we use margin-left on the container and offset the .text-box in the opposite direction.
For mobile we need to add an absolutely-positioned semi-transparent background element inside .text-box.
body {
background-color: #f6f6f6;
}
.container {
background-image: url("https://houniqueconstruction.com/wp-content/uploads/2023/02/peng-chen-WFgSotZQECo-unsplash-copy-1024x803.jpg");
background-repeat: no-repeat;
background-size: cover;
min-height:700px;
}
.container.desktop {
margin-left: 175px;
}
.flexbox {
display:flex;
align-items: center;
min-height: 700px;
}
.container.mobile .flexbox {
justify-content: center;
}
.text-box {
position: relative;
max-width:350px;
padding: 30px 20px;
}
.container.desktop .text-box {
left: -175px;
}
.text-background {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: #f6f6f6;
}
.container.mobile .text-background {
opacity: 0.5;
}
.text-content {
position: relative;
z-index: 1;
}
<div class="container desktop">
<div class="flexbox">
<div class="text-box">
<div class="text-background"></div>
<div class="text-content">
<h1>Complete Remodeling</h1>
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrude dolore magna.</p>
<a>CONTACT US</a>
</div>
</div>
</div>
</div>
<div class="container mobile">
<div class="flexbox">
<div class="text-box">
<div class="text-background"></div>
<div class="text-content">
<h1>Complete Remodeling</h1>
<p> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrude dolore magna.</p>
<a>CONTACT US</a>
</div>
</div>
</div>
</div>
.I want the breakpoint at 800px to place all three review divs side by side with 20px of padding around them but for whatever reason, it's not working. Can someone tell me where I went wrong? Currently, Reviewer 1 and 2 are stacked on top of each other to the left side of the container, while Review 3 is all by it's lonesome on the right. How do I change this?
Code snippet below:
HTML:
<div class="wrapper">
<div class="card-1">
<h3>Reviewer 1</h3>
<p class="border">
"Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam."</p>
</div>
<div class="card-2">
<h3>Reviewer 2</h3>
<p class="border">"Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam."</p>
</div>
</div>
<div class="card-3">
<h3>Reviewer 3</h3>
<p class="border">"Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam."</p>
</div>
</div>
CSS:
.cards {
display: flex;
flex-direction: column;
align-items: center;
}
#media screen and (min-width: 800px) {
.cards {
display: flex;
flex-direction: row;
padding: 10px;
border: 1px black solid;
}
.border {
width: 75%;
padding: 10px;
margin: auto;
}
}
So I figured it out ironically enough. I just changed the .wrapper div as follows:
.wrapper {
display: flex;
flex-direction: row;
}
Trying to make my first grid website, and everything is going well, but for some reason the Header text is crowded in a smaller browser.
I've tried playing with padding/margins of different elements. tried adding line heights, deleting flex properties, nothing has worked.
I want my showcase h1 to have proper line spacing.
body {
background: var(--primary);
margin: 30px 50px;
line-height: 1.6rem;
}
img {
max-width: 100%;
}
.wrapper {
display: grid;
grid-gap: 1.2rem;
}
.main-nav ul {
display: grid;
grid-gap: 1.3rem;
grid-template-columns: repeat(4, 1fr);
padding: 0;
list-style: none;
}
.main-nav a {
background: var(--dark);
display: block;
text-decoration: none;
padding: 0.8rem;
color: var(--primary);
text-transform: uppercase;
font-size: 1.1rem;
box-shadow: var(--shadow);
text-align: center;
}
.btn {
color: var(--primary);
background: var(--dark);
padding: 0.6rem 1.3rem;
text-decoration: none;
border: 0;
box-shadow: var(--shadow);
}
.main-nav a:hover {
background: var(--primary);
color: var(--dark);
}
.top-container {
display: grid;
grid-gap: 1.2rem;
grid-template-areas: 'showcase showcase top-box-a' 'showcase showcase top-box-b';
}
.showcase {
grid-area: showcase;
min-height: 400px;
background: url("https://source.unsplash.com/random");
background-size: cover;
background-position: center;
padding: 3rem;
display: flex;
flex-direction: column;
align-items: start;
justify-content: center;
color: black;
box-shadow: var(--shadow);
}
.showcase h1 {
font-size: 4rem;
margin-bottom: 0.5rem;
}
.showcase p {
font-size: 1.3rem;
margin-top: 0;
}
.top-box {
background: #545454;
color: var(--primary);
padding: 1.5rem;
text-align: center;
align-items: center;
box-shadow: var(--shadow);
justify-items: center;
}
.top-box .price {
font-size: 1.4rem;
}
.top-box-a {
grid-area: top-box-a;
}
.top-box-b {
grid-area: top-box-b;
}
.boxes {
display: grid;
grid-gap: 1.3rem;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
.box {
background: var(--dark);
color: var(--primary);
text-align: center;
padding: 1.5rem 2rem;
box-shadow: var(--shadow);
}
<link href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" rel="stylesheet" />
<!-- Wrapper Begins-->
<div class="wrapper">
<nav class="main-nav">
<ul>
<li>Home</li>
<li>Services</li>
<li>Contact</li>
<li>Gallery</li>
</ul>
</nav>
<section class="top-container">
<header class="showcase">
<h1>Victors Tree Service</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua!
</p>
Read More
</header>
<div class="top-box top-box-a">
<h2>Routine Services</h2>
<p class="price">Starting at $199</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
</div>
<div class="top-box top-box-b">
<h2>Tree Removal</h2>
<p class="price">From $299 - $799</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
</div>
</section>
<section class="boxes">
<div class="box">
<i class="fas fa-tree fa-4x"></i>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
<div class="box">
<i class="fas fa-tools fa-4x"></i>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
<div class="box">
<i class="fas fa-users fa-4x"></i>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
<div class="box">
<i class="fas fa-clock fa-4x"></i>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
</section>
<section class="info">
<img src="https://source.unsplash.com/random">
<div>
<h2>Over 20 Years Experience</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</section>
<section class="portfolio">
<img src="https://source.unsplash.com/random/200x200">
<img src="https://source.unsplash.com/random/200x201">
<img src="https://source.unsplash.com/random/200x202">
<img src="https://source.unsplash.com/random/200x203">
<img src="https://source.unsplash.com/random/200x204">
<img src="https://source.unsplash.com/random/200x205">
<img src="https://source.unsplash.com/random/200x206">
<img src="https://source.unsplash.com/random/200x207">
<img src="https://source.unsplash.com/random/200x208">
</section>
<footer>Victor's Tree Service © 2019</footer>
</div>
<!--Wrapper Ends -->
This is not a grid or flex or padding / margin issue. It's a line-height issue.
In particular, the problem is line-height: 1.6rem set on the body element.
The line-height property sets the minimum height of line boxes, where inline elements, such as text, exist.
A typical default value in most browsers is 1.2 (MDN) – notice the unitless value; more on that later.
In addition, the line-height property is inheritable (MDN), which means that elements down the HTML structure take the value you've set higher up.
Since you haven't defined a line-height value anywhere below the body element, that setting applies throughout your document.
So here's the problem:
You've set your h1 to 4rem.
But a 1.6rem line height cannot accommodate that size.
(4 * 16px) > (1.6 * 16px)
Here's your layout in normal desktop mode:
Notice how the heading is already exceeding the boundaries of the line box.
Because of this short line height, the text overlaps on wrap (as on smaller screens):
The solution, believe it or not, is simply to remove the unit of length from the line-height value.
So instead of line-height: 1.6rem, use line-height: 1.6.
You may even want to consider using the 1.2 default value (in which case, you can omit the line-height rule altogether).
The reason this works is as follows:
When you use line-height: 1.6rem, the line box height is calculated based on the root element's font size. In your case, that happens to be 16px (the browser's default value).
line box height: 1.6 * 16px = 25.6px
h1 font size: 4.0 * 16px = 64.0px
That's how the h1 exceeds the height of the line box.
But with line-height: 1.6, the line box height is calculated based on the size of the font itself.
line box height: 1.6 * (4 * 16px) = 102.4px
h1 font size: 4.0 * 16px = 64.0px
That's how the line box exceeds the height of the h1.
It's also why it makes sense to use unitless values in the line-height property.
See the spec for references and more details:
10.8 Line height calculations: the line-height and vertical-align properties
Use media query for responsiveness and it should be written at the end of the css so that css properties written under media query will not get override with any other css properties.
#media screen and (max-width: 600px) {
.main-nav a {
text-align: center;
float: none;
}
}
Instead of display:grid, use display:block so that it will take entire space and use float:left so that all the elements will be positioned to the left of the container.
body {
background: var(--primary);
margin: 30px 50px;
line-height: 1.6rem;
}
img {
max-width: 100%;
}
.wrapper {
display: grid;
grid-gap: 1.2rem;
}
.main-nav ul {
display: block;
padding: 0;
list-style: none;
}
.main-nav a {
background: var(--dark);
display: block;
text-decoration: none;
margin: 0.8rem;
/*use margin instead of padding so that it will be clickable only on the element*/
color: var(--primary);
text-transform: uppercase;
font-size: 1.1rem;
box-shadow: var(--shadow);
text-align: center;
float: left;
border: 1px solid red;
/*just to highlight the clickable el*/
}
.btn {
color: var(--primary);
background: var(--dark);
padding: 0.6rem 1.3rem;
text-decoration: none;
border: 0;
box-shadow: var(--shadow);
}
.main-nav a:hover {
background: var(--primary);
color: var(--dark);
}
.top-container {
display: grid;
grid-gap: 1.2rem;
grid-template-areas: 'showcase showcase top-box-a' 'showcase showcase top-box-b';
}
.showcase {
grid-area: showcase;
min-height: 400px;
background: url("https://source.unsplash.com/random");
background-size: cover;
background-position: center;
padding: 3rem;
display: flex;
flex-direction: column;
align-items: start;
justify-content: center;
color: black;
box-shadow: var(--shadow);
}
.showcase h1 {
font-size: 4rem;
margin-bottom: 0.5rem;
}
.showcase p {
font-size: 1.3rem;
margin-top: 0;
}
.top-box {
background: #545454;
color: var(--primary);
padding: 1.5rem;
text-align: center;
align-items: center;
box-shadow: var(--shadow);
justify-items: center;
}
.top-box .price {
font-size: 1.4rem;
}
.top-box-a {
grid-area: top-box-a;
}
.top-box-b {
grid-area: top-box-b;
}
.boxes {
display: grid;
grid-gap: 1.3rem;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
.box {
background: var(--dark);
color: var(--primary);
text-align: center;
padding: 1.5rem 2rem;
box-shadow: var(--shadow);
}
#media screen and (max-width: 600px) {
.main-nav a {
text-align: center;
float: none;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Victors Tree Service</title>
<link rel="stylesheet" type="text/css" href="C:\Users\geek\Desktop\Experimental\styles.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-
UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
</head>
<body>
<!-- Wrapper Begins-->
<div class="wrapper">
<nav class="main-nav">
<ul>
<li>Home</li>
<li>Services</li>
<li>Contact</li>
<li>Gallery</li>
</ul>
</nav>
<section class="top-container">
<header class="showcase">
<h1>Victors Tree Service</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua!
</p>
Read More
</header>
<div class="top-box top-box-a">
<h2>Routine Services</h2>
<p class="price">Starting at $199</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
</div>
<div class="top-box top-box-b">
<h2>Tree Removal</h2>
<p class="price">From $299 - $799</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
</div>
</section>
<section class="boxes">
<div class="box">
<i class="fas fa-tree fa-4x"></i>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
<div class="box">
<i class="fas fa-tools fa-4x"></i>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
<div class="box">
<i class="fas fa-users fa-4x"></i>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
<div class="box">
<i class="fas fa-clock fa-4x"></i>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
</section>
<section class="info">
<img src="https://source.unsplash.com/random">
<div>
<h2>Over 20 Years Experience</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</section>
<section class="portfolio">
<img src="https://source.unsplash.com/random/200x200">
<img src="https://source.unsplash.com/random/200x201">
<img src="https://source.unsplash.com/random/200x202">
<img src="https://source.unsplash.com/random/200x203">
<img src="https://source.unsplash.com/random/200x204">
<img src="https://source.unsplash.com/random/200x205">
<img src="https://source.unsplash.com/random/200x206">
<img src="https://source.unsplash.com/random/200x207">
<img src="https://source.unsplash.com/random/200x208">
</section>
<footer>Victor's Tree Service © 2019</footer>
</div>
<!--Wrapper Ends -->
</body>
</html>
I'm trying to create rows with svg + text with flexbox, and I'm facing an issue.
When the text is too long and takes 2 lines, the svg is shrinking. The more there are lines, the more it shrinks
Note : the svg is a placeholder for the moment
Here is the code :
<div class="wrapper">
<div class="container">
<div class="svg"></div>
<p>lorem</p>
</div>
<div class="container">
<div class="svg"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam. </p>
</div>
</div>
And the css :
.wrapper {
width: 800px;
margin-right: auto;
margin-left: auto;
display: flex;
flex-direction: column;
padding: 40px 12px;
}
.container {
display: flex;
align-items: flex-start;
margin-bottom: 20px;
}
.svg {
margin-right: 20px;
height: 20px;
width: 20px;
background-color: #DEDEDE;
}
p {
margin: 0;
}
Here is a codepen so you can see what I mean by "shrinking" : https://codepen.io/anon/pen/YdWyBM?editors=1100
Any ideas?
Thanks
Your magic trick is .svg {flex-shrink: 0;}. That's how you disable shrinking for a flex child.
.wrapper {
width: 800px;
margin-right: auto;
margin-left: auto;
display: flex;
flex-direction: column;
padding: 40px 12px;
}
.container {
display: flex;
align-items: flex-start;
margin-bottom: 20px;
}
.svg {
margin-right: 20px;
height: 20px;
width: 20px;
background-color: #DEDEDE;
flex-shrink: 0;
}
p {
margin: 0;
}
<div class="wrapper">
<div class="container">
<div class="svg"></div>
<p>lorem</p>
</div>
<div class="container">
<div class="svg"></div>
<p>lorem</p>
</div>
<div class="container">
<div class="svg"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p>
</div>
<div class="container">
<div class="svg"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris </p>
</div>
</div>
i added max-width:calc(100% - 40px) to p for margin-right:20px and width:20px
.wrapper {
width: 800px;
margin-right: auto;
margin-left: auto;
display: flex;
flex-direction: column;
padding: 40px 12px;
}
.container {
display: flex;
align-items: flex-start;
margin-bottom: 20px;
}
.svg {
margin-right: 20px;
height: 20px;
width: 20px;
background-color: #DEDEDE;
}
p {
margin: 0;
max-width:calc(100% - 40px);
}
<div class="wrapper">
<div class="container">
<div class="svg"></div>
<p>lorem</p>
</div>
<div class="container">
<div class="svg"></div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam. </p>
</div>
</div>
I want to have two div side by side in one container: the first is an image (the width is fixed but the code should be applied for different images with different widths) . The second will take the rest of the container.
.conteiner {
position: relative;
}
.image{
display: inline-block;
top: 0;
right: 0;
position: absolute;
width: auto;
}
.text{
display: inline-block;
position: relative;
max-width: 100%;
}
But the image will be placed over the text.
<div class="container">
<div class="text">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullmaco laboris nisi ut aliquip ex ea consequat.
</p>
</div>
<div class="image">
<img width="151" height="97">
</div>
In this example, the image had width 151px and 97 px height. But but for other images should not be the same.
Remove top: 0;right: 0;position: absolute; to stop your .image from being positioned absolutely so that the text does not overlap it.
alter your width's to match so that P doesn't take over the width.
also add vertical-align:top; to keep items to the top.
see bellow
.conteiner {
position: relative;
}
.align {
vertical-align: top;
display: inline-block;
position: relative;
}
.text {
max-width: 50%;
}
.image {
max-width: 50%;
}
<div class="container">
<div class="text align">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullmaco laboris nisi ut aliquip ex ea consequat.
</p>
</div>
<div class="image align">
<img width="151" height="97">
</div>
This might be a more prudent solution for what you are after.
.conteiner {
position: relative;
display:table;
}
.align {
vertical-align: top;
display: table-cell;
position: relative;
}
<div class="container">
<div class="text align">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullmaco laboris nisi ut aliquip ex ea consequat.
</p>
</div>
<div class="image align">
<img width="151" height="97">
</div>
This is achieved by giving the image (or the image container) a float:left and the text container a margin-left.
Please take a look at this example:
https://jsfiddle.net/k4054xkm/
HTML:
<div class="container">
<div class="image">
<img src="http://placehold.it/200x200">
</div>
<div class="text">
<h1>Testtext</h1>
<p>A bit of Testtext</p>
</div>
</div>
CSS
.container {
position: relative;
}
.image{
float:left;
width:200px;
}
.text{
margin-left:220px;
}
Please adjust the width and margin-left to your needs.
You have position absolute on .image this mean that this block not realy takes width and the second block is relative he will take the same place the absolute block takes .image (over or under it).
you can try to rewrite css like this:
.conteiner {
}
.image{
float:right;
width: auto;
}
.text{
float: right;
max-width: 100%;
}
you can replace float: right to float: left if you start from left side.