First, this is my HTML
<div class="header">
<div class="wrapper">
<div class="navi">
Logo
<ul><!--
--><li>Link 1</li><!--
--><li>Link 2</li><!--
--></ul>
</div>
</div>
</div>
<div class="wrapper">
<div class="content">
<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>
</div>
and CSS
.wrapper {
width: 90%;
max-width: 50em;
margin: 0 auto;
}
.header,
.navi {
width: 100%;
}
.header {
background: grey;
}
.navi {
background: green;
display: table;
text-align: center;
}
.navi ul {
display: table-cell;
vertical-align: middle;
text-align: right;
}
.navi li {
background: orange;
display: inline;
margin-left: 10px;
}
.navilink {
display: inline-block;
width: auto;
}
.logo {
background: red;
float: left;
}
.content {
width: 100%;
background: fuchsia;
}
Fiddle
Fullscreen Fiddle
You see, the wrapper ensures that there is always some kind of gap between content and edge of the viewport and beyond a certain point (50 em), the .wrapper doesn't exceeds any further.
The code I posted here works, but I would like to know if there is any chance to get rid of <div class="wrapper"> achieving the same result. I already tried to apply the .wrapper class directly to the elements, but that isn't working - why?
To clarify: My aim is it to make the markup cleaner. That's why I am interested in a solution then ensures that the elements behave like in the example I posted, but without the use of <div class="wrapper">. The class .wrapper has to stay of course, it's just that div that strikes me. Thats why I tried to add .wrapper directly to the elements.
Remove the wrapper div, and add the two CSS properties to the content div..
.content {
margin: 0 auto;
max-width: 50em;
}
Related
Struggled with a basic css management issue.
I have created a div that need to be squared one, so I just added :before element to the div to achieve the square box.
Now the problem is:
I need to add image into the squared box, It can be a rectangle, square or lengthy image.
Please take a look at the snippet below:
the image with more height breaks the div squareness.
* {
box-sizing: border-box;
}
.container {
display: flex;
}
.left {
width: 60%;
}
.right {
width: 40%;
padding-left: 15px;
}
.box {
width: 100%;
background-color: red;
display: flex;
text-align: center;
}
.box img {
width: 100%;
max-height: 100%;
margin: 0 auto;
}
.box::before {
padding-top: 100%;
content: "";
float: left;
}
<div class="container">
<div class="left">
<div class="box">
<img src="https://via.placeholder.com/800x1000" />
</div>
</div>
<div class="right">
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. 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.
</div>
</div>
How can I keep this div as a square one?
I have tried: image height 100%, 100vh & auto to make this, but not working well.
(please note: there is no problem if image is vertically or horizontally center)
Fiddle: https://jsfiddle.net/vishnuprasadps/xgvc65r1/
from comment:
.left {width: 60%;} is that 60% of the screen (60vw X 60vw will do)? On a desktop, that square is hudge ... do you need to shrink img / .left to be set to a max-width too ? absolute does the job as is https://jsfiddle.net/jLzhbu4q/
with the help of object-fit and border-box, you can add paddings and keep your image ratio.
* {
box-sizing: border-box;
}
.container {
display: flex;
}
.left {
width: 60%;
background:green;
padding:2em;
box-sizing:border-box;
}
.right {
width: 40%;
padding-left: 15px;
}
.box {
width: 100%;
background-color: red;
display: flex;
align-items:center;
justify-content:center;
text-align: center;
position:relative;
}
.box img {
box-sizing:border-box;
padding:5em;
/*width: 100%;
max-height: 100%;*/
width: auto; /* op's choice */
max-width: 100%; /* op's choice */
max-height: 100%;/* op's choice */
margin: auto;
position:absolute;
top:0;
bottom:0;
right:0;
left:0;
object-fit:contain;
}
.box::before {
padding-top: 100%;
content: "";
float: left;
}
<div class="container">
<div class="left">
<div class="box">
<img src="//via.placeholder.com/800x1000" />
</div>
</div>
<div class="right">
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. 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.
</div>
</div>
You can try it by changing the CSS with this code:
.box {
position: relative;
width: 50%;
background-color: red;
display: flex;
text-align: center;
}
img {
position: absolute;
width: 100%;
height: 100%;
}
.box::after {
content: "";
display: block;
padding-bottom: 100%;
}
Do you mean something like this?
.div{
width:150px;
height:150px;
border-radius:100%;
}
.div:before{
content:"";
background-image:url("https://reactgo.com/static/171499b9f35ca9aaa3bdb4a99c2a7f41/6728c/crop-image-square.png");
background-size:cover;
background-position:center center;
width:150px;
height:150px;
position:absolute;
z-index:-1;
border-radius:100%;
}
<div class="div"></div>
I will try to make this sound as easy as possible.
I am trying to place 2 div containers, side by side, and have them be the same height at all times.
The right div will be regular text. The amount of text in here will vary since I plan on using this for different pages.
The left div will be composed of 2 smaller containers - a title block, and an image block beneath it.
Here is a visual example of what I'm trying to achieve. The green box is supposed to be the full photo
Example Photo
I would like the photo in the image block of the left side to take up the full height/width of the box - (similar to background-position: cover that is used in CSS). I'd prefer to use a regular img tag instead of setting it as a div background.
The issue that I am having is that the image height on the left takes priority over the text box on the right hand side, and causes both containers to appear much longer than I want. I want the text block on the right to be prioritized, and the image block changes height based on that.
I've tried using object-fit: contain, but it isn't working, unfortunately. The closest I've gotten is to use width: 100%, but then it makes the height way too big.
Here's what I have so far:
.main {
display: flex;
}
.main .left {
width: 40%;
float: left;
}
.main .left .title {
background-color: black;
text-align: center;
display: block;
height: 90px;
padding: 50px;
color: white;
font-size: 40px;
}
.photo {
height: auto;
width: 100%;
}
.photo img {
width: 100%;
}
.main .right {
width: 60%;
float: right;
}
<div class="main">
<div class="left">
<div class="title">This is my Title</div>
<div class="photo"><img src="https://image.shutterstock.com/z/stock-photo-pastoral-green-field-with-long-shadows-in-tuscany-italy-275372477.jpg"></div>
</div>
<div class="right">
<p>text goes here lalalalalala</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. 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>
<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. 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>
</div>
<style>
.main {
display: flex;
}
.main .left {
width: 40%;
display: flex;
flex-direction: column;
}
.main .left .title {
background-color: black;
text-align: center;
display: block;
height: 90px;
padding: 50px;
color: white;
font-size: 40px;
}
.photo {
width: 100%;
overflow: hidden;
position: relative;
flex-grow: 1;
}
.photo img {
width: 100%;
position: absolute;
}
.main .right {
width: 60%;
}
</style>
Notes:
I made the image absolutely positioned so its own height won't stretch our flex row.
The image is being cropped by height. If the title is taller than the text (or the same height), you won't see the image at all.
I made the left column also display flex and the photo box flex grow so that the title can stay the same height and the photo box will stretch the rest of the way to match the right column.
We don't need float left/right for flex items.
I have a problem where I can't get two divs to align perfectly on top of each other. Whatever I try, there remains some white space between them.
Link to the fiddle
HTML
<header>
<div id="slider" class="row">
<div class="slideshow-container">
<div class="mySlides fade">
<img id="scroller1" class="scroller" src="https://www.pexels.com/blog/wp-content/uploads/pexels-photo-18-1280x420.jpg">
</div>
</div>
</div>
</header>
<main>
<div id="Project" class="row">
<div class="singlecol">
<h1>Header1</h1>
<h2>Header2</h2>
<p class="blocktext">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. 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>
</div>
</main>
CSS
body {
margin: 0;
}
h1, h2, p {
font-family: 'Open Sans', sans-serif;
text-align: center;
padding: 0 5% 0 5%;
margin: 0;
}
p {
padding: 1% 0 1% 0;
text-align: justify;
color: #383433;
max-width: 100%;
vertical-align: top;
}
h1 {
color: #383433;
padding: 0 0 0 0;
}
h2 {
color: #843a32;
}
.slideshow-container {
box-sizing: border-box;
max-width: 100%;
display: inline-block;
margin: 0;
}
img.scroller {
width: 100%;
max-height: 600px;
margin: 0;
}
.row {
display: flex;
}
.col {
flex: 1;
}
.singlecol {
background-color: red;
width: 100%;
text-align: center;
display: inline-block;
}
I've been trying to fix this for hours, deleting every line of css possible, but I can't seem to wrap my head around it. I've also checked earlier questions on this topic, but none of what I try resolves the problem.
Would appreciate some fresh eyes :-)
Make the img element display:block, as it inherits the inline from its parents.
img.scroller {
width: 100%;
max-height: 600px;
margin: 0;
display: block;
}
https://jsfiddle.net/kqL84Lmx/
Simply add
.mySlides.fade {
display: grid !important;
}
to css :)
https://jsfiddle.net/o4334uss/6/
I've developed an array of different skills in angular which are showing up fine and wrapping correctly on the desktop version of my site, but on smaller screens, the items are ignoring the container's padding and width and failing to wrap always at the same point.
Ironically, where the object 'Responsive Design' from the array is displayed in the list, it will not wrap the word 'Responsive', so this always pushes past the width of the container, and the word 'Design' correctly wraps on to a new line.
Codepen: http://codepen.io/nickwcook/full/KWePVx/.
HTML:
<section id="about" ng-controller="skillsController">
<div class="sectionContent">
<div id="bio">
<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. 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>
<div id="skillsList">
<p>My skills include:</p>
<p ng-repeat="skill in skills" class="skillItem">{{skill.name}}.</p>
</div>
</div>
</div>
</section>
CSS:
section
{
display: block;
margin: 0;
background: transparent;
z-index: 90;
}
.sectionContent
{
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: 100px 30px;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
}
/* ABOUT SECTION */
#about .sectionContent > div
{
width: 100%;
padding: 0 15%;
}
#about p
{
line-height: 26px;
text-align: center;
}
#about #skillsList
{
text-align: center;
margin-top: 50px;
}
#about #skillsList p:first-of-type
{
margin-bottom: 10px;
}
#about #skillsList p.skillItem
{
display: inline;
margin: 0 15px;
}
Change This
#about #skillsList p.skillItem {
display: inline;
margin: 0 15px;
}
to this
#about #skillsList p.skillItem {
display: inline-block;
margin: 0 15px;
}
-- That will fix your layout, but my recommendation is that you move from the P element to using the List ( ul > li ) for a more semantic approach.
I'm trying to create 2 columns, one simply filled with text while the other contains three coloured boxes of equal height (33.33%) which then add up to the same height as the text. The overall size of the container can't be a fixed height unfortunately as the site is responsive and the amount of text may change so I need the height of the two columns to be flexible.
Now I've used display: table; and display: table-cell; elsewhere in the site to achieve equal height between just two columns but am struggling to make this one work with the three equally sized boxes within one of the columns.
I've made a JSFiddle to show you what I've got:
http://jsfiddle.net/56yFp/
And here's the html:
<div class="column-row">
<div class="column column-cell column-text">
<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>
<div class="column column-cell column-boxes">
<div class="box green-box">Box 1</div>
<div class="box red-box">Box 2</div>
<div class="box blue-box">Box 3</div>
</div>
</div>
css:
.page-wrapper {
background-color: #FFFFFF;
}
/* Table */
.column-table {
display: table;
}
.column-row {
display: table-row;
}
.column-cell {
display: table-cell;
}
.column {
vertical-align: middle;
height: 100%;
}
.column-text {
width: 62.5%;
background-color: #e2e2e2;
}
.column-boxes {
width: 37.5%;
}
.box {
min-height: 33.33%;
width: 100%;
}
/* Colors */
.green-box {
background-color: #016354;
}
.red-box {
background-color: #eb5640;
}
.blue-box {
background-color: #93ceee;
}
Any thoughts SO community? Thanks
DEMO
.column {
display:inline-block
}