I have been trying to write CSS for responsive two columns layout (text and images), but I am having hard time with that. Layout looks like this:
two column layout
Requirements:
Content within columns is generated dynamically based on the data from database (so the height of the columns is dynamic)
Columns must have equal width and height (height depends on content)
Content within columns must be horizontally and vertically aligned
Layout must be responsive, so when screen size is changed columns and content must resize accordingly without loosing the ratio and layout of images. In mobile screen the second column goes below the first one (width of the column is 90% of the screen)
Content position in layout could change, so text could be on the right column and images on the left column (configured in CMS). Such change must not affect layout at all
There may be more similar sections on the page, so the distance between them must not be too long and the content of one section cannot cover any other section
It must be displayed correctly on IE11
I have been playing with it for a while and it does not seem to work correctly for me. I have tried to play with position absolute and relative. Unfortunately, the problem is always with images. When I change the screen size two smaller images do not hold orignal position (they move to the left or right). I am not sure if my approach is good or not, I do not even know if something like this is possible in CSS (well, I could write a lot of media rules for different screen sizes, but I am looking for nicer solutions), I have never seen this images layout before. I would be very keen to know what the best approach is.
I created some code snippet in order to demonstrate the issue:
.section {
margin: 0;
display: block;
width: 100%;
height: auto;
padding: 0;
text-align: center;
}
.content-left {
position: relative;
display: inline-block;
width: 45%;
vertical-align: middle;
text-align: center;
float: left;
}
.content-right {
position: relative;
display: inline-block;
width: 45%;
vertical-align: middle;
text-align: center;
float: right;
}
.first-image-wrapper {
position: relative;
z-index: 1;
height: 10rem;
width: 10rem;
text-align: center;
margin: auto;
vertical-align: middle;
right: -2rem;
top: 7rem;
}
.content-image-1 {
vertical-align: middle;
border-radius: 0.2rem;
width: inherit;
height: inherit;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.second-image-wrapper {
position: relative;
right: -1rem;
bottom: 7rem;
border-radius: 0.2rem;
height: 8.4rem;
width: 8.4rem;
z-index: 4;
}
.content-image-2 {
max-width: 100%;
height: auto;
display: inline-block;
vertical-align: middle;
}
.third-image-wrapper {
position: relative;
bottom: 18rem;
right: -10rem;
border-radius: 0.2rem;
height: 5rem;
width: 5rem;
background-size: cover;
z-index: 5;
}
.content-image-3 {
max-width: 100%;
height: auto;
display: inline-block;
vertical-align: middle;
}
#media (max-width: 500px) {
.content-left, .content-right {
width: 100%;
display: block;
}
}
<div class="section">
<div class="content-left">
<div class="content-text">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Praesent sapien massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Praesent sapien massa
</p>
</div>
</div>
<div class="content-right">
<div class="first-image-wrapper">
<img class="content-image-1" src="https://i.postimg.cc/BtB3G5dz/P1000003.jpg" alt="Image 1">
</div>
<div class="second-image-wrapper">
<img class="content-image-2 " src="https://i.postimg.cc/SnJb8BJ0/P1000064.jpg" alt="Image 2">
</div>
<div class="third-image-wrapper">
<img class="content-image-3" src="https://i.postimg.cc/ykfzD16X/P1000071.jpg" alt="Image 3">
</div>
</div>
</div>
Any help would be appreciated.
display:flex works for a genuine IE11 (I run one) :
You could do :
.section {
align-items: center;
display: flex;
width: 100%;
}
.section>div {
width: 50%;
}
.content-right {
margin: auto;
}
img {
display: block;
margin: auto;
}
img:nth-child(1) {
width: 20%;
margin-left: 70%;
margin-bottom: -2rem;
}
img:nth-child(2) {
width: 45%;
margin-left: 10%;
position:relative;
}
img:nth-child(3) {
width: 50%;
margin: -2rem 0 0 40%;
;
}
#media (max-width: 500px) {
.section {
display: block;
}
.section>div {
width: auto;
}
}
<div class="section">
<div class="content-left">
<div class="content-text">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus.
Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Praesent sapien massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Curabitur arcu erat, accumsan id
imperdiet et, porttitor at sem. Curabitur non nulla sit amet nisl tempus convallis quis ac lectus. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Praesent sapien massa
</p>
</div>
</div>
<div class="content-right">
<img class="content-image-1" src="https://i.postimg.cc/BtB3G5dz/P1000003.jpg" alt="Image 1">
<img class="content-image-2 " src="https://i.postimg.cc/SnJb8BJ0/P1000064.jpg" alt="Image 2">
<img class="content-image-3" src="https://i.postimg.cc/ykfzD16X/P1000071.jpg" alt="Image 3">
</div>
</div>
here is a jsbin that works and was made in IE11 https://jsbin.com/lemonopica/1/edit?html,css,output
If you don't fill okay with flex, you can use display:table and display:table-cell which allows vertical centering too , it is understood by every browsers. here is a table/table-cell demo https://jsbin.com/gecokurebe/1/edit?html,css,output
Related
This is the html for my box with the title text and image inside it.
<div id="about">
<div id="title"> <h3><b>About</b></h3></div>
<div id="text"><p>Text</p></div>
<div id="img"><img src="img/3.jpg" height="300" width= "400" alt="?">
</div>
and this is the css
#about {
color: white;
padding: 10px;
position: relative;
width: 90%;
height: 325px;
background: lightgrey;
top: 30px;
margin: 0 auto;
overflow: auto;
color: white;
background: #262626;
box-sizing: border-box;
}
#text {
width: 720px;
position: relative;
top: 30px;
float: right;
}
#title {
float: right;
position: relative;
right: 725px;
top: 0px;
}
#img {
}
My problem is that because my title is always 725px to the right, if I had a title larger than 5 letters it isnt right next to the picture or else I'll have to position it again, is there an easier way around this? Because doing it manually is frustrating.
Thanks.
You can solve this much easier using flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Put the title and text in a own wrapper .text-wrapper and place the img before the wrapper.
On your parent about use follow property, to place them each to other:
.about {
display: flex;
}
Some refactoring: You don't really ned a div around the image for this case. And I recommend you to use class in case of id. Check out the snippet:
.about {
display: flex;
}
.text-wrapper {
padding-left: 20px;
}
<div class="about">
<img src="https://www.telegraph.co.uk/content/dam/news/2016/08/23/106598324PandawaveNEWS_trans_NvBQzQNjv4Bqeo_i_u9APj8RuoebjoAHt0k9u7HhRJvuo-ZLenGRumA.jpg?imwidth=450" height="300" width="400" alt="?">
<div class="text-wrapper">
<div class="title">
<h3><b>Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Proin eget tortor risu</b></h3>
</div>
<div class="text">
<p>Vestibulum ac diam sit amet quam vehicula elementum sed sit amet dui. Proin eget tortor risus. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Pellentesque in ipsum id orci porta dapibus. Vestibulum ac diam sit amet quam vehicula
elementum sed sit amet dui.
suscipit tortor eget felis p
Sed porttitor lectus nibh.</p>
</div>
</div>
</div>
Try to remove floating from title and text and 'right' property from .title, then add 'float: left' to the picture.
I am a CSS/HTML auto learner, apologies if my question is stupid.
I want a div which is 40% wide minimum with its content justified but when more items will be added in this div its width grows accordingly.
This is what I have so far:
.hcontainerbutton {
display: flex;
width: 40%;
background-color: blue;
align-items: center;
justify-content: space-around;
right: 30%;
left: 30%;
position: absolute;
bottom: 0!important;
}
both with width:40% or min-width:40% the div doesn't grow if I add more items into it
Wrap .hcontainerbutton in a container and apply flexbox and height properties to it. This can be used to position .hcontainerbutton instead of position: absolute.
Add min-width value to .hcontainerbutton
You can test this layout by adding and remove .content divs and viewing in full screen.
body {
margin: 0;
}
.container {
display: flex;
justify-content: center;
height: 100vh;
}
.hcontainerbutton {
min-width: 40%;
background-color: blue;
display: flex;
justify-content: space-around;
margin-top: auto;
}
.content {
background: pink;
width: 100px;
height: 100px;
}
<div class="container">
<div class="hcontainerbutton">
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
<div class="content"></div>
</div>
</div>
Based on your requirements, this can be done with normal CSS itself, you need not go for flex, Here is an explanation on what is done.
First we set the width and height of html and body tag, then using margin:0px remove the margins set by the browser.
html,
body {
width: 100% height: auto;
margin: 0px;
}
Now the parent that will wrap the centered div will have to have the CSS property text-align:center, basically what this does is, it will center align elements with display property inline-block.
.parent {
text-align: center;
}
Then coming to the main div, which has the class hcontainerbutton, we can set the max-width(in the example I use 40%) and min-width(in the example I use 80%) to whatever is needed. The CSS property display:inline-block ensures it takes the width of the content alone. The CSS property word-wrap:break-word ensures the text is broken and maintains the widht of the div.
Below is a working snippet of the code.
html,
body {
width: 100% height: auto;
margin: 0px;
}
.parent {
text-align: center;
}
.hcontainerbutton {
word-wrap: break-word;
min-width: 40%;
max-width: 80%;
display: inline-block;
}
<div class="parent">
<div class="hcontainerbutton"> asdf
</div>
<div class="hcontainerbutton"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin eleifend magna augue. Morbi sagittis eu urna et facilisis. Nam finibus erat justo, vel porta magna aliquam a. Pellentesque faucibus molestie libero vitae condimentum. Nunc condimentum tincidunt
nulla, id suscipit magna dignissim id. Nulla dapibus suscipit velit et viverra. Mauris non gravida justo. Sed efficitur eleifend elementum. Integer non mattis mi. Etiam vestibulum viverra erat, eget dapibus tellus iaculis ut. Mauris ullamcorper magna
sapien, ac gravida odio blandit varius. Fusce eu placerat enim. Etiam nec elementum dui. In fermentum massa sed augue interdum aliquam. Nunc lacinia blandit elit a iaculis.
</div>
</div>
I'm trying to line up some images within a container and can't seem to move them to make them central. This is how they currently look
I need to sit completely centrally and as is evident they are too far over to the right. I've tried lots of different things but can't get it right. I don't think I'm identifying the correct element.
Here's my code for the section on the photo
body {
margin: 0 auto 0 auto;
}
.container {
margin: auto;
max-width: 100%;
padding-left: 10px;
padding-right: 10px;
}
section#welcome {
height: 500px;
max-width: 100%;
}
section#welcome div.row {
height: 250px;
text-align: center;
position: relative;
}
#welcome h4 {
color: #000000;
font-size: 20px;
padding-top: 50px;
line-height: 5px;
}
section#welcome p {
font-size: 10px;
color: #bdc3c7;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%)
}
/* centering an image within a column */
section#welcome .four {
position: relative;
display: inline-block;
bottom: 50px;
}
.four h3 {
position: absolute;
color: #FFF;
font-size: 20px;
margin: 0;
top: 50%;
left: 55%;
transform: translate(-50%, -50%);
}
section#welcome img {
display: block;
margin-bottom: 30px;
}
.images,
.four {
margin-right: 100px;
}
<section id="welcome">
<div class="container">
<div class="row">
<div class="twelve columns">
<h4>WELCOME TO FEATURE MEDIA</h4>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vel ex nisl. Vestibulum vitae ultricies nisl. Praesent sodales, leo at pellentesque pellentesque, nunc erat dapibus nunc, ut congue libero lorem in orci. Suspendisse potenti. Quisque
facilisis mauris in vestibulum tempor. Suspendisse nec venenatis nisi. Phasellus sodales viverra ante quis efficitur. Pellentesque quis orci mi. Phasellus tempus, sapien ut luctus pellentesque, lacus risus accumsan lorem, in porta urna tellus
ac nibh. Nunc varius elit non diam vehicula aliquet. In eget urna id orci molestie pulvinar. Integer quis risus eu erat iaculis aliquet ut at eros. Etiam feugiat, ante vel molestie finibus, lacus urna pharetra leo, ut lobortis massa lectus quis
lorem. Vestibulum rhoncus turpis sagittis sapien vulputate sagittis. Nunc ac velit sollicitudin, consequat arcu ac, tincidunt risus.</p>
</div>
</div>
<hr class="hrindeximages">
<div class="images row">
<div class="four columns">
<div id="video">
<h3>VIDEO</h3>
<img src="images/VIDEO.jpg" alt="Video" style="width:300px;height:150px;">
</div>
</div>
<div class="four columns">
<div id="blog">
<h3>BLOG</h3>
<img src="images/blog.jpg" alt="blog" style="width:300px;height:150px;">
</div>
</div>
<div class="four columns">
<div id="faq">
<h3>FAQ</h3>
<img src="images/faq.jpg" alt="FAQ" style="width:300px;height:150px;">
</div>
</div>
</div>
</div>
</section>
The issue is caused by the margin-right: 100px attribute in the .images, .four class. If you get rid of that, your images should be centered:
.images, .four {
margin-right: 100px; // this line causes your images to offset from center
}
If you still want to have that space in between your images, you can add a margin-left field to balance it out:
.images, .four {
margin-right: 50px;
margin-left: 50px;
}
Use the padding attribute to manually center the images.
This may be help you.
.images { text-align: center; width: 100%; }
Like #yelq said, get rid of the margin-right:100px.
And to make it even more flexible, I would use the display: flex property and use the margin short notation allowing you to change the separation between pictures in only one place.
I would remove the class row from <div class="images row"> since it is not necessary.
In your css, change it to:
.images, .four {
display: flex;
justify-content: center;
flex-wrap: wrap;
margin: 0 50px;
/*margin-right: 100px;*/
}
I would also remove the styling on the img elements and add it to the css, again to allow easier development incase of changes.
.images img {
width:300px;
height:150px;
}
How could to put the right box next to the left box (inline-block) with precise sizes? I can put it up, but the size will not be the same in different browsers. Then how could I put the left box near to the right box without that whitespace? Thank you.
div {
border: 1px solid green;
}
.entry {
width: 560px;
margin: auto;
border: 1px solid red;
display: block;
position: relative;
}
.entry .img-cont,.body-cont {
vertical-align: top;
}
.entry .img-cont {
width: 50px;
display: inline-block;
}
.entry .body-cont {
width: 508px;
display: inline-block;
}
.entry .body-cont p {
display: table;
}
<div class="entry">
<div class="img-cont">
<img src="http://bit.ly/1RabLNk"/>
</div>
<div class="body-cont">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi nulla libero, sollicitudin a erat semper, gravida pharetra augue. Phasellus convallis ultrices dolor vitae imperdiet. Curabitur mollis odio neque, in dictum nisi finibus nec. Vivamus pulvinar, turpis a volutpat semper, lacus diam convallis.</p>
</div>
</div>
see this
http://jsfiddle.net/leandroparrar/6omjqefj
Here is an example of what the problem looks like:
Your answer is pretty close to what you need.
If you zero-our the margins on the p element in .body-cont, that will get rid of the extra whitespace that appears on top of the paragraph.
If you try to use inline-block's, it is easy to get some extra whitespace between elements due to carriage-returns (newlines) in the HTML file.
If you use display: table-cell on .img-cont and .body-cont, then the
two elements will rest side-by-side and you can control the horizontal spacing using left/right padding as needed.
div {
border: 1px solid green;
}
.entry {
width: 560px;
margin: auto;
border: 1px solid red;
display: table;
}
.entry .img-cont, .entry .body-cont {
display: table-cell;
vertical-align: top;
}
.entry .img-cont img {
display: block;
}
.entry .body-cont p {
margin: 0;
}
<div class="entry">
<div class="img-cont">
<img src="http://bit.ly/1RabLNk" />
</div>
<div class="body-cont">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi nulla libero, sollicitudin a erat semper, gravida pharetra augue. Phasellus convallis ultrices dolor vitae imperdiet. Curabitur mollis odio neque, in dictum nisi finibus nec. Vivamus pulvinar,
turpis a volutpat semper, lacus diam convallis.</p>
</div>
</div>
I've got the following code:
* {
box-sizing: border-box;
}
.container {
width: 100%;
max-width: 60rem;
/* 960 */
margin: 0 auto;
}
.item {
width: 100%;
overflow: hidden;
margin-bottom: 5rem;
/* 80 */
}
.item__img,
.item__info {
width: 50%;
float: right;
}
.item__img {} .item__img .img-map {
width: 95%;
height: 18.750rem;
/* 300 */
}
.item__img img {
width: 95%;
height: 18.750rem;
/* 300 */
}
<div class="container" role="main">
<article class="item">
<div class="item__info">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ac sodales orci. Praesent sit amet consequat purus. Praesent lobortis mi quis rutrum fringilla. Phasellus velit arcu, ultricies vestibulum varius sed, convallis ut eros. Vestibulum
vel congue felis, ut lacinia tellus. Integer ullamcorper gravida ligula non convallis. Ut suscipit vulputate erat eu porttitor. Morbi sagittis vulputate bibendum. Aliquam ultricies finibus tortor, a elementum nisl aliquet at. In sed dui id mauris
rutrum ornare.</p>
</div>
<div class="item__img">
<div class="img-map">
<img src="http://biologypop.com/wp-content/uploads/2014/11/dog1.jpg" />
</div>
</div>
</article>
</div>
sorry for bad style, I've just started to learn CSS...
Now, after seeing that in the browser, I see the picture of a dog and next to it there's some text. I would like to have this text aligned in the center (vertically). Basically, currently it looks like this, and I would like to set it up like this. How should I modify my CSS code to display it as it is? Thanks!
EDIT My other question is - why the text is not lined up on the top to the top layer of the picture? I don't see any constraint for that in my css code, does anybody know how it works?
My suggestion is to ignore anyone that suggests using display:flex, because it doesn't have the browser support it needs for public domain. (currently as of 14/04/15. This will get better as time goes on and will probably be a more serious consideration once Windows 10 comes out later this year)
What you are wanting can be achieved with display:table; on the parent and display:table-cell; on the children. In the code below I have rearranged the HTML so the image is first and removed the float:right; (my experience leads me to not use float anymore as it causes so many headaches that can be avoided, but that's a much bigger discussion).
Adding vertical-align:middle; to the children will make them vertically align in their "cell".
The reason you were previously seeing space above your text is because each browser has a default style-sheet that is applied. For example Firefox has this:
p, dl, multicol {
display: block;
margin: 1em 0;
}
To aid your understanding of such things I suggest to use Mozilla Firefox and download the Firebug add-on.
Here's the full code:
* {
box-sizing: border-box;
}
.container {
width: 100%;
max-width: 60rem;
/* 960 */
margin: 0 auto;
}
.item {
width: 100%;
overflow: hidden;
margin-bottom: 5rem;
display:table;
/* 80 */
}
.item__img,
.item__info {
width: 50%;
display:table-cell;
vertical-align:middle;
}
.item__img {} .item__img .img-map {
width: 95%;
height: 18.750rem;
/* 300 */
}
.item__img img {
width: 95%;
height: 18.750rem;
/* 300 */
}
<div class="container" role="main">
<article class="item">
<div class="item__img">
<div class="img-map">
<img src="http://biologypop.com/wp-content/uploads/2014/11/dog1.jpg" />
</div>
</div>
<div class="item__info">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ac sodales orci. Praesent sit amet consequat purus. Praesent lobortis mi quis rutrum fringilla. Phasellus velit arcu, ultricies vestibulum varius sed, convallis ut eros. Vestibulum
vel congue felis, ut lacinia tellus. Integer ullamcorper gravida ligula non convallis. Ut suscipit vulputate erat eu porttitor. Morbi sagittis vulputate bibendum. Aliquam ultricies finibus tortor, a elementum nisl aliquet at. In sed dui id mauris
rutrum ornare.</p>
</div>
</article>
</div>
This link might help you, I use this trick very often when it comes to vertically aligning:
http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/
Add this code:
position: relative;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
to a container around the text (in your case the 'p'), and make sure to set the height of the container (article.item) that wraps around both the image and the text.
You want to set a height of your div and line-height aswell.
Like:
.div{ height: 100px;
line-height: 100px;}
See an example here: http://jsfiddle.net/BRxKX/
Use flexbox if you can, it allows you to do this without any odd rules or hacks.
For example:
HTML
<div class="container">
<div class="image">200 x 200</div>
<p>Lots of text here...</p>
</div>
CSS
* {
box-sizing: border-box;
}
.container {
display: flex;
align-items: center;
}
.image {
height: 200px;
width: 200px;
min-width: 200px;
}
See it here in action. Try editing the text within the p tag to see how it works.