I am trying to display an outer div with an image on the left and another div with some text to the right on it. I use:
img {
float: left;
}
div.out {
background-color: blue;
padding: 10px;
}
div.in {
background-color: yellow;
padding: 5px;
}
<div class="out">
<img src="https://i1.rgstatic.net/ii/profile.image/AS%3A272372255162386%401441950014433_m/Wachiraya_Imsabai.png">
<div class="in">
Flowers<br>are<br>cool
</div>
</div>
Code works almost fine as shown below but how can I make the first blue div extend to the very end of where the image ends? Also, how can I add a 10px space let's say between the image and the text? (And also, is my way of adding float: left; to the img the best way to display it to the left or is there a better way?)
Thanks a lot
img {
display: inline-block;
}
div.out {
background-color: blue;
padding: 10px;
display: flex;
}
div.in {
display: inline-block;
width: 100%;
background-color: yellow;
padding: 5px;
}
<div class="out">
<img src="https://i1.rgstatic.net/ii/profile.image/AS%3A272372255162386%401441950014433_m/Wachiraya_Imsabai.png">
<div class="in">
Flowers<br>are<br>cool
</div>
</div>
I recommend you should learn how to use flex
You can use the flexbox model for this. Remove the float and on the outer div use display: flex;, and the inner div flex-grow: 1;:
#flexbox_container {
display: flex;
background-color: blue;
padding: 10px;
}
div.flex_item {
background-color: yellow;
padding: 10px;
flex-grow: 1;
}
<div id="flexbox_container">
<img class="flex_item" src="https://i1.rgstatic.net/ii/profile.image/AS%3A272372255162386%401441950014433_m/Wachiraya_Imsabai.png">
<div class="flex_item item_selected">
Flowers
<br>are
<br>cool</div>
</div>
img {
float: left;
}
div.out {
background-color: blue;
padding: 10px;
}
div.in {
background-color: yellow;
padding: 5px;
}
.clr {
clear: both;
}
<div class="out">
<img src="https://i1.rgstatic.net/ii/profile.image/AS%3A272372255162386%401441950014433_m/Wachiraya_Imsabai.png">
<div class="in">
Flowers<br>are<br>cool
</div>
<div class="clr"></div>
</div>
If you're looking for the blue box to extend to the end of your floated content, your looking for a clear fix. See the minor change above with the added div with class clr.
-- Edit of all edits --
Realized after doing what you asked... it probably wasn't EXACTLY what you meant to ask. But I got the blue box to extend to where the image ends haha.
display:flex don't support in IE9 and earlier, So i use of other way :
.out {
background-color: blue;
padding: 15px;
}
.content {
background-color: yellow;
}
.content:after {
content: '';
display: block;
clear: both;
}
.in {
padding-top: 5px;
}
img {
float: left;
margin-right: 5px;
}
<div class="out">
<div class="content">
<img src="https://i1.rgstatic.net/ii/profile.image/AS%3A272372255162386%401441950014433_m/Wachiraya_Imsabai.png">
<div class="in">Flowers<br>are<br>cool</div>
</div>
</div>
Related
This is the portion of my template, CSS is inline for demo purposes only:
.header {
background-color: #333;
color: #FFF;
padding: 30px;
}
.content {
width: 750px;
}
.mc-info {
height: auto;
border: 2px solid;
width: 800px;
height: auto;
margin-top: 20px;
margin-bottom: 20px;
}
.mc-info-image {
width: 240px;
}
.mc-info-image img {
margin-right: 29px;
height: 340px;
float: left;
}
.description {
font-size: 16px;
float: right;
}
.vehiname {
font-size: 18px;
color: green;
}
.price {
color: red;
}
footer {
color: #333;
background-color: blue;
}
It's been causing problems with getting text and images to align properly, as images and DIV overrun, as seen at https://jsfiddle.net/r7podhq6/- the div does not resize with the content, even though a standard image is set.
I'm trying to do something like this for my layout, although with the bordered DIV's I'm using and keeping class mc-info at the width set.
Should I convert to flex or grid for this? If not, how could I improve this?
My problem is getting text and images to align for this basic template for a car dealer platform.
Looking for help and constructive criticism./
You should really consider learning CSS flex and grid. using float to align divs is not the best practice in 2021.
I have added display: flex to .mc-info and removed all the float properties and max-width:100% to auto scale.
.mc-info {
display: flex;
}
.mc-info-image {
max-width: 800px;
}
img {
max-width: 100%;
}
.info-wrapper {
padding-left: 20px;
}
And lastly wrapped the car information in <div class="info-wrapper"> </div>
Final code
<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<head>
<body>
<title>YourLeisure Motorhomes & Caravans</title>
<style>
.header {
background-color: #333;
color: #FFF;
padding: 30px;
}
.content {
width: 750px;
}
.mc-info {
display: flex;
height: auto;
border: 2px solid;
max-width: 800px;
height: auto;
margin-top: 20px;
margin-bottom: 20px;
}
.mc-info-image {
max-width: 800px;
}
.mc-info-image img {
margin-right: 29px;
height: 340px;
}
.description {
font-size: 16px;
float: right;
}
.vehiname {
font-size: 18px;
color: green;
}
.price {
color: red;
}
footer {
color: #333;
background-color: blue;
}
img {
max-width: 100%;
}
.info-wrapper {
padding-left: 20px;
}
</style>
</head>
<body>
<div class="header">
<h1>YOURLEISURE MOTORHOMES</h1>
<h2>49 High Street, Northtown</h2>
</div>
<div class="content">
<div class="mc-info">
<div class="mc-info-image">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b4/VW_T5_California_front_20071215.jpg/800px-VW_T5_California_front_20071215.jpg">
</div>
<div class="info-wrapper">
<div class="vehiname">
<h2>VOLKSWAGEN CALIFORNIA 2.0 BITDI 180</h2>
</div>
<div class="price">
<h2>£45,000</h2>
</div>
<div class="description">
This example has all the equipment you could want
</div>
</div>
</div>
<div class="mc-info">
<div class="mc-info-image">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/74/Ford_Transit_FT_130_Camper_%2818418440689%29.jpg/1024px-Ford_Transit_FT_130_Camper_%2818418440689%29.jpg">
</div>
<div class="info-wrapper">
<div class="vehiname">
<h2>FORD TRANSIT CAMPERVAN 2.0</h2>
</div>
<div class="price">
<h2>£1,000</h2>
</div>
<div class="description">
red/white
</div>
</div>
</div>
<footer>
Content to come
</footer>
</body>
</html>
The answer given by Josh answers your question.
Moreover, i'd recommend you to use flexbox/grid over float. They have a lot of properties that could be really helpful in making a responsive design.
I have this table-style DIV code for a used vehicle sales platform:
.mainwrapper {
border: 2px solid;
display: table;
}
.itemwrapper {
display: table-row;
width: 706px;
}
.mainwrapper {
margin-bottom: 20px;
}
.item {
width: 700px;
border: 1px solid;
padding: 1em;
background-color: blue;
color: white;
}
.item:nth-child(2) {
float: left;
margin: -2px;
}
.item1 {
display: table-cell;
text-align: left;
margin-left: -30px;
}
.item1 p {
margin-top: -30px;
}
.item-price {
width: 300px;
background-color: blue;
padding: 1em;
color: white;
text-align: center;
}
.picture, .item {
display: table-cell;
border: 1px solid;
}
.picture {
width: 90px;
margin: 1px;
border: 2px solid;
}
.picture img {
height: 185px;
}
<div class="mainwrapper">
<div class="itemwrapper">
<div class="item">1992 ELDDIS PAMPEROS XLi</div>
<div class="item-price">£1,000</div>
</div>
<div class="itemwrapper">
<div class="picture"><img src="https://complianz.io/wp-content/uploads/2019/03/placeholder-300x202.jpg.webp"></div>
<div class="item1"><p>2 berth, good condition</p></div>
</div>
</div>
<div class="mainwrapper">
<div class="itemwrapper">
<div class="item">2008 SWIFT CHALLENGER 540</div>
<div class="item-price">£13,000</div>
</div>
<div class="itemwrapper">
<div class="picture"><img src="https://complianz.io/wp-content/uploads/2019/03/placeholder-300x202.jpg.webp"></div>
<div class="item1"><p>4 berth end bedroom</p></div>
</div>
</div>
What I am trying to do is ensure the class item1 is opposite the image, with the text like this if you didn't have the £ per month div and list as table:
Basically, what I am trying to fix is the text that's in class item1 opposite the image (not with all the description or colored DIV there); see the image below.
I tried margin-left and margin-top, but it won't quite put the image opposite.
This is the result of my code:
I can't quite get it to work as I'd expected, text opposite image and size of DIV in the CSS; if anyone can help, I'd much welcome this.
It works OK - no major coding errors, but isn't quite esthetically working out, and that's the basic problem.
I'm trying this as basic HTML first before attempting anything with Javascript, just to ensure it works as a standalone design.
Edit: I tried vertical-align for text, that worked, but it's fixing the gap between image div and text that's the issue. There's a large amount of space I don't know how to fix.
As the answer for the text is solved. You can change the column width by changing the css property of item. you can do it as follows. The width was 700px in your code you can reduce to get a smaller width. I changed it to 400px.
.item {
width: 400px;
border: 1px solid;
padding: 1em;
background-color: blue;
color: white;
}
I need to position two of my elements on the right hand side of the parent element, however, when using the float: right property, it makes the elements to switch positions.
I had a look at this thread: Prevent Right Floated Elements from Swapping however, adding the display: inline-block and text-align: right didn't solve the problem.
Here is a
.container {
width: 300px;
height: 20px;
background-color: red;
margin: 0 auto;
}
.element1 {
float: right;
height: 20px;
width: 10px;
background-color: blue;
color: white;
padding: 10px;
}
.element2 {
float: right;
height: 20px;
width: 10px;
background-color: yellow;
padding: 10px;
}
<div class="container">
<div class="element1">1</div>
<div class="element2">2</div>
</div>
My desired result would be blue element followed by yellow element.
UPDATE:
I do understand that this is expected behaviour and the second element is send all the way to the right after the first element, and I do know that changing the elements around would fix the problem, however, just wondering if there is a CSS solution for it.
.container {
display: flex;
}
.element4 {
margin-right: auto;
}
.element5 {
margin-left: auto;
}
.container {
width: 300px;
background-color: red;
}
.element {
height: 20px;
width: 10px;
padding: 10px;
}
.element1 {
background-color: blue;
color: white;
}
.element2 {
background-color: yellow;
color: black;
}
.element3 {
background-color: green;
color: white;
}
.element4 {
background-color: gold;
color: black;
}
.element5 {
background-color: magenta;
color: black;
}
.element6 {
background-color: goldenrod;
color: white;
}
<div class="container">
<div class="element element1">1</div>
<div class="element element2">2</div>
<div class="element element3">3</div>
<div class="element element4">4</div>
<div class="element element5">5</div>
<div class="element element6">6</div>
</div>
This is expected behaviour, either switch your elements around in your HTML or use another method of positioning besides float.
It floats the first element first, then it sees the next one and this then needs to be floated over again so it moves past the original one.
use this.
.container{
width: 300px;
height: 20px;
background-color: red;
margin: 0 auto;
position:relative;
}
.element1 {
position:absolute;
right:0;
height: 20px;
width: 10px;
background-color: blue;
}
.element2 {
position:absolute;
right:10px;
height: 20px;
width: 10px;
background-color: yellow;
}
<div class="container">
<div class="element1">
</div>
<div class="element2">
</div>
</div>
Here is a fiddle of the below:
.filterDivActual, #filterSeparator {
display: inline-block;
}
.filterDivActual {
border: 2px solid grey;
width: 15%;
height: 50px;
line-height: 50px;
color: grey;
position: relative;
}
#filterSeparator {
height: 50px;
width: 5px;
background-color: black;
}
<div id='filterDiv'>
<div class='filterDivActual'>Top</div>
<div class='filterDivActual'>New</div>
<div id='filterSeparator'></div>
<div class='filterDivActual'>Today</div>
<div class='filterDivActual'>Yesterday</div>
<div class='filterDivActual'>None</div>
</div>
What I want is for the #filterSeparator to be aligned with the other divs.
For some reason, all the other divs are below the #filterSeparator.
If I put text inside #filterSeparator, then it works.
Is there a way for me to get it to work without placing any text inside #filterSeparator?
fiddle
For inline / inline-block elements, use the vertical-align property:
.filterDivActual, #filterSeperator {
display: inline-block;
vertical-align: middle ; /* or some other value: */
}
https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align
I don't know why it does this but you can fix it by using float:left; instead of display:inline-block
Putting content in the empty <div> will fix it.
<div id='filterSeperator'> </div>
#filterSeparator:before {
content: ".";
visibility: hidden;
}
.filterDivActual, #filterSeparator {
display: inline-block;
}
.filterDivActual {
border: 2px solid grey;
width: 15%;
height: 50px;
color: grey;
position: relative;
}
#filterSeparator {
height: 50px;
width: 5px;
background-color: black;
}
#filterSeparator:before {
content: ".";
visibility: hidden;
}
<div id='filterDiv'>
<div class='filterDivActual'>Top</div>
<div class='filterDivActual'>New</div>
<div id='filterSeparator'></div>
<div class='filterDivActual'>Today</div>
<div class='filterDivActual'>Yesterday</div>
<div class='filterDivActual'>None</div>
</div>
I'm having some trouble creating a (somewhat strange) layout and I can't find an example anywhere of exactly what I'm trying to do.
I would like to layout multiple blocks that look like this:
<div class="rel">
<div class="item">--- a</div>
<div class="item">- b</div>
<div class="item">c</div>
</div>
where all of the .item elements are on top of one another, but the .rel elements layout normally so they are all visible. It's important to note that all the .item elements within a .rel will be of exactly the same length, yet they may be of any length, so they might wrap onto a new line. Here is an image of what I'm trying to do:
I've created this CodePen.
SOLUTION
In case anyone else finds themselves needing this truly strange layout:
CodePen
I am not exactly sure what you are trying to achieve, but it seems that you want <div class="item"> to display inline. you can use float=left or display=inline-block instead of absolute positioning. your divs are on top of each other right now .
Something similar to this
<div class="container">
<div class="rel clearfix">
<div class="item">a</div>
<div class="item">b</div>
<div class="item">c</div>
</div>
</div>
and
.container {
width: 200px;
}
.rel {
width: 100%;
background: steelblue;
margin: 1em;
padding: 1em;
position: relative;
}
.item {
color: white;
background: gray;
margin: .1em;
float:left;
}
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.clearfix { display: inline-block; }
.rel-2 {
width: 100%;
background: steelblue;
margin: .2em;
position: relative;
}
check it out here http://codepen.io/anon/pen/vEExvM
Use this on item
display: table-cell;
and this on rel
display: inline-block;
Have a look at this fiddle. http://jsfiddle.net/h8rzw65p/
Total Code:
.container {
width: 200px;
}
.item {
color: white;
background: gray;
margin: .1em;
top: 0px;
left: 0px;
display: table-cell;
}
.rel-2 {
top: 0px;
left: 0px;
width: 100%;
background: steelblue;
margin: .2em;
display: inline-block;
}
Or instead of table-cell you could do this on item:
float: left;