Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

html - CSS Spacing is Wrong and Content Extends Beyond Container

Please bear with me. I'm new to coding.

I'm creating a web page. It has a desktop view, a tablet view, and a mobile view.

  • In desktop view, the gray boxes should be on the same line with spaces between them.

Desktop View

  • In tablet view, there should be 2 gray boxes with spaces between them on the first line, and the third box should be on it's own line.

Tablet View

  • In mobile view, each gray box should be on its line.

Mobile View

My questions are:

  • For desktop view how do I get spaces between boxes 1 and 2, and boxes 2 and 3?
  • For desktop view, when I go to Chrome developer tool and select the section elements, the gray boxes are not inside the container. The gray boxes extend below the container. However, when I go to tablet and mobile view and select the section elements, the gray boxes are inside the container. Why is that?

Chrome Developer Tools - Desktop View

Chrome Developer Tools - Tablet View

  • For tablet view, how do I get the first 2 boxes to be on line 1, and the third box on line 3?

Thank you in advance!! FYI, see below for code snippet.

-Ian

/**********Base Styles *****************/
*{
    box-sizing: border-box;

}

h1 {
    font-family: Comic Sans MS, Arial, cursive;
    text-align: center;
}

div {
    background-color: gray;
    

}

p {
    text-align: center;
    border: 1px solid black;
    width: 100px;
    margin-left: auto;
    position: relative;
    top: -27px;
    right: -11px;
}

section {
    margin: 10px;
}

div > section > div > div {
    padding: 10px;
    border: 1px solid black;    
}

div > p {
    
}

#p1 {
    background-color: pink;
    
}

#p2 {
    background-color: maroon;
    color: white;
}

#p3 {
    background-color: yellow;
}

/************* Desktop Devices Only ***********/
@media (min-width: 992px) {
    .col-lg-3 {
    float: left;
    border: 1px solid black;
    padding: 10px;
    margin: 20px;
    
    }

    .col-lg-3 {
        width: 25%      
    }
}


/********** Tablet Devices Only **************/
@media (min-width: 768px) and (max-width: 991px) {

    #section1 {
        width: 45%;
        float: left;


        }

    #section2 {
        width: 45%; 
        float: left;                            
    }

    #section3 { 
        float: left;
    }

    }


/************ Mobile Devices Only *************/
@media (max-width: 767px) {
    .col-sm-12 {
        float: left;
        border: 1px solid black;
        padding: 10px;
        margin: 10px;
    }

    .col-sm-12 {
        width: 100%;
    }
}
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
    <title>Our Menu</title>
    <link rel="stylesheet" href="css/test-css.css">
    <style>
        
        /********* Simple Responsive Framework ******/
        .row {
            width: 100%;                    
        }   


    </style>
</head>

<body>

<h1>Our Menu</h1>

    <div>
        
        <section id="section1" class="container">
            <div class="row">           
            <div class="col-lg-3 col-md-4 col-sm-12">

            <div><p id="p1">Chicken</p></div>
            
            
            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.
            

            </div>          
            </div>
        </section>
    </div>

    <div>
        
        <section id="section2" class="container">
            <div class="row">
            <div class="col-lg-3 col-md-4 col-sm-12"> 
            
            <div><p id="p2">Beef</p></div>

            
            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.
            

            </div>
            </div>
        </section>
    </div>

    <div>
        
        <section id="section3" class="container">
            <div class="row">
            <div class="col-lg-3 col-md-4 col-sm-12">
            
            <div><p id="p3">Sushi</p></div>

            
            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.
            

            </div>
        </div>
        </section>
    </div>


</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I changed the styling method to a CSS-Grid.

1st: I removed the margin of your sections: section { margin: 20px; }.

2nd: I wrapped all the 3 sections inside a div: <div class="grid-wrapper"> ... </div>

3rd: I deleted all your CSS within the media queries.

4th: I added following CSS for the div.grid-wrapper outside of any media queries: .grid-wrapper { padding: 20px; display: grid; grid-gap: 20px; }. This will create a spacing to the border of the screen of 20px (acts the same as your previos 20px margin). The grid-gap will keep the sections always exactly 20px apart from each other no matter in which media queries view.

5th: I added following CSS for desktop view: .grid-wrapper { grid-template-columns: repeat(3, 1fr); }. This will divide the wrapper into 3 equal wide columns.

6th: I added following CSS for tablet view: .grid-wrapper { grid-template-columns: repeat(2, 1fr); } .section3 { grid-column: span 2; }. This will divide the wrapper into 2 equal wide columns. The last 3rd section will span both columns.

7th: I added following CSS for mobile view: .grid-wrapper { grid-template-columns: repeat(1, 1fr); }. This way it will only be a single column and all sections are dispalyed below each other.

/********* Base Styles ******/

* {
  box-sizing: border-box;
}

h1 {
  font-weight: bold;
  text-align: center;
  font-family: Comic Sans MS, Arial, cursive;
}

section {
  background-color: gray;
}

div>p {
  border: 1px solid black;
}

p {
  text-align: center;
  width: 25%;
  margin-right: right;
  margin-left: auto;
  margin-top: auto;
}

#p1 {
  background-color: pink;
}

#p2 {
  background-color: maroon;
  color: white;
}

#p3 {
  background-color: yellow;
}

.grid-wrapper {
  padding: 20px;
  display: grid;
  grid-gap: 20px;
}


/********* Desktop Devices Only *******/

@media only screen 
  and (min-width: 992px) {
    .grid-wrapper {
      grid-template-columns: repeat(3, 1fr);
    }
}


/*********** Tablet Devices Only **********/

@media only screen 
  and (min-width: 768px) 
  and (max-width: 991px) {
    .grid-wrapper {
      grid-template-columns: repeat(2, 1fr);
    }
    
    #section3 {
      grid-column: span 2;
    }
}


/************* Mobile Devices Only **************/

@media only screen 
  and (max-width: 767px;) {
    .grid-wrapper {
      grid-template-columns: repeat(1, 1fr);
    }
}
<h1>Our Menu</h1>

<div class="grid-wrapper">
  <section id="section1" class="container">
    <div class="row">
      <div class="col-dk-4 col-mb-6">
        <div>
          <p id="p1">Chicken</p>
        </div>

        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.
      </div>

    </div>
  </section>

  <section id="section2" class="container">
    <div class="row">
      <div class="col-dk-4 col-mb-6">
        <div>
          <p id="p2">Beef</p>
        </div>

        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.
      </div>
    </div>
  </section>

  <section id="section3" class="container">
    <div class="row">
      <div class="col-dk-4 col-mb-6">
        <div>
          <p id="p3">Sushi</p>
        </div>

        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.
      </div>
    </div>
  </section>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...