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.
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 would like to have an element superimposed on an image, which should responsively adjust to the element's dimensions.
In the example below, the goal is to have img completely surround the sibling element. Right now, it only partially covers it:
section {
position: relative;
}
img {
width: 100%;
height: 100%;
}
div {
padding: 30px;
margin: 30px;
position: absolute;
top: 0;
background: red;
opacity: 50%;
}
<section>
<img src="https://via.placeholder.com/1000x100">
<div>
Content Goes here
<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>
</section>
I tried adding background-image to section, but that doesn't seem to work either. Right now my only option is to use media queries and manually adjust the height of img, but I was curious if there is a cleaner approach.
You could set the image as a background of the section. Please note, that I removed the position absolute of the div element so the section can growth with that element. You can play around with the different background options till you get your desired result.
section {
position: relative;
background-image: url("https://via.placeholder.com/1000x100");
background-position: center;
background-size: 100% 100%;
padding: 15px; /* optional padding */
}
div {
padding: 30px;
top: 0;
background: red;
opacity: 50%;
}
<section>
<div>
Content Goes here
<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>
</section>
Why not use transform on the div?
section {
position: relative;
}
img {
width: 100%;
display: block;
max-width: 100%;
}
div {
padding: 30px;
margin: 0 30px;
top: 0;
background: red;
opacity: 50%;
position: relative;
transform: translateY(-25px);
}
<section>
<img src="https://via.placeholder.com/1000x100">
<div>
Content Goes here
<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>
</section>
may be you can do it in this way, i feel like usually child element work or inherit the properties of the parent. even in the case of relative and absolute parent child relation, child element stays with the parent even though wherever the parent goes/moves
so, if you want a background image to be superimposed on some element, first of all, may be you have to set the parent element/background image's properties properly and then keep your content inside. if your content is overflowing/larger than background you might have to adjust it manually ( like you said, with media queries ) or you can try in this way.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.image-back {
padding: 30px;
margin: 30px;
width: 100%;
height: auto;
background-image: url('https://via.placeholder.com/1000x100');
}
.content {
background-color: red;
opacity: 50%;
overflow: hidden;
}
</style>
</head>
<body>
<header>
<div class="image-back">
<div class="content">
<p>Content Goes here</p>
<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>
</header>
</body>
</html>
here in this case, try to get an image that can contain the size of inner element and its border+padding+etc. and have a look at how it behaves with different screen resolutions.
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 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>
I know it's easy enough to wrap text around images by floating the image right or left & then putting your text after it, but what I am wanting to do is wrap other elements around it as well, such as div's.
I tried to set my div to inline & this worked fine, however once I added other divs inside that div it still looked fine, but when looking at it in Firebug the little blue line that shows the element you are hovering over in the code extended over the image as well & when I attempted to add padding to the container div it didn't work & you could see that was because the padding was added right at the end.
I ended up getting it to look ok but adding padding to the image, however it still doesn't seem the right way to go about it seeing as Firebug doesn't like it & I am worried about compatibility issues.
Here is an image of what I am trying to do.. the gray area is where I want the text/elements to wrap & the brown is the image.
Here is some example code: (This example is the not wrapping version)
<div class="main">
<img src="../images/work/example.png" width="275" height="233" class="screenshot" alt="Example" />
<div class="details">
<div class="about">
<div class="title">
About:
</div>
<div class="info">
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.
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.
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.
</div>
<!-- Info Ends -->
</div>
<!-- About Ends -->
</div>
<!-- Details Ends -->
<div class="contentClear"></div>
</div>
<!-- Main Ends -->
Example CSS:
#content .wrapper .left .main {
padding-top: 20px;
width: 550px;
}
#content .wrapper .left .main .screenshot {
float: right;
border: 1px solid #c0c0c0;
width: 275px;
}
#content .wrapper .left .main .details {
width: 263px;
padding-right: 10px;
}
#content .wrapper .left .main .details .title {
color: #0F5688;
font-size: 1.8em;
font-family: arial;
font-weight: bold;
}
#content .wrapper .left .main .details .info {
margin-top: 6px;
font-size: 1.3em;
font-family: Arial;
color: #636363;
line-height: 1.6;
}
Here is an image showing the issue FireBug has with it (from the JSFiddle example), as I say it looks fine on the browser, but seeing as the firebug bar extends all the way over the image I was worried that may cause problems..
Yes, the correct way to move something to one side and have stuff wrap around it is to float the element.
In the example below (simplified from your code), adding padding to the floated image works just fine.
CSS:
.main .screenshot {
float: right;
border: 1px solid #c0c0c0;
padding: 5px;
}
.main .title{
font-size: 140%;
}
HTML:
<div class="main">
<img src="img/png" width="150" height="117" class="screenshot" alt="Example" />
<div class="details">
<div class="about">
<div class="title">About:</div>
<div class="info">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. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
</div>
</div>
</div>
Demo jsFiddle